From bcde4cd0bbf1e4f5f4e90c21da0b2e0b27b007db Mon Sep 17 00:00:00 2001 From: Kyle Cripps Date: Wed, 24 Jan 2024 07:16:33 -0800 Subject: [PATCH] Improve diagnostics for StructExpression --- ir/expression.cpp | 13 +++++++++++++ ir/expression.def | 1 + testdata/p4_16_errors_outputs/issue2220.p4-stderr | 2 +- testdata/p4_16_errors_outputs/issue3671-2.p4-stderr | 2 +- .../structure-valued-expr-errs-1.p4-stderr | 12 ++++++------ 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/ir/expression.cpp b/ir/expression.cpp index fc3a004074f..55016a413b2 100644 --- a/ir/expression.cpp +++ b/ir/expression.cpp @@ -145,3 +145,16 @@ IR::Constant IR::Constant::operator-() const { return IR::Constant(-value); } IR::Constant IR::Constant::GetMask(unsigned width) { return (IR::Constant(1) << width) - IR::Constant(1); } + +cstring IR::StructExpression::toString() const { + 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 + " }"; +} diff --git a/ir/expression.def b/ir/expression.def index 7c58a4d6d81..bbbf30e0a02 100644 --- a/ir/expression.def +++ b/ir/expression.def @@ -482,6 +482,7 @@ class StructExpression : Expression { size_t size = components.size(); return components.at(size - 1)->is(); } + cstring toString() const; } /// Can be an invalid header or header_union diff --git a/testdata/p4_16_errors_outputs/issue2220.p4-stderr b/testdata/p4_16_errors_outputs/issue2220.p4-stderr index df80b4ab415..20a248d690e 100644 --- a/testdata/p4_16_errors_outputs/issue2220.p4-stderr +++ b/testdata/p4_16_errors_outputs/issue2220.p4-stderr @@ -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) diff --git a/testdata/p4_16_errors_outputs/issue3671-2.p4-stderr b/testdata/p4_16_errors_outputs/issue3671-2.p4-stderr index 4040627b761..c1552819099 100644 --- a/testdata/p4_16_errors_outputs/issue3671-2.p4-stderr +++ b/testdata/p4_16_errors_outputs/issue3671-2.p4-stderr @@ -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) diff --git a/testdata/p4_16_errors_outputs/structure-valued-expr-errs-1.p4-stderr b/testdata/p4_16_errors_outputs/structure-valued-expr-errs-1.p4-stderr index f9b65139c5b..28221a7ce76 100644 --- a/testdata/p4_16_errors_outputs/structure-valued-expr-errs-1.p4-stderr +++ b/testdata/p4_16_errors_outputs/structure-valued-expr-errs-1.p4-stderr @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)