Skip to content

Commit bcab59e

Browse files
committed
lowering: simplify HoFs
1 parent adc6572 commit bcab59e

File tree

2 files changed

+26
-41
lines changed

2 files changed

+26
-41
lines changed

src/librustc_ast_lowering/item.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
2626
}
2727

2828
impl<'a, 'lowering, 'hir> ItemLowerer<'a, 'lowering, 'hir> {
29-
fn with_trait_impl_ref<F>(&mut self, trait_impl_ref: &Option<TraitRef>, f: F)
30-
where
31-
F: FnOnce(&mut Self),
32-
{
29+
fn with_trait_impl_ref(&mut self, impl_ref: &Option<TraitRef>, f: impl FnOnce(&mut Self)) {
3330
let old = self.lctx.is_in_trait_impl;
34-
self.lctx.is_in_trait_impl = if let &None = trait_impl_ref { false } else { true };
31+
self.lctx.is_in_trait_impl = if let &None = impl_ref { false } else { true };
3532
f(self);
3633
self.lctx.is_in_trait_impl = old;
3734
}

src/librustc_ast_lowering/lib.rs

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
433433
}
434434
}
435435

436-
fn with_hir_id_owner<F, T>(&mut self, owner: Option<NodeId>, f: F) -> T
437-
where
438-
F: FnOnce(&mut Self) -> T,
439-
{
436+
fn with_hir_id_owner<T>(
437+
&mut self,
438+
owner: Option<NodeId>,
439+
f: impl FnOnce(&mut Self) -> T,
440+
) -> T {
440441
let old = mem::replace(&mut self.hir_id_owner, owner);
441442
let r = f(self);
442443
self.hir_id_owner = old;
@@ -577,10 +578,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
577578
lowered
578579
}
579580

580-
fn lower_node_id_generic<F>(&mut self, ast_node_id: NodeId, alloc_hir_id: F) -> hir::HirId
581-
where
582-
F: FnOnce(&mut Self) -> hir::HirId,
583-
{
581+
fn lower_node_id_generic(
582+
&mut self,
583+
ast_node_id: NodeId,
584+
alloc_hir_id: impl FnOnce(&mut Self) -> hir::HirId,
585+
) -> hir::HirId {
584586
if ast_node_id == DUMMY_NODE_ID {
585587
return hir::DUMMY_HIR_ID;
586588
}
@@ -604,10 +606,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
604606
}
605607
}
606608

607-
fn with_hir_id_owner<F, T>(&mut self, owner: NodeId, f: F) -> T
608-
where
609-
F: FnOnce(&mut Self) -> T,
610-
{
609+
fn with_hir_id_owner<T>(&mut self, owner: NodeId, f: impl FnOnce(&mut Self) -> T) -> T {
611610
let counter = self
612611
.item_local_id_counters
613612
.insert(owner, HIR_ID_COUNTER_LOCKED)
@@ -736,15 +735,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
736735
/// Presuming that in-band lifetimes are enabled, then
737736
/// `self.anonymous_lifetime_mode` will be updated to match the
738737
/// parameter while `f` is running (and restored afterwards).
739-
fn collect_in_band_defs<T, F>(
738+
fn collect_in_band_defs<T>(
740739
&mut self,
741740
parent_id: DefId,
742741
anonymous_lifetime_mode: AnonymousLifetimeMode,
743-
f: F,
744-
) -> (Vec<hir::GenericParam<'hir>>, T)
745-
where
746-
F: FnOnce(&mut Self) -> (Vec<hir::GenericParam<'hir>>, T),
747-
{
742+
f: impl FnOnce(&mut Self) -> (Vec<hir::GenericParam<'hir>>, T),
743+
) -> (Vec<hir::GenericParam<'hir>>, T) {
748744
assert!(!self.is_collecting_in_band_lifetimes);
749745
assert!(self.lifetimes_to_define.is_empty());
750746
let old_anonymous_lifetime_mode = self.anonymous_lifetime_mode;
@@ -847,10 +843,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
847843
// This is used to track which lifetimes have already been defined, and
848844
// which are new in-band lifetimes that need to have a definition created
849845
// for them.
850-
fn with_in_scope_lifetime_defs<T, F>(&mut self, params: &[GenericParam], f: F) -> T
851-
where
852-
F: FnOnce(&mut Self) -> T,
853-
{
846+
fn with_in_scope_lifetime_defs<T>(
847+
&mut self,
848+
params: &[GenericParam],
849+
f: impl FnOnce(&mut Self) -> T,
850+
) -> T {
854851
let old_len = self.in_scope_lifetimes.len();
855852
let lt_def_names = params.iter().filter_map(|param| match param.kind {
856853
GenericParamKind::Lifetime { .. } => Some(ParamName::Plain(param.ident.modern())),
@@ -870,16 +867,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
870867
/// Presuming that in-band lifetimes are enabled, then
871868
/// `self.anonymous_lifetime_mode` will be updated to match the
872869
/// parameter while `f` is running (and restored afterwards).
873-
fn add_in_band_defs<F, T>(
870+
fn add_in_band_defs<T>(
874871
&mut self,
875872
generics: &Generics,
876873
parent_id: DefId,
877874
anonymous_lifetime_mode: AnonymousLifetimeMode,
878-
f: F,
879-
) -> (hir::Generics<'hir>, T)
880-
where
881-
F: FnOnce(&mut Self, &mut Vec<hir::GenericParam<'hir>>) -> T,
882-
{
875+
f: impl FnOnce(&mut Self, &mut Vec<hir::GenericParam<'hir>>) -> T,
876+
) -> (hir::Generics<'hir>, T) {
883877
let (in_band_defs, (mut lowered_generics, res)) =
884878
self.with_in_scope_lifetime_defs(&generics.params, |this| {
885879
this.collect_in_band_defs(parent_id, anonymous_lifetime_mode, |this| {
@@ -917,10 +911,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
917911
(lowered_generics, res)
918912
}
919913

920-
fn with_dyn_type_scope<T, F>(&mut self, in_scope: bool, f: F) -> T
921-
where
922-
F: FnOnce(&mut Self) -> T,
923-
{
914+
fn with_dyn_type_scope<T>(&mut self, in_scope: bool, f: impl FnOnce(&mut Self) -> T) -> T {
924915
let was_in_dyn_type = self.is_in_dyn_type;
925916
self.is_in_dyn_type = in_scope;
926917

@@ -931,10 +922,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
931922
result
932923
}
933924

934-
fn with_new_scopes<T, F>(&mut self, f: F) -> T
935-
where
936-
F: FnOnce(&mut Self) -> T,
937-
{
925+
fn with_new_scopes<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
938926
let was_in_loop_condition = self.is_in_loop_condition;
939927
self.is_in_loop_condition = false;
940928

0 commit comments

Comments
 (0)