Skip to content

Commit c85f635

Browse files
authored
Rollup merge of rust-lang#66883 - eddyb:we-cant-have-nice-things, r=oli-obk
rustc_typeck: gate AnonConst's generics on feature(const_generics). This PR employs the fix for rust-lang#43408 when `#![feature(const_generics)]` is enabled, making the feature-gate the opt-in for all the possible breakage this may incur. For example, if this PR lands, this will cause a cycle error (due to rust-lang#60471): ```rust #![feature(const_generics)] fn foo<T: Into<[u8; 4]>>() {} ``` And so will anything with type-level const expressions, in its bounds. Surprisingly, `impl`s don't seem to be affected (if they were, even libcore wouldn't compile). One thing I'm worried about is not knowing how much unstable code out there, using const-generics, will be broken. But types like `Foo<{N+1}>` never really worked, and do after this PR, just not in bounds - so ironically, it's type-level const expressions that don't depend on generics, which will break (in bounds). Also, if we do this, we'll have effectively blocked stabilization of const generics on rust-lang#60471. r? @oli-obk cc @varkor @yodaldevoid @nikomatsakis
2 parents 472bee2 + 584ede5 commit c85f635

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

src/librustc/ty/sty.rs

+35-14
Original file line numberDiff line numberDiff line change
@@ -2330,22 +2330,43 @@ impl<'tcx> Const<'tcx> {
23302330
tcx: TyCtxt<'tcx>,
23312331
param_env: ParamEnv<'tcx>,
23322332
) -> &Const<'tcx> {
2333-
// FIXME(const_generics): this doesn't work right now,
2334-
// because it tries to relate an `Infer` to a `Param`.
2333+
let try_const_eval = |did, param_env: ParamEnv<'tcx>, substs| {
2334+
let param_env_and_substs = param_env.with_reveal_all().and(substs);
2335+
2336+
// Avoid querying `tcx.const_eval(...)` with any e.g. inference vars.
2337+
if param_env_and_substs.has_local_value() {
2338+
return None;
2339+
}
2340+
2341+
let (param_env, substs) = param_env_and_substs.into_parts();
2342+
2343+
// try to resolve e.g. associated constants to their definition on an impl
2344+
let instance = ty::Instance::resolve(tcx, param_env, did, substs)?;
2345+
let gid = GlobalId {
2346+
instance,
2347+
promoted: None,
2348+
};
2349+
tcx.const_eval(param_env.and(gid)).ok()
2350+
};
2351+
23352352
match self.val {
23362353
ConstKind::Unevaluated(did, substs) => {
2337-
// if `substs` has no unresolved components, use and empty param_env
2338-
let (param_env, substs) = param_env.with_reveal_all().and(substs).into_parts();
2339-
// try to resolve e.g. associated constants to their definition on an impl
2340-
let instance = match ty::Instance::resolve(tcx, param_env, did, substs) {
2341-
Some(instance) => instance,
2342-
None => return self,
2343-
};
2344-
let gid = GlobalId {
2345-
instance,
2346-
promoted: None,
2347-
};
2348-
tcx.const_eval(param_env.and(gid)).unwrap_or(self)
2354+
// HACK(eddyb) when substs contain e.g. inference variables,
2355+
// attempt using identity substs instead, that will succeed
2356+
// when the expression doesn't depend on any parameters.
2357+
// FIXME(eddyb) make `const_eval` a canonical query instead,
2358+
// that would properly handle inference variables in `substs`.
2359+
if substs.has_local_value() {
2360+
let identity_substs = InternalSubsts::identity_for_item(tcx, did);
2361+
// The `ParamEnv` needs to match the `identity_substs`.
2362+
let identity_param_env = tcx.param_env(did);
2363+
match try_const_eval(did, identity_param_env, identity_substs) {
2364+
Some(ct) => ct.subst(tcx, substs),
2365+
None => self,
2366+
}
2367+
} else {
2368+
try_const_eval(did, param_env, substs).unwrap_or(self)
2369+
}
23492370
},
23502371
_ => self,
23512372
}

src/librustc_typeck/collect.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -909,14 +909,12 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics {
909909
let parent_id = tcx.hir().get_parent_item(hir_id);
910910
Some(tcx.hir().local_def_id(parent_id))
911911
}
912-
// FIXME(#43408) enable this in all cases when we get lazy normalization.
913-
Node::AnonConst(&anon_const) => {
914-
// HACK(eddyb) this provides the correct generics when the workaround
915-
// for a const parameter `AnonConst` is being used elsewhere, as then
916-
// there won't be the kind of cyclic dependency blocking #43408.
917-
let expr = &tcx.hir().body(anon_const.body).value;
918-
let icx = ItemCtxt::new(tcx, def_id);
919-
if AstConv::const_param_def_id(&icx, expr).is_some() {
912+
// FIXME(#43408) enable this always when we get lazy normalization.
913+
Node::AnonConst(_) => {
914+
// HACK(eddyb) this provides the correct generics when
915+
// `feature(const_generics)` is enabled, so that const expressions
916+
// used with const generics, e.g. `Foo<{N+1}>`, can work at all.
917+
if tcx.features().const_generics {
920918
let parent_id = tcx.hir().get_parent_item(hir_id);
921919
Some(tcx.hir().local_def_id(parent_id))
922920
} else {

0 commit comments

Comments
 (0)