Reject duplicate match_kind declarations#5656
Open
aeron-gh wants to merge 1 commit into
Open
Conversation
Per P4-16 spec section 7.1.3, all match_kind identifiers are inserted into the top-level namespace and it is an error to declare the same one more than once. p4c already enforced this within a single match_kind block but silently accepted duplicates spread across separate blocks, including the common case of a program redeclaring a match_kind that core.p4 already defines. Handle match_kind the same way the frontend already handles error: the parser driver merges every match_kind declaration into a single node, so IndexedVector emits the duplicate-declaration error, and ToP4 prints only the user-defined members so the merged node is not hidden behind the system include. pipe.p4 redeclared ternary and exact from core.p4, so its redundant block is removed. Other samples that add genuinely new match_kinds now print that block next to the include, mirroring how error blocks are emitted. Fixes p4lang#5085 Signed-off-by: aeron-gh <agab0323@gmail.com>
0329297 to
14b30a0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5085.
The P4-16 spec (section 7.1.3) says all
match_kindidentifiers are inserted into the top-level namespace and that it is an error to declare the same one more than once. The frontend already enforced this within a singlematch_kindblock, but duplicates spread across separate blocks were silently accepted, e.g.:This also let programs redeclare a
match_kindalready defined bycore.p4(the existingpipe.p4sample did exactly this).Approach
errordeclarations already have this exact behavior, so I followed the same pattern:P4ParserDrivernow merges everymatch_kinddeclaration into a single node (onReadMatchKindDeclaration), the same wayonReadErrorDeclarationworks. Appending the members into the sharedIndexedVectoris what makes the existing duplicate-declaration check fire across blocks.core.p4),ToP4is taught to always visitDeclaration_MatchKindand print only the user-defined members, mirroring howType_Erroris handled. Otherwise user-added match_kinds would be hidden behind the#include.Test / sample updates
decl-matchkind-with-duplicate-fields-in-separate-declarationscovers duplicates across separate blocks.pipe.p4redeclaredternaryandexactfromcore.p4; that redundant block is removed.issue982, the twodashpipelines) now print that block right after the include, the same way mergederrorblocks are emitted. Reference outputs are regenerated accordingly.