Skip to content
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

Forbid case-after-default, tweak error messages #4831

Merged
merged 4 commits into from
Jul 29, 2024

Conversation

vlstill
Copy link
Contributor

@vlstill vlstill commented Jul 25, 2024

Fixes #4290. This forbids compilation of code which contains a switch label after a default label. This behaviour matches the P4 spec ($12.7.3):

It is optional to have a switch case with the default label, but if one is present, it must be the last one in the switch statement.

See #4290 (comment) for detains why I believe there is no way to allow this and be consistent.

I've also changed the error messages to be more readable. Note that neither SwitchStatement nor DefaultExpression have a reasonable string representation for the error messages, so there is no point in including anything more that the debug info in the message.

@fruffy, while this can't break "assumptions in back-end code" I guess it should still be marked as breaking change as it can actually break compilation of P4. Such P4 was not adhering to spec though and it should be simple to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This issue apparently has nothing to do with case-after-default, and it just masks the "real" problem (the one the issue referred to).

@vlstill vlstill added breaking-change This change may break assumptions of compiler back ends. core Topics concerning the core segments of the compiler (frontend, midend, parser) labels Jul 25, 2024
@vlstill vlstill marked this pull request as ready for review July 25, 2024 12:08
@jafingerhut
Copy link
Contributor

Would it perhaps be a good idea to add a modified version of default-switch.p4, that puts default last, in case that helps improve test coverage of whatever issue it was originally created for? That modified version would have no errors, and so belong in the testdata/p4_16_samples directory, where default-switch.p4 is before this PR.

@jafingerhut
Copy link
Contributor

Regarding the comment about a breaking change, yes I agree this is a kind of breaking change.

Fortunately, it is what I think could be termed a "safe" breaking change: any program that breaks because of it, will have a clear error message and a straightforward path to update the code to work again.

As opposed to what I would call an "unsafe" breaking change, where some programs change their behavior with no warning or error messages at compile time. Those are the really nasty ones I would strongly suggest we bump up the major version of the language, if we make a change like that. I can't think of one that has been made in P4_16, or if there was, it was very early on, perhaps before version 1.0 was released.

@asl
Copy link
Contributor

asl commented Jul 25, 2024

Fortunately, it is what I think could be termed a "safe" breaking change: any program that breaks because of it, will have a clear error message and a straightforward path to update the code to work again.

I think the semantics for "breaking change" was (as this is in description): "This change may break assumptions of compiler back ends", not about the input source. Yes, it may break malformed input, but I think it is ok, that next version of compiler is more strict wrt the spec.

This certainly does not affect backends

@kfcripps
Copy link
Contributor

I think the semantics for "breaking change" was (as this is in description): "This change may break assumptions of compiler back ends", not about the input source.

Agree with Anton. Maybe we should add a different label for changes that cause existing input sources to no longer compile, or to have different semantics?

@fruffy
Copy link
Collaborator

fruffy commented Jul 25, 2024

Agree with Anton. Maybe we should add a different label for changes that cause existing input sources to no longer compile, or to have different semantics?

We could probably broaden the definition of the p4-spec label since this is a spec-related change.

Copy link
Contributor

@jafingerhut jafingerhut left a comment

Choose a reason for hiding this comment

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

LGTM. As usual, I am reviewing the test programs and their expected outputs, and not paying careful attention to the C++ code changes (even as small as they are).

@vlstill vlstill added p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). and removed breaking-change This change may break assumptions of compiler back ends. labels Jul 29, 2024
@vlstill
Copy link
Contributor Author

vlstill commented Jul 29, 2024

Agree with Anton. Maybe we should add a different label for changes that cause existing input sources to no longer compile, or to have different semantics?

We could probably broaden the definition of the p4-spec label since this is a spec-related change.

Yep I think the p4-spec label matches it well (I'm not sure if you have already modified it :-D). I've added p4-spec and removed breaking-change.

@vlstill
Copy link
Contributor Author

vlstill commented Jul 29, 2024

Would it perhaps be a good idea to add a modified version of default-switch.p4, that puts default last, in case that helps improve test coverage of whatever issue it was originally created for? That modified version would have no errors, and so belong in the testdata/p4_16_samples directory, where default-switch.p4 is before this PR.

Right, I should have done it that way originally. Added the positive version of the test now.

@vlstill vlstill force-pushed the vstill/forbid-case-after-default branch from eb31164 to e9bf6a8 Compare July 29, 2024 07:14
@vlstill vlstill added this pull request to the merge queue Jul 29, 2024
Merged via the queue into main with commit 8c70f3b Jul 29, 2024
16 of 17 checks passed
@vlstill vlstill deleted the vstill/forbid-case-after-default branch July 29, 2024 09:38
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) p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch statement: default label has to be last (according to specification)
7 participants