Skip to content

Commit 83a28ef

Browse files
committed
Auto merge of rust-lang#106129 - compiler-errors:compare_method-tweaks, r=BoxyUwU
Some `compare_method` tweaks 1. Make some of the comparison functions' names more regular 2. Reduce pub scope of some of the things in `compare_method` ~3. Remove some unnecessary opaque type handling code -- `InferCtxt` already is in a mode that doesn't define opaque types~ * moved to a different PR 4. Bubble up `ErrorGuaranteed` for region constraint errors in `compare_method` - Improves a redundant error message in one unit test. 5. Move the `compare_method` module to have a more general name, since it's more like `compare_impl_item` :) 6. Rename `collect_trait_impl_trait_tys`
2 parents 6a20f7d + 96d8011 commit 83a28ef

File tree

17 files changed

+89
-72
lines changed

17 files changed

+89
-72
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::check::intrinsicck::InlineAsmCtxt;
22
use crate::errors::LinkageType;
33

4-
use super::compare_method::check_type_bounds;
5-
use super::compare_method::{compare_impl_method, compare_ty_impl};
4+
use super::compare_impl_item::check_type_bounds;
5+
use super::compare_impl_item::{compare_impl_method, compare_impl_ty};
66
use super::*;
77
use rustc_attr as attr;
88
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan};
@@ -468,7 +468,7 @@ fn check_opaque_meets_bounds<'tcx>(
468468
// Can have different predicates to their defining use
469469
hir::OpaqueTyOrigin::TyAlias => {
470470
let outlives_environment = OutlivesEnvironment::new(param_env);
471-
infcx.check_region_obligations_and_report_errors(
471+
let _ = infcx.check_region_obligations_and_report_errors(
472472
defining_use_anchor,
473473
&outlives_environment,
474474
);
@@ -774,7 +774,7 @@ fn check_impl_items_against_trait<'tcx>(
774774
let impl_item_full = tcx.hir().impl_item(impl_item.id);
775775
match impl_item_full.kind {
776776
hir::ImplItemKind::Const(..) => {
777-
let _ = tcx.compare_assoc_const_impl_item_with_trait_item((
777+
let _ = tcx.compare_impl_const((
778778
impl_item.id.owner_id.def_id,
779779
ty_impl_item.trait_item_def_id.unwrap(),
780780
));
@@ -791,7 +791,7 @@ fn check_impl_items_against_trait<'tcx>(
791791
}
792792
hir::ImplItemKind::Type(impl_ty) => {
793793
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
794-
compare_ty_impl(
794+
compare_impl_ty(
795795
tcx,
796796
&ty_impl_item,
797797
impl_ty.span,

compiler/rustc_hir_analysis/src/check/compare_method.rs renamed to compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+53-20
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::iter;
3434
/// - `impl_m_span`: span to use for reporting errors
3535
/// - `trait_m`: the method in the trait
3636
/// - `impl_trait_ref`: the TraitRef corresponding to the trait implementation
37-
pub(crate) fn compare_impl_method<'tcx>(
37+
pub(super) fn compare_impl_method<'tcx>(
3838
tcx: TyCtxt<'tcx>,
3939
impl_m: &ty::AssocItem,
4040
trait_m: &ty::AssocItem,
@@ -71,7 +71,7 @@ pub(crate) fn compare_impl_method<'tcx>(
7171
return;
7272
}
7373

74-
if let Err(_) = compare_predicate_entailment(
74+
if let Err(_) = compare_method_predicate_entailment(
7575
tcx,
7676
impl_m,
7777
impl_m_span,
@@ -150,7 +150,7 @@ pub(crate) fn compare_impl_method<'tcx>(
150150
/// Finally we register each of these predicates as an obligation and check that
151151
/// they hold.
152152
#[instrument(level = "debug", skip(tcx, impl_m_span, impl_trait_ref))]
153-
fn compare_predicate_entailment<'tcx>(
153+
fn compare_method_predicate_entailment<'tcx>(
154154
tcx: TyCtxt<'tcx>,
155155
impl_m: &ty::AssocItem,
156156
impl_m_span: Span,
@@ -337,7 +337,7 @@ fn compare_predicate_entailment<'tcx>(
337337
if !errors.is_empty() {
338338
match check_implied_wf {
339339
CheckImpliedWfMode::Check => {
340-
return compare_predicate_entailment(
340+
return compare_method_predicate_entailment(
341341
tcx,
342342
impl_m,
343343
impl_m_span,
@@ -374,7 +374,7 @@ fn compare_predicate_entailment<'tcx>(
374374
// becomes a hard error (i.e. ideally we'd just call `resolve_regions_and_report_errors`
375375
match check_implied_wf {
376376
CheckImpliedWfMode::Check => {
377-
return compare_predicate_entailment(
377+
return compare_method_predicate_entailment(
378378
tcx,
379379
impl_m,
380380
impl_m_span,
@@ -407,7 +407,7 @@ enum CheckImpliedWfMode {
407407
/// re-check with `Skip`, and emit a lint if it succeeds.
408408
Check,
409409
/// Skips checking implied well-formedness of the impl method, but will emit
410-
/// a lint if the `compare_predicate_entailment` succeeded. This means that
410+
/// a lint if the `compare_method_predicate_entailment` succeeded. This means that
411411
/// the reason that we had failed earlier during `Check` was due to the impl
412412
/// having stronger requirements than the trait.
413413
Skip,
@@ -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 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> {
@@ -550,13 +583,13 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
550583
// Unify the whole function signature. We need to do this to fully infer
551584
// the lifetimes of the return type, but do this after unifying just the
552585
// return types, since we want to avoid duplicating errors from
553-
// `compare_predicate_entailment`.
586+
// `compare_method_predicate_entailment`.
554587
match ocx.eq(&cause, param_env, trait_fty, impl_fty) {
555588
Ok(()) => {}
556589
Err(terr) => {
557-
// This function gets called during `compare_predicate_entailment` when normalizing a
590+
// This function gets called during `compare_method_predicate_entailment` when normalizing a
558591
// signature that contains RPITIT. When the method signatures don't match, we have to
559-
// emit an error now because `compare_predicate_entailment` will not report the error
592+
// emit an error now because `compare_method_predicate_entailment` will not report the error
560593
// when normalization fails.
561594
let emitted = report_trait_method_mismatch(
562595
infcx,
@@ -589,7 +622,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
589622
infcx.check_region_obligations_and_report_errors(
590623
impl_m.def_id.expect_local(),
591624
&outlives_environment,
592-
);
625+
)?;
593626

594627
let mut collected_tys = FxHashMap::default();
595628
for (def_id, (ty, substs)) in collector.types {
@@ -1516,8 +1549,8 @@ fn compare_generic_param_kinds<'tcx>(
15161549
Ok(())
15171550
}
15181551

1519-
/// Use `tcx.compare_assoc_const_impl_item_with_trait_item` instead
1520-
pub(crate) fn raw_compare_const_impl(
1552+
/// Use `tcx.compare_impl_const` instead
1553+
pub(super) fn compare_impl_const_raw(
15211554
tcx: TyCtxt<'_>,
15221555
(impl_const_item_def, trait_const_item_def): (LocalDefId, DefId),
15231556
) -> Result<(), ErrorGuaranteed> {
@@ -1617,13 +1650,13 @@ pub(crate) fn raw_compare_const_impl(
16171650
return Err(infcx.err_ctxt().report_fulfillment_errors(&errors, None));
16181651
}
16191652

1620-
// FIXME return `ErrorReported` if region obligations error?
16211653
let outlives_environment = OutlivesEnvironment::new(param_env);
1622-
infcx.check_region_obligations_and_report_errors(impl_const_item_def, &outlives_environment);
1654+
infcx.check_region_obligations_and_report_errors(impl_const_item_def, &outlives_environment)?;
1655+
16231656
Ok(())
16241657
}
16251658

1626-
pub(crate) fn compare_ty_impl<'tcx>(
1659+
pub(super) fn compare_impl_ty<'tcx>(
16271660
tcx: TyCtxt<'tcx>,
16281661
impl_ty: &ty::AssocItem,
16291662
impl_ty_span: Span,
@@ -1645,7 +1678,7 @@ pub(crate) fn compare_ty_impl<'tcx>(
16451678
})();
16461679
}
16471680

1648-
/// The equivalent of [compare_predicate_entailment], but for associated types
1681+
/// The equivalent of [compare_method_predicate_entailment], but for associated types
16491682
/// instead of associated functions.
16501683
fn compare_type_predicate_entailment<'tcx>(
16511684
tcx: TyCtxt<'tcx>,
@@ -1730,7 +1763,7 @@ fn compare_type_predicate_entailment<'tcx>(
17301763
infcx.check_region_obligations_and_report_errors(
17311764
impl_ty.def_id.expect_local(),
17321765
&outlives_environment,
1733-
);
1766+
)?;
17341767

17351768
Ok(())
17361769
}
@@ -1749,7 +1782,7 @@ fn compare_type_predicate_entailment<'tcx>(
17491782
/// from the impl could be overridden). We also can't normalize generic
17501783
/// associated types (yet) because they contain bound parameters.
17511784
#[instrument(level = "debug", skip(tcx))]
1752-
pub fn check_type_bounds<'tcx>(
1785+
pub(super) fn check_type_bounds<'tcx>(
17531786
tcx: TyCtxt<'tcx>,
17541787
trait_ty: &ty::AssocItem,
17551788
impl_ty: &ty::AssocItem,
@@ -1944,7 +1977,7 @@ pub fn check_type_bounds<'tcx>(
19441977
infcx.check_region_obligations_and_report_errors(
19451978
impl_ty.def_id.expect_local(),
19461979
&outlives_environment,
1947-
);
1980+
)?;
19481981

19491982
let constraints = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
19501983
for (key, value) in constraints {

compiler/rustc_hir_analysis/src/check/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ a type parameter).
6363
*/
6464

6565
mod check;
66-
mod compare_method;
66+
mod compare_impl_item;
6767
pub mod dropck;
6868
pub mod intrinsic;
6969
pub mod intrinsicck;
@@ -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_method::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,8 +103,8 @@ pub fn provide(providers: &mut Providers) {
103103
adt_destructor,
104104
check_mod_item_types,
105105
region_scope_tree,
106-
collect_trait_impl_trait_tys,
107-
compare_assoc_const_impl_item_with_trait_item: compare_method::raw_compare_const_impl,
106+
collect_return_position_impl_trait_in_trait_tys,
107+
compare_impl_const: compare_impl_item::compare_impl_const_raw,
108108
..*providers
109109
};
110110
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
115115
let outlives_environment =
116116
OutlivesEnvironment::with_bounds(param_env, Some(infcx), implied_bounds);
117117

118-
infcx.check_region_obligations_and_report_errors(body_def_id, &outlives_environment);
118+
let _ = infcx.check_region_obligations_and_report_errors(body_def_id, &outlives_environment);
119119
}
120120

121121
fn check_well_formed(tcx: TyCtxt<'_>, def_id: hir::OwnerId) {

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
325325

326326
// Finally, resolve all regions.
327327
let outlives_env = OutlivesEnvironment::new(param_env);
328-
infcx.check_region_obligations_and_report_errors(impl_did, &outlives_env);
328+
let _ = infcx.check_region_obligations_and_report_errors(impl_did, &outlives_env);
329329
}
330330
}
331331
_ => {
@@ -565,7 +565,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
565565

566566
// Finally, resolve all regions.
567567
let outlives_env = OutlivesEnvironment::new(param_env);
568-
infcx.check_region_obligations_and_report_errors(impl_did, &outlives_env);
568+
let _ = infcx.check_region_obligations_and_report_errors(impl_did, &outlives_env);
569569

570570
CoerceUnsizedInfo { custom_kind: kind }
571571
}

compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ fn get_impl_substs(
181181

182182
let implied_bounds = infcx.implied_bounds_tys(param_env, impl1_hir_id, assumed_wf_types);
183183
let outlives_env = OutlivesEnvironment::with_bounds(param_env, Some(infcx), implied_bounds);
184-
infcx.check_region_obligations_and_report_errors(impl1_def_id, &outlives_env);
184+
let _ = infcx.check_region_obligations_and_report_errors(impl1_def_id, &outlives_env);
185185
let Ok(impl2_substs) = infcx.fully_resolve(impl2_substs) else {
186186
let span = tcx.def_span(impl1_def_id);
187187
tcx.sess.emit_err(SubstsOnOverriddenImpl { span });

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
18361836

18371837
// In some (most?) cases cause.body_id points to actual body, but in some cases
18381838
// it's an actual definition. According to the comments (e.g. in
1839-
// rustc_hir_analysis/check/compare_method.rs:compare_predicate_entailment) the latter
1839+
// rustc_hir_analysis/check/compare_impl_item.rs:compare_predicate_entailment) the latter
18401840
// is relied upon by some other code. This might (or might not) need cleanup.
18411841
let body_owner_def_id =
18421842
self.tcx.hir().opt_local_def_id(cause.body_id).unwrap_or_else(|| {

compiler/rustc_infer/src/infer/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
16931693
&self,
16941694
generic_param_scope: LocalDefId,
16951695
outlives_env: &OutlivesEnvironment<'tcx>,
1696-
) -> Option<ErrorGuaranteed> {
1696+
) -> Result<(), ErrorGuaranteed> {
16971697
let errors = self.resolve_regions(outlives_env);
16981698

16991699
if let None = self.tainted_by_errors() {
@@ -1705,9 +1705,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
17051705
self.report_region_errors(generic_param_scope, &errors);
17061706
}
17071707

1708-
(!errors.is_empty()).then(|| {
1709-
self.tcx.sess.delay_span_bug(rustc_span::DUMMY_SP, "error should have been emitted")
1710-
})
1708+
if errors.is_empty() {
1709+
Ok(())
1710+
} else {
1711+
Err(self
1712+
.tcx
1713+
.sess
1714+
.delay_span_bug(rustc_span::DUMMY_SP, "error should have been emitted"))
1715+
}
17111716
}
17121717

17131718
// [Note-Type-error-reporting]

compiler/rustc_infer/src/infer/outlives/obligations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<'tcx> InferCtxt<'tcx> {
178178
&self,
179179
generic_param_scope: LocalDefId,
180180
outlives_env: &OutlivesEnvironment<'tcx>,
181-
) -> Option<ErrorGuaranteed> {
181+
) -> Result<(), ErrorGuaranteed> {
182182
self.process_registered_region_obligations(
183183
outlives_env.region_bound_pairs(),
184184
outlives_env.param_env,

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

+2-2
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" }
@@ -2117,7 +2117,7 @@ rustc_queries! {
21172117
desc { "checking to see if `{}` permits being left zeroed", key.ty }
21182118
}
21192119

2120-
query compare_assoc_const_impl_item_with_trait_item(
2120+
query compare_impl_const(
21212121
key: (LocalDefId, DefId)
21222122
) -> Result<(), ErrorGuaranteed> {
21232123
desc { |tcx| "checking assoc const `{}` has the same type as trait item", tcx.def_path_str(key.0.to_def_id()) }

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)