Skip to content

Commit ea1cdda

Browse files
committed
Auto merge of rust-lang#136429 - fmease:gci-fix-def-site-checks, r=<try>
GCI: At their def site, actually wfcheck the where-clause & always eval free lifetime-generic constants 1st commit: Partially addresses rust-lang#136204 by turning const eval errors from post to pre-mono for free lifetime-generic constants. Re. 2nd commit: Oof, that's embarrassing! How could I miss that in the initial impl? This doesn't fully address rust-lang#136204 because I still haven't figured out how & where to properly & best suppress const eval of free constants whose predicates don't hold at the def site. The motivating example is `#![feature(trivial_bounds)] const _UNUSED: () = () where String: Copy;` which can also be found over at the tracking issue rust-lang#113521. r? compiler-errors or reassign
2 parents 8239a37 + 5a8ee9b commit ea1cdda

File tree

7 files changed

+86
-11
lines changed

7 files changed

+86
-11
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
307307
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
308308
}
309309
hir::ItemKind::Const(ty, ..) => {
310-
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
310+
let res = check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid);
311+
res.and(enter_wf_checking_ctxt(tcx, item.span, def_id, |wfcx| {
312+
check_where_clauses(wfcx, item.span, def_id);
313+
Ok(())
314+
}))
311315
}
312316
hir::ItemKind::Struct(_, hir_generics) => {
313317
let res = check_type_defn(tcx, item, false);

compiler/rustc_hir_analysis/src/lib.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ use rustc_abi::ExternAbi;
9797
use rustc_hir as hir;
9898
use rustc_hir::def::DefKind;
9999
use rustc_middle::middle;
100-
use rustc_middle::mir::interpret::GlobalId;
101100
use rustc_middle::query::Providers;
102-
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
101+
use rustc_middle::ty::{Const, Ty, TyCtxt};
103102
use rustc_span::Span;
104103
use rustc_trait_selection::traits;
105104

@@ -168,11 +167,8 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
168167
let def_kind = tcx.def_kind(item_def_id);
169168
match def_kind {
170169
DefKind::Static { .. } => tcx.ensure().eval_static_initializer(item_def_id),
171-
DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
172-
let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty());
173-
let cid = GlobalId { instance, promoted: None };
174-
let typing_env = ty::TypingEnv::fully_monomorphized();
175-
tcx.ensure().eval_to_const_value_raw(typing_env.as_query_input(cid));
170+
DefKind::Const if !tcx.generics_of(item_def_id).requires_monomorphization(tcx) => {
171+
tcx.ensure().const_eval_poly(item_def_id.into())
176172
}
177173
_ => (),
178174
}

tests/ui/generic-const-items/def-site-eval.fail.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0080]: evaluation of `_::<'_>` failed
2-
--> $DIR/def-site-eval.rs:14:20
2+
--> $DIR/def-site-eval.rs:13:20
33
|
44
LL | const _<'_a>: () = panic!();
5-
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/def-site-eval.rs:14:20
5+
| ^^^^^^^^ the evaluated program panicked at 'explicit panic', $DIR/def-site-eval.rs:13:20
66
|
77
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
88

tests/ui/generic-const-items/def-site-eval.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![allow(incomplete_features)]
55

66
//@ revisions: fail pass
7-
//@[fail] build-fail (we require monomorphization)
87
//@[pass] build-pass (we require monomorphization)
98

109
const _<_T>: () = panic!();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0277]: the size for values of type `str` cannot be known at compilation time
2+
--> $DIR/def-site-predicates-wf.rs:11:5
3+
|
4+
LL | <Vec<str> as Discard>::Output:;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `str`
8+
note: required by an implicit `Sized` bound in `Vec`
9+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
10+
11+
error[E0080]: evaluation of constant value failed
12+
--> $DIR/def-site-predicates-wf.rs:9:1
13+
|
14+
LL | / const _: () = ()
15+
LL | | where
16+
LL | | <Vec<str> as Discard>::Output:;
17+
| |___________________________________^ entering unreachable code
18+
19+
error: aborting due to 2 previous errors
20+
21+
Some errors have detailed explanations: E0080, E0277.
22+
For more information about an error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error[E0277]: the size for values of type `str` cannot be known at compilation time
2+
--> $DIR/def-site-predicates-wf.rs:11:5
3+
|
4+
LL | <Vec<str> as Discard>::Output:;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `str`
8+
note: required by an implicit `Sized` bound in `Vec`
9+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
10+
11+
error[E0277]: the size for values of type `str` cannot be known at compilation time
12+
--> $DIR/def-site-predicates-wf.rs:17:36
13+
|
14+
LL | <Vec<str> as Discard>::Output: Sized;
15+
| ^^^^^ doesn't have a size known at compile-time
16+
|
17+
= help: the trait `Sized` is not implemented for `str`
18+
note: required by an implicit `Sized` bound in `Vec`
19+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
20+
21+
error[E0080]: evaluation of constant value failed
22+
--> $DIR/def-site-predicates-wf.rs:9:1
23+
|
24+
LL | / const _: () = ()
25+
LL | | where
26+
LL | | <Vec<str> as Discard>::Output:;
27+
| |___________________________________^ entering unreachable code
28+
29+
error: aborting due to 3 previous errors
30+
31+
Some errors have detailed explanations: E0080, E0277.
32+
For more information about an error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Ensure that we check the predicates at the definition site for well-formedness.
2+
#![feature(generic_const_items)]
3+
#![allow(incomplete_features)]
4+
5+
//@ revisions: current next
6+
//@[next] compile-flags: -Znext-solver
7+
//@ ignore-compare-mode-next-solver (explicit revisions)
8+
9+
const _: () = ()
10+
where
11+
<Vec<str> as Discard>::Output:; //~ ERROR the size for values of type `str` cannot be known at compilation time
12+
//~^^^ ERROR evaluation of constant value failed
13+
14+
// FIXME(#100041): This should error in the current solver, too.
15+
const _: () = ()
16+
where
17+
<Vec<str> as Discard>::Output: Sized; //[next]~ ERROR the size for values of type `str` cannot be known at compilation time
18+
19+
trait Discard { type Output; }
20+
impl<T> Discard for T { type Output = (); }
21+
22+
fn main() {}

0 commit comments

Comments
 (0)