From bcab59ed8341a585e7cac5475bc2a41e1f96defa Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Mon, 6 Jan 2020 05:32:17 +0100 Subject: [PATCH] lowering: simplify HoFs --- src/librustc_ast_lowering/item.rs | 7 ++-- src/librustc_ast_lowering/lib.rs | 60 +++++++++++++------------------ 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/src/librustc_ast_lowering/item.rs b/src/librustc_ast_lowering/item.rs index a5892a22d9d..7aef4b11ae3 100644 --- a/src/librustc_ast_lowering/item.rs +++ b/src/librustc_ast_lowering/item.rs @@ -26,12 +26,9 @@ pub(super) struct ItemLowerer<'a, 'lowering, 'hir> { } impl<'a, 'lowering, 'hir> ItemLowerer<'a, 'lowering, 'hir> { - fn with_trait_impl_ref(&mut self, trait_impl_ref: &Option, f: F) - where - F: FnOnce(&mut Self), - { + fn with_trait_impl_ref(&mut self, impl_ref: &Option, f: impl FnOnce(&mut Self)) { let old = self.lctx.is_in_trait_impl; - self.lctx.is_in_trait_impl = if let &None = trait_impl_ref { false } else { true }; + self.lctx.is_in_trait_impl = if let &None = impl_ref { false } else { true }; f(self); self.lctx.is_in_trait_impl = old; } diff --git a/src/librustc_ast_lowering/lib.rs b/src/librustc_ast_lowering/lib.rs index 385153b62ce..5b68018683c 100644 --- a/src/librustc_ast_lowering/lib.rs +++ b/src/librustc_ast_lowering/lib.rs @@ -433,10 +433,11 @@ fn allocate_use_tree_hir_id_counters(&mut self, tree: &UseTree, owner: DefIndex) } } - fn with_hir_id_owner(&mut self, owner: Option, f: F) -> T - where - F: FnOnce(&mut Self) -> T, - { + fn with_hir_id_owner( + &mut self, + owner: Option, + f: impl FnOnce(&mut Self) -> T, + ) -> T { let old = mem::replace(&mut self.hir_id_owner, owner); let r = f(self); self.hir_id_owner = old; @@ -577,10 +578,11 @@ fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId { lowered } - fn lower_node_id_generic(&mut self, ast_node_id: NodeId, alloc_hir_id: F) -> hir::HirId - where - F: FnOnce(&mut Self) -> hir::HirId, - { + fn lower_node_id_generic( + &mut self, + ast_node_id: NodeId, + alloc_hir_id: impl FnOnce(&mut Self) -> hir::HirId, + ) -> hir::HirId { if ast_node_id == DUMMY_NODE_ID { return hir::DUMMY_HIR_ID; } @@ -604,10 +606,7 @@ fn lower_node_id_generic(&mut self, ast_node_id: NodeId, alloc_hir_id: F) -> } } - fn with_hir_id_owner(&mut self, owner: NodeId, f: F) -> T - where - F: FnOnce(&mut Self) -> T, - { + fn with_hir_id_owner(&mut self, owner: NodeId, f: impl FnOnce(&mut Self) -> T) -> T { let counter = self .item_local_id_counters .insert(owner, HIR_ID_COUNTER_LOCKED) @@ -736,15 +735,12 @@ fn with_anonymous_lifetime_mode( /// Presuming that in-band lifetimes are enabled, then /// `self.anonymous_lifetime_mode` will be updated to match the /// parameter while `f` is running (and restored afterwards). - fn collect_in_band_defs( + fn collect_in_band_defs( &mut self, parent_id: DefId, anonymous_lifetime_mode: AnonymousLifetimeMode, - f: F, - ) -> (Vec>, T) - where - F: FnOnce(&mut Self) -> (Vec>, T), - { + f: impl FnOnce(&mut Self) -> (Vec>, T), + ) -> (Vec>, T) { assert!(!self.is_collecting_in_band_lifetimes); assert!(self.lifetimes_to_define.is_empty()); let old_anonymous_lifetime_mode = self.anonymous_lifetime_mode; @@ -847,10 +843,11 @@ fn collect_fresh_in_band_lifetime(&mut self, span: Span) -> ParamName { // This is used to track which lifetimes have already been defined, and // which are new in-band lifetimes that need to have a definition created // for them. - fn with_in_scope_lifetime_defs(&mut self, params: &[GenericParam], f: F) -> T - where - F: FnOnce(&mut Self) -> T, - { + fn with_in_scope_lifetime_defs( + &mut self, + params: &[GenericParam], + f: impl FnOnce(&mut Self) -> T, + ) -> T { let old_len = self.in_scope_lifetimes.len(); let lt_def_names = params.iter().filter_map(|param| match param.kind { GenericParamKind::Lifetime { .. } => Some(ParamName::Plain(param.ident.modern())), @@ -870,16 +867,13 @@ fn with_in_scope_lifetime_defs(&mut self, params: &[GenericParam], f: F) - /// Presuming that in-band lifetimes are enabled, then /// `self.anonymous_lifetime_mode` will be updated to match the /// parameter while `f` is running (and restored afterwards). - fn add_in_band_defs( + fn add_in_band_defs( &mut self, generics: &Generics, parent_id: DefId, anonymous_lifetime_mode: AnonymousLifetimeMode, - f: F, - ) -> (hir::Generics<'hir>, T) - where - F: FnOnce(&mut Self, &mut Vec>) -> T, - { + f: impl FnOnce(&mut Self, &mut Vec>) -> T, + ) -> (hir::Generics<'hir>, T) { let (in_band_defs, (mut lowered_generics, res)) = self.with_in_scope_lifetime_defs(&generics.params, |this| { this.collect_in_band_defs(parent_id, anonymous_lifetime_mode, |this| { @@ -917,10 +911,7 @@ fn add_in_band_defs( (lowered_generics, res) } - fn with_dyn_type_scope(&mut self, in_scope: bool, f: F) -> T - where - F: FnOnce(&mut Self) -> T, - { + fn with_dyn_type_scope(&mut self, in_scope: bool, f: impl FnOnce(&mut Self) -> T) -> T { let was_in_dyn_type = self.is_in_dyn_type; self.is_in_dyn_type = in_scope; @@ -931,10 +922,7 @@ fn with_dyn_type_scope(&mut self, in_scope: bool, f: F) -> T result } - fn with_new_scopes(&mut self, f: F) -> T - where - F: FnOnce(&mut Self) -> T, - { + fn with_new_scopes(&mut self, f: impl FnOnce(&mut Self) -> T) -> T { let was_in_loop_condition = self.is_in_loop_condition; self.is_in_loop_condition = false;