Skip to content

[ASTGen] Handle #warning/#error in switch case position#90074

Open
nenadvulic wants to merge 5 commits into
swiftlang:mainfrom
nenadvulic:fix-switch-pound-diagnostic
Open

[ASTGen] Handle #warning/#error in switch case position#90074
nenadvulic wants to merge 5 commits into
swiftlang:mainfrom
nenadvulic:fix-switch-pound-diagnostic

Conversation

@nenadvulic

Copy link
Copy Markdown

Companion to swiftlang/swift-syntax#3363.

That PR makes swift-syntax parse #warning/#error in case position of a switch as a MacroExpansionDecl element of the case list (instead of diagnosing them), to match the compiler. As a result SwitchCaseListSyntax.Element gains a .macroExpansionDecl case, which makes the exhaustive switch in generate(switchCaseList:) no longer compile.

This handles the new case in ASTGen:

  • Stmts.swift: in generate(switchCaseList:), handle .macroExpansionDecl by evaluating it with maybeGenerateBuiltinPound (for the #warning/#error diagnostic) and skipping it, since it isn't a case.
  • ASTGen+CompilerBuildConfiguration.swift: add a skip case to IfConfigOrUnderlying so the split closure can handle an element itself without producing a flat element to visit.
  • BuiltinPound.swift: handlePoundDiagnostic asserted that a pound diagnostic must be at code-block/member-block level; the parent in this case is a SwitchCaseListSyntax, so accept that parent kind too.

Added an ASTGen test covering #warning/#error in case position, including inside an #if.

This only builds against the swift-syntax branch above, so it needs to be cross-repo tested with it.

@hamishknight hamishknight 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.

Thanks!

Comment thread test/ASTGen/pound_diagnostic_switch.swift
@hamishknight

Copy link
Copy Markdown
Contributor

swiftlang/swift-syntax#3363

@swift-ci please smoke test

@nenadvulic

Copy link
Copy Markdown
Author

@hamishknight my mistake, I forgot to cover all cases. I'm checking it now

@hamishknight

Copy link
Copy Markdown
Contributor

swiftlang/swift-syntax#3363

@swift-ci please smoke test

@nenadvulic

Copy link
Copy Markdown
Author

The smoke test caught a parser bug on the swift-syntax side, not here: a #warning/#error at the start of a switch was being consumed as unexpected code before a later #if in the file. Fixed in swiftlang/swift-syntax#3363 (the #warning/#error case is now checked before the #if recovery, with a regression test). This PR is unchanged — could you re-run the cross-repo smoke test when you get a chance?

Allows the split closure of visitIfConfigElements to handle an element
itself (e.g. evaluating a #warning/#error for its diagnostic) without
producing a flat element to visit.
The parent of a pound diagnostic that appears in case position of a
switch is a SwitchCaseListSyntax, so accept that parent kind instead of
hitting the 'must be declaration level' assertion.
swift-syntax now parses #warning/#error in case position of a switch as
a MacroExpansionDecl element of the case list (swiftlang/swift-syntax#3363).
Generate the builtin pound for its diagnostic and skip it, since it is
not a case.
Verify that pound diagnostics in case position (including inside an #if)
are evaluated and not misparsed.
@hamishknight hamishknight force-pushed the fix-switch-pound-diagnostic branch from 41e06a9 to b4f636b Compare June 21, 2026 09:37
@hamishknight

Copy link
Copy Markdown
Contributor

swiftlang/swift-syntax#3363

@swift-ci please smoke test

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.

2 participants