Skip to content

Commit 237576c

Browse files
Continue inside a labeled block now lowers to a error when to lowering the ast
1 parent 53ed660 commit 237576c

File tree

12 files changed

+156
-20
lines changed

12 files changed

+156
-20
lines changed

compiler/rustc_ast_lowering/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ ast_lowering_clobber_abi_not_supported =
4848
4949
ast_lowering_closure_cannot_be_static = closures cannot be static
5050
51+
ast_lowering_continue_labeled_block =
52+
`continue` pointing to a labeled block
53+
.label = labeled blocks cannot be `continue`'d
54+
.block_label = labeled block the `continue` points to
55+
5156
ast_lowering_coroutine_too_many_parameters =
5257
too many parameters for a coroutine (expected 0 or 1 parameters)
5358

compiler/rustc_ast_lowering/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,13 @@ pub(crate) struct AsyncBoundOnlyForFnTraits {
405405
#[primary_span]
406406
pub span: Span,
407407
}
408+
409+
#[derive(Diagnostic)]
410+
#[diag(ast_lowering_continue_labeled_block, code = E0696)]
411+
pub struct ContinueLabeledBlock {
412+
#[primary_span]
413+
#[label]
414+
pub span: Span,
415+
#[label(ast_lowering_block_label)]
416+
pub block_span: Span,
417+
}

compiler/rustc_ast_lowering/src/expr.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::errors::{
88
};
99
use super::ResolverAstLoweringExt;
1010
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
11+
use crate::errors::ContinueLabeledBlock;
1112
use crate::{FnDeclKind, ImplTraitPosition};
1213
use rustc_ast::ptr::P as AstP;
1314
use rustc_ast::*;
@@ -283,7 +284,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
283284
hir::ExprKind::Break(self.lower_jump_destination(e.id, *opt_label), opt_expr)
284285
}
285286
ExprKind::Continue(opt_label) => {
286-
hir::ExprKind::Continue(self.lower_jump_destination(e.id, *opt_label))
287+
if let Some(_) = opt_label {
288+
if let Some((_, is_loop, block_span)) = self.resolver.get_label_res(e.id) {
289+
if is_loop {
290+
hir::ExprKind::Continue(
291+
self.lower_jump_destination(e.id, *opt_label),
292+
)
293+
} else {
294+
hir::ExprKind::Err(
295+
self.dcx().emit_err(ContinueLabeledBlock {
296+
span: e.span,
297+
block_span,
298+
}),
299+
)
300+
}
301+
} else {
302+
hir::ExprKind::Continue(self.lower_jump_destination(e.id, *opt_label))
303+
}
304+
} else {
305+
hir::ExprKind::Continue(self.lower_jump_destination(e.id, *opt_label))
306+
}
287307
}
288308
ExprKind::Ret(e) => {
289309
let e = e.as_ref().map(|x| self.lower_expr(x));
@@ -1429,8 +1449,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
14291449
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
14301450
let target_id = match destination {
14311451
Some((id, _)) => {
1432-
if let Some(loop_id) = self.resolver.get_label_res(id) {
1433-
Ok(self.lower_node_id(loop_id))
1452+
if let Some((id, _is_loop, _)) = self.resolver.get_label_res(id) {
1453+
Ok(self.lower_node_id(id))
14341454
} else {
14351455
Err(hir::LoopIdError::UnresolvedLabel)
14361456
}

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl ResolverAstLowering {
229229
}
230230

231231
/// Obtains resolution for a label with the given `NodeId`.
232-
fn get_label_res(&self, id: NodeId) -> Option<NodeId> {
232+
fn get_label_res(&self, id: NodeId) -> Option<(NodeId, bool, Span)> {
233233
self.label_res_map.get(&id).copied()
234234
}
235235

compiler/rustc_middle/src/ty/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ pub struct ResolverAstLowering {
200200
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
201201
pub import_res_map: NodeMap<hir::def::PerNS<Option<Res<ast::NodeId>>>>,
202202
/// Resolutions for labels (node IDs of their corresponding blocks or loops).
203-
pub label_res_map: NodeMap<ast::NodeId>,
203+
/// The boolean stores if the node is loop. The span is the span of the node.
204+
pub label_res_map: NodeMap<(ast::NodeId, bool, Span)>,
204205
/// Resolutions for lifetimes.
205206
pub lifetimes_res_map: NodeMap<LifetimeRes>,
206207
/// Lifetime parameters that lowering will have to introduce.

compiler/rustc_resolve/src/late.rs

+31-13
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ struct LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
658658
last_block_rib: Option<Rib<'a>>,
659659

660660
/// The current set of local scopes, for labels.
661-
label_ribs: Vec<Rib<'a, NodeId>>,
661+
label_ribs: Vec<Rib<'a, (NodeId, bool, Span)>>,
662662

663663
/// The current set of local scopes for lifetimes.
664664
lifetime_ribs: Vec<LifetimeRib>,
@@ -2215,7 +2215,10 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
22152215

22162216
/// Searches the current set of local scopes for labels. Returns the `NodeId` of the resolved
22172217
/// label and reports an error if the label is not found or is unreachable.
2218-
fn resolve_label(&mut self, mut label: Ident) -> Result<(NodeId, Span), ResolutionError<'a>> {
2218+
fn resolve_label(
2219+
&mut self,
2220+
mut label: Ident,
2221+
) -> Result<((NodeId, bool, Span), Span), ResolutionError<'a>> {
22192222
let mut suggestion = None;
22202223

22212224
for i in (0..self.label_ribs.len()).rev() {
@@ -4184,7 +4187,14 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
41844187
Ok(Some(result))
41854188
}
41864189

4187-
fn with_resolved_label(&mut self, label: Option<Label>, id: NodeId, f: impl FnOnce(&mut Self)) {
4190+
fn with_resolved_label(
4191+
&mut self,
4192+
label: Option<Label>,
4193+
id: NodeId,
4194+
is_loop: bool,
4195+
span: Span,
4196+
f: impl FnOnce(&mut Self),
4197+
) {
41884198
if let Some(label) = label {
41894199
if label.ident.as_str().as_bytes()[1] != b'_' {
41904200
self.diagnostic_metadata.unused_labels.insert(id, label.ident.span);
@@ -4196,16 +4206,22 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
41964206

41974207
self.with_label_rib(RibKind::Normal, |this| {
41984208
let ident = label.ident.normalize_to_macro_rules();
4199-
this.label_ribs.last_mut().unwrap().bindings.insert(ident, id);
4209+
this.label_ribs.last_mut().unwrap().bindings.insert(ident, (id, is_loop, span));
42004210
f(this);
42014211
});
42024212
} else {
42034213
f(self);
42044214
}
42054215
}
42064216

4207-
fn resolve_labeled_block(&mut self, label: Option<Label>, id: NodeId, block: &'ast Block) {
4208-
self.with_resolved_label(label, id, |this| this.visit_block(block));
4217+
fn resolve_labeled_block(
4218+
&mut self,
4219+
label: Option<Label>,
4220+
id: NodeId,
4221+
block: &'ast Block,
4222+
is_loop: bool,
4223+
) {
4224+
self.with_resolved_label(label, id, is_loop, block.span, |this| this.visit_block(block));
42094225
}
42104226

42114227
fn resolve_block(&mut self, block: &'ast Block) {
@@ -4348,10 +4364,10 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
43484364

43494365
ExprKind::Break(Some(label), _) | ExprKind::Continue(Some(label)) => {
43504366
match self.resolve_label(label.ident) {
4351-
Ok((node_id, _)) => {
4367+
Ok((node, _)) => {
43524368
// Since this res is a label, it is never read.
4353-
self.r.label_res_map.insert(expr.id, node_id);
4354-
self.diagnostic_metadata.unused_labels.remove(&node_id);
4369+
self.r.label_res_map.insert(expr.id, node);
4370+
self.diagnostic_metadata.unused_labels.remove(&node.0);
43554371
}
43564372
Err(error) => {
43574373
self.report_error(label.ident.span, error);
@@ -4386,11 +4402,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
43864402
}
43874403

43884404
ExprKind::Loop(ref block, label, _) => {
4389-
self.resolve_labeled_block(label, expr.id, block)
4405+
self.resolve_labeled_block(label, expr.id, block, true)
43904406
}
43914407

43924408
ExprKind::While(ref cond, ref block, label) => {
4393-
self.with_resolved_label(label, expr.id, |this| {
4409+
self.with_resolved_label(label, expr.id, true, block.span, |this| {
43944410
this.with_rib(ValueNS, RibKind::Normal, |this| {
43954411
let old = this.diagnostic_metadata.in_if_condition.replace(cond);
43964412
this.visit_expr(cond);
@@ -4404,11 +4420,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
44044420
self.visit_expr(iter);
44054421
self.with_rib(ValueNS, RibKind::Normal, |this| {
44064422
this.resolve_pattern_top(pat, PatternSource::For);
4407-
this.resolve_labeled_block(label, expr.id, body);
4423+
this.resolve_labeled_block(label, expr.id, body, true);
44084424
});
44094425
}
44104426

4411-
ExprKind::Block(ref block, label) => self.resolve_labeled_block(label, block.id, block),
4427+
ExprKind::Block(ref block, label) => {
4428+
self.resolve_labeled_block(label, block.id, block, false)
4429+
}
44124430

44134431
// Equivalent to `visit::walk_expr` + passing some context to children.
44144432
ExprKind::Field(ref subexpression, _) => {

compiler/rustc_resolve/src/late/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
951951
if let Some(err_code) = err.code {
952952
if err_code == E0425 {
953953
for label_rib in &self.label_ribs {
954-
for (label_ident, node_id) in &label_rib.bindings {
954+
for (label_ident, (node_id, _, _)) in &label_rib.bindings {
955955
let ident = path.last().unwrap().ident;
956956
if format!("'{ident}") == label_ident.to_string() {
957957
err.span_label(label_ident.span, "a label with a similar name exists");

compiler/rustc_resolve/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,8 @@ pub struct Resolver<'a, 'tcx> {
999999
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
10001000
import_res_map: NodeMap<PerNS<Option<Res>>>,
10011001
/// Resolutions for labels (node IDs of their corresponding blocks or loops).
1002-
label_res_map: NodeMap<NodeId>,
1002+
/// The boolean stores if the node is loop. The span is the span of the node.
1003+
label_res_map: NodeMap<(NodeId, bool, Span)>,
10031004
/// Resolutions for lifetimes.
10041005
lifetimes_res_map: NodeMap<LifetimeRes>,
10051006
/// Lifetime parameters that lowering will have to introduce.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![allow(incomplete_features)]
2+
#![feature(adt_const_params)]
3+
4+
trait Trait<const S: &'static str> {}
5+
6+
struct Bug<T>
7+
where
8+
T: Trait<
9+
{
10+
'b: { //~ ERROR [E0308]
11+
continue 'b; //~ ERROR [E0696]
12+
}
13+
},
14+
>,
15+
{
16+
t: T,
17+
}
18+
19+
fn f() -> impl Sized {
20+
'b: {
21+
continue 'b;
22+
//~^ ERROR [E0696]
23+
}
24+
}
25+
26+
fn main() {
27+
f();
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0696]: `continue` pointing to a labeled block
2+
--> $DIR/cont-in-fn-issue-113379.rs:11:17
3+
|
4+
LL | / 'b: {
5+
LL | | continue 'b;
6+
| | ^^^^^^^^^^^ labeled blocks cannot be `continue`'d
7+
LL | | }
8+
| |_____________- labeled block the `continue` points to
9+
10+
error[E0696]: `continue` pointing to a labeled block
11+
--> $DIR/cont-in-fn-issue-113379.rs:21:9
12+
|
13+
LL | / 'b: {
14+
LL | | continue 'b;
15+
| | ^^^^^^^^^^^ labeled blocks cannot be `continue`'d
16+
LL | |
17+
LL | | }
18+
| |_____- labeled block the `continue` points to
19+
20+
error[E0308]: mismatched types
21+
--> $DIR/cont-in-fn-issue-113379.rs:10:13
22+
|
23+
LL | / 'b: {
24+
LL | | continue 'b;
25+
LL | | }
26+
| |_____________^ expected `&str`, found `()`
27+
28+
error: aborting due to 3 previous errors
29+
30+
Some errors have detailed explanations: E0308, E0696.
31+
For more information about an error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
match () {
3+
_ => 'b: {
4+
continue 'b;
5+
//~^ ERROR [E0696]
6+
}
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0696]: `continue` pointing to a labeled block
2+
--> $DIR/cont-in-match-arm-issue-121623.rs:4:13
3+
|
4+
LL | _ => 'b: {
5+
| ______________-
6+
LL | | continue 'b;
7+
| | ^^^^^^^^^^^ labeled blocks cannot be `continue`'d
8+
LL | |
9+
LL | | }
10+
| |_________- labeled block the `continue` points to
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0696`.

0 commit comments

Comments
 (0)