Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 320ae9c

Browse files
authored
Unrolled build for rust-lang#140218
Rollup merge of rust-lang#140218 - fmease:hirtylo-clean-up-path-low, r=compiler-errors HIR ty lowering: Clean up & refactor the lowering of type-relative paths While rebasing rust-lang#126651 I realized that HIR ty lowering could benefit from some *spring cleaning* now that it's been extended to handle RTN and mGCA paths. More seriously, similar to my merged PR rust-lang#118668 which unified the handling of all *associated item constraints* (assoc ty, const (ACE) & fn (RTN)), this PR (commit rust-lang@695fcf5) partially[^1] deduplicates the resolution code for all *type-relative paths* (assoc ty, const (mGCA) & fn (RTN)). **Why**? DRY'ing that part of the code means PR rust-lang#126651 will automatically support RTN paths like `Ty::AssocTy::assoc_fn(..)` and it also implies shared diagnostic code and thus better diagnostics for RTN. --- The other commits represent cleanups, renamings, moves. More notably, I've renamed path lowering methods to be a lot more descriptive, so ones lowering `QPath(Resolved)` paths now have `_resolved_` in their name and ones lowering `QPath(TypeRelative)` paths now have `_type_relative_` in their name. This should make it stupidly obvious what their purpose is. --- Best reviewed commit by commit. The changes are close to trivial but the diff might make it look hairier. r? compiler-errors [^1]: Sadly, I couldn't unify as much compared to the other PR without introducing unnecessary `unreachable!()`s or rendering the code otherwise illegible with flags and micro helper traits.
2 parents 2eef478 + 8c37c8c commit 320ae9c

24 files changed

+626
-518
lines changed

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ hir_analysis_assoc_kind_mismatch = expected {$expected}, found {$got}
3737
3838
hir_analysis_assoc_kind_mismatch_wrap_in_braces_sugg = consider adding braces here
3939
40-
hir_analysis_associated_type_trait_uninferred_generic_params = cannot use the associated {$what} of a trait with uninferred generic parameters
40+
hir_analysis_associated_type_trait_uninferred_generic_params = cannot use the {$what} of a trait with uninferred generic parameters
4141
.suggestion = use a fully qualified path with inferred lifetimes
4242
4343
hir_analysis_associated_type_trait_uninferred_generic_params_multipart_suggestion = use a fully qualified path with explicit lifetimes

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use rustc_trait_selection::traits::ObligationCtxt;
4444
use tracing::{debug, instrument};
4545

4646
use crate::errors;
47-
use crate::hir_ty_lowering::errors::assoc_tag_str;
4847
use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer, RegionInferReason};
4948

5049
pub(crate) mod dump;
@@ -444,13 +443,12 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
444443
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_ident))
445444
}
446445

447-
fn lower_assoc_shared(
446+
fn lower_assoc_item_path(
448447
&self,
449448
span: Span,
450449
item_def_id: DefId,
451450
item_segment: &rustc_hir::PathSegment<'tcx>,
452451
poly_trait_ref: ty::PolyTraitRef<'tcx>,
453-
assoc_tag: ty::AssocTag,
454452
) -> Result<(DefId, ty::GenericArgsRef<'tcx>), ErrorGuaranteed> {
455453
if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
456454
let item_args = self.lowerer().lower_generic_args_of_assoc_item(
@@ -525,7 +523,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
525523
inferred_sugg,
526524
bound,
527525
mpart_sugg,
528-
what: assoc_tag_str(assoc_tag),
526+
what: self.tcx.def_descr(item_def_id),
529527
}))
530528
}
531529
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 39 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
44
use rustc_errors::codes::*;
55
use rustc_errors::struct_span_code_err;
66
use rustc_hir as hir;
7+
use rustc_hir::AmbigArg;
78
use rustc_hir::def::{DefKind, Res};
89
use rustc_hir::def_id::{DefId, LocalDefId};
9-
use rustc_hir::{AmbigArg, HirId};
1010
use rustc_middle::bug;
1111
use rustc_middle::ty::{
1212
self as ty, IsSuggestable, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, TypeVisitable,
@@ -309,7 +309,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
309309
false => "`?Sized`",
310310
};
311311
// There was a `?Trait` bound, but it was neither `?Sized` nor `experimental_default_bounds`.
312-
tcx.dcx().span_err(
312+
self.dcx().span_err(
313313
unbound.span,
314314
format!(
315315
"relaxing a default bound only does something for {}; \
@@ -675,7 +675,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
675675

676676
// Good error for `where Trait::method(..): Send`.
677677
let Some(self_ty) = opt_self_ty else {
678-
let guar = self.error_missing_qpath_self_ty(
678+
let guar = self.report_missing_self_ty_for_resolved_path(
679679
trait_def_id,
680680
hir_ty.span,
681681
item_segment,
@@ -713,120 +713,51 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
713713
Err(guar) => Ty::new_error(tcx, guar),
714714
}
715715
}
716-
hir::QPath::TypeRelative(qself, item_segment)
717-
if item_segment.args.is_some_and(|args| {
716+
hir::QPath::TypeRelative(hir_self_ty, segment)
717+
if segment.args.is_some_and(|args| {
718718
matches!(args.parenthesized, hir::GenericArgsParentheses::ReturnTypeNotation)
719719
}) =>
720720
{
721-
match self
722-
.resolve_type_relative_return_type_notation(
723-
qself,
724-
item_segment,
725-
hir_ty.hir_id,
726-
hir_ty.span,
727-
)
728-
.and_then(|(candidate, item_def_id)| {
729-
self.lower_return_type_notation_ty(candidate, item_def_id, hir_ty.span)
730-
}) {
731-
Ok(ty) => Ty::new_alias(tcx, ty::Projection, ty),
732-
Err(guar) => Ty::new_error(tcx, guar),
733-
}
734-
}
735-
_ => self.lower_ty(hir_ty),
736-
}
737-
}
738-
739-
/// Perform type-dependent lookup for a *method* for return type notation.
740-
/// This generally mirrors `<dyn HirTyLowerer>::lower_assoc_path`.
741-
fn resolve_type_relative_return_type_notation(
742-
&self,
743-
qself: &'tcx hir::Ty<'tcx>,
744-
item_segment: &'tcx hir::PathSegment<'tcx>,
745-
qpath_hir_id: HirId,
746-
span: Span,
747-
) -> Result<(ty::PolyTraitRef<'tcx>, DefId), ErrorGuaranteed> {
748-
let tcx = self.tcx();
749-
let qself_ty = self.lower_ty(qself);
750-
let assoc_ident = item_segment.ident;
751-
let qself_res = if let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = &qself.kind {
752-
path.res
753-
} else {
754-
Res::Err
755-
};
756-
757-
let bound = match (qself_ty.kind(), qself_res) {
758-
(_, Res::SelfTyAlias { alias_to: impl_def_id, is_trait_impl: true, .. }) => {
759-
// `Self` in an impl of a trait -- we have a concrete self type and a
760-
// trait reference.
761-
let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) else {
762-
// A cycle error occurred, most likely.
763-
self.dcx().span_bug(span, "expected cycle error");
764-
};
765-
766-
self.probe_single_bound_for_assoc_item(
767-
|| {
768-
traits::supertraits(
769-
tcx,
770-
ty::Binder::dummy(trait_ref.instantiate_identity()),
771-
)
772-
},
773-
AssocItemQSelf::SelfTyAlias,
721+
let self_ty = self.lower_ty(hir_self_ty);
722+
let (item_def_id, bound) = match self.resolve_type_relative_path(
723+
self_ty,
724+
hir_self_ty,
774725
ty::AssocTag::Fn,
775-
assoc_ident,
776-
span,
726+
segment,
727+
hir_ty.hir_id,
728+
hir_ty.span,
777729
None,
778-
)?
779-
}
780-
(
781-
&ty::Param(_),
782-
Res::SelfTyParam { trait_: param_did } | Res::Def(DefKind::TyParam, param_did),
783-
) => self.probe_single_ty_param_bound_for_assoc_item(
784-
param_did.expect_local(),
785-
qself.span,
786-
ty::AssocTag::Fn,
787-
assoc_ident,
788-
span,
789-
)?,
790-
_ => {
791-
if let Err(reported) = qself_ty.error_reported() {
792-
return Err(reported);
793-
} else {
794-
// FIXME(return_type_notation): Provide some structured suggestion here.
795-
let err = struct_span_code_err!(
796-
self.dcx(),
797-
span,
798-
E0223,
799-
"ambiguous associated function"
730+
) {
731+
Ok(result) => result,
732+
Err(guar) => return Ty::new_error(tcx, guar),
733+
};
734+
735+
// Don't let `T::method` resolve to some `for<'a> <T as Tr<'a>>::method`,
736+
// which may happen via a higher-ranked where clause or supertrait.
737+
// This is the same restrictions as associated types; even though we could
738+
// support it, it just makes things a lot more difficult to support in
739+
// `resolve_bound_vars`, since we'd need to introduce those as elided
740+
// bound vars on the where clause too.
741+
if bound.has_bound_vars() {
742+
return Ty::new_error(
743+
tcx,
744+
self.dcx().emit_err(errors::AssociatedItemTraitUninferredGenericParams {
745+
span: hir_ty.span,
746+
inferred_sugg: Some(hir_ty.span.with_hi(segment.ident.span.lo())),
747+
bound: format!("{}::", tcx.anonymize_bound_vars(bound).skip_binder()),
748+
mpart_sugg: None,
749+
what: tcx.def_descr(item_def_id),
750+
}),
800751
);
801-
return Err(err.emit());
802752
}
803-
}
804-
};
805753

806-
// Don't let `T::method` resolve to some `for<'a> <T as Tr<'a>>::method`,
807-
// which may happen via a higher-ranked where clause or supertrait.
808-
// This is the same restrictions as associated types; even though we could
809-
// support it, it just makes things a lot more difficult to support in
810-
// `resolve_bound_vars`, since we'd need to introduce those as elided
811-
// bound vars on the where clause too.
812-
if bound.has_bound_vars() {
813-
return Err(self.tcx().dcx().emit_err(
814-
errors::AssociatedItemTraitUninferredGenericParams {
815-
span,
816-
inferred_sugg: Some(span.with_hi(item_segment.ident.span.lo())),
817-
bound: format!("{}::", tcx.anonymize_bound_vars(bound).skip_binder(),),
818-
mpart_sugg: None,
819-
what: "function",
820-
},
821-
));
754+
match self.lower_return_type_notation_ty(bound, item_def_id, hir_ty.span) {
755+
Ok(ty) => Ty::new_alias(tcx, ty::Projection, ty),
756+
Err(guar) => Ty::new_error(tcx, guar),
757+
}
758+
}
759+
_ => self.lower_ty(hir_ty),
822760
}
823-
824-
let trait_def_id = bound.def_id();
825-
let assoc_ty = self
826-
.probe_assoc_item(assoc_ident, ty::AssocTag::Fn, qpath_hir_id, span, trait_def_id)
827-
.expect("failed to find associated type");
828-
829-
Ok((bound, assoc_ty.def_id))
830761
}
831762

832763
/// Do the common parts of lowering an RTN type. This involves extending the

compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn validate_cmse_abi<'tcx>(
3535
_ => tcx.hir_span(hir_id),
3636
};
3737
struct_span_code_err!(
38-
tcx.dcx(),
38+
dcx,
3939
span,
4040
E0781,
4141
"the `\"C-cmse-nonsecure-call\"` ABI is only allowed on function pointers"

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
7878

7979
// We don't support empty trait objects.
8080
if regular_traits.is_empty() && auto_traits.is_empty() {
81-
let guar = self.report_trait_object_with_no_traits_error(
82-
span,
83-
user_written_bounds.iter().copied(),
84-
);
81+
let guar =
82+
self.report_trait_object_with_no_traits(span, user_written_bounds.iter().copied());
8583
return Ty::new_error(tcx, guar);
8684
}
8785
// We don't support >1 principal
8886
if regular_traits.len() > 1 {
89-
let guar = self.report_trait_object_addition_traits_error(&regular_traits);
87+
let guar = self.report_trait_object_addition_traits(&regular_traits);
9088
return Ty::new_error(tcx, guar);
9189
}
9290
// Don't create a dyn trait if we have errors in the principal.
@@ -132,7 +130,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
132130
if references_self {
133131
// With trait alias and type alias combined, type resolver
134132
// may not be able to catch all illegal `Self` usages (issue 139082)
135-
let guar = tcx.dcx().emit_err(SelfInTypeAlias { span });
133+
let guar = self.dcx().emit_err(SelfInTypeAlias { span });
136134
b.term = replace_dummy_self_with_error(tcx, b.term, guar);
137135
}
138136
}
@@ -360,7 +358,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
360358
hir_bound.trait_ref.path.res == Res::Def(DefKind::Trait, trait_ref.def_id)
361359
&& hir_bound.span.contains(span)
362360
});
363-
self.complain_about_missing_type_params(
361+
self.report_missing_type_params(
364362
missing_type_params,
365363
trait_ref.def_id,
366364
span,

0 commit comments

Comments
 (0)