From c6b69017e21610b1ef491c028697728fbd3f581a Mon Sep 17 00:00:00 2001
From: b-naber <bn263@gmx.de>
Date: Mon, 25 Oct 2021 15:34:59 +0200
Subject: [PATCH 1/2] expose default substs in param_env

---
 compiler/rustc_ty_utils/src/ty.rs | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs
index bc77c94809eb5..af3706f886e9c 100644
--- a/compiler/rustc_ty_utils/src/ty.rs
+++ b/compiler/rustc_ty_utils/src/ty.rs
@@ -247,6 +247,7 @@ fn trait_of_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
 }
 
 /// See `ParamEnv` struct definition for details.
+#[instrument(level = "debug", skip(tcx))]
 fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
     // The param_env of an impl Trait type is its defining function's param_env
     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<'_> {
         predicates.extend(environment);
     }
 
+    // It's important that we include the default substs in unevaluated
+    // constants, since `Unevaluated` instances in predicates whose substs are None
+    // can lead to "duplicate" caller bounds candidates during trait selection,
+    // duplicate in the sense that both have their default substs, but the
+    // candidate that resulted from a superpredicate still uses `None` in its
+    // `substs_` field of `Unevaluated` to indicate that it has its default substs,
+    // whereas the other candidate has `substs_: Some(default_substs)`, see
+    // issue #89334
+    predicates = tcx.expose_default_const_substs(predicates);
+
     let unnormalized_env =
         ty::ParamEnv::new(tcx.intern_predicates(&predicates), traits::Reveal::UserFacing);
 
+    debug!("unnormalized_env caller bounds: {:?}", unnormalized_env.caller_bounds());
     let body_id = def_id
         .as_local()
         .map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))

From 0199a81304fe7cf07f0d2c718a4243f47c1620ea Mon Sep 17 00:00:00 2001
From: b-naber <bn263@gmx.de>
Date: Mon, 25 Oct 2021 16:04:23 +0200
Subject: [PATCH 2/2] add tests

---
 .../expose-default-substs-param-env.rs           |  9 +++++++++
 src/test/ui/const-generics/issues/issue-89334.rs | 16 ++++++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 src/test/ui/const-generics/expose-default-substs-param-env.rs
 create mode 100644 src/test/ui/const-generics/issues/issue-89334.rs

diff --git a/src/test/ui/const-generics/expose-default-substs-param-env.rs b/src/test/ui/const-generics/expose-default-substs-param-env.rs
new file mode 100644
index 0000000000000..e40c93116af4b
--- /dev/null
+++ b/src/test/ui/const-generics/expose-default-substs-param-env.rs
@@ -0,0 +1,9 @@
+// build-pass
+
+#![feature(generic_const_exprs)]
+#![allow(unused_braces, incomplete_features)]
+
+pub trait Foo<const N: usize> {}
+pub trait Bar: Foo<{ 1 }> { }
+
+fn main() {}
diff --git a/src/test/ui/const-generics/issues/issue-89334.rs b/src/test/ui/const-generics/issues/issue-89334.rs
new file mode 100644
index 0000000000000..b15b7428cdd0c
--- /dev/null
+++ b/src/test/ui/const-generics/issues/issue-89334.rs
@@ -0,0 +1,16 @@
+// build-pass
+
+#![feature(generic_const_exprs)]
+#![allow(unused_braces, incomplete_features)]
+
+pub trait AnotherTrait{
+    const ARRAY_SIZE: usize;
+}
+pub trait Shard<T: AnotherTrait>:
+    AsMut<[[u8; T::ARRAY_SIZE]]>
+where
+    [(); T::ARRAY_SIZE]: Sized
+{
+}
+
+fn main() {}