Skip to content

Commit 4cefee0

Browse files
committed
Fix illegal instruction error when breaking within condition of labelled while loop
1 parent 90463a6 commit 4cefee0

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

src/librustc_passes/loops.rs

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
215215
}
216216
return false;
217217
}
218+
218219
fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
219220
struct_span_err!(self.sess, span, E0590,
220221
"`break` or `continue` with no label in the condition of a `while` loop")

src/librustc_resolve/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ loop {
10971097
}
10981098
```
10991099
1100-
Please verify you spelt or declare the label correctly. Example:
1100+
Please verify you spelt and declared the label correctly. Example:
11011101
11021102
```
11031103
'a: loop {

src/librustc_resolve/lib.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -2112,11 +2112,9 @@ impl<'a> Resolver<'a> {
21122112
}
21132113
}
21142114

2115-
/// Searches the current set of local scopes for labels. Returns the first non-None label that
2116-
/// is returned by the given predicate function
2117-
///
2118-
/// Stops after meeting a closure.
2119-
fn search_label<P, R>(&self, mut ident: Ident, pred: P) -> Option<R>
2115+
/// Searches the current set of local scopes for labels. Returns the first non-`None` label
2116+
/// that is returned by the given predicate function. Stops after meeting a closure.
2117+
fn search_label<P, R: std::fmt::Debug>(&self, mut ident: Ident, pred: P) -> Option<R>
21202118
where P: Fn(&Rib, Ident) -> Option<R>
21212119
{
21222120
for rib in self.label_ribs.iter().rev() {
@@ -2130,7 +2128,7 @@ impl<'a> Resolver<'a> {
21302128
}
21312129
}
21322130
_ => {
2133-
// Do not resolve labels across function boundary
2131+
// Do not resolve labels across function boundary.
21342132
return None;
21352133
}
21362134
}
@@ -3731,8 +3729,8 @@ impl<'a> Resolver<'a> {
37313729
match self.search_label(label.ident, |rib, id| rib.bindings.get(&id).cloned()) {
37323730
None => {
37333731
// Search again for close matches...
3734-
// Picks the first label that is "close enough", which is not necessarily
3735-
// the closest match
3732+
// Picks the first label that is "close enough", which is not
3733+
// necessarily the closest match.
37363734
let close_match = self.search_label(label.ident, |rib, ident| {
37373735
let names = rib.bindings.iter().map(|(id, _)| &id.name);
37383736
find_best_match_for_name(names, &*ident.name.as_str(), None)
@@ -3775,15 +3773,15 @@ impl<'a> Resolver<'a> {
37753773
ExprKind::Loop(ref block, label) => self.resolve_labeled_block(label, expr.id, &block),
37763774

37773775
ExprKind::While(ref subexpression, ref block, label) => {
3776+
self.visit_expr(subexpression);
37783777
self.with_resolved_label(label, expr.id, |this| {
3779-
this.visit_expr(subexpression);
37803778
this.visit_block(block);
37813779
});
37823780
}
37833781

37843782
ExprKind::WhileLet(ref pats, ref subexpression, ref block, label) => {
3783+
self.visit_expr(subexpression);
37853784
self.with_resolved_label(label, expr.id, |this| {
3786-
this.visit_expr(subexpression);
37873785
this.ribs[ValueNS].push(Rib::new(NormalRibKind));
37883786
let mut bindings_list = FxHashMap();
37893787
for pat in pats {
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
'a: while break 'a {} //~ ERROR: use of undeclared label
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0426]: use of undeclared label `'a`
2+
--> $DIR/while-label-break-condition.rs:12:21
3+
|
4+
LL | 'a: while break 'a {} //~ ERROR: use of undeclared label
5+
| ^^ undeclared label `'a`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0426`.

0 commit comments

Comments
 (0)