Skip to content

Commit 26e9a71

Browse files
authored
Rollup merge of #90266 - b-naber:uneval_substs, r=lcnr
Prevent duplicate caller bounds candidates by exposing default substs in Unevaluated Fixes #89334 The changes introduced in #87280 allowed for "duplicate" caller bounds candidates to be assembled that only differed in their default substs having been "exposed" or not and resulted in an ambiguity error during trait selection. To fix this we expose the defaults substs during the creation of the ParamEnv. r? `@lcnr`
2 parents d35ecb9 + 0199a81 commit 26e9a71

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compiler/rustc_ty_utils/src/ty.rs

+12
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ fn trait_of_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
247247
}
248248

249249
/// See `ParamEnv` struct definition for details.
250+
#[instrument(level = "debug", skip(tcx))]
250251
fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
251252
// The param_env of an impl Trait type is its defining function's param_env
252253
if let Some(parent) = ty::is_impl_trait_defn(tcx, def_id) {
@@ -274,9 +275,20 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
274275
predicates.extend(environment);
275276
}
276277

278+
// It's important that we include the default substs in unevaluated
279+
// constants, since `Unevaluated` instances in predicates whose substs are None
280+
// can lead to "duplicate" caller bounds candidates during trait selection,
281+
// duplicate in the sense that both have their default substs, but the
282+
// candidate that resulted from a superpredicate still uses `None` in its
283+
// `substs_` field of `Unevaluated` to indicate that it has its default substs,
284+
// whereas the other candidate has `substs_: Some(default_substs)`, see
285+
// issue #89334
286+
predicates = tcx.expose_default_const_substs(predicates);
287+
277288
let unnormalized_env =
278289
ty::ParamEnv::new(tcx.intern_predicates(&predicates), traits::Reveal::UserFacing);
279290

291+
debug!("unnormalized_env caller bounds: {:?}", unnormalized_env.caller_bounds());
280292
let body_id = def_id
281293
.as_local()
282294
.map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// build-pass
2+
3+
#![feature(generic_const_exprs)]
4+
#![allow(unused_braces, incomplete_features)]
5+
6+
pub trait Foo<const N: usize> {}
7+
pub trait Bar: Foo<{ 1 }> { }
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// build-pass
2+
3+
#![feature(generic_const_exprs)]
4+
#![allow(unused_braces, incomplete_features)]
5+
6+
pub trait AnotherTrait{
7+
const ARRAY_SIZE: usize;
8+
}
9+
pub trait Shard<T: AnotherTrait>:
10+
AsMut<[[u8; T::ARRAY_SIZE]]>
11+
where
12+
[(); T::ARRAY_SIZE]: Sized
13+
{
14+
}
15+
16+
fn main() {}

0 commit comments

Comments
 (0)