Skip to content

Allow explicit cast of an expression to its own type#5657

Open
aeron-gh wants to merge 1 commit into
p4lang:mainfrom
aeron-gh:allow-struct-self-cast
Open

Allow explicit cast of an expression to its own type#5657
aeron-gh wants to merge 1 commit into
p4lang:mainfrom
aeron-gh:allow-struct-self-cast

Conversation

@aeron-gh

Copy link
Copy Markdown

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 reported cast not supported. So a redundant self-cast such as

struct s { bit t; bit t1; }

s func1(s t1) {
    return (s)(s)(s)t1;
}

failed 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 and invalid-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 to return t1;).

testdata/issue3233.p4 was an error test for exactly this case; it now compiles, so it moves from p4_16_errors to p4_16_samples with 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).

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 jafingerhut left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@aeron-gh aeron-gh requested a review from jafingerhut June 17, 2026 16:14
@fruffy fruffy requested a review from ChrisDodd June 17, 2026 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explicit self-casts to struct types should not be rejected

2 participants