Skip to content

Commit 1da6864

Browse files
committed
Remove not needed anymore NonAllocatedGenericArg, some cleanups
1 parent e302996 commit 1da6864

2 files changed

Lines changed: 21 additions & 47 deletions

File tree

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -692,48 +692,42 @@ impl<'hir> LoweringContext<'_, 'hir> {
692692
let mut segment = segment.clone();
693693
let mut generated_args_iterator = result.generics.create_args_iterator();
694694

695-
let new_args = match segment.args {
696-
Some(args) if !args.is_empty() => {
695+
let new_args = segment
696+
.args
697+
.filter(|args| !args.is_empty())
698+
.map(|args| {
697699
let mut new_args = vec![];
698700

699701
for (idx, arg) in args.args.iter().enumerate() {
700702
if infer_indices.contains(&idx) {
701-
let non_alloc_arg = generated_args_iterator
703+
let arg = generated_args_iterator
702704
.next(self, |_| arg.hir_id())
703705
.expect("there should be one param for each infer");
704706

705-
new_args.push(non_alloc_arg.allocate_hir_generic_arg(self.arena));
707+
new_args.push(arg);
706708
} else {
707709
new_args.push(*arg);
708710
}
709711
}
710712

711713
self.arena.alloc_from_iter(new_args.into_iter())
712-
}
713-
_ => self.arena.alloc_from_iter(
714-
generated_args_iterator
715-
.consume_all(self)
716-
.into_iter()
717-
.map(|non_alloc_arg| non_alloc_arg.allocate_hir_generic_arg(self.arena)),
718-
),
719-
};
714+
})
715+
.unwrap_or_else(|| {
716+
self.arena.alloc_from_iter(generated_args_iterator.consume_all(self).into_iter())
717+
});
720718

721719
// Needed for better error messages (`trait-impl-wrong-args-count.rs` test).
722-
segment.args = if new_args.is_empty() {
723-
None
724-
} else {
725-
let new_args = self.arena.alloc(hir::GenericArgs {
720+
segment.args = (!new_args.is_empty()).then(|| {
721+
&*self.arena.alloc(hir::GenericArgs {
726722
args: new_args,
727723
constraints: &[],
728724
parenthesized: hir::GenericArgsParentheses::No,
729725
span_ext: match segment.args {
730726
Some(args) => args.span_ext,
731727
None => span,
732728
},
733-
});
734-
735-
Some(&*new_args)
736-
};
729+
})
730+
});
737731

738732
result.args_segment_id = segment.hir_id;
739733
result.use_for_sig_inheritance = !result.generics.is_trait_impl();

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,6 @@ pub(super) struct GenericsGenerationResults<'hir> {
8989
pub(super) self_ty_propagation_kind: Option<hir::DelegationSelfTyPropagationKind>,
9090
}
9191

92-
#[derive(Debug)]
93-
pub(super) enum NonAllocatedGenericArg<'hir> {
94-
Lifetime(hir::Lifetime),
95-
Type(hir::Ty<'hir, hir::AmbigArg>),
96-
Const(hir::ConstArg<'hir, hir::AmbigArg>),
97-
}
98-
99-
impl<'hir> NonAllocatedGenericArg<'hir> {
100-
pub(super) fn allocate_hir_generic_arg(
101-
self,
102-
arena: &'hir hir::Arena<'hir>,
103-
) -> hir::GenericArg<'hir> {
104-
match self {
105-
NonAllocatedGenericArg::Lifetime(lt) => hir::GenericArg::Lifetime(arena.alloc(lt)),
106-
NonAllocatedGenericArg::Type(ty) => hir::GenericArg::Type(arena.alloc(ty)),
107-
NonAllocatedGenericArg::Const(c) => hir::GenericArg::Const(arena.alloc(c)),
108-
}
109-
}
110-
}
111-
11292
pub(super) struct DelegationGenericArgsIterator<'hir> {
11393
index: usize = Default::default(),
11494
params: &'hir [hir::GenericParam<'hir>],
@@ -125,7 +105,7 @@ impl<'hir> DelegationGenericArgsIterator<'hir> {
125105
&mut self,
126106
ctx: &mut LoweringContext<'_, 'hir>,
127107
hir_id_factory: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> HirId,
128-
) -> Option<NonAllocatedGenericArg<'hir>> {
108+
) -> Option<hir::GenericArg<'hir>> {
129109
let p = loop {
130110
if self.index >= self.params.len() {
131111
return None;
@@ -144,35 +124,35 @@ impl<'hir> DelegationGenericArgsIterator<'hir> {
144124

145125
let hir_id = hir_id_factory(ctx);
146126

147-
match p.kind {
127+
Some(match p.kind {
148128
hir::GenericParamKind::Lifetime { .. } => {
149-
Some(NonAllocatedGenericArg::Lifetime(hir::Lifetime {
129+
hir::GenericArg::Lifetime(ctx.arena.alloc(hir::Lifetime {
150130
hir_id,
151131
ident: p.name.ident(),
152132
kind: hir::LifetimeKind::Param(p.def_id),
153133
source: hir::LifetimeSource::Path { angle_brackets: hir::AngleBrackets::Full },
154134
syntax: hir::LifetimeSyntax::ExplicitBound,
155135
}))
156136
}
157-
hir::GenericParamKind::Type { .. } => Some(NonAllocatedGenericArg::Type(hir::Ty {
137+
hir::GenericParamKind::Type { .. } => hir::GenericArg::Type(ctx.arena.alloc(hir::Ty {
158138
hir_id,
159139
span: p.span,
160140
kind: hir::TyKind::Path(Self::create_generic_arg_path(ctx, &p)),
161141
})),
162142
hir::GenericParamKind::Const { .. } => {
163-
Some(NonAllocatedGenericArg::Const(hir::ConstArg {
143+
hir::GenericArg::Const(ctx.arena.alloc(hir::ConstArg {
164144
hir_id,
165145
kind: hir::ConstArgKind::Path(Self::create_generic_arg_path(ctx, &p)),
166146
span: p.span,
167147
}))
168148
}
169-
}
149+
})
170150
}
171151

172152
pub(super) fn consume_all(
173153
mut self,
174154
ctx: &mut LoweringContext<'_, 'hir>,
175-
) -> Vec<NonAllocatedGenericArg<'hir>> {
155+
) -> Vec<hir::GenericArg<'hir>> {
176156
let mut args = vec![];
177157
while let Some(arg) = self.next(ctx, |ctx| ctx.next_id()) {
178158
args.push(arg);

0 commit comments

Comments
 (0)