Skip to content

Commit 665bf9e

Browse files
Rollup merge of #82067 - BoxyUwU:hahaicantthinkofabadpun, r=oli-obk
const_generics: Fix incorrect ty::ParamEnv::empty() usage Fixes #80561 Not sure if I should keep the `debug!(..)`s or not but its the second time I've needed them so they sure seem useful lol cc ``@lcnr`` r? ``@oli-obk``
2 parents 7842b5d + 7bd7126 commit 665bf9e

File tree

9 files changed

+49
-8
lines changed

9 files changed

+49
-8
lines changed

compiler/rustc_infer/src/infer/canonical/query_response.rs

+4
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,10 @@ struct QueryTypeRelatingDelegate<'a, 'tcx> {
639639
}
640640

641641
impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> {
642+
fn param_env(&self) -> ty::ParamEnv<'tcx> {
643+
self.param_env
644+
}
645+
642646
fn create_next_universe(&mut self) -> ty::UniverseIndex {
643647
self.infcx.create_next_universe()
644648
}

compiler/rustc_infer/src/infer/combine.rs

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
221221
/// As `3 + 4` contains `N` in its substs, this must not succeed.
222222
///
223223
/// See `src/test/ui/const-generics/occurs-check/` for more examples where this is relevant.
224+
#[instrument(level = "debug", skip(self))]
224225
fn unify_const_variable(
225226
&self,
226227
param_env: ty::ParamEnv<'tcx>,

compiler/rustc_infer/src/infer/nll_relate/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ where
7272
}
7373

7474
pub trait TypeRelatingDelegate<'tcx> {
75+
fn param_env(&self) -> ty::ParamEnv<'tcx>;
76+
7577
/// Push a constraint `sup: sub` -- this constraint must be
7678
/// satisfied for the two types to be related. `sub` and `sup` may
7779
/// be regions from the type or new variables created through the
@@ -473,9 +475,8 @@ where
473475
self.infcx.tcx
474476
}
475477

476-
// FIXME(oli-obk): not sure how to get the correct ParamEnv
477478
fn param_env(&self) -> ty::ParamEnv<'tcx> {
478-
ty::ParamEnv::empty()
479+
self.delegate.param_env()
479480
}
480481

481482
fn tag(&self) -> &'static str {
@@ -819,9 +820,8 @@ where
819820
self.infcx.tcx
820821
}
821822

822-
// FIXME(oli-obk): not sure how to get the correct ParamEnv
823823
fn param_env(&self) -> ty::ParamEnv<'tcx> {
824-
ty::ParamEnv::empty()
824+
self.delegate.param_env()
825825
}
826826

827827
fn tag(&self) -> &'static str {

compiler/rustc_middle/src/mir/interpret/queries.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl<'tcx> TyCtxt<'tcx> {
3131
/// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still
3232
/// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
3333
/// returned.
34+
#[instrument(level = "debug", skip(self))]
3435
pub fn const_eval_resolve(
3536
self,
3637
param_env: ty::ParamEnv<'tcx>,

compiler/rustc_middle/src/ty/instance.rs

+1
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ impl<'tcx> Instance<'tcx> {
347347
}
348348

349349
// This should be kept up to date with `resolve`.
350+
#[instrument(level = "debug", skip(tcx))]
350351
pub fn resolve_opt_const_arg(
351352
tcx: TyCtxt<'tcx>,
352353
param_env: ty::ParamEnv<'tcx>,

compiler/rustc_mir/src/borrow_check/type_check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11011101
) -> Fallible<()> {
11021102
relate_tys::relate_types(
11031103
self.infcx,
1104+
self.param_env,
11041105
a,
11051106
v,
11061107
b,

compiler/rustc_mir/src/borrow_check/type_check/relate_tys.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::borrow_check::type_check::{BorrowCheckContext, Locations};
1818
/// variables, but not the type `b`.
1919
pub(super) fn relate_types<'tcx>(
2020
infcx: &InferCtxt<'_, 'tcx>,
21+
param_env: ty::ParamEnv<'tcx>,
2122
a: Ty<'tcx>,
2223
v: ty::Variance,
2324
b: Ty<'tcx>,
@@ -28,7 +29,7 @@ pub(super) fn relate_types<'tcx>(
2829
debug!("relate_types(a={:?}, v={:?}, b={:?}, locations={:?})", a, v, b, locations);
2930
TypeRelating::new(
3031
infcx,
31-
NllTypeRelatingDelegate::new(infcx, borrowck_context, locations, category),
32+
NllTypeRelatingDelegate::new(infcx, borrowck_context, param_env, locations, category),
3233
v,
3334
)
3435
.relate(a, b)?;
@@ -39,6 +40,8 @@ struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
3940
infcx: &'me InferCtxt<'me, 'tcx>,
4041
borrowck_context: Option<&'me mut BorrowCheckContext<'bccx, 'tcx>>,
4142

43+
param_env: ty::ParamEnv<'tcx>,
44+
4245
/// Where (and why) is this relation taking place?
4346
locations: Locations,
4447

@@ -50,14 +53,19 @@ impl NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
5053
fn new(
5154
infcx: &'me InferCtxt<'me, 'tcx>,
5255
borrowck_context: Option<&'me mut BorrowCheckContext<'bccx, 'tcx>>,
56+
param_env: ty::ParamEnv<'tcx>,
5357
locations: Locations,
5458
category: ConstraintCategory,
5559
) -> Self {
56-
Self { infcx, borrowck_context, locations, category }
60+
Self { infcx, borrowck_context, param_env, locations, category }
5761
}
5862
}
5963

6064
impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
65+
fn param_env(&self) -> ty::ParamEnv<'tcx> {
66+
self.param_env
67+
}
68+
6169
fn create_next_universe(&mut self) -> ty::UniverseIndex {
6270
self.infcx.create_next_universe()
6371
}

compiler/rustc_ty_utils/src/instance.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use traits::{translate_substs, Reveal};
1010

1111
use tracing::debug;
1212

13+
#[instrument(level = "debug", skip(tcx))]
1314
fn resolve_instance<'tcx>(
1415
tcx: TyCtxt<'tcx>,
1516
key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)>,
@@ -38,13 +39,13 @@ fn resolve_instance_of_const_arg<'tcx>(
3839
)
3940
}
4041

42+
#[instrument(level = "debug", skip(tcx))]
4143
fn inner_resolve_instance<'tcx>(
4244
tcx: TyCtxt<'tcx>,
4345
key: ty::ParamEnvAnd<'tcx, (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>)>,
4446
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
4547
let (param_env, (def, substs)) = key.into_parts();
4648

47-
debug!("resolve(def={:?}, substs={:?})", def.did, substs);
4849
let result = if let Some(trait_def_id) = tcx.trait_of_item(def.did) {
4950
debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
5051
let item = tcx.associated_item(def.did);
@@ -93,7 +94,7 @@ fn inner_resolve_instance<'tcx>(
9394
};
9495
Ok(Some(Instance { def, substs }))
9596
};
96-
debug!("resolve(def.did={:?}, substs={:?}) = {:?}", def.did, substs, result);
97+
debug!("inner_resolve_instance: result={:?}", result);
9798
result
9899
}
99100

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// check-pass
2+
#![feature(const_generics, const_evaluatable_checked)]
3+
#![allow(incomplete_features)]
4+
5+
// This tests that the correct `param_env` is used so that
6+
// attempting to normalize `Self::N` does not cause an ICE.
7+
8+
pub struct Foo<const N: usize>;
9+
10+
impl<const N: usize> Foo<N> {
11+
pub fn foo() {}
12+
}
13+
14+
pub trait Bar {
15+
const N: usize;
16+
fn bar()
17+
where
18+
[(); Self::N]: ,
19+
{
20+
Foo::<{ Self::N }>::foo();
21+
}
22+
}
23+
24+
fn main() {}

0 commit comments

Comments
 (0)