From 9f0693969a0ac0a0193c7c367e3292b5315b2747 Mon Sep 17 00:00:00 2001
From: Jack Huey <jack.huey@umassmed.edu>
Date: Sun, 25 Apr 2021 15:11:49 -0400
Subject: [PATCH 1/2] Deduplicate ParamCandidates with the same value except
 for bound vars

---
 .../src/traits/select/mod.rs                  | 12 ++++++++++-
 src/test/ui/lifetimes/issue-84398.rs          | 20 +++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 src/test/ui/lifetimes/issue-84398.rs

diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs
index e4aabbdb7ede7..c9ba8e6dc8ce7 100644
--- a/compiler/rustc_trait_selection/src/traits/select/mod.rs
+++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs
@@ -1361,7 +1361,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
             ) => false,
 
             (ParamCandidate(other), ParamCandidate(victim)) => {
-                if other.value == victim.value && victim.constness == Constness::NotConst {
+                let value_same_except_bound_vars = other.value.skip_binder()
+                    == victim.value.skip_binder()
+                    && !other.value.skip_binder().has_escaping_bound_vars();
+                if value_same_except_bound_vars {
+                    // See issue #84398. In short, we can generate multiple ParamCandidates which are
+                    // the same except for unused bound vars. Just pick the current one (the should
+                    // both evaluate to the same answer). This is probably best characterized as a
+                    // "hack", since we might prefer to just do our best to *not* create essentially
+                    // duplicate candidates in the first place.
+                    true
+                } else if other.value == victim.value && victim.constness == Constness::NotConst {
                     // Drop otherwise equivalent non-const candidates in favor of const candidates.
                     true
                 } else {
diff --git a/src/test/ui/lifetimes/issue-84398.rs b/src/test/ui/lifetimes/issue-84398.rs
new file mode 100644
index 0000000000000..1912fa59b7990
--- /dev/null
+++ b/src/test/ui/lifetimes/issue-84398.rs
@@ -0,0 +1,20 @@
+// check-pass
+
+pub trait Deserialize<'de>: Sized {}
+pub trait DeserializeOwned: for<'de> Deserialize<'de> {}
+
+pub trait Extensible {
+    type Config;
+}
+
+// The `C` here generates a `C: Sized` candidate
+pub trait Installer<C> {
+    fn init<B: Extensible<Config = C>>(&mut self) -> ()
+    where
+        // This clause generates a `for<'de> C: Sized` candidate
+        B::Config: DeserializeOwned,
+    {
+    }
+}
+
+fn main() {}

From c1ef0f3050c3575d864740e14b4e49b36bb3dae1 Mon Sep 17 00:00:00 2001
From: Jack Huey <31162821+jackh726@users.noreply.github.com>
Date: Thu, 6 May 2021 10:19:51 -0400
Subject: [PATCH 2/2] Pick candidate with fewer bound vars

---
 .../rustc_trait_selection/src/traits/select/mod.rs     | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs
index c9ba8e6dc8ce7..727285e4927a0 100644
--- a/compiler/rustc_trait_selection/src/traits/select/mod.rs
+++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs
@@ -1366,11 +1366,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
                     && !other.value.skip_binder().has_escaping_bound_vars();
                 if value_same_except_bound_vars {
                     // See issue #84398. In short, we can generate multiple ParamCandidates which are
-                    // the same except for unused bound vars. Just pick the current one (the should
-                    // both evaluate to the same answer). This is probably best characterized as a
-                    // "hack", since we might prefer to just do our best to *not* create essentially
-                    // duplicate candidates in the first place.
-                    true
+                    // the same except for unused bound vars. Just pick the one with the fewest bound vars
+                    // or the current one if tied (they should both evaluate to the same answer). This is
+                    // probably best characterized as a "hack", since we might prefer to just do our
+                    // best to *not* create essentially duplicate candidates in the first place.
+                    other.value.bound_vars().len() <= victim.value.bound_vars().len()
                 } else if other.value == victim.value && victim.constness == Constness::NotConst {
                     // Drop otherwise equivalent non-const candidates in favor of const candidates.
                     true