diff --git a/frontends/p4/typeChecking/typeChecker.cpp b/frontends/p4/typeChecking/typeChecker.cpp index f06f33bd08..e7f52fb882 100644 --- a/frontends/p4/typeChecking/typeChecker.cpp +++ b/frontends/p4/typeChecking/typeChecker.cpp @@ -3052,6 +3052,9 @@ const IR::Node *TypeInference::postorder(IR::PathExpression *expression) { if (type != nullptr) // may be nullptr because typechecking may have failed type = cloneWithFreshTypeVariables(type->to()); + } else if (decl->is()) { + typeError("%1%: Type cannot be used here, expecting an expression.", expression); + return expression; } if (type == nullptr) { diff --git a/testdata/p4_16_errors/type-in-expr-lex0.p4 b/testdata/p4_16_errors/type-in-expr-lex0.p4 new file mode 100644 index 0000000000..f456861c6f --- /dev/null +++ b/testdata/p4_16_errors/type-in-expr-lex0.p4 @@ -0,0 +1,9 @@ +// tests problems related to https://github.com/p4lang/p4c/pull/4411 + +#include + +control ctrl(bool val); + +package pkg( + ctrl val = ctrl(H > 3) +); diff --git a/testdata/p4_16_errors/type-in-expr-lex1.p4 b/testdata/p4_16_errors/type-in-expr-lex1.p4 new file mode 100644 index 0000000000..6db9804577 --- /dev/null +++ b/testdata/p4_16_errors/type-in-expr-lex1.p4 @@ -0,0 +1,10 @@ +// tests problems related to https://github.com/p4lang/p4c/pull/4411 + +#include + +parser pars(in bit<16> buf, out H hdrs) { + bool var; + state start { + var = buf > H; + } +} diff --git a/testdata/p4_16_errors/type-in-expr.p4 b/testdata/p4_16_errors/type-in-expr.p4 new file mode 100644 index 0000000000..57cfea6cd2 --- /dev/null +++ b/testdata/p4_16_errors/type-in-expr.p4 @@ -0,0 +1,10 @@ +// tests problem solved by https://github.com/p4lang/p4c/pull/4411 + +#include + +@foo[bar=4(in H hdrs, out bool flag) +{ + apply { + } +} diff --git a/testdata/p4_16_errors_outputs/type-in-expr-lex0.p4-stderr b/testdata/p4_16_errors_outputs/type-in-expr-lex0.p4-stderr new file mode 100644 index 0000000000..7e0d1bfe23 --- /dev/null +++ b/testdata/p4_16_errors_outputs/type-in-expr-lex0.p4-stderr @@ -0,0 +1,4 @@ +type-in-expr-lex0.p4(8):syntax error, unexpected >, expecting ( + ctrl val = ctrl(H > + ^ +[--Werror=overlimit] error: 1 errors encountered, aborting compilation diff --git a/testdata/p4_16_errors_outputs/type-in-expr-lex1.p4-stderr b/testdata/p4_16_errors_outputs/type-in-expr-lex1.p4-stderr new file mode 100644 index 0000000000..5fcf398beb --- /dev/null +++ b/testdata/p4_16_errors_outputs/type-in-expr-lex1.p4-stderr @@ -0,0 +1,4 @@ +type-in-expr-lex1.p4(8):syntax error, unexpected ;, expecting ( + var = buf > H; + ^ +[--Werror=overlimit] error: 1 errors encountered, aborting compilation diff --git a/testdata/p4_16_errors_outputs/type-in-expr.p4 b/testdata/p4_16_errors_outputs/type-in-expr.p4 new file mode 100644 index 0000000000..b260bf5c0c --- /dev/null +++ b/testdata/p4_16_errors_outputs/type-in-expr.p4 @@ -0,0 +1,7 @@ +#include + +@foo[bar=4 < H] control p(in H hdrs, out bool flag) { + apply { + } +} + diff --git a/testdata/p4_16_errors_outputs/type-in-expr.p4-stderr b/testdata/p4_16_errors_outputs/type-in-expr.p4-stderr new file mode 100644 index 0000000000..d5e1399530 --- /dev/null +++ b/testdata/p4_16_errors_outputs/type-in-expr.p4-stderr @@ -0,0 +1,12 @@ +type-in-expr.p4(5): [--Werror=type-error] error: H: Type cannot be used here, expecting an expression. +@foo[bar=4(in H hdrs, out bool flag) + ^