Skip to content

Commit ab95f53

Browse files
committed
Auto merge of #158556 - aerooneqq:delegation-child-segment-perf, r=<try>
delegation: store child segment flag in `PathSegment`
2 parents 63f05e3 + 2bee069 commit ab95f53

11 files changed

Lines changed: 36 additions & 14 deletions

File tree

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ use rustc_span::def_id::{DefId, LocalDefId};
5555
use rustc_span::symbol::kw;
5656
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
5757

58-
use crate::delegation::generics::{GenericsGenerationResult, GenericsGenerationResults};
58+
use crate::delegation::generics::{
59+
GenericsGenerationResult, GenericsGenerationResults, GenericsPosition,
60+
};
5961
use crate::diagnostics::{
6062
CycleInDelegationSignatureResolution, DelegationAttemptedBlockWithDefsDeletion,
6163
DelegationBlockSpecifiedWhenNoParams, UnresolvedDelegationCallee,
@@ -505,6 +507,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
505507
res: Res::Local(param_id),
506508
args: None,
507509
infer_args: false,
510+
delegation_child_segment: false,
508511
}));
509512

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

720+
segment.delegation_child_segment = result.generics.pos() == GenericsPosition::Child;
721+
717722
segment
718723
}
719724

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_span::{Ident, Span, sym};
1212
use crate::LoweringContext;
1313
use crate::diagnostics::DelegationInfersMismatch;
1414

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

161161
*self = HirOrTyGenerics::Hir(DelegationGenerics {
@@ -218,6 +218,13 @@ impl<'hir> HirOrTyGenerics<'hir> {
218218
.expect("`Self` generic param is not found while expected"),
219219
}
220220
}
221+
222+
pub(crate) fn pos(&self) -> GenericsPosition {
223+
match self {
224+
HirOrTyGenerics::Ty(ty) => ty.pos,
225+
HirOrTyGenerics::Hir(hir) => hir.pos,
226+
}
227+
}
221228
}
222229

223230
impl<'hir> GenericsGenerationResult<'hir> {
@@ -590,6 +597,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
590597
ident: p.name.ident(),
591598
infer_args: false,
592599
res,
600+
delegation_child_segment: false,
593601
}]),
594602
res,
595603
span: p.span,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10141014
res,
10151015
args,
10161016
infer_args: args.is_none(),
1017+
delegation_child_segment: false,
10171018
}]),
10181019
})
10191020
}

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
412412
} else {
413413
Some(generic_args.into_generic_args(self))
414414
},
415+
delegation_child_segment: false,
415416
}
416417
}
417418

compiler/rustc_hir/src/hir.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,24 @@ pub struct PathSegment<'hir> {
387387
/// out of those only the segments with no type parameters
388388
/// to begin with, e.g., `Vec::new` is `<Vec<..>>::new::<..>`.
389389
pub infer_args: bool,
390+
391+
/// Whether this segment is a delegation's child segment:
392+
/// `reuse Trait::foo`, in this case `foo` is a delegation's child segment.
393+
/// Used for faster check during generic args lowering.
394+
pub delegation_child_segment: bool,
390395
}
391396

392397
impl<'hir> PathSegment<'hir> {
393398
/// Converts an identifier to the corresponding segment.
394399
pub fn new(ident: Ident, hir_id: HirId, res: Res) -> PathSegment<'hir> {
395-
PathSegment { ident, hir_id, res, infer_args: true, args: None }
400+
PathSegment {
401+
ident,
402+
hir_id,
403+
res,
404+
infer_args: true,
405+
args: None,
406+
delegation_child_segment: false,
407+
}
396408
}
397409

398410
pub fn invalid() -> Self {

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ pub fn walk_path_segment<'v, V: Visitor<'v>>(
14681468
visitor: &mut V,
14691469
segment: &'v PathSegment<'v>,
14701470
) -> V::Result {
1471-
let PathSegment { ident, hir_id, res: _, args, infer_args: _ } = segment;
1471+
let PathSegment { ident, hir_id, args, .. } = segment;
14721472
try_visit!(visitor.visit_ident(*ident));
14731473
try_visit!(visitor.visit_id(*hir_id));
14741474
visit_opt!(visitor, visit_generic_args, *args);

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
467467
res: Res::Err,
468468
args: Some(constraint.gen_args),
469469
infer_args: false,
470+
delegation_child_segment: false,
470471
};
471472

472473
let alias_args = self.lower_generic_args_of_assoc_item(

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
471471
res: Res::Err,
472472
args: Some(constraint.gen_args),
473473
infer_args: false,
474+
delegation_child_segment: false,
474475
};
475476

476477
let alias_args = self.lower_generic_args_of_assoc_item(

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ pub(crate) fn check_generic_arg_count(
434434

435435
// Suppress this warning for delegations as it is compiler generated and lifetimes are
436436
// propagated while late-bound lifetimes may be present.
437-
let explicit_late_bound = match tcx.hir_is_delegation_child_segment(seg) {
437+
let explicit_late_bound = match seg.delegation_child_segment {
438438
true => ExplicitLateBound::No,
439439
false => prohibit_explicit_late_bound_lifetimes(cx, gen_params, gen_args, gen_pos),
440440
};

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
870870
span,
871871
generic_args: segment.args(),
872872
infer_args: segment.infer_args,
873-
create_synth_args: tcx.hir_is_delegation_child_segment(segment),
873+
create_synth_args: segment.delegation_child_segment,
874874
incorrect_args: &arg_count.correct,
875875
};
876876

0 commit comments

Comments
 (0)