Skip to content

Commit b657dc5

Browse files
committed
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`
2 parents 0d6a9b2 + 281c227 commit b657dc5

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

compiler/rustc_trait_selection/src/infer.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
2-
use crate::traits::{self, ObligationCtxt};
2+
use crate::traits::{self, DefiningAnchor, ObligationCtxt};
33

44
use rustc_hir::def_id::DefId;
55
use rustc_hir::lang_items::LangItem;
@@ -80,7 +80,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
8080

8181
pub trait InferCtxtBuilderExt<'tcx> {
8282
fn enter_canonical_trait_query<K, R>(
83-
&mut self,
83+
self,
8484
canonical_key: &Canonical<'tcx, K>,
8585
operation: impl FnOnce(&ObligationCtxt<'_, 'tcx>, K) -> Result<R, NoSolution>,
8686
) -> Result<CanonicalQueryResponse<'tcx, R>, NoSolution>
@@ -108,7 +108,7 @@ impl<'tcx> InferCtxtBuilderExt<'tcx> for InferCtxtBuilder<'tcx> {
108108
/// have `'tcx` be free on this function so that we can talk about
109109
/// `K: TypeFoldable<TyCtxt<'tcx>>`.)
110110
fn enter_canonical_trait_query<K, R>(
111-
&mut self,
111+
self,
112112
canonical_key: &Canonical<'tcx, K>,
113113
operation: impl FnOnce(&ObligationCtxt<'_, 'tcx>, K) -> Result<R, NoSolution>,
114114
) -> Result<CanonicalQueryResponse<'tcx, R>, NoSolution>
@@ -117,8 +117,9 @@ impl<'tcx> InferCtxtBuilderExt<'tcx> for InferCtxtBuilder<'tcx> {
117117
R: Debug + TypeFoldable<TyCtxt<'tcx>>,
118118
Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable<'tcx>,
119119
{
120-
let (infcx, key, canonical_inference_vars) =
121-
self.build_with_canonical(DUMMY_SP, canonical_key);
120+
let (infcx, key, canonical_inference_vars) = self
121+
.with_opaque_type_inference(DefiningAnchor::Bubble)
122+
.build_with_canonical(DUMMY_SP, canonical_key);
122123
let ocx = ObligationCtxt::new(&infcx);
123124
let value = operation(&ocx, key)?;
124125
ocx.make_canonicalized_query_response(canonical_inference_vars, value)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! The trait query `foo: Fn() -> u8` is a valid defining use of RPIT.
2+
3+
// build-pass
4+
5+
fn returns_u8(_: impl Fn() -> u8) {}
6+
7+
pub fn foo() -> impl Sized {
8+
returns_u8(foo);
9+
0u8
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// User type annotation in fn bodies is a a valid defining site for opaque types.
2+
// check-pass
3+
#![feature(type_alias_impl_trait)]
4+
5+
trait Equate { type Proj; }
6+
impl<T> Equate for T { type Proj = T; }
7+
8+
trait Indirect { type Ty; }
9+
impl<A, B: Equate<Proj = A>> Indirect for (A, B) { type Ty = (); }
10+
11+
type Opq = impl Sized;
12+
fn define_1(_: Opq) {
13+
let _ = None::<<(Opq, u8) as Indirect>::Ty>;
14+
}
15+
fn define_2() -> Opq {
16+
0u8
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)