Skip to content

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

Draft
willpuhr wants to merge 1 commit into
rust-lang:mainfrom
willpuhr:fix-156200
Draft

Fix misleading error messages for keywords that escape a const initializer#156471
willpuhr wants to merge 1 commit into
rust-lang:mainfrom
willpuhr:fix-156200

Conversation

@willpuhr
Copy link
Copy Markdown

@willpuhr willpuhr commented May 11, 2026

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.

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