Skip to content

Fix misleading error messages for keywords that escape a const initializer - #156471

Draft
willpuhr wants to merge 2 commits into
rust-lang:mainfrom
willpuhr:fix-156200
Draft

willpuhr wants to merge 2 commits into
rust-lang:mainfrom
willpuhr:fix-156200

Conversation

@willpuhr

@willpuhr willpuhr commented May 11, 2026 •

Copy link
Copy Markdown

Fixes #156200

In addition to the ? operator, handles the cases listed below where a keyword would move control flow out of a const initializer. The current error misleadingly states the keyword was used outside its relevant scope.

return inside a function body:

const fn foo() -> Result<(), ()> { Ok(()) }

fn main() {
    const A: () = {
        // error[E0572]: return statement outside of function body
        return;
        ()
    };
}

continue or break inside a loop:

fn main() {
    loop {
        const A: () = {
            // error[E0268]: `continue` outside of a loop
            continue;
            ()
        };
    }
}

break inside a labeled block:

fn main() {
    'a: {
        const A: () = {
            // error[E0767]: use of unreachable label `'a`
            // error[E0268]: `break` outside of a loop or labeled block
            break 'a;
            ()
        };
    }
}

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 11, 2026
@rust-log-analyzer

This comment has been minimized.

…?`. The new error doc is currently a bit confusing as it only covers the existing `?` case but will be completed with other cases
@rust-bors

rust-bors Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #157863) made this pull request unmergeable. Please resolve the merge conflicts.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using ? in a constant offers a confusing error message

3 participants