Disallow duplicate declarations within the same scope - #1376
Conversation
Signed-off-by: jaehyun1ee <99jaehyunlee@kaist.ac.kr>
2f3d5cb to
9dde515
Compare
|
Thanks @rcgoodfellow |
|
@jafingerhut Review this. |
| extern void f(in bit<8> x); | ||
| extern void f(in bit<8> y); // allowed: different parameter names | ||
| extern void f(in bit<16> x, in bit<8> y); // allowed: different number of parameters | ||
| extern void f(in bit<16> x, in bit<8> x); // Error: duplicate declaration of previous function |
There was a problem hiding this comment.
I am confused by this example for a couple of reasons.
(1) There are two parameters named x. That seems like the primary reason that it would be an error to me, regardless of overloading considerations.
(2) If we changed the parameters names to 'x' and 'z' instead of 'x' and 'x', would it be legal? The sequence of parameter names is different, so it is not clear to me whether that is sufficient to be similar to the example a couple of lines above where it has the comment "allowed: different parameter names". Does each individual parameter name need to be different? Or does the entire sequence of names need to differ in at least one of the names?
(3) I had not realized (or perhaps seen it before, but forgotten) that the example with the comment "allowed: different parameter names" was allowed. Is it expected that calls to such an overloaded function must use the parameter name in calls, in order to distinguish which instance of f is meant? e.g. f(x = 5) or f(y=a+b) are legal, but f(5) is not?
There was a problem hiding this comment.
Thank you for pointing it out, it was actually a typo, where I fixed the example to:
extern void f(in bit<8> x);
extern void f(in bit<8> y); // allowed: different parameter names
extern void f(in bit<16> x, in bit<8> y); // allowed: different number of parameters
extern void f(in bit<16> x, in bit<8> y); // Error: duplicate declaration of previous functionSo I believe this resolves (1) and (2), sorry for making the confusion. And yes, declaration such as extern void f(in bit<16> x, in bit<8> x); is already invalid because it has duplicate parameter names.
For (3), in P4, overload resolution occurs via parameter names and the number of parameters. So, extern void f(in bit<8> x); and extern void f(in bit<8> y); are overloaded, and the caller MUST explicitly disambiguate by specifying either x=... or y=... in the argument. It is an error if they cannot be distinguished, as per the spec:
When overloading is used, the compiler must be able to disambiguate at compile-time which method or function is being called, either by the number of arguments or by the names of the arguments, when calls are specifying argument names. (7.2.11.2)
Signed-off-by: jaehyun1ee <99jaehyunlee@kaist.ac.kr>
This PR revises the specification to disallow duplicate declarations within the same scope. This has been discussed in P4 LDWG.
Some related past issues in p4-spec and p4c are:
In particular, this PR modifies sections 6.6.1. Scopes, 6.9. Name Resolution, and 7.2.11.2. Extern Objects.