From affc088829fe8a0d65b8a524999cb74bb88aec9d Mon Sep 17 00:00:00 2001 From: Kyle Cripps <60898032+kfcripps@users.noreply.github.com> Date: Thu, 25 Jan 2024 11:29:58 -0800 Subject: [PATCH] Improve diagnostics for `StructExpression` (#4357) * Improve diagnostics for StructExpression * Move function def to expression.def. * Properly initialize empty cstring. --- backends/p4tools/p4tools.def | 2 +- ir/expression.def | 12 ++++++++++++ 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, 21 insertions(+), 9 deletions(-) diff --git a/backends/p4tools/p4tools.def b/backends/p4tools/p4tools.def index dc4ba07728..988be28034 100644 --- a/backends/p4tools/p4tools.def +++ b/backends/p4tools/p4tools.def @@ -208,7 +208,7 @@ public: inline cstring concolicMethodName = ""; toString { - cstring argumentStr; + cstring argumentStr = ""; cstring sep = ""; for (const auto *arg : *arguments) { argumentStr += sep + arg->toString(); diff --git a/ir/expression.def b/ir/expression.def index 7c58a4d6d8..e708327d2c 100644 --- a/ir/expression.def +++ b/ir/expression.def @@ -482,6 +482,18 @@ class StructExpression : Expression { size_t size = components.size(); return components.at(size - 1)->is(); } + 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 diff --git a/testdata/p4_16_errors_outputs/issue2220.p4-stderr b/testdata/p4_16_errors_outputs/issue2220.p4-stderr index df80b4ab41..20a248d690 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 4040627b76..c155281909 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 f9b65139c5..28221a7ce7 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)