Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::symbol::kw;
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};

use crate::delegation::generics::{GenericsGenerationResult, GenericsGenerationResults};
use crate::delegation::generics::{
GenericsGenerationResult, GenericsGenerationResults, GenericsPosition,
};
use crate::diagnostics::{
CycleInDelegationSignatureResolution, DelegationAttemptedBlockWithDefsDeletion,
DelegationBlockSpecifiedWhenNoParams, UnresolvedDelegationCallee,
Expand Down Expand Up @@ -505,6 +507,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
res: Res::Local(param_id),
args: None,
infer_args: false,
delegation_child_segment: false,
}));

let path = self.arena.alloc(hir::Path { span, res: Res::Local(param_id), segments });
Expand Down Expand Up @@ -714,6 +717,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
result.args_segment_id = segment.hir_id;
result.use_for_sig_inheritance = !result.generics.is_trait_impl();

segment.delegation_child_segment = result.generics.pos() == GenericsPosition::Child;

segment
}

Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_ast_lowering/src/delegation/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_span::{Ident, Span, sym};
use crate::LoweringContext;
use crate::diagnostics::DelegationInfersMismatch;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub(super) enum GenericsPosition {
Parent,
Child,
Expand Down Expand Up @@ -155,7 +155,7 @@ impl<'hir> DelegationGenericArgsIterator<'hir> {
impl<'hir> HirOrTyGenerics<'hir> {
pub(super) fn into_hir_generics(&mut self, ctx: &mut LoweringContext<'_, 'hir>, span: Span) {
if let HirOrTyGenerics::Ty(ty) = self {
let rename_self = matches!(ty.pos, GenericsPosition::Child);
let rename_self = ty.pos == GenericsPosition::Child;
let params = ctx.uplift_delegation_generic_params(span, &ty.data, rename_self);

*self = HirOrTyGenerics::Hir(DelegationGenerics {
Expand Down Expand Up @@ -218,6 +218,13 @@ impl<'hir> HirOrTyGenerics<'hir> {
.expect("`Self` generic param is not found while expected"),
}
}

pub(crate) fn pos(&self) -> GenericsPosition {
match self {
HirOrTyGenerics::Ty(ty) => ty.pos,
HirOrTyGenerics::Hir(hir) => hir.pos,
}
}
}

impl<'hir> GenericsGenerationResult<'hir> {
Expand Down Expand Up @@ -590,6 +597,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ident: p.name.ident(),
infer_args: false,
res,
delegation_child_segment: false,
}]),
res,
span: p.span,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
res,
args,
infer_args: args.is_none(),
delegation_child_segment: false,
}]),
})
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_lowering/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
} else {
Some(generic_args.into_generic_args(self))
},
delegation_child_segment: false,
}
}

Expand Down
14 changes: 13 additions & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,24 @@ pub struct PathSegment<'hir> {
/// out of those only the segments with no type parameters
/// to begin with, e.g., `Vec::new` is `<Vec<..>>::new::<..>`.
pub infer_args: bool,

/// Whether this segment is a delegation's child segment:
/// `reuse Trait::foo`, in this case `foo` is a delegation's child segment.
/// Used for faster check during generic args lowering.
pub delegation_child_segment: bool,

@petrochenkov petrochenkov Jun 29, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: the PathSegment's size doesn't change, there's a corresponding assert at the bottom of the file.

View changes since the review

}

impl<'hir> PathSegment<'hir> {
/// Converts an identifier to the corresponding segment.
pub fn new(ident: Ident, hir_id: HirId, res: Res) -> PathSegment<'hir> {
PathSegment { ident, hir_id, res, infer_args: true, args: None }
PathSegment {
ident,
hir_id,
res,
infer_args: true,
args: None,
delegation_child_segment: false,
}
}

pub fn invalid() -> Self {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,8 @@ pub fn walk_path_segment<'v, V: Visitor<'v>>(
visitor: &mut V,
segment: &'v PathSegment<'v>,
) -> V::Result {
let PathSegment { ident, hir_id, res: _, args, infer_args: _ } = segment;
let PathSegment { ident, hir_id, res: _, args, infer_args: _, delegation_child_segment: _ } =
segment;
try_visit!(visitor.visit_ident(*ident));
try_visit!(visitor.visit_id(*hir_id));
visit_opt!(visitor, visit_generic_args, *args);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
res: Res::Err,
args: Some(constraint.gen_args),
infer_args: false,
delegation_child_segment: false,
};

let alias_args = self.lower_generic_args_of_assoc_item(
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
res: Res::Err,
args: Some(constraint.gen_args),
infer_args: false,
delegation_child_segment: false,
};

let alias_args = self.lower_generic_args_of_assoc_item(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ pub(crate) fn check_generic_arg_count(

// Suppress this warning for delegations as it is compiler generated and lifetimes are
// propagated while late-bound lifetimes may be present.
let explicit_late_bound = match tcx.hir_is_delegation_child_segment(seg) {
let explicit_late_bound = match seg.delegation_child_segment {
true => ExplicitLateBound::No,
false => prohibit_explicit_late_bound_lifetimes(cx, gen_params, gen_args, gen_pos),
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
span,
generic_args: segment.args(),
infer_args: segment.infer_args,
create_synth_args: tcx.hir_is_delegation_child_segment(segment),
create_synth_args: segment.delegation_child_segment,
incorrect_args: &arg_count.correct,
};

Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_middle/src/hir/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,13 +879,6 @@ impl<'tcx> TyCtxt<'tcx> {
self.hir_opt_delegation_info(delegation_id).expect("processing delegation")
}

pub fn hir_is_delegation_child_segment(self, segment: &PathSegment<'_>) -> bool {
let parent_def = self.hir_get_parent_item(segment.hir_id).def_id;

self.hir_opt_delegation_info(parent_def)
.is_some_and(|info| info.child_seg_id == segment.hir_id)
}

#[inline]
fn hir_opt_ident(self, id: HirId) -> Option<Ident> {
match self.hir_node(id) {
Expand Down
Loading