Skip to content

fix(noirc_frontend): improve error for struct literal in if condition#11557

Merged
TomAFrench merged 4 commits intonoir-lang:masterfrom
illegalcall:issue-11111-if-struct-literal-diagnostic
Feb 20, 2026
Merged

fix(noirc_frontend): improve error for struct literal in if condition#11557
TomAFrench merged 4 commits intonoir-lang:masterfrom
illegalcall:issue-11111-if-struct-literal-diagnostic

Conversation

@illegalcall
Copy link
Copy Markdown
Contributor

@illegalcall illegalcall commented Feb 11, 2026

Description

Problem

Resolves #11111

Summary

When a struct literal is used as an if condition (e.g., if MyStruct { field: true }.field {}),
the parser previously produced ~7 confusing, cascading errors. This PR detects the pattern and
emits a single clear diagnostic: "struct literals are not allowed in if conditions" with a hint
to wrap the struct literal in parentheses.

Additional Context

This matches Rust's behavior for the same ambiguity — { after the condition is parsed as the
start of the if-then block, not as a struct literal constructor. The fix adds a heuristic in
parse_if_expr that checks whether the parsed block content looks like struct literal fields
(pattern: ident: after the opening brace), suppresses the misleading cascading errors, and
emits a single actionable diagnostic instead.

User Documentation

  • No user documentation needed.

PR Checklist

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your contribution to the Noir language.

Please do not force push to this branch after the Noir team have started review of this PR. Doing so will only delay us merging your PR as we will need to start the review process from scratch.

Thanks for your understanding.

Copy link
Copy Markdown
Collaborator

@asterite asterite left a comment

Choose a reason for hiding this comment

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

Thank you, it looks good!

However, can you add a test? There are some existing tests for errors, showing where in the source they happen and what their message (or error kind) is.

@illegalcall
Copy link
Copy Markdown
Contributor Author

Sure, i will do that. Thank you

@TomAFrench TomAFrench self-requested a review February 20, 2026 13:15
@TomAFrench TomAFrench enabled auto-merge February 20, 2026 13:20
@TomAFrench TomAFrench disabled auto-merge February 20, 2026 16:32
@TomAFrench TomAFrench merged commit 721d7ab into noir-lang:master Feb 20, 2026
129 of 132 checks passed
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.

Improve error message when struct literal is used in if condition

3 participants