about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-07-19 05:40:58 +0000
committerbors <bors@rust-lang.org>2023-07-19 05:40:58 +0000
commitb657dc555b1ecf837cc4ea471fbae6a731529d55 (patch)
tree72e56bff53e73f3a05c815dc99d053a12e5df061
parent0d6a9b2bf7f823ed563ca3a75392ba9bf9ff1566 (diff)
parent281c2271bea8dea7085988211d5512a67c659c73 (diff)
downloadrust-b657dc555b1ecf837cc4ea471fbae6a731529d55.tar.gz
rust-b657dc555b1ecf837cc4ea471fbae6a731529d55.zip
Auto merge of #113690 - aliemjay:opaque-defined-by-trait, r=compiler-errors
allow opaques to be defined by trait queries, again

This basically reverts #112963.

Moreover, all call-sites of `enter_canonical_trait_query` can now define opaque types, see the ui test `defined-by-user-annotation.rs`.

Fixes #113689

r? `@compiler-errors` `@oli-obk`
-rw-r--r--compiler/rustc_trait_selection/src/infer.rs11
-rw-r--r--tests/ui/impl-trait/defined-by-trait-resolution.rs12
-rw-r--r--tests/ui/type-alias-impl-trait/defined-by-user-annotation.rs19
3 files changed, 37 insertions, 5 deletions
diff --git a/compiler/rustc_trait_selection/src/infer.rs b/compiler/rustc_trait_selection/src/infer.rs
index 312bd38178f..6efc1e7302c 100644
--- a/compiler/rustc_trait_selection/src/infer.rs
+++ b/compiler/rustc_trait_selection/src/infer.rs
@@ -1,5 +1,5 @@
 use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
-use crate::traits::{self, ObligationCtxt};
+use crate::traits::{self, DefiningAnchor, ObligationCtxt};
 
 use rustc_hir::def_id::DefId;
 use rustc_hir::lang_items::LangItem;
@@ -80,7 +80,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
 
 pub trait InferCtxtBuilderExt<'tcx> {
     fn enter_canonical_trait_query<K, R>(
-        &mut self,
+        self,
         canonical_key: &Canonical<'tcx, K>,
         operation: impl FnOnce(&ObligationCtxt<'_, 'tcx>, K) -> Result<R, NoSolution>,
     ) -> Result<CanonicalQueryResponse<'tcx, R>, NoSolution>
@@ -108,7 +108,7 @@ impl<'tcx> InferCtxtBuilderExt<'tcx> for InferCtxtBuilder<'tcx> {
     /// have `'tcx` be free on this function so that we can talk about
     /// `K: TypeFoldable<TyCtxt<'tcx>>`.)
     fn enter_canonical_trait_query<K, R>(
-        &mut self,
+        self,
         canonical_key: &Canonical<'tcx, K>,
         operation: impl FnOnce(&ObligationCtxt<'_, 'tcx>, K) -> Result<R, NoSolution>,
     ) -> Result<CanonicalQueryResponse<'tcx, R>, NoSolution>
@@ -117,8 +117,9 @@ impl<'tcx> InferCtxtBuilderExt<'tcx> for InferCtxtBuilder<'tcx> {
         R: Debug + TypeFoldable<TyCtxt<'tcx>>,
         Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>,
     {
-        let (infcx, key, canonical_inference_vars) =
-            self.build_with_canonical(DUMMY_SP, canonical_key);
+        let (infcx, key, canonical_inference_vars) = self
+            .with_opaque_type_inference(DefiningAnchor::Bubble)
+            .build_with_canonical(DUMMY_SP, canonical_key);
         let ocx = ObligationCtxt::new(&infcx);
         let value = operation(&ocx, key)?;
         ocx.make_canonicalized_query_response(canonical_inference_vars, value)
diff --git a/tests/ui/impl-trait/defined-by-trait-resolution.rs b/tests/ui/impl-trait/defined-by-trait-resolution.rs
new file mode 100644
index 00000000000..1744046ddbb
--- /dev/null
+++ b/tests/ui/impl-trait/defined-by-trait-resolution.rs
@@ -0,0 +1,12 @@
+//! The trait query `foo: Fn() -> u8` is a valid defining use of RPIT.
+
+// build-pass
+
+fn returns_u8(_: impl Fn() -> u8) {}
+
+pub fn foo() -> impl Sized {
+    returns_u8(foo);
+    0u8
+}
+
+fn main() {}
diff --git a/tests/ui/type-alias-impl-trait/defined-by-user-annotation.rs b/tests/ui/type-alias-impl-trait/defined-by-user-annotation.rs
new file mode 100644
index 00000000000..5a421ea1dc0
--- /dev/null
+++ b/tests/ui/type-alias-impl-trait/defined-by-user-annotation.rs
@@ -0,0 +1,19 @@
+// User type annotation in fn bodies is a a valid defining site for opaque types.
+// check-pass
+#![feature(type_alias_impl_trait)]
+
+trait Equate { type Proj; }
+impl<T> Equate for T { type Proj = T; }
+
+trait Indirect { type Ty; }
+impl<A, B: Equate<Proj = A>> Indirect for (A, B) { type Ty = (); }
+
+type Opq = impl Sized;
+fn define_1(_: Opq) {
+    let _ = None::<<(Opq, u8) as Indirect>::Ty>;
+}
+fn define_2() -> Opq {
+    0u8
+}
+
+fn main() {}