Skip to content
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

Fix illegal instruction error when breaking within condition of labelled while loop #50877

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/librustc_passes/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,16 @@ E0590: r##"
Example of erroneous code:

```compile_fail
while break {}
loop {
while break {}
}
```

To fix this, add a label specifying which loop is being broken out of:
```
'foo: while break 'foo {}
'foo: loop {
while break 'foo {}
}
```
"##,

Expand Down
1 change: 1 addition & 0 deletions src/librustc_passes/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
}
return false;
}

fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
struct_span_err!(self.sess, span, E0590,
"`break` or `continue` with no label in the condition of a `while` loop")
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ loop {
}
```

Please verify you spelt or declare the label correctly. Example:
Please verify you spelt and declared the label correctly. Example:

```
'a: loop {
Expand Down
18 changes: 8 additions & 10 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2112,11 +2112,9 @@ impl<'a> Resolver<'a> {
}
}

/// Searches the current set of local scopes for labels. Returns the first non-None label that
/// is returned by the given predicate function
///
/// Stops after meeting a closure.
fn search_label<P, R>(&self, mut ident: Ident, pred: P) -> Option<R>
/// Searches the current set of local scopes for labels. Returns the first non-`None` label
/// that is returned by the given predicate function. Stops after meeting a closure.
fn search_label<P, R: std::fmt::Debug>(&self, mut ident: Ident, pred: P) -> Option<R>
where P: Fn(&Rib, Ident) -> Option<R>
{
for rib in self.label_ribs.iter().rev() {
Expand All @@ -2130,7 +2128,7 @@ impl<'a> Resolver<'a> {
}
}
_ => {
// Do not resolve labels across function boundary
// Do not resolve labels across function boundary.
return None;
}
}
Expand Down Expand Up @@ -3731,8 +3729,8 @@ impl<'a> Resolver<'a> {
match self.search_label(label.ident, |rib, id| rib.bindings.get(&id).cloned()) {
None => {
// Search again for close matches...
// Picks the first label that is "close enough", which is not necessarily
// the closest match
// Picks the first label that is "close enough", which is not
// necessarily the closest match.
let close_match = self.search_label(label.ident, |rib, ident| {
let names = rib.bindings.iter().map(|(id, _)| &id.name);
find_best_match_for_name(names, &*ident.name.as_str(), None)
Expand Down Expand Up @@ -3775,15 +3773,15 @@ impl<'a> Resolver<'a> {
ExprKind::Loop(ref block, label) => self.resolve_labeled_block(label, expr.id, &block),

ExprKind::While(ref subexpression, ref block, label) => {
self.visit_expr(subexpression);
self.with_resolved_label(label, expr.id, |this| {
this.visit_expr(subexpression);
this.visit_block(block);
});
}

ExprKind::WhileLet(ref pats, ref subexpression, ref block, label) => {
self.visit_expr(subexpression);
self.with_resolved_label(label, expr.id, |this| {
this.visit_expr(subexpression);
this.ribs[ValueNS].push(Rib::new(NormalRibKind));
let mut bindings_list = FxHashMap();
for pat in pats {
Expand Down
16 changes: 16 additions & 0 deletions src/test/ui/while-label-break-condition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(unreachable_code)]

fn main() {
'a: while break 'a {} //~ ERROR: use of undeclared label
'b: while break 'b {}; //~ ERROR: use of undeclared label
}
15 changes: 15 additions & 0 deletions src/test/ui/while-label-break-condition.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0426]: use of undeclared label `'a`
--> $DIR/while-label-break-condition.rs:14:21
|
LL | 'a: while break 'a {} //~ ERROR: use of undeclared label
| ^^ undeclared label `'a`

error[E0426]: use of undeclared label `'b`
--> $DIR/while-label-break-condition.rs:15:21
|
LL | 'b: while break 'b {}; //~ ERROR: use of undeclared label
| ^^ undeclared label `'b`

error: aborting due to 2 previous errors

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