Skip to content

Commit 38d3834

Browse files
committed
typeck: workaround WF hole in to_const.
1 parent 2bbc33a commit 38d3834

16 files changed

+112
-16
lines changed

src/librustc_typeck/check/mod.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -3311,8 +3311,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33113311
}
33123312

33133313
pub fn to_const(&self, ast_c: &hir::AnonConst) -> &'tcx ty::Const<'tcx> {
3314-
let c = self.tcx.hir().local_def_id(ast_c.hir_id).expect_local();
3315-
ty::Const::from_anon_const(self.tcx, c)
3314+
let const_def_id = self.tcx.hir().local_def_id(ast_c.hir_id).expect_local();
3315+
let c = ty::Const::from_anon_const(self.tcx, const_def_id);
3316+
3317+
// HACK(eddyb) emulate what a `WellFormedConst` obligation would do.
3318+
// This code should be replaced with the proper WF handling ASAP.
3319+
if let ty::ConstKind::Unevaluated(def_id, substs, promoted) = c.val {
3320+
assert!(promoted.is_none());
3321+
3322+
// HACK(eddyb) let's hope these are always empty.
3323+
// let obligations = self.nominal_obligations(def_id, substs);
3324+
// self.out.extend(obligations);
3325+
3326+
let cause = traits::ObligationCause::new(
3327+
self.tcx.def_span(const_def_id.to_def_id()),
3328+
self.body_id,
3329+
traits::MiscObligation,
3330+
);
3331+
self.register_predicate(traits::Obligation::new(
3332+
cause,
3333+
self.param_env,
3334+
ty::Predicate::ConstEvaluatable(def_id, substs),
3335+
));
3336+
}
3337+
3338+
c
33163339
}
33173340

33183341
// If the type given by the user has free regions, save it for later, since

src/test/compile-fail/issue-52443.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ fn main() {
88
//~| WARN denote infinite loops with
99
[(); { for _ in 0usize.. {}; 0}];
1010
//~^ ERROR `for` is not allowed in a `const`
11+
//~| ERROR calls in constants are limited to constant functions
12+
//~| ERROR references in constants may only refer to immutable values
13+
//~| ERROR calls in constants are limited to constant functions
14+
//~| ERROR constant contains unimplemented expression type
15+
//~| ERROR evaluation of constant value failed
1116
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#![feature(const_generics)]
22
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
33

4-
// build-pass
5-
64
fn foo<const N: usize>() {
75
let _ = [0u64; N + 1];
6+
//~^ ERROR constant expression depends on a generic parameter
87
}
98

109
fn main() {}

src/test/ui/const-generics/issues/issue-62456.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@ LL | #![feature(const_generics)]
66
|
77
= note: `#[warn(incomplete_features)]` on by default
88

9-
warning: 1 warning emitted
9+
error: constant expression depends on a generic parameter
10+
--> $DIR/issue-62456.rs:5:20
11+
|
12+
LL | let _ = [0u64; N + 1];
13+
| ^^^^^
14+
|
15+
= note: this may fail depending on what value the parameter takes
16+
17+
error: aborting due to previous error; 1 warning emitted
1018

src/test/ui/const-generics/issues/issue-62504.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl<const X: usize> ArrayHolder<X> {
1717
pub const fn new() -> Self {
1818
ArrayHolder([0; Self::SIZE])
1919
//~^ ERROR: mismatched types
20+
//~| ERROR constant expression depends on a generic parameter
2021
}
2122
}
2223

src/test/ui/const-generics/issues/issue-62504.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ LL | ArrayHolder([0; Self::SIZE])
77
= note: expected array `[u32; _]`
88
found array `[u32; _]`
99

10-
error: aborting due to previous error
10+
error: constant expression depends on a generic parameter
11+
--> $DIR/issue-62504.rs:18:25
12+
|
13+
LL | ArrayHolder([0; Self::SIZE])
14+
| ^^^^^^^^^^
15+
|
16+
= note: this may fail depending on what value the parameter takes
17+
18+
error: aborting due to 2 previous errors
1119

1220
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// check-pass
2-
31
#![allow(incomplete_features, dead_code, unconditional_recursion)]
42
#![feature(const_generics)]
53

64
fn fact<const N: usize>() {
75
fact::<{ N - 1 }>();
6+
//~^ ERROR constant expression depends on a generic parameter
87
}
98

109
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/issue-66205.rs:5:12
3+
|
4+
LL | fact::<{ N - 1 }>();
5+
| ^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+

src/test/ui/const-generics/issues/issue-67739.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Regression test for #67739
22

3-
// check-pass
4-
53
#![allow(incomplete_features)]
64
#![feature(const_generics)]
75

@@ -12,6 +10,7 @@ pub trait Trait {
1210

1311
fn associated_size(&self) -> usize {
1412
[0u8; mem::size_of::<Self::Associated>()];
13+
//~^ ERROR constant expression depends on a generic parameter
1514
0
1615
}
1716
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/issue-67739.rs:12:15
3+
|
4+
LL | [0u8; mem::size_of::<Self::Associated>()];
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: aborting due to previous error
10+
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
fn main() {
22
[(); { &loop { break } as *const _ as usize } ];
33
//~^ ERROR `loop` is not allowed in a `const`
4+
//~| ERROR casting pointers to integers in constants is unstable
5+
//~| ERROR evaluation of constant value failed
46
}

src/test/ui/consts/const-eval/issue-52442.stderr

+18-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ LL | [(); { &loop { break } as *const _ as usize } ];
77
= note: see issue #52000 <https://github.com/rust-lang/rust/issues/52000> for more information
88
= help: add `#![feature(const_loop)]` to the crate attributes to enable
99

10-
error: aborting due to previous error
10+
error[E0658]: casting pointers to integers in constants is unstable
11+
--> $DIR/issue-52442.rs:2:13
12+
|
13+
LL | [(); { &loop { break } as *const _ as usize } ];
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
17+
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
18+
19+
error[E0080]: evaluation of constant value failed
20+
--> $DIR/issue-52442.rs:2:13
21+
|
22+
LL | [(); { &loop { break } as *const _ as usize } ];
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
24+
25+
error: aborting due to 3 previous errors
1126

12-
For more information about this error, try `rustc --explain E0658`.
27+
Some errors have detailed explanations: E0080, E0658.
28+
For more information about an error, try `rustc --explain E0080`.

src/test/ui/consts/issue-52432.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ fn main() {
66
//~| ERROR: type annotations needed
77
[(); &(static || {}) as *const _ as usize];
88
//~^ ERROR: closures cannot be static
9+
//~| ERROR evaluation of constant value failed
910
}

src/test/ui/consts/issue-52432.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ error[E0282]: type annotations needed
1616
LL | [(); &(static |x| {}) as *const _ as usize];
1717
| ^ consider giving this closure parameter a type
1818

19-
error: aborting due to 3 previous errors
19+
error[E0080]: evaluation of constant value failed
20+
--> $DIR/issue-52432.rs:7:10
21+
|
22+
LL | [(); &(static || {}) as *const _ as usize];
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
24+
25+
error: aborting due to 4 previous errors
2026

21-
Some errors have detailed explanations: E0282, E0697.
22-
For more information about an error, try `rustc --explain E0282`.
27+
Some errors have detailed explanations: E0080, E0282, E0697.
28+
For more information about an error, try `rustc --explain E0080`.

src/test/ui/issues/issue-69602-type-err-during-codegen-ice.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ impl TraitB for B { //~ ERROR not all trait items implemented, missing: `MyA`
1919

2020
fn main() {
2121
let _ = [0; B::VALUE];
22+
//~^ ERROR constant expression depends on a generic parameter
2223
}

src/test/ui/issues/issue-69602-type-err-during-codegen-ice.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ LL | type MyA: TraitA;
1313
LL | impl TraitB for B {
1414
| ^^^^^^^^^^^^^^^^^ missing `MyA` in implementation
1515

16-
error: aborting due to 2 previous errors
16+
error: constant expression depends on a generic parameter
17+
--> $DIR/issue-69602-type-err-during-codegen-ice.rs:21:17
18+
|
19+
LL | let _ = [0; B::VALUE];
20+
| ^^^^^^^^
21+
|
22+
= note: this may fail depending on what value the parameter takes
23+
24+
error: aborting due to 3 previous errors
1725

1826
Some errors have detailed explanations: E0046, E0437.
1927
For more information about an error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)