about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2023-04-16 06:55:21 +0900
committerGitHub <noreply@github.com>2023-04-16 06:55:21 +0900
commit52d23c92530e04ba4c8d556f8abe3ac33e56e501 (patch)
tree0e6714ca065865df9185429e028fc24cf6063524
parent7e6983e27dc1788c63f297026ee5258791298aae (diff)
parent4c80f58d4188165cc20f72223ec4d1ff46bfb4a8 (diff)
downloadrust-52d23c92530e04ba4c8d556f8abe3ac33e56e501.tar.gz
rust-52d23c92530e04ba4c8d556f8abe3ac33e56e501.zip
Rollup merge of #110272 - Ezrashaw:fix-unconned-lt-in-implbounds, r=aliemjay
fix: skip implied bounds if unconstrained lifetime exists

Fixes #110161

r? ````@aliemjay````
-rw-r--r--compiler/rustc_trait_selection/src/traits/outlives_bounds.rs13
-rw-r--r--tests/ui/implied-bounds/issue-110161.rs26
-rw-r--r--tests/ui/implied-bounds/issue-110161.stderr12
3 files changed, 50 insertions, 1 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs b/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs
index cff3d277a78..e01a57ea4fe 100644
--- a/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs
+++ b/compiler/rustc_trait_selection/src/traits/outlives_bounds.rs
@@ -55,7 +55,18 @@ impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> {
     ) -> Vec<OutlivesBound<'tcx>> {
         let ty = self.resolve_vars_if_possible(ty);
         let ty = OpportunisticRegionResolver::new(self).fold_ty(ty);
-        assert!(!ty.needs_infer());
+
+        // We do not expect existential variables in implied bounds.
+        // We may however encounter unconstrained lifetime variables in invalid
+        // code. See #110161 for context.
+        assert!(!ty.has_non_region_infer());
+        if ty.needs_infer() {
+            self.tcx.sess.delay_span_bug(
+                self.tcx.def_span(body_id),
+                "skipped implied_outlives_bounds due to unconstrained lifetimes",
+            );
+            return vec![];
+        }
 
         let span = self.tcx.def_span(body_id);
         let result = param_env
diff --git a/tests/ui/implied-bounds/issue-110161.rs b/tests/ui/implied-bounds/issue-110161.rs
new file mode 100644
index 00000000000..e52c8356b52
--- /dev/null
+++ b/tests/ui/implied-bounds/issue-110161.rs
@@ -0,0 +1,26 @@
+// ICE regression relating to unconstrained lifetimes in implied
+// bounds. See #110161.
+
+// compile-flags: --crate-type=lib
+
+trait LtTrait {
+    type Ty;
+}
+
+// erroneous `Ty` impl
+impl LtTrait for () {
+//~^ ERROR not all trait items implemented, missing: `Ty` [E0046]
+}
+
+// `'lt` is not constrained by the erroneous `Ty`
+impl<'lt, T> LtTrait for Box<T>
+where
+    T: LtTrait<Ty = &'lt ()>,
+{
+    type Ty = &'lt ();
+}
+
+// unconstrained lifetime appears in implied bounds
+fn test(_: <Box<()> as LtTrait>::Ty) {}
+
+fn test2<'x>(_: &'x <Box<()> as LtTrait>::Ty) {}
diff --git a/tests/ui/implied-bounds/issue-110161.stderr b/tests/ui/implied-bounds/issue-110161.stderr
new file mode 100644
index 00000000000..9e0188694ed
--- /dev/null
+++ b/tests/ui/implied-bounds/issue-110161.stderr
@@ -0,0 +1,12 @@
+error[E0046]: not all trait items implemented, missing: `Ty`
+  --> $DIR/issue-110161.rs:11:1
+   |
+LL |     type Ty;
+   |     ------- `Ty` from trait
+...
+LL | impl LtTrait for () {
+   | ^^^^^^^^^^^^^^^^^^^ missing `Ty` in implementation
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0046`.