forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Loop match proper errors #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
folkertdev
merged 3 commits into
trifectatechfoundation:loop_match_attr
from
folkertdev:loop-match-proper-errors
Mar 13, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ use rustc_middle::{bug, span_bug}; | |
| use rustc_span::{Span, sym}; | ||
| use tracing::{debug, info, instrument, trace}; | ||
|
|
||
| use crate::errors::*; | ||
| use crate::thir::cx::ThirBuildCx; | ||
|
|
||
| impl<'tcx> ThirBuildCx<'tcx> { | ||
|
|
@@ -798,15 +799,20 @@ impl<'tcx> ThirBuildCx<'tcx> { | |
| .any(|attr| attr.has_name(sym::const_continue)); | ||
| if is_const_continue { | ||
| match dest.target_id { | ||
| Ok(target_id) => ExprKind::ConstContinue { | ||
| label: region::Scope { | ||
| local_id: target_id.local_id, | ||
| data: region::ScopeData::Node, | ||
| }, | ||
| value: self.mirror_expr( | ||
| value.expect("missing value for #[const_continue] break"), | ||
| ), | ||
| }, | ||
| Ok(target_id) => { | ||
| let Some(value) = value else { | ||
| let span = expr.span; | ||
| self.tcx.dcx().emit_fatal(ConstContinueMissingValue { span }) | ||
| }; | ||
|
|
||
| ExprKind::ConstContinue { | ||
| label: region::Scope { | ||
| local_id: target_id.local_id, | ||
| data: region::ScopeData::Node, | ||
| }, | ||
| value: self.mirror_expr(value), | ||
| } | ||
| } | ||
| Err(err) => bug!("invalid loop id for break: {}", err), | ||
| } | ||
| } else { | ||
|
|
@@ -863,27 +869,57 @@ impl<'tcx> ThirBuildCx<'tcx> { | |
| .iter() | ||
| .any(|attr| attr.has_name(sym::loop_match)); | ||
| if is_loop_match { | ||
| assert!(body.stmts.is_empty()); | ||
| let hir::ExprKind::Assign(state, block_expr, _) = body.expr.unwrap().kind | ||
| else { | ||
| panic!(); | ||
| let dcx = self.tcx.dcx(); | ||
|
|
||
| if let ([first, ..], [.., last]) = (body.stmts, body.stmts) { | ||
| dcx.emit_fatal(LoopMatchBadStatements { span: first.span.to(last.span) }) | ||
| } | ||
|
|
||
| let Some(loop_body_expr) = body.expr else { | ||
| dcx.emit_fatal(LoopMatchMissingAssignment { span: body.span }) | ||
| }; | ||
| let hir::ExprKind::Block(block_body, _) = block_expr.kind else { | ||
| panic!(); | ||
|
|
||
| let hir::ExprKind::Assign(state, rhs_expr, _) = loop_body_expr.kind else { | ||
| dcx.emit_fatal(LoopMatchMissingAssignment { span: loop_body_expr.span }) | ||
| }; | ||
| assert!(block_body.stmts.is_empty()); | ||
| let hir::ExprKind::Match(discr, arms, match_source) = | ||
| block_body.expr.unwrap().kind | ||
|
|
||
| let hir::ExprKind::Block(block_body, _) = rhs_expr.kind else { | ||
| dcx.emit_fatal(LoopMatchBadRhs { span: rhs_expr.span }) | ||
| }; | ||
|
|
||
| if let Some(first) = block_body.stmts.first() { | ||
| let span = first.span.to(block_body.stmts.last().unwrap().span); | ||
| dcx.emit_fatal(LoopMatchBadStatements { span }) | ||
| } | ||
|
|
||
| let Some(block_body_expr) = block_body.expr else { | ||
| dcx.emit_fatal(LoopMatchBadRhs { span: block_body.span }) | ||
| }; | ||
|
|
||
| let hir::ExprKind::Match(scrutinee, arms, match_source) = block_body_expr.kind | ||
| else { | ||
| panic!(); | ||
| dcx.emit_fatal(LoopMatchBadRhs { span: block_body_expr.span }) | ||
| }; | ||
|
|
||
| fn local(expr: &rustc_hir::Expr<'_>) -> Option<hir::HirId> { | ||
| if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind { | ||
| if let Res::Local(hir_id) = path.res { | ||
| return Some(hir_id); | ||
| } | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
Comment on lines
+904
to
+912
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ideally this would be a method somewhere, but I'm not sure where that should be
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure either. Can be left for later. |
||
|
|
||
| let Some(scrutinee_hir_id) = local(scrutinee) else { | ||
| dcx.emit_fatal(LoopMatchInvalidMatch { span: scrutinee.span }) | ||
| }; | ||
| match (state.kind, discr.kind) { | ||
| ( | ||
| hir::ExprKind::Path(hir::QPath::Resolved(_, state)), | ||
| hir::ExprKind::Path(hir::QPath::Resolved(_, discr)), | ||
| ) if state.segments.iter().map(|seg| seg.ident).collect::<Vec<_>>() | ||
| == discr.segments.iter().map(|seg| seg.ident).collect::<Vec<_>>() => {} | ||
| _ => panic!(), | ||
|
|
||
| if local(state) != Some(scrutinee_hir_id) { | ||
| dcx.emit_fatal(LoopMatchInvalidUpdate { | ||
| scrutinee: scrutinee.span, | ||
| lhs: state.span, | ||
| }) | ||
| } | ||
|
|
||
| ExprKind::LoopMatch { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| #![feature(loop_match)] | ||
| #![crate_type = "lib"] | ||
|
|
||
| enum State { | ||
| A, | ||
| B, | ||
| C, | ||
| } | ||
|
|
||
| fn invalid_update() { | ||
| let mut fake = State::A; | ||
| let state = State::A; | ||
| #[loop_match] | ||
| loop { | ||
| fake = 'blk: { | ||
| //~^ ERROR: invalid update of the `loop_match` state | ||
| match state { | ||
| _ => State::B, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn invalid_scrutinee() { | ||
| let state = State::A; | ||
| #[loop_match] | ||
| loop { | ||
| state = 'blk: { | ||
| match State::A { | ||
| //~^ ERROR: invalid match on `loop_match` state | ||
| _ => State::B, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn bad_statements_1() { | ||
| let state = State::A; | ||
| #[loop_match] | ||
| loop { | ||
| 1; | ||
| //~^ ERROR: statements are not allowed in this position within a `loop_match` | ||
| state = 'blk: { | ||
| match State::A { | ||
| _ => State::B, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn bad_statements_2() { | ||
| let state = State::A; | ||
| #[loop_match] | ||
| loop { | ||
| state = 'blk: { | ||
| 1; | ||
| //~^ ERROR: statements are not allowed in this position within a `loop_match` | ||
| match State::A { | ||
| _ => State::B, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn bad_rhs_1() { | ||
| let state = State::A; | ||
| #[loop_match] | ||
| loop { | ||
| state = State::B | ||
| //~^ ERROR: this expression must be a single `match` wrapped in a labelled block | ||
| } | ||
| } | ||
|
|
||
| fn bad_rhs_2() { | ||
| let state = State::A; | ||
| #[loop_match] | ||
| loop { | ||
| state = 'blk: { | ||
| State::B | ||
| //~^ ERROR: this expression must be a single `match` wrapped in a labelled block | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn bad_rhs_3() { | ||
| let state = (); | ||
| #[loop_match] | ||
| loop { | ||
| state = 'blk: { | ||
| //~^ ERROR: this expression must be a single `match` wrapped in a labelled block | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn missing_assignment() { | ||
| let state = State::A; | ||
| #[loop_match] | ||
| loop { | ||
| () //~ ERROR: expected a single assignment expression | ||
| } | ||
| } | ||
|
|
||
| fn empty_loop_body() { | ||
| let state = State::A; | ||
| #[loop_match] | ||
| loop { | ||
| //~^ ERROR: expected a single assignment expression | ||
| } | ||
| } | ||
|
|
||
| fn break_without_value() { | ||
| let state = State::A; | ||
| #[loop_match] | ||
| 'a: loop { | ||
| state = 'blk: { | ||
| match state { | ||
| State::A => { | ||
| #[const_continue] | ||
| break 'blk; | ||
| //~^ ERROR: mismatched types | ||
| } | ||
| _ => break 'a, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn break_without_value_unit() { | ||
| let state = (); | ||
| #[loop_match] | ||
| 'a: loop { | ||
| state = 'blk: { | ||
| match state { | ||
| () => { | ||
| #[const_continue] | ||
| break 'blk; | ||
| //~^ ERROR: a `const_continue` must break to a label with a value | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally this would be a regular error and produce
ExprKind::Errinstead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no error variant for this type I think? https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/thir/enum.ExprKind.html