diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index bde17c39e2bc4..8e8c99e2a76ca 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -986,28 +986,16 @@ pub(crate) fn check_type_defn<'tcx>( let variants = adt_def.variants(); let packed = adt_def.repr().packed(); + let own_params_require_monomorphization = + LazyCell::new(|| tcx.generics_of(item).own_requires_monomorphization()); + for variant in variants.iter() { // All field types must be well-formed. for field in &variant.fields { if let Some(def_id) = field.value - && let Some(_ty) = tcx.type_of(def_id).no_bound_vars() + && !*own_params_require_monomorphization { - // FIXME(generic_const_exprs, default_field_values): this is a hack and needs to - // be refactored to check the instantiate-ability of the code better. - if let Some(def_id) = def_id.as_local() - && let DefKind::AnonConst = tcx.def_kind(def_id) - && let hir::Node::AnonConst(anon) = tcx.hir_node_by_def_id(def_id) - && let expr = &tcx.hir_body(anon.body).value - && let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind - && let Res::Def(DefKind::ConstParam, _def_id) = path.res - { - // Do not evaluate bare `const` params, as those would ICE and are only - // usable if `#![feature(generic_const_exprs)]` is enabled. - } else { - // Evaluate the constant proactively, to emit an error if the constant has - // an unconditional error. We only do so if the const has no type params. - let _ = tcx.const_eval_poly(def_id); - } + let _ = tcx.const_eval_poly(def_id); } let field_id = field.did.expect_local(); let span = tcx.ty_span(field_id); diff --git a/tests/ui/structs/default-field-values/invalid-const.rs b/tests/ui/structs/default-field-values/invalid-const.rs index ddb73f688659c..11e434525c286 100644 --- a/tests/ui/structs/default-field-values/invalid-const.rs +++ b/tests/ui/structs/default-field-values/invalid-const.rs @@ -1,15 +1,13 @@ -#![feature(default_field_values, generic_const_exprs)] -#![allow(incomplete_features)] +#![feature(default_field_values)] pub struct Bat { - pub bax: u8 = panic!("asdf"), - //~^ ERROR evaluation panicked: asdf + pub bax: u8 = panic!("asdf"), //~ ERROR evaluation panicked: asdf + pub bat: u8 = 130 + 130, //~ ERROR attempt to compute `130_u8 + 130_u8`, which would overflow } pub struct Baz { pub bax: u8 = 130 + C, // ok - pub bat: u8 = 130 + 130, - //~^ ERROR attempt to compute `130_u8 + 130_u8`, which would overflow + pub bat: u8 = 130 + 130, // ok pub bay: u8 = 1, // ok } diff --git a/tests/ui/structs/default-field-values/invalid-const.stderr b/tests/ui/structs/default-field-values/invalid-const.stderr index 545783e6c74ce..1a975a153b8be 100644 --- a/tests/ui/structs/default-field-values/invalid-const.stderr +++ b/tests/ui/structs/default-field-values/invalid-const.stderr @@ -1,14 +1,14 @@ error[E0080]: evaluation panicked: asdf - --> $DIR/invalid-const.rs:5:19 + --> $DIR/invalid-const.rs:4:19 | LL | pub bax: u8 = panic!("asdf"), | ^^^^^^^^^^^^^^ evaluation of `Bat::bax::{constant#0}` failed here error[E0080]: attempt to compute `130_u8 + 130_u8`, which would overflow - --> $DIR/invalid-const.rs:11:19 + --> $DIR/invalid-const.rs:5:19 | LL | pub bat: u8 = 130 + 130, - | ^^^^^^^^^ evaluation of `Baz::::bat::{constant#0}` failed here + | ^^^^^^^^^ evaluation of `Bat::bat::{constant#0}` failed here error: aborting due to 2 previous errors