From 3f0a327fbbb28c90fb33425aa8871d0c0d4a9c48 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= <me@fmease.dev>
Date: Tue, 3 Oct 2023 13:50:07 +0200
Subject: [PATCH] non_lifetime_binders: fix ICE in lint
 opaque-hidden-inferred-bound

---
 .../src/opaque_hidden_inferred_bound.rs          | 10 +++-------
 tests/ui/traits/non_lifetime_binders/on-rpit.rs  | 16 ++++++++++++++++
 .../traits/non_lifetime_binders/on-rpit.stderr   | 11 +++++++++++
 3 files changed, 30 insertions(+), 7 deletions(-)
 create mode 100644 tests/ui/traits/non_lifetime_binders/on-rpit.rs
 create mode 100644 tests/ui/traits/non_lifetime_binders/on-rpit.stderr

diff --git a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs
index 79b0b32bef248..c24846ca93988 100644
--- a/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs
+++ b/compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs
@@ -37,8 +37,6 @@ declare_lint! {
     ///     type Assoc: Duh;
     /// }
     ///
-    /// struct Struct;
-    ///
     /// impl<F: Duh> Trait for F {
     ///     type Assoc = F;
     /// }
@@ -53,12 +51,12 @@ declare_lint! {
     /// {{produces}}
     ///
     /// In this example, `test` declares that the associated type `Assoc` for
-    /// `impl Trait` is `impl Sized`, which does not satisfy the `Send` bound
+    /// `impl Trait` is `impl Sized`, which does not satisfy the bound `Duh`
     /// on the associated type.
     ///
     /// Although the hidden type, `i32` does satisfy this bound, we do not
     /// consider the return type to be well-formed with this lint. It can be
-    /// fixed by changing `Tait = impl Sized` into `Tait = impl Sized + Send`.
+    /// fixed by changing `Tait = impl Sized` into `Tait = impl Sized + Duh`.
     pub OPAQUE_HIDDEN_INFERRED_BOUND,
     Warn,
     "detects the use of nested `impl Trait` types in associated type bounds that are not general enough"
@@ -79,9 +77,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
         for (pred, pred_span) in
             cx.tcx.explicit_item_bounds(def_id).instantiate_identity_iter_copied()
         {
-            // Liberate bound regions in the predicate since we
-            // don't actually care about lifetimes in this check.
-            let predicate = cx.tcx.liberate_late_bound_regions(def_id, pred.kind());
+            let predicate = infcx.instantiate_binder_with_placeholders(pred.kind());
             let ty::ClauseKind::Projection(proj) = predicate else {
                 continue;
             };
diff --git a/tests/ui/traits/non_lifetime_binders/on-rpit.rs b/tests/ui/traits/non_lifetime_binders/on-rpit.rs
new file mode 100644
index 0000000000000..c501e057e2830
--- /dev/null
+++ b/tests/ui/traits/non_lifetime_binders/on-rpit.rs
@@ -0,0 +1,16 @@
+// check-pass
+
+#![feature(non_lifetime_binders)]
+//~^ WARN the feature `non_lifetime_binders` is incomplete
+
+trait Trait<T: ?Sized> {}
+
+impl<T: ?Sized> Trait<T> for i32 {}
+
+fn produce() -> impl for<T> Trait<T> {
+    16
+}
+
+fn main() {
+    let _ = produce();
+}
diff --git a/tests/ui/traits/non_lifetime_binders/on-rpit.stderr b/tests/ui/traits/non_lifetime_binders/on-rpit.stderr
new file mode 100644
index 0000000000000..34c56068c5cc5
--- /dev/null
+++ b/tests/ui/traits/non_lifetime_binders/on-rpit.stderr
@@ -0,0 +1,11 @@
+warning: the feature `non_lifetime_binders` is incomplete and may not be safe to use and/or cause compiler crashes
+  --> $DIR/on-rpit.rs:3:12
+   |
+LL | #![feature(non_lifetime_binders)]
+   |            ^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: see issue #108185 <https://github.com/rust-lang/rust/issues/108185> for more information
+   = note: `#[warn(incomplete_features)]` on by default
+
+warning: 1 warning emitted
+