Skip to content

Commit

Permalink
Improve diagnostics for StructExpression (#4357)
Browse files Browse the repository at this point in the history
* Improve diagnostics for StructExpression
* Move function def to expression.def.
* Properly initialize empty cstring.
  • Loading branch information
kfcripps authored Jan 25, 2024
1 parent 955f3af commit affc088
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion backends/p4tools/p4tools.def
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public:
inline cstring concolicMethodName = "";

toString {
cstring argumentStr;
cstring argumentStr = "";
cstring sep = "";
for (const auto *arg : *arguments) {
argumentStr += sep + arg->toString();
Expand Down
12 changes: 12 additions & 0 deletions ir/expression.def
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,18 @@ class StructExpression : Expression {
size_t size = components.size();
return components.at(size - 1)->is<IR::NamedDots>();
}
toString {
cstring str = "{";
if (!components.empty()) {
cstring exprStr = components.at(0)->expression->toString();
str += " " + components.at(0)->toString() + " = " + exprStr;
}
for (unsigned i = 1; i < size(); i++) {
cstring exprStr = components.at(i)->expression->toString();
str += ", " + components.at(i)->toString() + " = " + exprStr;
}
return str + " }";
}
}

/// Can be an invalid header or header_union
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_errors_outputs/issue2220.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
issue2220.p4(11): [--Werror=type-error] error: StructExpression: values of type 'bit<8>' cannot be implicitly cast to type 'myEnum'
issue2220.p4(11): [--Werror=type-error] error: { val = 8w0 }: values of type 'bit<8>' cannot be implicitly cast to type 'myEnum'
S s1 = { val = (bit<8>)0 };
^^^^^^^^^^^^^^^^^^^
issue2220.p4(3)
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_errors_outputs/issue3671-2.p4-stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
issue3671-2.p4(17): [--Werror=type-error] error: StructExpression: argument does not match declaration in actions list: StructExpression
issue3671-2.p4(17): [--Werror=type-error] error: { f1 = ext1, f0 = ext2 }: argument does not match declaration in actions list: { f0 = ext1, f1 = ext2 }
default_action = a1({f1 = ext1(),f0 = ext2()});
^^^^^^^^^^^^^^^^^^^^^^^^^
issue3671-2.p4(15)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ structure-valued-expr-errs-1.p4(31)
header h1_t {
^^^^
---- Originating from:
structure-valued-expr-errs-1.p4(112): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h1_t'
structure-valued-expr-errs-1.p4(112): Source expression '{ f2 = 5, f1 = 2 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h1_t'
hdr.h1 = {f2=5, f1=2};
^^^^^^^^^^^^
structure-valued-expr-errs-1.p4(112)
Expand All @@ -186,7 +186,7 @@ structure-valued-expr-errs-1.p4(32): No initializer for field f1
bit<8> f1;
^^
---- Originating from:
structure-valued-expr-errs-1.p4(113): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h1_t'
structure-valued-expr-errs-1.p4(113): Source expression '{ f2 = 5 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h1_t'
hdr.h1 = {f2=5};
^^^^^^
structure-valued-expr-errs-1.p4(113)
Expand All @@ -206,7 +206,7 @@ structure-valued-expr-errs-1.p4(43)
struct s1_t {
^^^^
---- Originating from:
structure-valued-expr-errs-1.p4(114): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s1_t'
structure-valued-expr-errs-1.p4(114): Source expression '{ f2 = 5, f1 = 2 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s1_t'
hdr.hstructs.s1 = {f2=5, f1=2};
^^^^^^^^^^^^
structure-valued-expr-errs-1.p4(114)
Expand All @@ -223,7 +223,7 @@ structure-valued-expr-errs-1.p4(44): No initializer for field f1
bit<8> f1;
^^
---- Originating from:
structure-valued-expr-errs-1.p4(115): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s1_t'
structure-valued-expr-errs-1.p4(115): Source expression '{ f2 = 5 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s1_t'
hdr.hstructs.s1 = {f2=5};
^^^^^^
structure-valued-expr-errs-1.p4(115)
Expand All @@ -243,7 +243,7 @@ structure-valued-expr-errs-1.p4(35)
header h2_t {
^^^^
---- Originating from:
structure-valued-expr-errs-1.p4(120): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h2_t'
structure-valued-expr-errs-1.p4(120): Source expression '{ f2 = 5 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'header h2_t'
hdr.h2 = {f2=5};
^^^^^^
structure-valued-expr-errs-1.p4(120)
Expand All @@ -263,7 +263,7 @@ structure-valued-expr-errs-1.p4(47)
struct s2_t {
^^^^
---- Originating from:
structure-valued-expr-errs-1.p4(121): Source expression 'StructExpression' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s2_t'
structure-valued-expr-errs-1.p4(121): Source expression '{ f2 = 5 }' produces a result of type 'unknown struct' which cannot be assigned to a left-value with type 'struct s2_t'
hdr.hstructs.s2 = {f2=5};
^^^^^^
structure-valued-expr-errs-1.p4(121)
Expand Down

0 comments on commit affc088

Please sign in to comment.