about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-03-31 04:57:26 +0200
committerGitHub <noreply@github.com>2022-03-31 04:57:26 +0200
commit64a3767fee24bfca0534edf596d96659dee68b29 (patch)
tree2ae3558739499e498896fddf720080c2ca4f4951
parent4ce6567daa7aab5618aa27f69ceff779cbe2bd7d (diff)
parent11446779b006b25300eb2b4ad6707d6d3303da5e (diff)
downloadrust-64a3767fee24bfca0534edf596d96659dee68b29.tar.gz
rust-64a3767fee24bfca0534edf596d96659dee68b29.zip
Rollup merge of #95471 - oli-obk:tait_ice, r=estebank
Don't ICE when opaque types get their hidden type constrained again.

Contrary to popular belief, `codegen_fulfill_obligation` does not get used solely in codegen, so we cannot rely on `param_env` being set to RevealAll and thus revealing the hidden types instead of constraining them.

Fixes #89312 (for real this time)
-rw-r--r--compiler/rustc_trait_selection/src/traits/codegen.rs19
-rw-r--r--src/test/ui/type-alias-impl-trait/assoc-projection-ice.rs24
2 files changed, 30 insertions, 13 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/codegen.rs b/compiler/rustc_trait_selection/src/traits/codegen.rs
index 4b6784aee97..866bb6109e0 100644
--- a/compiler/rustc_trait_selection/src/traits/codegen.rs
+++ b/compiler/rustc_trait_selection/src/traits/codegen.rs
@@ -19,6 +19,7 @@ use rustc_middle::ty::{self, TyCtxt};
 /// obligations *could be* resolved if we wanted to.
 ///
 /// This also expects that `trait_ref` is fully normalized.
+#[instrument(level = "debug", skip(tcx))]
 pub fn codegen_fulfill_obligation<'tcx>(
     tcx: TyCtxt<'tcx>,
     (param_env, trait_ref): (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>),
@@ -27,11 +28,6 @@ pub fn codegen_fulfill_obligation<'tcx>(
     let trait_ref = tcx.erase_regions(trait_ref);
     // We expect the input to be fully normalized.
     debug_assert_eq!(trait_ref, tcx.normalize_erasing_regions(param_env, trait_ref));
-    debug!(
-        "codegen_fulfill_obligation(trait_ref={:?}, def_id={:?})",
-        (param_env, trait_ref),
-        trait_ref.def_id()
-    );
 
     // Do the initial selection for the obligation. This yields the
     // shallow result we are looking for -- that is, what specific impl.
@@ -80,25 +76,22 @@ pub fn codegen_fulfill_obligation<'tcx>(
             }
         };
 
-        debug!("fulfill_obligation: selection={:?}", selection);
+        debug!(?selection);
 
         // Currently, we use a fulfillment context to completely resolve
         // all nested obligations. This is because they can inform the
         // inference of the impl's type parameters.
         let mut fulfill_cx = FulfillmentContext::new();
         let impl_source = selection.map(|predicate| {
-            debug!("fulfill_obligation: register_predicate_obligation {:?}", predicate);
             fulfill_cx.register_predicate_obligation(&infcx, predicate);
         });
         let impl_source = drain_fulfillment_cx_or_panic(&infcx, &mut fulfill_cx, impl_source);
 
-        // There should be no opaque types during codegen, they all get revealed.
-        let opaque_types = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
-        if !opaque_types.is_empty() {
-            bug!("{:#?}", opaque_types);
-        }
+        // Opaque types may have gotten their hidden types constrained, but we can ignore them safely
+        // as they will get constrained elsewhere, too.
+        let _opaque_types = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
 
-        debug!("Cache miss: {:?} => {:?}", trait_ref, impl_source);
+        debug!("Cache miss: {trait_ref:?} => {impl_source:?}");
         Ok(&*tcx.arena.alloc(impl_source))
     })
 }
diff --git a/src/test/ui/type-alias-impl-trait/assoc-projection-ice.rs b/src/test/ui/type-alias-impl-trait/assoc-projection-ice.rs
new file mode 100644
index 00000000000..703e3e8693e
--- /dev/null
+++ b/src/test/ui/type-alias-impl-trait/assoc-projection-ice.rs
@@ -0,0 +1,24 @@
+#![feature(type_alias_impl_trait)]
+
+// build-pass
+
+trait T { type Item; }
+
+type Alias<'a> = impl T<Item = &'a ()>;
+
+struct S;
+impl<'a> T for &'a S {
+    type Item = &'a ();
+}
+
+fn filter_positive<'a>() -> Alias<'a> {
+    &S
+}
+
+fn with_positive(fun: impl Fn(Alias<'_>)) {
+    fun(filter_positive());
+}
+
+fn main() {
+    with_positive(|_| ());
+}