Skip to content

Fix #5653: Infer type for untyped {#} literals in header contexts#5661

Closed
Sanketjadhav31 wants to merge 1 commit into
p4lang:mainfrom
Sanketjadhav31:fix-issue-5653
Closed

Fix #5653: Infer type for untyped {#} literals in header contexts#5661
Sanketjadhav31 wants to merge 1 commit into
p4lang:mainfrom
Sanketjadhav31:fix-issue-5653

Conversation

@Sanketjadhav31

Copy link
Copy Markdown

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 == {#} and meta.h = {#} do not work because the {#} expression is considered as an untyped Invalid expression 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 to InvalidHeader and InvalidHeaderUnion expressions.
  • Contextual type inference is done for comparison (==, !=) operations where {#} is used.
  • Conversion logic is reused while type checking for assignment operation.
  • Regression test is added for compare/assignment of headers using untyped {#} literals.

Testing

  • Checked if meta.h == {#} and meta.h != {#} expressions can be compiled successfully.
  • Checked if meta.h = {#} works without casting the {#} literal to header.
  • Executed the new regression test (issue5653.p4).

Fixes #5653

Signed-off-by: Sanketjadhav31 <sj546400@gmail.com>
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

result = new IR::InvalidHeader(expr->srcInfo, type, type);
} else {
result = new IR::InvalidHeaderUnion(expr->srcInfo, type, type);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comparison with {#} literal without explicit cast fails to compile

2 participants