Skip to content

Conversation

@jaehyun1ee
Copy link
Contributor

This PR revises the specification to disallow duplicate declarations within the same scope. This has been discussed in P4 LDWG.

  • It disallows duplicate declarations, e.g.,
control p() {
    const bit<8> x = 8;
    const bit<4> x = 4;   // Error: duplicate declaration of x
    apply {}
}
  • But it still allows declaration shadowing (same identifier in an inner scope), e.g.,
const bit<4> x = 1;
control p() {
    const bit<8> x = 8;   // x declaration shadows global x
    apply {}
}
  • And it still allows functions and methods with the same names (up to overload resolution). Recall that P4 allows overloading of functions and methods, when they can be differentiated by either the number of parameters or the names of parameters:
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

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.

  • 6.6.1 add that duplicate declarations are disallowed.
  • 6.9 adds a detailed explanation of duplicate declaration, and also explains that name shadowing (up to compiler warning) and function/method overloading are still allowed.
  • The part explaining function/method overloading in 7.2.11.2 was moved to 6.9.

@jaehyun1ee jaehyun1ee added the ldwg-discussion Plan to discuss at next LDWG label Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ldwg-discussion Plan to discuss at next LDWG

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant