Skip to content

Commit d718532

Browse files
committed
Loop check anon consts on their own
1 parent 349681f commit d718532

File tree

5 files changed

+45
-45
lines changed

5 files changed

+45
-45
lines changed

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,7 @@ fn typeck_with_inspect<'tcx>(
188188
tcx.type_of(def_id).instantiate_identity()
189189
};
190190

191-
// TODO: anon consts are currently loop checked with their containing body, even though
192-
// they are typecked on their own.
193-
if let DefKind::AssocConst | DefKind::Const | DefKind::Static { .. } = tcx.def_kind(def_id)
194-
{
195-
loops::check(tcx, def_id, body);
196-
}
191+
loops::check(tcx, def_id, body);
197192

198193
let expected_type = fcx.normalize(body.value.span, expected_type);
199194

compiler/rustc_hir_typeck/src/loops.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fmt;
33

44
use Context::*;
55
use rustc_hir as hir;
6+
use rustc_hir::def::DefKind;
67
use rustc_hir::def_id::LocalDefId;
78
use rustc_hir::intravisit::{self, Visitor};
89
use rustc_hir::{Destination, Node};
@@ -72,10 +73,14 @@ struct CheckLoopVisitor<'tcx> {
7273
block_breaks: BTreeMap<Span, BlockInfo>,
7374
}
7475

75-
pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, _def_id: LocalDefId, body: &'tcx hir::Body<'tcx>) {
76+
pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'tcx hir::Body<'tcx>) {
7677
let mut check =
7778
CheckLoopVisitor { tcx, cx_stack: vec![Normal], block_breaks: Default::default() };
78-
check.with_context(Fn, |v| v.visit_body(body));
79+
let cx = match tcx.def_kind(def_id) {
80+
DefKind::AnonConst => AnonConst,
81+
_ => Fn,
82+
};
83+
check.with_context(cx, |v| v.visit_body(body));
7984
check.report_outside_loop_error();
8085
}
8186

@@ -86,8 +91,8 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
8691
self.tcx
8792
}
8893

89-
fn visit_anon_const(&mut self, c: &'hir hir::AnonConst) {
90-
self.with_context(AnonConst, |v| intravisit::walk_anon_const(v, c));
94+
fn visit_anon_const(&mut self, _: &'hir hir::AnonConst) {
95+
// Typecked on its own.
9196
}
9297

9398
fn visit_inline_const(&mut self, c: &'hir hir::ConstBlock) {

tests/ui/inline-const/break-inside-inline-const-issue-128604.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ error[E0268]: `break` outside of a loop or labeled block
1010
LL | break;
1111
| ^^^^^ cannot `break` outside of a loop or labeled block
1212

13-
error[E0268]: `break` outside of a loop or labeled block
14-
--> $DIR/break-inside-inline-const-issue-128604.rs:2:21
15-
|
16-
LL | let _ = ['a'; { break 2; 1 }];
17-
| ^^^^^^^ cannot `break` outside of a loop or labeled block
18-
|
19-
help: consider labeling this block to be able to break within it
20-
|
21-
LL | let _ = ['a'; 'block: { break 'block 2; 1 }];
22-
| +++++++ ++++++
23-
2413
error[E0268]: `break` outside of a loop or labeled block
2514
--> $DIR/break-inside-inline-const-issue-128604.rs:9:13
2615
|
@@ -34,6 +23,17 @@ LL |
3423
LL ~ break 'block;
3524
|
3625

26+
error[E0268]: `break` outside of a loop or labeled block
27+
--> $DIR/break-inside-inline-const-issue-128604.rs:2:21
28+
|
29+
LL | let _ = ['a'; { break 2; 1 }];
30+
| ^^^^^^^ cannot `break` outside of a loop or labeled block
31+
|
32+
help: consider labeling this block to be able to break within it
33+
|
34+
LL | let _ = ['a'; 'block: { break 'block 2; 1 }];
35+
| +++++++ ++++++
36+
3737
error[E0268]: `break` outside of a loop or labeled block
3838
--> $DIR/break-inside-inline-const-issue-128604.rs:27:17
3939
|

tests/ui/typeck/issue-114529-illegal-break-with-value.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
error[E0571]: `break` with value from a `while` loop
2+
--> $DIR/issue-114529-illegal-break-with-value.rs:22:9
3+
|
4+
LL | while true {
5+
| ---------- you can't `break` with a value in a `while` loop
6+
LL | / break (|| {
7+
LL | | let local = 9;
8+
LL | | });
9+
| |__________^ can only break with a value inside `loop` or breakable block
10+
|
11+
help: use `break` on its own without a value inside this `while` loop
12+
|
13+
LL - break (|| {
14+
LL - let local = 9;
15+
LL - });
16+
LL + break;
17+
|
18+
119
error[E0571]: `break` with value from a `while` loop
220
--> $DIR/issue-114529-illegal-break-with-value.rs:9:13
321
|
@@ -26,24 +44,6 @@ LL - break v;
2644
LL + break;
2745
|
2846

29-
error[E0571]: `break` with value from a `while` loop
30-
--> $DIR/issue-114529-illegal-break-with-value.rs:22:9
31-
|
32-
LL | while true {
33-
| ---------- you can't `break` with a value in a `while` loop
34-
LL | / break (|| {
35-
LL | | let local = 9;
36-
LL | | });
37-
| |__________^ can only break with a value inside `loop` or breakable block
38-
|
39-
help: use `break` on its own without a value inside this `while` loop
40-
|
41-
LL - break (|| {
42-
LL - let local = 9;
43-
LL - });
44-
LL + break;
45-
|
46-
4747
error: aborting due to 3 previous errors
4848

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

tests/ui/unsafe/break-inside-unsafe-block-issue-128604.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
error[E0268]: `break` outside of a loop or labeled block
2-
--> $DIR/break-inside-unsafe-block-issue-128604.rs:2:28
3-
|
4-
LL | let a = ["_"; unsafe { break; 1 + 2 }];
5-
| ^^^^^ cannot `break` outside of a loop or labeled block
6-
71
error[E0268]: `break` outside of a loop or labeled block
82
--> $DIR/break-inside-unsafe-block-issue-128604.rs:14:9
93
|
@@ -37,6 +31,12 @@ LL | unsafe {
3731
LL ~ break 'block;
3832
|
3933

34+
error[E0268]: `break` outside of a loop or labeled block
35+
--> $DIR/break-inside-unsafe-block-issue-128604.rs:2:28
36+
|
37+
LL | let a = ["_"; unsafe { break; 1 + 2 }];
38+
| ^^^^^ cannot `break` outside of a loop or labeled block
39+
4040
error: aborting due to 4 previous errors
4141

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

0 commit comments

Comments
 (0)