-
Notifications
You must be signed in to change notification settings - Fork 494
Updates to Switch expr #5430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MathewZach123
wants to merge
1
commit into
p4lang:main
Choose a base branch
from
MathewZach123:test_5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Updates to Switch expr #5430
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| @command_line("--preferSwitch") | ||
|
|
||
| extern void __e(in bit<28> arg); | ||
| extern void __e2(in bit<28> arg); | ||
|
|
||
| control C() { | ||
| action bar(bool a, bool b) { | ||
| bit<28> x; bit<28> y; | ||
| switch (a) { | ||
| b: { | ||
| __e(x); | ||
| } | ||
| default: { | ||
| if (b) { | ||
| __e2(y); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| action baz() { | ||
| bar(true, false); | ||
| } | ||
|
|
||
| action foo() { | ||
| baz(); | ||
|
|
||
| } | ||
|
|
||
| table t { | ||
| actions = { foo; } | ||
| default_action = foo; | ||
| } | ||
|
|
||
| apply { | ||
| t.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(); | ||
| package top(proto p); | ||
|
|
||
| top(C()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| @command_line("--preferSwitch") | ||
|
|
||
| // Generic helper over a type variable T. | ||
| // The switch is written in terms of T; T is instantiated later. | ||
| void bar_generic<T>(T v, out bit<8> y) { | ||
| switch (v) { | ||
| 0b01: { y = 1; } | ||
| 0b10: { y = 2; } | ||
| default: { y = 0; } | ||
| } | ||
| } | ||
|
|
||
| control C(out bit<8> y) { | ||
| action foo() { | ||
| // Here T is instantiated as bit<2>, so switch(v) is on bit<2>. | ||
| bar_generic<bit<2>>(1, y); | ||
| } | ||
|
|
||
| table t { | ||
| actions = { foo; } | ||
| default_action = foo; | ||
| } | ||
|
|
||
| apply { | ||
| t.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(out bit<8> y); | ||
| package top(proto p); | ||
|
|
||
| top(C()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| @command_line("--preferSwitch") | ||
|
|
||
| type bit<2> TwoBitsAlias; | ||
|
|
||
| void bar(bit<2> x, out bit<8> y) { | ||
| TwoBitsAlias a = (TwoBitsAlias)x; | ||
| switch (a) { | ||
| 0b01: { y = 4; } | ||
| 0b10: { y = 5; } | ||
| default: { y = y; } | ||
| } | ||
| } | ||
|
|
||
| control C(out bit<8> y) { | ||
| action foo() { | ||
| bar(1, y); | ||
| } | ||
|
|
||
| table t { | ||
| actions = { foo; } | ||
| default_action = foo; | ||
| } | ||
|
|
||
| apply { | ||
| t.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(out bit<8> y); | ||
| package top(proto p); | ||
|
|
||
| top(C()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| @command_line("--preferSwitch") extern void __e(in bit<28> arg); | ||
| extern void __e2(in bit<28> arg); | ||
| control C() { | ||
| action bar(bool a, bool b) { | ||
| bit<28> x; | ||
| bit<28> y; | ||
| switch (a) { | ||
| b: { | ||
| __e(x); | ||
| } | ||
| default: { | ||
| if (b) { | ||
| __e2(y); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| action baz() { | ||
| bar(true, false); | ||
| } | ||
| action foo() { | ||
| baz(); | ||
| } | ||
| table t { | ||
| actions = { | ||
| foo; | ||
| } | ||
| default_action = foo; | ||
| } | ||
| apply { | ||
| t.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(); | ||
| package top(proto p); | ||
| top(C()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| issue5120.p4(9): [--Werror=type-error] error: a: switch expression cannot be of type bool | ||
| switch (a) { | ||
| ^ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| @command_line("--preferSwitch") void bar_generic<T>(T v, out bit<8> y) { | ||
| switch (v) { | ||
| 0b1: { | ||
| y = 1; | ||
| } | ||
| 0b10: { | ||
| y = 2; | ||
| } | ||
| default: { | ||
| y = 0; | ||
| } | ||
| } | ||
| } | ||
| control C(out bit<8> y) { | ||
| action foo() { | ||
| bar_generic<bit<2>>(1, y); | ||
| } | ||
| table t { | ||
| actions = { | ||
| foo; | ||
| } | ||
| default_action = foo; | ||
| } | ||
| apply { | ||
| t.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(out bit<8> y); | ||
| package top(proto p); | ||
| top(C()) main; |
3 changes: 3 additions & 0 deletions
3
testdata/p4_16_errors_outputs/issue5313_type_variable.p4-stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| issue5313_type_variable.p4(6): [--Werror=type-error] error: v: switch expression cannot be of type T | ||
| switch (v) { | ||
| ^ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| @command_line("--preferSwitch") type bit<2> TwoBitsAlias; | ||
| void bar(bit<2> x, out bit<8> y) { | ||
| TwoBitsAlias a = (TwoBitsAlias)x; | ||
| switch (a) { | ||
| 0b1: { | ||
| y = 4; | ||
| } | ||
| 0b10: { | ||
| y = 5; | ||
| } | ||
| default: { | ||
| y = y; | ||
| } | ||
| } | ||
| } | ||
| control C(out bit<8> y) { | ||
| action foo() { | ||
| bar(1, y); | ||
| } | ||
| table t { | ||
| actions = { | ||
| foo; | ||
| } | ||
| default_action = foo; | ||
| } | ||
| apply { | ||
| t.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(out bit<8> y); | ||
| package top(proto p); | ||
| top(C()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| issue5313_typealiass.p4(7): [--Werror=type-error] error: a: switch expression cannot be of type TwoBitsAlias | ||
| switch (a) { | ||
| ^ |
2 changes: 1 addition & 1 deletion
2
testdata/p4_16_errors_outputs/stmt-switch-on-unsupported-type.p4-stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| stmt-switch-on-unsupported-type.p4(3): [--Wwarn=unused] warning: control 'ctrl' is unused | ||
| control ctrl()() { | ||
| ^^^^ | ||
| stmt-switch-on-unsupported-type.p4(5): [--Werror=type-error] error: ...: could not find default value | ||
| stmt-switch-on-unsupported-type.p4(5): [--Werror=type-error] error: ...: switch expression cannot be of type ANYTYPE | ||
| switch (...) { } | ||
| ^^^ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| @command_line("--preferSwitch") | ||
|
|
||
| void bar(bit<2> x, out bit<8> y) { | ||
| switch (x) { | ||
| 0b01: { y = 2; } | ||
| 0b10: { y = 3; } | ||
| } | ||
| } | ||
|
|
||
| control C(out bit<8> y) { | ||
| action foo() { | ||
| bar(1, y); | ||
| } | ||
|
|
||
| table t { | ||
| actions = { foo; } | ||
| default_action = foo; | ||
| } | ||
|
|
||
| apply { | ||
| t.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(out bit<8> y); | ||
| package top(proto p); | ||
|
|
||
| top(C()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| @command_line("--preferSwitch") | ||
|
|
||
| typedef bit<2> TwoBits; | ||
|
|
||
| void bar(TwoBits x, out bit<8> y) { | ||
| switch (x) { | ||
| 0b01: { y = 2; } | ||
| 0b10: { y = 3; } | ||
| } | ||
| } | ||
|
|
||
| control C(out bit<8> y) { | ||
| action foo() { | ||
| bar(1, y); | ||
| } | ||
|
|
||
| table t { | ||
| actions = { foo; } | ||
| default_action = foo; | ||
| } | ||
|
|
||
| apply { | ||
| t.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(out bit<8> y); | ||
| package top(proto p); | ||
|
|
||
| top(C()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| @command_line("--preferSwitch") void bar(bit<2> x, out bit<8> y) { | ||
| switch (x) { | ||
| 2w0b1: { | ||
| y = 8w2; | ||
| } | ||
| 2w0b10: { | ||
| y = 8w3; | ||
| } | ||
| } | ||
| } | ||
| control C(out bit<8> y) { | ||
| action foo() { | ||
| bar(2w1, y); | ||
| } | ||
| table t { | ||
| actions = { | ||
| foo(); | ||
| } | ||
| default_action = foo(); | ||
| } | ||
| apply { | ||
| t.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(out bit<8> y); | ||
| package top(proto p); | ||
| top(C()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| control C(out bit<8> y) { | ||
| @name("C.y_0") bit<8> y_1; | ||
| @name("C.foo") action foo() { | ||
| switch (2w1) { | ||
| 2w0b1: { | ||
| y_1 = 8w2; | ||
| } | ||
| 2w0b10: { | ||
| y_1 = 8w3; | ||
| } | ||
| } | ||
| y = y_1; | ||
| } | ||
| @name("C.t") table t_0 { | ||
| actions = { | ||
| foo(); | ||
| } | ||
| default_action = foo(); | ||
| } | ||
| apply { | ||
| t_0.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(out bit<8> y); | ||
| package top(proto p); | ||
| top(C()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| control C(out bit<8> y) { | ||
| @name("C.y_0") bit<8> y_1; | ||
| @name("C.foo") action foo() { | ||
| switch (2w1) { | ||
| 2w0b1: { | ||
| y_1 = 8w2; | ||
| } | ||
| 2w0b10: { | ||
| y_1 = 8w3; | ||
| } | ||
| } | ||
| y = y_1; | ||
| } | ||
| @name("C.t") table t_0 { | ||
| actions = { | ||
| foo(); | ||
| } | ||
| default_action = foo(); | ||
| } | ||
| apply { | ||
| t_0.apply(); | ||
| } | ||
| } | ||
|
|
||
| control proto(out bit<8> y); | ||
| package top(proto p); | ||
| top(C()) main; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's about typedefs and type aliases? Are they already resolved here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we have any test cases that cover that. Also of potential concern is a Type_Variable (where the type may not have been inferenced yet). Best would be to add test cases to cover all of those.