@@ -13,7 +13,7 @@ use rustc::session::Session;
13
13
14
14
use rustc:: hir:: map:: Map ;
15
15
use rustc:: hir:: intravisit:: { self , Visitor , NestedVisitorMap } ;
16
- use rustc:: hir;
16
+ use rustc:: hir:: { self , Destination } ;
17
17
use syntax:: ast;
18
18
use syntax_pos:: Span ;
19
19
@@ -91,6 +91,8 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
91
91
self . with_context ( LabeledBlock , |v| v. visit_block ( & b) ) ;
92
92
}
93
93
hir:: ExprBreak ( label, ref opt_expr) => {
94
+ self . require_label_in_labeled_block ( e. span , & label, "break" ) ;
95
+
94
96
let loop_id = match label. target_id . into ( ) {
95
97
Ok ( loop_id) => loop_id,
96
98
Err ( hir:: LoopIdError :: OutsideLoopScope ) => ast:: DUMMY_NODE_ID ,
@@ -109,14 +111,6 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
109
111
}
110
112
111
113
if self . cx == LabeledBlock {
112
- if label. label . is_none ( ) {
113
- struct_span_err ! ( self . sess, e. span, E0695 ,
114
- "unlabeled `break` inside of a labeled block" )
115
- . span_label ( e. span ,
116
- "`break` statements that would diverge to or through \
117
- a labeled block need to bear a label")
118
- . emit ( ) ;
119
- }
120
114
return ;
121
115
}
122
116
@@ -156,6 +150,8 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
156
150
self . require_break_cx ( "break" , e. span ) ;
157
151
}
158
152
hir:: ExprAgain ( label) => {
153
+ self . require_label_in_labeled_block ( e. span , & label, "continue" ) ;
154
+
159
155
if let Err ( hir:: LoopIdError :: UnlabeledCfInWhileCondition ) = label. target_id {
160
156
self . emit_unlabled_cf_in_while_condition ( e. span , "continue" ) ;
161
157
}
@@ -193,6 +189,18 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
193
189
}
194
190
}
195
191
192
+ fn require_label_in_labeled_block ( & mut self , span : Span , label : & Destination , cf_type : & str ) {
193
+ if self . cx == LabeledBlock {
194
+ if label. label . is_none ( ) {
195
+ struct_span_err ! ( self . sess, span, E0695 ,
196
+ "unlabeled `{}` inside of a labeled block" , cf_type)
197
+ . span_label ( span,
198
+ format ! ( "`{}` statements that would diverge to or through \
199
+ a labeled block need to bear a label", cf_type) )
200
+ . emit ( ) ;
201
+ }
202
+ }
203
+ }
196
204
fn emit_unlabled_cf_in_while_condition ( & mut self , span : Span , cf_type : & str ) {
197
205
struct_span_err ! ( self . sess, span, E0590 ,
198
206
"`break` or `continue` with no label in the condition of a `while` loop" )
0 commit comments