Skip to content

Commit f526b86

Browse files
committed
WIP (needs 32 bit run and/or figuring out how to evaluate constants in rustc_trait_selection::traits::query::normalize without segfaulting the compiler while compiling the compiler_builtins crate. Maybe I just need to turn on LLVM assertions to get a better message than a segfault? I copied over the evaluation code from ConstKind::try_eval (+ changing the call from const_eval_for_ty to const_eval_resolve).
1 parent a0efd41 commit f526b86

24 files changed

+226
-355
lines changed

compiler/rustc_middle/src/mir/visit.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,7 @@ macro_rules! make_mir_visitor {
872872
self.visit_span(span);
873873
drop(user_ty); // no visit method for this
874874
match literal {
875-
// Type level constants don't contain anything interesting
876-
ConstantKind::Ty(_) => {},
875+
ConstantKind::Ty(ct) => self.visit_const(ct, location),
877876
ConstantKind::Val(_, t) => self.visit_ty(t, TyContext::Location(location)),
878877
}
879878
}

compiler/rustc_middle/src/ty/fold.rs

+4
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ impl<'a, 'tcx> TypeFolder<'tcx> for RegionFolder<'a, 'tcx> {
431431
}
432432
}
433433
}
434+
435+
fn fold_mir_const(&mut self, constant: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
436+
constant.super_fold_with(self)
437+
}
434438
}
435439

436440
///////////////////////////////////////////////////////////////////////////

compiler/rustc_trait_selection/src/traits/query/normalize.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
217217
}
218218

219219
fn fold_mir_const(&mut self, constant: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> {
220-
constant.super_fold_with(self)
220+
match constant {
221+
mir::ConstantKind::Ty(ct) => mir::ConstantKind::Ty(ct.super_fold_with(self)),
222+
mir::ConstantKind::Val(val, ty) => mir::ConstantKind::Val(val, ty.fold_with(self)),
223+
}
221224
}
222225
}

src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir

+16-16
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,44 @@ fn main() -> () {
2121
}
2222

2323
alloc0 (static: FOO, size: 16, align: 8) {
24-
╾───────alloc27───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
24+
╾───────alloc21───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
2525
}
2626

27-
alloc27 (size: 72, align: 8) {
28-
0x00 │ 00 00 00 00 __ __ __ __ ╾───────alloc12───────╼ │ ....░░░░╾──────╼
27+
alloc21 (size: 72, align: 8) {
28+
0x00 │ 00 00 00 00 __ __ __ __ ╾───────alloc8────────╼ │ ....░░░░╾──────╼
2929
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░
30-
0x20 │ ╾───────alloc17───────╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........
31-
0x30 │ 01 00 00 00 2a 00 00 00 ╾───────alloc25───────╼ │ ....*...╾──────╼
30+
0x20 │ ╾───────alloc13───────╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........
31+
0x30 │ 01 00 00 00 2a 00 00 00 ╾───────alloc19───────╼ │ ....*...╾──────╼
3232
0x40 │ 03 00 00 00 00 00 00 00 │ ........
3333
}
3434

35-
alloc12 (size: 0, align: 8) {}
35+
alloc8 (size: 0, align: 8) {}
3636

37-
alloc17 (size: 16, align: 8) {
38-
╾───────alloc15───────╼ ╾───────alloc16───────╼ │ ╾──────╼╾──────╼
37+
alloc13 (size: 16, align: 8) {
38+
╾───────alloc11───────╼ ╾───────alloc12───────╼ │ ╾──────╼╾──────╼
3939
}
4040

41-
alloc15 (size: 1, align: 1) {
41+
alloc11 (size: 1, align: 1) {
4242
05 │ .
4343
}
4444

45-
alloc16 (size: 1, align: 1) {
45+
alloc12 (size: 1, align: 1) {
4646
06 │ .
4747
}
4848

49-
alloc25 (size: 24, align: 8) {
50-
0x00 │ ╾─────alloc21+0x3─────╼ ╾───────alloc22───────╼ │ ╾──────╼╾──────╼
51-
0x10 │ ╾─────alloc24+0x2─────╼ │ ╾──────╼
49+
alloc19 (size: 24, align: 8) {
50+
0x00 │ ╾─────alloc16+0x3─────╼ ╾───────alloc17───────╼ │ ╾──────╼╾──────╼
51+
0x10 │ ╾─────alloc18+0x2─────╼ │ ╾──────╼
5252
}
5353

54-
alloc21 (size: 4, align: 1) {
54+
alloc16 (size: 4, align: 1) {
5555
2a 45 15 6f │ *E.o
5656
}
5757

58-
alloc22 (size: 1, align: 1) {
58+
alloc17 (size: 1, align: 1) {
5959
2a │ *
6060
}
6161

62-
alloc24 (size: 4, align: 1) {
62+
alloc18 (size: 4, align: 1) {
6363
2a 45 15 6f │ *E.o
6464
}

src/test/mir-opt/inline/inline_into_box_place.main.Inline.64bit.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
- (*_2) = Vec::<u32>::new() -> [return: bb1, unwind: bb4]; // scope 0 at $DIR/inline-into-box-place.rs:8:33: 8:43
2121
+ StorageLive(_4); // scope 0 at $DIR/inline-into-box-place.rs:8:33: 8:43
2222
+ _4 = &mut (*_2); // scope 0 at $DIR/inline-into-box-place.rs:8:33: 8:43
23-
+ ((*_4).0: alloc::raw_vec::RawVec<u32>) = const alloc::raw_vec::RawVec::<u32> { ptr: Unique::<u32> { pointer: {0x4 as *const u32}, _marker: PhantomData::<u32> }, cap: 0_usize, alloc: std::alloc::Global }; // scope 2 at $DIR/inline-into-box-place.rs:8:33: 8:43
23+
+ ((*_4).0: alloc::raw_vec::RawVec<u32>) = const alloc::raw_vec::RawVec::<u32>::NEW; // scope 2 at $DIR/inline-into-box-place.rs:8:33: 8:43
2424
+ // ty::Const
2525
+ // + ty: alloc::raw_vec::RawVec<u32>
26-
+ // + val: Value(ByRef { alloc: Allocation { bytes: [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [65535], len: Size { raw: 16 } }, size: Size { raw: 16 }, align: Align { pow2: 3 }, mutability: Not, extra: () }, offset: Size { raw: 0 } })
26+
+ // + val: Unevaluated(alloc::raw_vec::RawVec::<T>::NEW, [u32], None)
2727
// mir::Constant
2828
- // + span: $DIR/inline-into-box-place.rs:8:33: 8:41
2929
- // + user_ty: UserType(1)
@@ -33,7 +33,7 @@
3333
- bb1: {
3434
+ // + span: $DIR/inline-into-box-place.rs:8:33: 8:43
3535
+ // + user_ty: UserType(0)
36-
+ // + literal: Const { ty: alloc::raw_vec::RawVec<u32>, val: Value(ByRef { alloc: Allocation { bytes: [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [65535], len: Size { raw: 16 } }, size: Size { raw: 16 }, align: Align { pow2: 3 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
36+
+ // + literal: Const { ty: alloc::raw_vec::RawVec<u32>, val: Unevaluated(Unevaluated { def: WithOptConstParam { did: DefId(5:4209 ~ alloc[65c8]::raw_vec::{impl#0}::NEW), const_param_did: None }, substs: [u32], promoted: None }) }
3737
+ ((*_4).1: usize) = const 0_usize; // scope 2 at $DIR/inline-into-box-place.rs:8:33: 8:43
3838
+ StorageDead(_4); // scope 0 at $DIR/inline-into-box-place.rs:8:33: 8:43
3939
_1 = move _2; // scope 0 at $DIR/inline-into-box-place.rs:8:29: 8:43

src/test/ui/associated-consts/defaults-cyclic-fail.stderr

+2-46
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,15 @@
1-
<<<<<<< HEAD
2-
error[E0391]: cycle detected when normalizing `<() as Tr>::A`
3-
|
4-
note: ...which requires simplifying constant for the type system `Tr::A`...
5-
--> $DIR/defaults-cyclic-fail.rs:6:5
6-
||||||| parent of ab70bbeec72... Use ValTree in all type level constants
7-
error[E0391]: cycle detected when simplifying constant for the type system `Tr::A`
8-
--> $DIR/defaults-cyclic-fail.rs:5:5
9-
=======
10-
error[E0391]: cycle detected when evaluating constant for MIR opt/codegen `Tr::A`
11-
--> $DIR/defaults-cyclic-fail.rs:5:5
12-
>>>>>>> ab70bbeec72... Use ValTree in all type level constants
13-
|
14-
LL | const A: u8 = Self::B;
15-
| ^^^^^^^^^^^^^^^^^^^^^^
16-
note: ...which requires simplifying constant for the type system `Tr::A`...
17-
--> $DIR/defaults-cyclic-fail.rs:6:5
18-
|
19-
LL | const A: u8 = Self::B;
20-
| ^^^^^^^^^^^^^^^^^^^^^^
21-
note: ...which requires const-evaluating + checking `Tr::A`...
1+
error[E0391]: cycle detected when const-evaluating + checking `Tr::A`
222
--> $DIR/defaults-cyclic-fail.rs:6:5
233
|
244
LL | const A: u8 = Self::B;
255
| ^^^^^^^^^^^^^^^^^^^^^^
26-
<<<<<<< HEAD
27-
= note: ...which requires normalizing `<() as Tr>::B`...
28-
note: ...which requires simplifying constant for the type system `Tr::B`...
29-
--> $DIR/defaults-cyclic-fail.rs:8:5
30-
|
31-
LL | const B: u8 = Self::A;
32-
| ^^^^^^^^^^^^^^^^^^^^^^
33-
note: ...which requires simplifying constant for the type system `Tr::B`...
34-
--> $DIR/defaults-cyclic-fail.rs:8:5
35-
||||||| parent of ab70bbeec72... Use ValTree in all type level constants
36-
note: ...which requires simplifying constant for the type system `Tr::B`...
37-
--> $DIR/defaults-cyclic-fail.rs:7:5
38-
=======
39-
note: ...which requires evaluating constant for MIR opt/codegen `Tr::B`...
40-
--> $DIR/defaults-cyclic-fail.rs:7:5
41-
>>>>>>> ab70bbeec72... Use ValTree in all type level constants
426
|
43-
LL | const B: u8 = Self::A;
44-
| ^^^^^^^^^^^^^^^^^^^^^^
457
note: ...which requires const-evaluating + checking `Tr::B`...
468
--> $DIR/defaults-cyclic-fail.rs:8:5
479
|
4810
LL | const B: u8 = Self::A;
4911
| ^^^^^^^^^^^^^^^^^^^^^^
50-
<<<<<<< HEAD
51-
= note: ...which again requires normalizing `<() as Tr>::A`, completing the cycle
52-
||||||| parent of ab70bbeec72... Use ValTree in all type level constants
53-
= note: ...which again requires simplifying constant for the type system `Tr::A`, completing the cycle
54-
=======
55-
= note: ...which again requires evaluating constant for MIR opt/codegen `Tr::A`, completing the cycle
56-
>>>>>>> ab70bbeec72... Use ValTree in all type level constants
12+
= note: ...which again requires const-evaluating + checking `Tr::A`, completing the cycle
5713
note: cycle used when const-evaluating + checking `main::promoted[1]`
5814
--> $DIR/defaults-cyclic-fail.rs:14:1
5915
|

src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.stderr

+12-37
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,9 @@
1-
error[E0391]: cycle detected when evaluating constant for MIR opt/codegen `IMPL_REF_BAR`
1+
error[E0391]: cycle detected when const-evaluating + checking `IMPL_REF_BAR`
22
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
33
|
44
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
note: ...which requires evaluating constant for MIR opt/codegen `IMPL_REF_BAR`...
8-
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
9-
|
10-
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: ...which requires const-evaluating + checking `IMPL_REF_BAR`...
13-
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
14-
|
15-
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17-
<<<<<<< HEAD
18-
= note: ...which requires normalizing `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`...
19-
note: ...which requires simplifying constant for the type system `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`...
20-
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
21-
|
22-
LL | const BAR: u32 = IMPL_REF_BAR;
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24-
note: ...which requires simplifying constant for the type system `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`...
25-
||||||| parent of ab70bbeec72... Use ValTree in all type level constants
26-
note: ...which requires simplifying constant for the type system `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`...
27-
=======
28-
note: ...which requires evaluating constant for MIR opt/codegen `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`...
29-
>>>>>>> ab70bbeec72... Use ValTree in all type level constants
30-
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
31-
|
32-
LL | const BAR: u32 = IMPL_REF_BAR;
33-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
347
note: ...which requires const-evaluating + checking `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`...
358
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
369
|
@@ -41,15 +14,17 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
4114
|
4215
LL | const BAR: u32 = IMPL_REF_BAR;
4316
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44-
<<<<<<< HEAD
45-
= note: ...which requires normalizing `IMPL_REF_BAR`...
46-
= note: ...which again requires simplifying constant for the type system `IMPL_REF_BAR`, completing the cycle
47-
||||||| parent of ab70bbeec72... Use ValTree in all type level constants
48-
= note: ...which again requires simplifying constant for the type system `IMPL_REF_BAR`, completing the cycle
49-
=======
50-
= note: ...which again requires evaluating constant for MIR opt/codegen `IMPL_REF_BAR`, completing the cycle
51-
>>>>>>> ab70bbeec72... Use ValTree in all type level constants
52-
= note: cycle used when running analysis passes on this crate
17+
note: ...which requires const-evaluating + checking `IMPL_REF_BAR`...
18+
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
19+
|
20+
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
= note: ...which again requires const-evaluating + checking `IMPL_REF_BAR`, completing the cycle
23+
note: cycle used when evaluating constant for MIR opt/codegen `IMPL_REF_BAR`
24+
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
25+
|
26+
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5328

5429
error: aborting due to previous error
5530

src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait-default.stderr

+12-37
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,9 @@
1-
error[E0391]: cycle detected when evaluating constant for MIR opt/codegen `DEFAULT_REF_BAR`
1+
error[E0391]: cycle detected when const-evaluating + checking `DEFAULT_REF_BAR`
22
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
33
|
44
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
note: ...which requires evaluating constant for MIR opt/codegen `DEFAULT_REF_BAR`...
8-
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
9-
|
10-
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: ...which requires const-evaluating + checking `DEFAULT_REF_BAR`...
13-
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
14-
|
15-
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17-
<<<<<<< HEAD
18-
= note: ...which requires normalizing `<GlobalDefaultRef as FooDefault>::BAR`...
19-
note: ...which requires simplifying constant for the type system `FooDefault::BAR`...
20-
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
21-
|
22-
LL | const BAR: u32 = DEFAULT_REF_BAR;
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24-
note: ...which requires simplifying constant for the type system `FooDefault::BAR`...
25-
||||||| parent of ab70bbeec72... Use ValTree in all type level constants
26-
note: ...which requires simplifying constant for the type system `FooDefault::BAR`...
27-
=======
28-
note: ...which requires evaluating constant for MIR opt/codegen `FooDefault::BAR`...
29-
>>>>>>> ab70bbeec72... Use ValTree in all type level constants
30-
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
31-
|
32-
LL | const BAR: u32 = DEFAULT_REF_BAR;
33-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
347
note: ...which requires const-evaluating + checking `FooDefault::BAR`...
358
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
369
|
@@ -41,15 +14,17 @@ note: ...which requires caching mir of `FooDefault::BAR` for CTFE...
4114
|
4215
LL | const BAR: u32 = DEFAULT_REF_BAR;
4316
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44-
<<<<<<< HEAD
45-
= note: ...which requires normalizing `DEFAULT_REF_BAR`...
46-
= note: ...which again requires simplifying constant for the type system `DEFAULT_REF_BAR`, completing the cycle
47-
||||||| parent of ab70bbeec72... Use ValTree in all type level constants
48-
= note: ...which again requires simplifying constant for the type system `DEFAULT_REF_BAR`, completing the cycle
49-
=======
50-
= note: ...which again requires evaluating constant for MIR opt/codegen `DEFAULT_REF_BAR`, completing the cycle
51-
>>>>>>> ab70bbeec72... Use ValTree in all type level constants
52-
= note: cycle used when running analysis passes on this crate
17+
note: ...which requires const-evaluating + checking `DEFAULT_REF_BAR`...
18+
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
19+
|
20+
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
= note: ...which again requires const-evaluating + checking `DEFAULT_REF_BAR`, completing the cycle
23+
note: cycle used when evaluating constant for MIR opt/codegen `DEFAULT_REF_BAR`
24+
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
25+
|
26+
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5328

5429
error: aborting due to previous error
5530

0 commit comments

Comments
 (0)