Skip to content

Updates to Switch expr#5430

Draft
MathewZach123 wants to merge 1 commit intop4lang:mainfrom
MathewZach123:test_5
Draft

Updates to Switch expr#5430
MathewZach123 wants to merge 1 commit intop4lang:mainfrom
MathewZach123:test_5

Conversation

@MathewZach123
Copy link
Contributor

This PR aims to add a type check for generalized switch statements so that the switch expression is only allowed to be bit<W>, int<W>, enum (with or without an underlying type), or error, matching P4-16 spec https://p4.org/wp-content/uploads/sites/53/2024/10/P4-16-spec-v1.2.5.html#sec-switch-stmt

@MathewZach123 MathewZach123 marked this pull request as draft December 1, 2025 20:03
}
} else {
// switch (expression)
if (!(type->is<IR::Type_Bits>() || type->is<IR::Type_InfInt>() ||
Copy link
Contributor

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?

Copy link
Contributor

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.

@MathewZach123 MathewZach123 force-pushed the test_5 branch 3 times, most recently from 3e478ed to 5abda23 Compare December 1, 2025 22:35
@kfcripps kfcripps requested review from ChrisDodd and fruffy December 1, 2025 22:36
@kfcripps kfcripps added the core Topics concerning the core segments of the compiler (frontend, midend, parser) label Dec 1, 2025
@MathewZach123 MathewZach123 marked this pull request as ready for review December 2, 2025 00:07
Signed-off-by: Mathew Suresh Zachariah <mazachar@pensando.io>
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would seem to be ok/correct, but is still being flagged as an error.


void bar(bit<2> x, out bit<8> y) {
TwoBitsAlias a = (TwoBitsAlias)x;
switch (a) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too should be ok, not an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Topics concerning the core segments of the compiler (frontend, midend, parser)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash when switch expression is a constant-valued directionless argument Compiler Bug in simplifySwitch.cpp

4 participants