Skip to content

Commit 96d8011

Browse files
better names and a comment
1 parent c7b414a commit 96d8011

File tree

7 files changed

+42
-9
lines changed

7 files changed

+42
-9
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,41 @@ fn compare_asyncness<'tcx>(
441441
Ok(())
442442
}
443443

444+
/// Given a method def-id in an impl, compare the method signature of the impl
445+
/// against the trait that it's implementing. In doing so, infer the hidden types
446+
/// that this method's signature provides to satisfy each return-position `impl Trait`
447+
/// in the trait signature.
448+
///
449+
/// The method is also responsible for making sure that the hidden types for each
450+
/// RPITIT actually satisfy the bounds of the `impl Trait`, i.e. that if we infer
451+
/// `impl Trait = Foo`, that `Foo: Trait` holds.
452+
///
453+
/// For example, given the sample code:
454+
///
455+
/// ```
456+
/// #![feature(return_position_impl_trait_in_trait)]
457+
///
458+
/// use std::ops::Deref;
459+
///
460+
/// trait Foo {
461+
/// fn bar() -> impl Deref<Target = impl Sized>;
462+
/// // ^- RPITIT #1 ^- RPITIT #2
463+
/// }
464+
///
465+
/// impl Foo for () {
466+
/// fn bar() -> Box<String> { Box::new(String::new()) }
467+
/// }
468+
/// ```
469+
///
470+
/// The hidden types for the RPITITs in `bar` would be inferred to:
471+
/// * `impl Deref` (RPITIT #1) = `Box<String>`
472+
/// * `impl Sized` (RPITIT #2) = `String`
473+
///
474+
/// The relationship between these two types is straightforward in this case, but
475+
/// may be more tenuously connected via other `impl`s and normalization rules for
476+
/// cases of more complicated nested RPITITs.
444477
#[instrument(skip(tcx), level = "debug", ret)]
445-
pub(super) fn collect_trait_impl_trait_tys<'tcx>(
478+
pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
446479
tcx: TyCtxt<'tcx>,
447480
def_id: DefId,
448481
) -> Result<&'tcx FxHashMap<DefId, Ty<'tcx>>, ErrorGuaranteed> {

compiler/rustc_hir_analysis/src/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ use std::num::NonZeroU32;
9494
use crate::require_c_abi_if_c_variadic;
9595
use crate::util::common::indenter;
9696

97-
use self::compare_impl_item::collect_trait_impl_trait_tys;
97+
use self::compare_impl_item::collect_return_position_impl_trait_in_trait_tys;
9898
use self::region::region_scope_tree;
9999

100100
pub fn provide(providers: &mut Providers) {
@@ -103,7 +103,7 @@ pub fn provide(providers: &mut Providers) {
103103
adt_destructor,
104104
check_mod_item_types,
105105
region_scope_tree,
106-
collect_trait_impl_trait_tys,
106+
collect_return_position_impl_trait_in_trait_tys,
107107
compare_impl_const: compare_impl_item::compare_impl_const_raw,
108108
..*providers
109109
};

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ provide! { tcx, def_id, other, cdata,
223223
generator_kind => { table }
224224
trait_def => { table }
225225
deduced_param_attrs => { table }
226-
collect_trait_impl_trait_tys => {
226+
collect_return_position_impl_trait_in_trait_tys => {
227227
Ok(cdata
228228
.root
229229
.tables

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11971197
record!(self.tables.params_in_repr[def_id] <- params_in_repr);
11981198
}
11991199
if should_encode_trait_impl_trait_tys(tcx, def_id)
1200-
&& let Ok(table) = self.tcx.collect_trait_impl_trait_tys(def_id)
1200+
&& let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id)
12011201
{
12021202
record!(self.tables.trait_impl_trait_tys[def_id] <- table);
12031203
}

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ rustc_queries! {
169169
separate_provide_extern
170170
}
171171

172-
query collect_trait_impl_trait_tys(key: DefId)
172+
query collect_return_position_impl_trait_in_trait_tys(key: DefId)
173173
-> Result<&'tcx FxHashMap<DefId, Ty<'tcx>>, ErrorGuaranteed>
174174
{
175175
desc { "comparing an impl and trait method signature, inferring any hidden `impl Trait` types in the process" }

compiler/rustc_middle/src/ty/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,11 @@ impl<'tcx> TyCtxt<'tcx> {
641641
ty::EarlyBinder(self.type_of(def_id))
642642
}
643643

644-
pub fn bound_trait_impl_trait_tys(
644+
pub fn bound_return_position_impl_trait_in_trait_tys(
645645
self,
646646
def_id: DefId,
647647
) -> ty::EarlyBinder<Result<&'tcx FxHashMap<DefId, Ty<'tcx>>, ErrorGuaranteed>> {
648-
ty::EarlyBinder(self.collect_trait_impl_trait_tys(def_id))
648+
ty::EarlyBinder(self.collect_return_position_impl_trait_in_trait_tys(def_id))
649649
}
650650

651651
pub fn bound_fn_sig(self, def_id: DefId) -> ty::EarlyBinder<ty::PolyFnSig<'tcx>> {

compiler/rustc_trait_selection/src/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,7 @@ fn confirm_impl_trait_in_trait_candidate<'tcx>(
22842284
obligation.param_env,
22852285
cause.clone(),
22862286
obligation.recursion_depth + 1,
2287-
tcx.bound_trait_impl_trait_tys(impl_fn_def_id)
2287+
tcx.bound_return_position_impl_trait_in_trait_tys(impl_fn_def_id)
22882288
.map_bound(|tys| {
22892289
tys.map_or_else(|_| tcx.ty_error(), |tys| tys[&obligation.predicate.def_id])
22902290
})

0 commit comments

Comments
 (0)