From fd82d6202db2b5d8a80972bee0bf804e549ebdb9 Mon Sep 17 00:00:00 2001 From: Mihai Budiu Date: Wed, 20 Apr 2022 16:50:42 -0700 Subject: [PATCH] Do not allow casts to struct types Signed-off-by: Mihai Budiu --- frontends/p4/typeChecking/typeChecker.cpp | 10 ++++------ testdata/p4_16_errors/issue3233.p4 | 15 +++++++++++++++ testdata/p4_16_errors_outputs/issue3233.p4 | 11 +++++++++++ testdata/p4_16_errors_outputs/issue3233.p4-stderr | 6 ++++++ 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 testdata/p4_16_errors/issue3233.p4 create mode 100644 testdata/p4_16_errors_outputs/issue3233.p4 create mode 100644 testdata/p4_16_errors_outputs/issue3233.p4-stderr diff --git a/frontends/p4/typeChecking/typeChecker.cpp b/frontends/p4/typeChecking/typeChecker.cpp index 918f37c58a..6f2f7ea566 100644 --- a/frontends/p4/typeChecking/typeChecker.cpp +++ b/frontends/p4/typeChecking/typeChecker.cpp @@ -2605,11 +2605,7 @@ const IR::Node* TypeInference::postorder(IR::Cast* expression) { setType(result, st); return result; } else { - auto sit = getTypeType(se->type); - if (typeMap->equivalent(st, sit)) - return expression->expr; - else - typeError("%1%: cast not supported", expression->destType); + typeError("%1%: cast not supported", expression->destType); return expression; } } else if (auto le = expression->expr->to()) { @@ -2621,8 +2617,10 @@ const IR::Node* TypeInference::postorder(IR::Cast* expression) { auto src = assignment(expression, fieldI->type, compI); vec.push_back(new IR::NamedExpression(fieldI->name, src)); } + auto setype = castType->getP4Type(); + setType(setype, new IR::Type_Type(st)); auto result = new IR::StructExpression( - le->srcInfo, castType->getP4Type(), vec); + le->srcInfo, setype, setype, vec); setType(result, st); return result; } else { diff --git a/testdata/p4_16_errors/issue3233.p4 b/testdata/p4_16_errors/issue3233.p4 new file mode 100644 index 0000000000..5968db59fe --- /dev/null +++ b/testdata/p4_16_errors/issue3233.p4 @@ -0,0 +1,15 @@ +struct s +{ + bit t; + bit t1; +} + +s func(bit t, bit t1) +{ + return (s)(s)(s){t, t1}; +} + +s func1(s t1) +{ + return (s)(s)(s)t1; +} diff --git a/testdata/p4_16_errors_outputs/issue3233.p4 b/testdata/p4_16_errors_outputs/issue3233.p4 new file mode 100644 index 0000000000..eaec138c1d --- /dev/null +++ b/testdata/p4_16_errors_outputs/issue3233.p4 @@ -0,0 +1,11 @@ +struct s { + bit<1> t; + bit<1> t1; +} + +s func(bit<1> t, bit<1> t1) { + return (s)(s)(s){ t, t1 }; +} +s func1(s t1) { + return (s)(s)(s)t1; +} diff --git a/testdata/p4_16_errors_outputs/issue3233.p4-stderr b/testdata/p4_16_errors_outputs/issue3233.p4-stderr new file mode 100644 index 0000000000..55f61223b0 --- /dev/null +++ b/testdata/p4_16_errors_outputs/issue3233.p4-stderr @@ -0,0 +1,6 @@ +issue3233.p4(9): [--Werror=type-error] error: s: cast not supported + return (s)(s)(s){t, t1}; + ^ +issue3233.p4(14): [--Werror=type-error] error: s: cast not supported + return (s)(s)(s)t1; + ^