Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const_generics: Fix incorrect ty::ParamEnv::empty() usage #82067

Merged
merged 4 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_infer/src/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_infer/src/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
ct: &'tcx ty::Const<'tcx>,
vid_is_expected: bool,
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
debug!("unify_const_variable: param_env={:?}", param_env);
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
let (for_universe, span) = {
let mut inner = self.inner.borrow_mut();
let variable_table = &mut inner.const_unification_table();
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_infer/src/infer/nll_relate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/mir/interpret/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl<'tcx> TyCtxt<'tcx> {
promoted: Option<mir::Promoted>,
span: Option<Span>,
) -> EvalToConstValueResult<'tcx> {
debug!("const_eval_resolve: param_env={:?}", param_env);
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
match ty::Instance::resolve_opt_const_arg(self, param_env, def, substs) {
Ok(Some(instance)) => {
let cid = GlobalId { instance, promoted };
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ impl<'tcx> Instance<'tcx> {
def: ty::WithOptConstParam<DefId>,
substs: SubstsRef<'tcx>,
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
debug!("resolve_opt_const_arg: param_env={:?},substs={:?}", param_env, substs);
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
// All regions in the result of this query are erased, so it's
// fine to erase all of the input regions.

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir/src/borrow_check/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
) -> Fallible<()> {
relate_tys::relate_types(
self.infcx,
self.param_env,
a,
v,
b,
Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_mir/src/borrow_check/type_check/relate_tys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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)?;
Expand All @@ -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,

Expand All @@ -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()
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_ty_utils/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn resolve_instance<'tcx>(
tcx: TyCtxt<'tcx>,
key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)>,
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
debug!("resolve_instance: key = {:?}", key);
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
let (param_env, (did, substs)) = key.into_parts();
if let Some(did) = did.as_local() {
if let Some(param_did) = tcx.opt_const_param_of(did) {
Expand Down Expand Up @@ -44,7 +45,7 @@ fn inner_resolve_instance<'tcx>(
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
let (param_env, (def, substs)) = key.into_parts();

debug!("resolve(def={:?}, substs={:?})", def.did, substs);
debug!("inner_resolve_instance: key={:?}", key);
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand Down Expand Up @@ -93,7 +94,10 @@ fn inner_resolve_instance<'tcx>(
};
Ok(Some(Instance { def, substs }))
};
debug!("resolve(def.did={:?}, substs={:?}) = {:?}", def.did, substs, result);
debug!(
"inner_resolve_instance: resolve(def.did={:?}, substs={:?}) = {:?}",
def.did, substs, result
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after that change, you can just print the result here (though I think there's also a way to tell #[instrument] to do that

result
}

Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/const-generics/issue-80561-incorrect-param-env.rs
Original file line number Diff line number Diff line change
@@ -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<const N: usize>;

impl<const N: usize> Foo<N> {
pub fn foo() {}
}

pub trait Bar {
const N: usize;
fn bar()
where
[(); Self::N]: ,
{
Foo::<{ Self::N }>::foo();
}
}

fn main() {}