diff --git a/compiler/rustc_infer/src/infer/canonical/query_response.rs b/compiler/rustc_infer/src/infer/canonical/query_response.rs index 1546c1e559f5..074c9252481a 100644 --- a/compiler/rustc_infer/src/infer/canonical/query_response.rs +++ b/compiler/rustc_infer/src/infer/canonical/query_response.rs @@ -639,6 +639,10 @@ struct QueryTypeRelatingDelegate<'a, 'tcx> { } impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> { + fn param_env(&self) -> ty::ParamEnv<'tcx> { + self.param_env + } + fn create_next_universe(&mut self) -> ty::UniverseIndex { self.infcx.create_next_universe() } diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs index e034ac5e8fd7..5e11932eafc4 100644 --- a/compiler/rustc_infer/src/infer/combine.rs +++ b/compiler/rustc_infer/src/infer/combine.rs @@ -221,6 +221,7 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { /// As `3 + 4` contains `N` in its substs, this must not succeed. /// /// See `src/test/ui/const-generics/occurs-check/` for more examples where this is relevant. + #[instrument(level = "debug", skip(self))] fn unify_const_variable( &self, param_env: ty::ParamEnv<'tcx>, diff --git a/compiler/rustc_infer/src/infer/nll_relate/mod.rs b/compiler/rustc_infer/src/infer/nll_relate/mod.rs index 97ef685cf6f8..e720a6f13086 100644 --- a/compiler/rustc_infer/src/infer/nll_relate/mod.rs +++ b/compiler/rustc_infer/src/infer/nll_relate/mod.rs @@ -72,6 +72,8 @@ where } pub trait TypeRelatingDelegate<'tcx> { + fn param_env(&self) -> ty::ParamEnv<'tcx>; + /// Push a constraint `sup: sub` -- this constraint must be /// satisfied for the two types to be related. `sub` and `sup` may /// be regions from the type or new variables created through the @@ -473,9 +475,8 @@ where self.infcx.tcx } - // FIXME(oli-obk): not sure how to get the correct ParamEnv fn param_env(&self) -> ty::ParamEnv<'tcx> { - ty::ParamEnv::empty() + self.delegate.param_env() } fn tag(&self) -> &'static str { @@ -819,9 +820,8 @@ where self.infcx.tcx } - // FIXME(oli-obk): not sure how to get the correct ParamEnv fn param_env(&self) -> ty::ParamEnv<'tcx> { - ty::ParamEnv::empty() + self.delegate.param_env() } fn tag(&self) -> &'static str { diff --git a/compiler/rustc_middle/src/mir/interpret/queries.rs b/compiler/rustc_middle/src/mir/interpret/queries.rs index 0517ec5bb1a8..3e7b93b32a6a 100644 --- a/compiler/rustc_middle/src/mir/interpret/queries.rs +++ b/compiler/rustc_middle/src/mir/interpret/queries.rs @@ -31,6 +31,7 @@ impl<'tcx> TyCtxt<'tcx> { /// constant `bar::()` requires a substitution for `T`, if the substitution for `T` is still /// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is /// returned. + #[instrument(level = "debug", skip(self))] pub fn const_eval_resolve( self, param_env: ty::ParamEnv<'tcx>, diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index 6ca5dcc532d2..de012a695741 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -347,6 +347,7 @@ impl<'tcx> Instance<'tcx> { } // This should be kept up to date with `resolve`. + #[instrument(level = "debug", skip(tcx))] pub fn resolve_opt_const_arg( tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, diff --git a/compiler/rustc_mir/src/borrow_check/type_check/mod.rs b/compiler/rustc_mir/src/borrow_check/type_check/mod.rs index 2861a9eac025..f64848e694cc 100644 --- a/compiler/rustc_mir/src/borrow_check/type_check/mod.rs +++ b/compiler/rustc_mir/src/borrow_check/type_check/mod.rs @@ -1101,6 +1101,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { ) -> Fallible<()> { relate_tys::relate_types( self.infcx, + self.param_env, a, v, b, diff --git a/compiler/rustc_mir/src/borrow_check/type_check/relate_tys.rs b/compiler/rustc_mir/src/borrow_check/type_check/relate_tys.rs index 6665eb5ad5ff..249945f04b7b 100644 --- a/compiler/rustc_mir/src/borrow_check/type_check/relate_tys.rs +++ b/compiler/rustc_mir/src/borrow_check/type_check/relate_tys.rs @@ -18,6 +18,7 @@ use crate::borrow_check::type_check::{BorrowCheckContext, Locations}; /// variables, but not the type `b`. pub(super) fn relate_types<'tcx>( infcx: &InferCtxt<'_, 'tcx>, + param_env: ty::ParamEnv<'tcx>, a: Ty<'tcx>, v: ty::Variance, b: Ty<'tcx>, @@ -28,7 +29,7 @@ pub(super) fn relate_types<'tcx>( debug!("relate_types(a={:?}, v={:?}, b={:?}, locations={:?})", a, v, b, locations); TypeRelating::new( infcx, - NllTypeRelatingDelegate::new(infcx, borrowck_context, locations, category), + NllTypeRelatingDelegate::new(infcx, borrowck_context, param_env, locations, category), v, ) .relate(a, b)?; @@ -39,6 +40,8 @@ struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> { infcx: &'me InferCtxt<'me, 'tcx>, borrowck_context: Option<&'me mut BorrowCheckContext<'bccx, 'tcx>>, + param_env: ty::ParamEnv<'tcx>, + /// Where (and why) is this relation taking place? locations: Locations, @@ -50,14 +53,19 @@ impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> { fn new( infcx: &'me InferCtxt<'me, 'tcx>, borrowck_context: Option<&'me mut BorrowCheckContext<'bccx, 'tcx>>, + param_env: ty::ParamEnv<'tcx>, locations: Locations, category: ConstraintCategory, ) -> Self { - Self { infcx, borrowck_context, locations, category } + Self { infcx, borrowck_context, param_env, locations, category } } } impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> { + fn param_env(&self) -> ty::ParamEnv<'tcx> { + self.param_env + } + fn create_next_universe(&mut self) -> ty::UniverseIndex { self.infcx.create_next_universe() } diff --git a/compiler/rustc_ty_utils/src/instance.rs b/compiler/rustc_ty_utils/src/instance.rs index cf2c6efb4711..1cc580a19838 100644 --- a/compiler/rustc_ty_utils/src/instance.rs +++ b/compiler/rustc_ty_utils/src/instance.rs @@ -10,6 +10,7 @@ use traits::{translate_substs, Reveal}; use tracing::debug; +#[instrument(level = "debug", skip(tcx))] fn resolve_instance<'tcx>( tcx: TyCtxt<'tcx>, key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)>, @@ -38,13 +39,13 @@ fn resolve_instance_of_const_arg<'tcx>( ) } +#[instrument(level = "debug", skip(tcx))] fn inner_resolve_instance<'tcx>( tcx: TyCtxt<'tcx>, key: ty::ParamEnvAnd<'tcx, (ty::WithOptConstParam, SubstsRef<'tcx>)>, ) -> Result>, ErrorReported> { let (param_env, (def, substs)) = key.into_parts(); - debug!("resolve(def={:?}, substs={:?})", def.did, substs); let result = if let Some(trait_def_id) = tcx.trait_of_item(def.did) { debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env); let item = tcx.associated_item(def.did); @@ -93,7 +94,7 @@ fn inner_resolve_instance<'tcx>( }; Ok(Some(Instance { def, substs })) }; - debug!("resolve(def.did={:?}, substs={:?}) = {:?}", def.did, substs, result); + debug!("inner_resolve_instance: result={:?}", result); result } diff --git a/src/test/ui/const-generics/issue-80561-incorrect-param-env.rs b/src/test/ui/const-generics/issue-80561-incorrect-param-env.rs new file mode 100644 index 000000000000..a34d74b29e9f --- /dev/null +++ b/src/test/ui/const-generics/issue-80561-incorrect-param-env.rs @@ -0,0 +1,24 @@ +// check-pass +#![feature(const_generics, const_evaluatable_checked)] +#![allow(incomplete_features)] + +// This tests that the correct `param_env` is used so that +// attempting to normalize `Self::N` does not cause an ICE. + +pub struct Foo; + +impl Foo { + pub fn foo() {} +} + +pub trait Bar { + const N: usize; + fn bar() + where + [(); Self::N]: , + { + Foo::<{ Self::N }>::foo(); + } +} + +fn main() {}