diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 591ceaf28a6c6..fe69c9e634635 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -32,6 +32,9 @@ //! get confused if the spans from leaf AST nodes occur in multiple places //! in the HIR, especially for multiple identifiers. +mod expr; +mod item; + use crate::dep_graph::DepGraph; use crate::hir::{self, ParamName}; use crate::hir::HirVec; @@ -53,7 +56,7 @@ use rustc_data_structures::indexed_vec::IndexVec; use rustc_data_structures::thin_vec::ThinVec; use rustc_data_structures::sync::Lrc; -use std::collections::{BTreeSet, BTreeMap}; +use std::collections::BTreeMap; use std::mem; use smallvec::SmallVec; use syntax::attr; @@ -561,90 +564,11 @@ impl<'a> LoweringContext<'a> { } } - struct ItemLowerer<'tcx, 'interner> { - lctx: &'tcx mut LoweringContext<'interner>, - } - - impl<'tcx, 'interner> ItemLowerer<'tcx, 'interner> { - fn with_trait_impl_ref(&mut self, trait_impl_ref: &Option, f: F) - where - F: 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 - }; - f(self); - self.lctx.is_in_trait_impl = old; - } - } - - impl<'tcx, 'interner> Visitor<'tcx> for ItemLowerer<'tcx, 'interner> { - fn visit_mod(&mut self, m: &'tcx Mod, _s: Span, _attrs: &[Attribute], n: NodeId) { - self.lctx.modules.insert(n, hir::ModuleItems { - items: BTreeSet::new(), - trait_items: BTreeSet::new(), - impl_items: BTreeSet::new(), - }); - - let old = self.lctx.current_module; - self.lctx.current_module = n; - visit::walk_mod(self, m); - self.lctx.current_module = old; - } - - fn visit_item(&mut self, item: &'tcx Item) { - let mut item_hir_id = None; - self.lctx.with_hir_id_owner(item.id, |lctx| { - if let Some(hir_item) = lctx.lower_item(item) { - item_hir_id = Some(hir_item.hir_id); - lctx.insert_item(hir_item); - } - }); - - if let Some(hir_id) = item_hir_id { - self.lctx.with_parent_item_lifetime_defs(hir_id, |this| { - let this = &mut ItemLowerer { lctx: this }; - if let ItemKind::Impl(.., ref opt_trait_ref, _, _) = item.node { - this.with_trait_impl_ref(opt_trait_ref, |this| { - visit::walk_item(this, item) - }); - } else { - visit::walk_item(this, item); - } - }); - } - } - - fn visit_trait_item(&mut self, item: &'tcx TraitItem) { - self.lctx.with_hir_id_owner(item.id, |lctx| { - let hir_item = lctx.lower_trait_item(item); - let id = hir::TraitItemId { hir_id: hir_item.hir_id }; - lctx.trait_items.insert(id, hir_item); - lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id); - }); - - visit::walk_trait_item(self, item); - } - - fn visit_impl_item(&mut self, item: &'tcx ImplItem) { - self.lctx.with_hir_id_owner(item.id, |lctx| { - let hir_item = lctx.lower_impl_item(item); - let id = hir::ImplItemId { hir_id: hir_item.hir_id }; - lctx.impl_items.insert(id, hir_item); - lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id); - }); - visit::walk_impl_item(self, item); - } - } - self.lower_node_id(CRATE_NODE_ID); debug_assert!(self.node_id_to_hir_id[CRATE_NODE_ID] == hir::CRATE_HIR_ID); visit::walk_crate(&mut MiscCollector { lctx: &mut self, hir_id_owner: None }, c); - visit::walk_crate(&mut ItemLowerer { lctx: &mut self }, c); + visit::walk_crate(&mut item::ItemLowerer { lctx: &mut self }, c); let module = self.lower_mod(&c.module); let attrs = self.lower_attrs(&c.attrs); @@ -783,57 +707,6 @@ impl<'a> LoweringContext<'a> { }) } - fn generator_movability_for_fn( - &mut self, - decl: &ast::FnDecl, - fn_decl_span: Span, - generator_kind: Option, - movability: Movability, - ) -> Option { - match generator_kind { - Some(hir::GeneratorKind::Gen) => { - if !decl.inputs.is_empty() { - span_err!( - self.sess, - fn_decl_span, - E0628, - "generators cannot have explicit arguments" - ); - self.sess.abort_if_errors(); - } - Some(match movability { - Movability::Movable => hir::GeneratorMovability::Movable, - Movability::Static => hir::GeneratorMovability::Static, - }) - }, - Some(hir::GeneratorKind::Async) => { - bug!("non-`async` closure body turned `async` during lowering"); - }, - None => { - if movability == Movability::Static { - span_err!( - self.sess, - fn_decl_span, - E0697, - "closures cannot be static" - ); - } - None - }, - } - } - - fn record_body(&mut self, arguments: HirVec, value: hir::Expr) -> hir::BodyId { - let body = hir::Body { - generator_kind: self.generator_kind, - arguments, - value, - }; - let id = body.id(); - self.bodies.insert(id, body); - id - } - fn next_id(&mut self) -> hir::HirId { self.lower_node_id(self.sess.next_node_id()) } @@ -1037,38 +910,6 @@ impl<'a> LoweringContext<'a> { res } - // Same as the method above, but accepts `hir::GenericParam`s - // instead of `ast::GenericParam`s. - // This should only be used with generics that have already had their - // in-band lifetimes added. In practice, this means that this function is - // only used when lowering a child item of a trait or impl. - fn with_parent_item_lifetime_defs(&mut self, - parent_hir_id: hir::HirId, - f: F - ) -> T where - F: FnOnce(&mut LoweringContext<'_>) -> T, - { - let old_len = self.in_scope_lifetimes.len(); - - let parent_generics = match self.items.get(&parent_hir_id).unwrap().node { - hir::ItemKind::Impl(_, _, _, ref generics, ..) - | hir::ItemKind::Trait(_, _, ref generics, ..) => { - &generics.params[..] - } - _ => &[], - }; - let lt_def_names = parent_generics.iter().filter_map(|param| match param.kind { - hir::GenericParamKind::Lifetime { .. } => Some(param.name.ident().modern()), - _ => None, - }); - self.in_scope_lifetimes.extend(lt_def_names); - - let res = f(self); - - self.in_scope_lifetimes.truncate(old_len); - res - } - /// Appends in-band lifetime defs and argument-position `impl /// Trait` defs to the existing set of generics. /// @@ -1130,131 +971,6 @@ impl<'a> LoweringContext<'a> { (lowered_generics, res) } - fn with_catch_scope(&mut self, catch_id: NodeId, f: F) -> T - where - F: FnOnce(&mut LoweringContext<'_>) -> T, - { - let len = self.catch_scopes.len(); - self.catch_scopes.push(catch_id); - - let result = f(self); - assert_eq!( - len + 1, - self.catch_scopes.len(), - "catch scopes should be added and removed in stack order" - ); - - self.catch_scopes.pop().unwrap(); - - result - } - - fn make_async_expr( - &mut self, - capture_clause: CaptureBy, - closure_node_id: NodeId, - ret_ty: Option>, - span: Span, - body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr, - ) -> hir::ExprKind { - let capture_clause = self.lower_capture_clause(capture_clause); - let output = match ret_ty { - Some(ty) => FunctionRetTy::Ty(ty), - None => FunctionRetTy::Default(span), - }; - let ast_decl = FnDecl { - inputs: vec![], - output, - c_variadic: false - }; - let decl = self.lower_fn_decl(&ast_decl, None, /* impl trait allowed */ false, None); - let body_id = self.lower_fn_body(&ast_decl, |this| { - this.generator_kind = Some(hir::GeneratorKind::Async); - body(this) - }); - let generator = hir::Expr { - hir_id: self.lower_node_id(closure_node_id), - node: hir::ExprKind::Closure(capture_clause, decl, body_id, span, - Some(hir::GeneratorMovability::Static)), - span, - attrs: ThinVec::new(), - }; - - let unstable_span = self.mark_span_with_reason( - DesugaringKind::Async, - span, - self.allow_gen_future.clone(), - ); - let gen_future = self.expr_std_path( - unstable_span, &[sym::future, sym::from_generator], None, ThinVec::new()); - hir::ExprKind::Call(P(gen_future), hir_vec![generator]) - } - - fn lower_body( - &mut self, - f: impl FnOnce(&mut LoweringContext<'_>) -> (HirVec, hir::Expr), - ) -> hir::BodyId { - let prev_gen_kind = self.generator_kind.take(); - let (arguments, result) = f(self); - let body_id = self.record_body(arguments, result); - self.generator_kind = prev_gen_kind; - body_id - } - - fn lower_fn_body( - &mut self, - decl: &FnDecl, - body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr, - ) -> hir::BodyId { - self.lower_body(|this| ( - decl.inputs.iter().map(|x| this.lower_arg(x)).collect(), - body(this), - )) - } - - fn lower_const_body(&mut self, expr: &Expr) -> hir::BodyId { - self.lower_body(|this| (hir_vec![], this.lower_expr(expr))) - } - - fn with_loop_scope(&mut self, loop_id: NodeId, f: F) -> T - where - F: FnOnce(&mut LoweringContext<'_>) -> T, - { - // We're no longer in the base loop's condition; we're in another loop. - let was_in_loop_condition = self.is_in_loop_condition; - self.is_in_loop_condition = false; - - let len = self.loop_scopes.len(); - self.loop_scopes.push(loop_id); - - let result = f(self); - assert_eq!( - len + 1, - self.loop_scopes.len(), - "loop scopes should be added and removed in stack order" - ); - - self.loop_scopes.pop().unwrap(); - - self.is_in_loop_condition = was_in_loop_condition; - - result - } - - fn with_loop_condition_scope(&mut self, f: F) -> T - where - F: FnOnce(&mut LoweringContext<'_>) -> T, - { - let was_in_loop_condition = self.is_in_loop_condition; - self.is_in_loop_condition = true; - - let result = f(self); - - self.is_in_loop_condition = was_in_loop_condition; - - result - } - fn with_dyn_type_scope(&mut self, in_scope: bool, f: F) -> T where F: FnOnce(&mut LoweringContext<'_>) -> T, @@ -1295,36 +1011,6 @@ impl<'a> LoweringContext<'a> { } } - fn lower_label(&mut self, label: Option