typeck: workaround WF hole in to_const
.
This commit is contained in:
parent
2bbc33aaf0
commit
38d38349f3
@ -3311,8 +3311,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||
}
|
||||
|
||||
pub fn to_const(&self, ast_c: &hir::AnonConst) -> &'tcx ty::Const<'tcx> {
|
||||
let c = self.tcx.hir().local_def_id(ast_c.hir_id).expect_local();
|
||||
ty::Const::from_anon_const(self.tcx, c)
|
||||
let const_def_id = self.tcx.hir().local_def_id(ast_c.hir_id).expect_local();
|
||||
let c = ty::Const::from_anon_const(self.tcx, const_def_id);
|
||||
|
||||
// HACK(eddyb) emulate what a `WellFormedConst` obligation would do.
|
||||
// This code should be replaced with the proper WF handling ASAP.
|
||||
if let ty::ConstKind::Unevaluated(def_id, substs, promoted) = c.val {
|
||||
assert!(promoted.is_none());
|
||||
|
||||
// HACK(eddyb) let's hope these are always empty.
|
||||
// let obligations = self.nominal_obligations(def_id, substs);
|
||||
// self.out.extend(obligations);
|
||||
|
||||
let cause = traits::ObligationCause::new(
|
||||
self.tcx.def_span(const_def_id.to_def_id()),
|
||||
self.body_id,
|
||||
traits::MiscObligation,
|
||||
);
|
||||
self.register_predicate(traits::Obligation::new(
|
||||
cause,
|
||||
self.param_env,
|
||||
ty::Predicate::ConstEvaluatable(def_id, substs),
|
||||
));
|
||||
}
|
||||
|
||||
c
|
||||
}
|
||||
|
||||
// If the type given by the user has free regions, save it for later, since
|
||||
|
@ -8,4 +8,9 @@ fn main() {
|
||||
//~| WARN denote infinite loops with
|
||||
[(); { for _ in 0usize.. {}; 0}];
|
||||
//~^ ERROR `for` is not allowed in a `const`
|
||||
//~| ERROR calls in constants are limited to constant functions
|
||||
//~| ERROR references in constants may only refer to immutable values
|
||||
//~| ERROR calls in constants are limited to constant functions
|
||||
//~| ERROR constant contains unimplemented expression type
|
||||
//~| ERROR evaluation of constant value failed
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
#![feature(const_generics)]
|
||||
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
|
||||
|
||||
// build-pass
|
||||
|
||||
fn foo<const N: usize>() {
|
||||
let _ = [0u64; N + 1];
|
||||
//~^ ERROR constant expression depends on a generic parameter
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
@ -6,5 +6,13 @@ LL | #![feature(const_generics)]
|
||||
|
|
||||
= note: `#[warn(incomplete_features)]` on by default
|
||||
|
||||
warning: 1 warning emitted
|
||||
error: constant expression depends on a generic parameter
|
||||
--> $DIR/issue-62456.rs:5:20
|
||||
|
|
||||
LL | let _ = [0u64; N + 1];
|
||||
| ^^^^^
|
||||
|
|
||||
= note: this may fail depending on what value the parameter takes
|
||||
|
||||
error: aborting due to previous error; 1 warning emitted
|
||||
|
||||
|
@ -17,6 +17,7 @@ impl<const X: usize> ArrayHolder<X> {
|
||||
pub const fn new() -> Self {
|
||||
ArrayHolder([0; Self::SIZE])
|
||||
//~^ ERROR: mismatched types
|
||||
//~| ERROR constant expression depends on a generic parameter
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,14 @@ LL | ArrayHolder([0; Self::SIZE])
|
||||
= note: expected array `[u32; _]`
|
||||
found array `[u32; _]`
|
||||
|
||||
error: aborting due to previous error
|
||||
error: constant expression depends on a generic parameter
|
||||
--> $DIR/issue-62504.rs:18:25
|
||||
|
|
||||
LL | ArrayHolder([0; Self::SIZE])
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
= note: this may fail depending on what value the parameter takes
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
||||
|
@ -1,10 +1,9 @@
|
||||
// check-pass
|
||||
|
||||
#![allow(incomplete_features, dead_code, unconditional_recursion)]
|
||||
#![feature(const_generics)]
|
||||
|
||||
fn fact<const N: usize>() {
|
||||
fact::<{ N - 1 }>();
|
||||
//~^ ERROR constant expression depends on a generic parameter
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
10
src/test/ui/const-generics/issues/issue-66205.stderr
Normal file
10
src/test/ui/const-generics/issues/issue-66205.stderr
Normal file
@ -0,0 +1,10 @@
|
||||
error: constant expression depends on a generic parameter
|
||||
--> $DIR/issue-66205.rs:5:12
|
||||
|
|
||||
LL | fact::<{ N - 1 }>();
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
= note: this may fail depending on what value the parameter takes
|
||||
|
||||
error: aborting due to previous error
|
||||
|
@ -1,7 +1,5 @@
|
||||
// Regression test for #67739
|
||||
|
||||
// check-pass
|
||||
|
||||
#![allow(incomplete_features)]
|
||||
#![feature(const_generics)]
|
||||
|
||||
@ -12,6 +10,7 @@ pub trait Trait {
|
||||
|
||||
fn associated_size(&self) -> usize {
|
||||
[0u8; mem::size_of::<Self::Associated>()];
|
||||
//~^ ERROR constant expression depends on a generic parameter
|
||||
0
|
||||
}
|
||||
}
|
||||
|
10
src/test/ui/const-generics/issues/issue-67739.stderr
Normal file
10
src/test/ui/const-generics/issues/issue-67739.stderr
Normal file
@ -0,0 +1,10 @@
|
||||
error: constant expression depends on a generic parameter
|
||||
--> $DIR/issue-67739.rs:12:15
|
||||
|
|
||||
LL | [0u8; mem::size_of::<Self::Associated>()];
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: this may fail depending on what value the parameter takes
|
||||
|
||||
error: aborting due to previous error
|
||||
|
@ -1,4 +1,6 @@
|
||||
fn main() {
|
||||
[(); { &loop { break } as *const _ as usize } ];
|
||||
//~^ ERROR `loop` is not allowed in a `const`
|
||||
//~| ERROR casting pointers to integers in constants is unstable
|
||||
//~| ERROR evaluation of constant value failed
|
||||
}
|
||||
|
@ -7,6 +7,22 @@ LL | [(); { &loop { break } as *const _ as usize } ];
|
||||
= note: see issue #52000 <https://github.com/rust-lang/rust/issues/52000> for more information
|
||||
= help: add `#![feature(const_loop)]` to the crate attributes to enable
|
||||
|
||||
error: aborting due to previous error
|
||||
error[E0658]: casting pointers to integers in constants is unstable
|
||||
--> $DIR/issue-52442.rs:2:13
|
||||
|
|
||||
LL | [(); { &loop { break } as *const _ as usize } ];
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information
|
||||
= help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable
|
||||
|
||||
For more information about this error, try `rustc --explain E0658`.
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $DIR/issue-52442.rs:2:13
|
||||
|
|
||||
LL | [(); { &loop { break } as *const _ as usize } ];
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0080, E0658.
|
||||
For more information about an error, try `rustc --explain E0080`.
|
||||
|
@ -6,4 +6,5 @@ fn main() {
|
||||
//~| ERROR: type annotations needed
|
||||
[(); &(static || {}) as *const _ as usize];
|
||||
//~^ ERROR: closures cannot be static
|
||||
//~| ERROR evaluation of constant value failed
|
||||
}
|
||||
|
@ -16,7 +16,13 @@ error[E0282]: type annotations needed
|
||||
LL | [(); &(static |x| {}) as *const _ as usize];
|
||||
| ^ consider giving this closure parameter a type
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $DIR/issue-52432.rs:7:10
|
||||
|
|
||||
LL | [(); &(static || {}) as *const _ as usize];
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
|
||||
|
||||
Some errors have detailed explanations: E0282, E0697.
|
||||
For more information about an error, try `rustc --explain E0282`.
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0080, E0282, E0697.
|
||||
For more information about an error, try `rustc --explain E0080`.
|
||||
|
@ -19,4 +19,5 @@ impl TraitB for B { //~ ERROR not all trait items implemented, missing: `MyA`
|
||||
|
||||
fn main() {
|
||||
let _ = [0; B::VALUE];
|
||||
//~^ ERROR constant expression depends on a generic parameter
|
||||
}
|
||||
|
@ -13,7 +13,15 @@ LL | type MyA: TraitA;
|
||||
LL | impl TraitB for B {
|
||||
| ^^^^^^^^^^^^^^^^^ missing `MyA` in implementation
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
error: constant expression depends on a generic parameter
|
||||
--> $DIR/issue-69602-type-err-during-codegen-ice.rs:21:17
|
||||
|
|
||||
LL | let _ = [0; B::VALUE];
|
||||
| ^^^^^^^^
|
||||
|
|
||||
= note: this may fail depending on what value the parameter takes
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0046, E0437.
|
||||
For more information about an error, try `rustc --explain E0046`.
|
||||
|
Loading…
Reference in New Issue
Block a user