Skip to content

Commit

Permalink
Improve error message when select types do not match (p4lang#3352)
Browse files Browse the repository at this point in the history
* Improve error message when select types do not match

Signed-off-by: Mihai Budiu <mbudiu@vmware.com>

* Changed reference output

Signed-off-by: Mihai Budiu <mbudiu@vmware.com>
  • Loading branch information
Mihai Budiu authored and github-sajan committed May 26, 2022
1 parent c7aacc0 commit 393f3c0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
7 changes: 4 additions & 3 deletions frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3610,9 +3610,10 @@ TypeInference::matchCase(const IR::SelectExpression* select, const IR::Type_Base
}
useSelType = selectType->components.at(0);
}
auto tvs = unify(select, useSelType, caseType,
"'match' case label type '%1%' does not match expected type '%2%'",
{ caseType, useSelType });
auto tvs = unify(
select, useSelType, caseType,
"'match' case label '%1%' has type '%2%' which does not match the expected type '%3%'",
{ selectCase->keyset, caseType, useSelType });
if (tvs == nullptr)
return nullptr;
ConstantTypeSubstitution cts(tvs, refMap, typeMap, this);
Expand Down
8 changes: 8 additions & 0 deletions testdata/p4_16_errors/issue3345.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parser MyParser2(in bool t) {
state start {
transition select(t) {
1w1: accept;
_: reject;
}
}
}
4 changes: 3 additions & 1 deletion testdata/p4_16_errors_outputs/issue1986-1.p4-stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ issue1986-1.p4(22): [--Werror=type-error] error: SelectExpression
---- Actual error:
Cannot unify type 'bit<0>' with type 'bit<2>'
---- Originating from:
'match' case label type 'bit<0>' does not match expected type 'bit<2>'
issue1986-1.p4(23): 'match' case label '0' has type 'bit<0>' which does not match the expected type 'bit<2>'
0w0: parse_h2;
^^^
5 changes: 4 additions & 1 deletion testdata/p4_16_errors_outputs/issue1986.p4-stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ issue1986.p4(17): [--Werror=type-error] error: SelectExpression
---- Actual error:
Cannot unify type 'bit<0>' with type 'bit<1>'
---- Originating from:
issue1986.p4(18): 'match' case label type 'tuple<bit<0>>' does not match expected type 'tuple<bit<1>>'
issue1986.p4(18): 'match' case label 'ListExpression' has type 'tuple<bit<0>>' which does not match the expected type 'tuple<bit<1>>'
{0w0}: accept;
^^^^^
issue1986.p4(18)
{0w0}: accept;
^^^^^
issue1986.p4(17)
Expand Down
9 changes: 9 additions & 0 deletions testdata/p4_16_errors_outputs/issue3345.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parser MyParser2(in bool t) {
state start {
transition select(t) {
1w1: accept;
default: reject;
}
}
}

14 changes: 14 additions & 0 deletions testdata/p4_16_errors_outputs/issue3345.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
issue3345.p4(3): [--Werror=type-error] error: SelectExpression
transition select(t) {
^
---- Actual error:
issue3345.p4(1): Cannot unify type 'bit<1>' with type 'bool'
parser MyParser2(in bool t) {
^^^^
---- Originating from:
issue3345.p4(4): 'match' case label '1w1' has type 'bit<1>' which does not match the expected type 'bool'
1w1: accept;
^^^
issue3345.p4(1)
parser MyParser2(in bool t) {
^^^^

0 comments on commit 393f3c0

Please sign in to comment.