Allow explicit cast of an expression to its own type#5657
Open
aeron-gh wants to merge 1 commit into
Open
Conversation
An explicit cast of an expression to its own type is a no-op and is
permitted by the spec (section 8.12.1), but the type checker rejected
casts to a struct/header/union type unless the operand was a struct
initializer, reporting "cast not supported". As a result a program like
return (s)(s)(s)x;
failed to compile even though every cast targets x's own type.
Handle the case where the source and destination types are equivalent
up front in postorder(Cast) and treat it as a no-op. The existing
struct-initializer and invalid-header handling is left unchanged, so the
only programs whose behaviour changes are self-casts that previously
errored.
issue3233.p4 now compiles, so move it from p4_16_errors to
p4_16_samples and add the expected outputs.
Fixes p4lang#5527
Signed-off-by: aeron-gh <agab0323@gmail.com>
jafingerhut
approved these changes
Jun 17, 2026
jafingerhut
left a comment
Contributor
There was a problem hiding this comment.
I reviewed only the updated test case and expected output files for them. Those look good to me. I have not attempted to review the C++ code changes in the compiler, leaving that part of the review for others more knowledgeable about compiler internals.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #5527.
The spec (section 8.12.1) says an explicit cast of an expression to its own type is a no-op and is allowed. The type checker did not honour this for struct/header/union types: a cast to such a type was only accepted when the operand was a struct initializer (
(S){...}), otherwise it reportedcast not supported. So a redundant self-cast such asfailed to type-check even though every cast targets
t1's own type.postorder(IR::Cast)now checks, when the destination is a struct-like type, whether the source and destination types are equivalent, and if so treats the cast as a no-op. The existing struct-initializer andinvalid-header paths are untouched, so the only programs that change behaviour are self-casts that previously errored — the redundant casts are simply dropped (e.g. the program above pretty-prints back toreturn t1;).testdata/issue3233.p4was an error test for exactly this case; it now compiles, so it moves fromp4_16_errorstop4_16_sampleswith the generated outputs.I confirmed all existing
explicit-cast-*/ struct / union / tuple cast error tests still fail with their expected diagnostics (the change is gated on type equivalence, so genuinely-incompatible casts are unaffected).