Skip to content

rustc suggests incorrect labeling of try block #160987

Description

@CenTdemeern1

Code

#![feature(try_blocks)]

fn main() {
    try {
        break;
        None?;
    };
}

Current output

error[E0268]: `break` outside of a loop or labeled block
 --> thingy2.rs:5:9
  |
5 |         break;
  |         ^^^^^ cannot `break` outside of a loop or labeled block
  |
help: consider labeling this block to be able to break within it
  |
4 ~     try 'block: {
5 ~         break 'block;
  |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0268`.

Desired output

help: consider labeling this block to be able to break within it
  |
4 ~     'block: try {
5 ~         break 'block;
  |

If/When it's valid, OR

help: consider labeling this block to be able to break within it
  |
4 ~     'block: { try {
5 ~         break 'block;
6 |         None?;
7 ~     } };
  |

Rationale and extra context

rustc should not be suggesting invalid syntax. Also, I'm not sure labels can be placed on try blocks directly right now, and the label should probably be outside of the try keyword rather than the block itself (matching stuff like loop)

Other cases

Rust Version

rustc 1.99.0-nightly (3d6c19bb9 2026-08-11)
binary: rustc
commit-hash: 3d6c19bb9ab4798ecfb2ee943df01a811720fc27
commit-date: 2026-08-11
host: aarch64-apple-darwin
release: 1.99.0-nightly
LLVM version: 23.1.0

Anything else?

No response

Activity

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.F-try_blocks`#![feature(try_blocks)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions