Skip to content

Commit 72a2d35

Browse files
committed
fix is_const_context
1 parent 672b272 commit 72a2d35

File tree

7 files changed

+12
-85
lines changed

7 files changed

+12
-85
lines changed

src/librustc_middle/hir/map/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,16 @@ impl<'hir> Map<'hir> {
374374
}
375375
}
376376

377+
pub fn enclosing_body_owner(&self, hir_id: HirId) -> HirId {
378+
for (parent, _) in self.parent_iter(hir_id) {
379+
if let Some(body) = self.maybe_body_owned_by(parent) {
380+
return self.body_owner(body);
381+
}
382+
}
383+
384+
bug!("no `enclosing_body_owner` for hir_id `{}`", hir_id);
385+
}
386+
377387
/// Returns the `HirId` that corresponds to the definition of
378388
/// which this is the body of, i.e., a `fn`, `const` or `static`
379389
/// item (possibly associated), a closure, or a `hir::AnonConst`.
@@ -577,17 +587,7 @@ impl<'hir> Map<'hir> {
577587
/// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context.
578588
/// Used exclusively for diagnostics, to avoid suggestion function calls.
579589
pub fn is_const_context(&self, hir_id: HirId) -> bool {
580-
let parent_id = self.get_parent_item(hir_id);
581-
match self.get(parent_id) {
582-
Node::Item(&Item { kind: ItemKind::Const(..) | ItemKind::Static(..), .. })
583-
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Const(..), .. })
584-
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Const(..), .. })
585-
| Node::AnonConst(_) => true,
586-
Node::Item(&Item { kind: ItemKind::Fn(ref sig, ..), .. }) => {
587-
sig.header.constness == Constness::Const
588-
}
589-
_ => false,
590-
}
590+
self.body_const_context(self.local_def_id(self.enclosing_body_owner(hir_id))).is_some()
591591
}
592592

593593
/// Whether `hir_id` corresponds to a `mod` or a crate.

src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
495495
let closure_id = hir.as_local_hir_id(self.mir_def_id.expect_local());
496496
let fn_call_id = hir.get_parent_node(closure_id);
497497
let node = hir.get(fn_call_id);
498-
let item_id = hir.get_parent_item(fn_call_id);
498+
let item_id = hir.enclosing_body_owner(fn_call_id);
499499
let mut look_at_return = true;
500500
// If we can detect the expression to be an `fn` call where the closure was an argument,
501501
// we point at the `fn` definition argument...

src/test/ui/consts/enum-discr-type-err.stderr

-8
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ LL | | }
1111
| |_- in this macro invocation
1212
|
1313
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
14-
help: you can convert an `i32` to `isize` and panic if the converted value wouldn't fit
15-
|
16-
LL | $( $v = $s::V.try_into().unwrap(), )*
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1814

1915
error[E0308]: mismatched types
2016
--> $DIR/enum-discr-type-err.rs:18:21
@@ -29,10 +25,6 @@ LL | | }
2925
| |_- in this macro invocation
3026
|
3127
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
32-
help: you can convert an `i32` to `isize` and panic if the converted value wouldn't fit
33-
|
34-
LL | $( $v = $s::V.try_into().unwrap(), )*
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
3628

3729
error: aborting due to 2 previous errors
3830

src/test/ui/discrim/discrim-ill-typed.stderr

-40
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,48 @@ error[E0308]: mismatched types
33
|
44
LL | OhNo = 0_u8,
55
| ^^^^ expected `i8`, found `u8`
6-
|
7-
help: change the type of the numeric literal from `u8` to `i8`
8-
|
9-
LL | OhNo = 0_i8,
10-
| ^^^^
116

127
error[E0308]: mismatched types
138
--> $DIR/discrim-ill-typed.rs:30:16
149
|
1510
LL | OhNo = 0_i8,
1611
| ^^^^ expected `u8`, found `i8`
17-
|
18-
help: change the type of the numeric literal from `i8` to `u8`
19-
|
20-
LL | OhNo = 0_u8,
21-
| ^^^^
2212

2313
error[E0308]: mismatched types
2414
--> $DIR/discrim-ill-typed.rs:43:16
2515
|
2616
LL | OhNo = 0_u16,
2717
| ^^^^^ expected `i16`, found `u16`
28-
|
29-
help: change the type of the numeric literal from `u16` to `i16`
30-
|
31-
LL | OhNo = 0_i16,
32-
| ^^^^^
3318

3419
error[E0308]: mismatched types
3520
--> $DIR/discrim-ill-typed.rs:56:16
3621
|
3722
LL | OhNo = 0_i16,
3823
| ^^^^^ expected `u16`, found `i16`
39-
|
40-
help: change the type of the numeric literal from `i16` to `u16`
41-
|
42-
LL | OhNo = 0_u16,
43-
| ^^^^^
4424

4525
error[E0308]: mismatched types
4626
--> $DIR/discrim-ill-typed.rs:69:16
4727
|
4828
LL | OhNo = 0_u32,
4929
| ^^^^^ expected `i32`, found `u32`
50-
|
51-
help: change the type of the numeric literal from `u32` to `i32`
52-
|
53-
LL | OhNo = 0_i32,
54-
| ^^^^^
5530

5631
error[E0308]: mismatched types
5732
--> $DIR/discrim-ill-typed.rs:82:16
5833
|
5934
LL | OhNo = 0_i32,
6035
| ^^^^^ expected `u32`, found `i32`
61-
|
62-
help: change the type of the numeric literal from `i32` to `u32`
63-
|
64-
LL | OhNo = 0_u32,
65-
| ^^^^^
6636

6737
error[E0308]: mismatched types
6838
--> $DIR/discrim-ill-typed.rs:95:16
6939
|
7040
LL | OhNo = 0_u64,
7141
| ^^^^^ expected `i64`, found `u64`
72-
|
73-
help: change the type of the numeric literal from `u64` to `i64`
74-
|
75-
LL | OhNo = 0_i64,
76-
| ^^^^^
7742

7843
error[E0308]: mismatched types
7944
--> $DIR/discrim-ill-typed.rs:108:16
8045
|
8146
LL | OhNo = 0_i64,
8247
| ^^^^^ expected `u64`, found `i64`
83-
|
84-
help: change the type of the numeric literal from `i64` to `u64`
85-
|
86-
LL | OhNo = 0_u64,
87-
| ^^^^^
8848

8949
error: aborting due to 8 previous errors
9050

src/test/ui/issues/issue-31910.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ error[E0308]: mismatched types
33
|
44
LL | X = Trait::Number,
55
| ^^^^^^^^^^^^^ expected `isize`, found `i32`
6-
|
7-
help: you can convert an `i32` to `isize` and panic if the converted value wouldn't fit
8-
|
9-
LL | X = Trait::Number.try_into().unwrap(),
10-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116

127
error: aborting due to previous error
138

src/test/ui/issues/issue-8761.stderr

-10
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,12 @@ error[E0308]: mismatched types
33
|
44
LL | A = 1i64,
55
| ^^^^ expected `isize`, found `i64`
6-
|
7-
help: change the type of the numeric literal from `i64` to `isize`
8-
|
9-
LL | A = 1isize,
10-
| ^^^^^^
116

127
error[E0308]: mismatched types
138
--> $DIR/issue-8761.rs:5:9
149
|
1510
LL | B = 2u8
1611
| ^^^ expected `isize`, found `u8`
17-
|
18-
help: change the type of the numeric literal from `u8` to `isize`
19-
|
20-
LL | B = 2isize
21-
| ^^^^^^
2212

2313
error: aborting due to 2 previous errors
2414

src/test/ui/repeat_count.stderr

-10
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,12 @@ error[E0308]: mismatched types
3939
|
4040
LL | let f = [0; -4_isize];
4141
| ^^^^^^^^ expected `usize`, found `isize`
42-
|
43-
help: you can convert an `isize` to `usize` and panic if the converted value wouldn't fit
44-
|
45-
LL | let f = [0; (-4_isize).try_into().unwrap()];
46-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4742

4843
error[E0308]: mismatched types
4944
--> $DIR/repeat_count.rs:22:23
5045
|
5146
LL | let f = [0_usize; -1_isize];
5247
| ^^^^^^^^ expected `usize`, found `isize`
53-
|
54-
help: you can convert an `isize` to `usize` and panic if the converted value wouldn't fit
55-
|
56-
LL | let f = [0_usize; (-1_isize).try_into().unwrap()];
57-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5848

5949
error: aborting due to 8 previous errors
6050

0 commit comments

Comments
 (0)