Fix #5653: Infer type for untyped {#} literals in header contexts#5661
Closed
Sanketjadhav31 wants to merge 1 commit into
Closed
Fix #5653: Infer type for untyped {#} literals in header contexts#5661Sanketjadhav31 wants to merge 1 commit into
Sanketjadhav31 wants to merge 1 commit into
Conversation
Signed-off-by: Sanketjadhav31 <sj546400@gmail.com>
ChrisDodd
reviewed
Jun 22, 2026
| const IR::Expression *sourceExpression) { | ||
| if (destType->is<IR::Type_Unknown>()) BUG("Unknown destination type"); | ||
| if (destType->is<IR::Type_Dontcare>()) return sourceExpression; | ||
| sourceExpression = convertUntypedInvalid(sourceExpression, destType); |
Contributor
There was a problem hiding this comment.
Should only do this if sourceExpression->type == Type_Uknown? Or can that case not happen, as we'll never just set the type on an IR::Invalid; it will always be converted to a InvalidHeader or InvalidHeaderUnion instead? In which case, why the check for Type_Unknown in tyepCheckExpr.cpp below?
ChrisDodd
reviewed
Jun 22, 2026
| result = new IR::InvalidHeader(expr->srcInfo, type, type); | ||
| } else { | ||
| result = new IR::InvalidHeaderUnion(expr->srcInfo, type, type); | ||
| } |
Contributor
There was a problem hiding this comment.
Could refactor the code at typeCheckExpr.cpp lines 1196-1217 to call this; we probably want the error check from there here too (in case concreteType ends up being something other than header/header_union somehow)
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.
Summary
This PR addresses the issue of type checking of untyped
{#}literals when they are used in header or header union compare or assignment expressions.At present, expressions like
meta.h == {#}andmeta.h = {#}do not work because the{#}expression is considered as an untypedInvalidexpression and no contextual type is inferred. This PR changes the way untyped{#}literals are converted by making sure they are converted to the proper typed invalid expression considering their contexts.Changes
convertUntypedInvalid()function is added to convert untyped{#}literals toInvalidHeaderandInvalidHeaderUnionexpressions.==,!=) operations where{#}is used.{#}literals.Testing
meta.h == {#}andmeta.h != {#}expressions can be compiled successfully.meta.h = {#}works without casting the{#}literal toheader.issue5653.p4).Fixes #5653