diff --git a/src/ci/docker/dist-x86-linux/Dockerfile b/src/ci/docker/dist-x86-linux/Dockerfile index 852ce1806ec62..cd4c81912dff5 100644 --- a/src/ci/docker/dist-x86-linux/Dockerfile +++ b/src/ci/docker/dist-x86-linux/Dockerfile @@ -6,6 +6,7 @@ RUN yum upgrade -y && yum install -y \ curl \ bzip2 \ gcc \ + gcc-c++ \ make \ glibc-devel \ perl \ diff --git a/src/ci/docker/dist-x86-linux/build-gcc.sh b/src/ci/docker/dist-x86-linux/build-gcc.sh index 06198eb0c97fc..ab2562538d6d7 100755 --- a/src/ci/docker/dist-x86-linux/build-gcc.sh +++ b/src/ci/docker/dist-x86-linux/build-gcc.sh @@ -13,12 +13,14 @@ set -ex source shared.sh -curl https://ftp.gnu.org/gnu/gcc/gcc-4.7.4/gcc-4.7.4.tar.bz2 | tar xjf - -cd gcc-4.7.4 +GCC=4.8.5 + +curl https://ftp.gnu.org/gnu/gcc/gcc-$GCC/gcc-$GCC.tar.bz2 | tar xjf - +cd gcc-$GCC ./contrib/download_prerequisites mkdir ../gcc-build cd ../gcc-build -hide_output ../gcc-4.7.4/configure \ +hide_output ../gcc-$GCC/configure \ --prefix=/rustroot \ --enable-languages=c,c++ hide_output make -j10 @@ -27,5 +29,5 @@ ln -nsf gcc /rustroot/bin/cc cd .. rm -rf gcc-build -rm -rf gcc-4.7.4 -yum erase -y gcc binutils +rm -rf gcc-$GCC +yum erase -y gcc gcc-c++ binutils diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 909e44df20abb..07a7d2f95b1f2 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -405,7 +405,7 @@ impl *const T { } /// Calculates the offset from a pointer. `count` is in units of T; e.g. a - /// `count` of 3 represents a pointer offset of `3 * sizeof::()` bytes. + /// `count` of 3 represents a pointer offset of `3 * size_of::()` bytes. /// /// # Safety /// @@ -435,7 +435,7 @@ impl *const T { /// Calculates the offset from a pointer using wrapping arithmetic. /// `count` is in units of T; e.g. a `count` of 3 represents a pointer - /// offset of `3 * sizeof::()` bytes. + /// offset of `3 * size_of::()` bytes. /// /// # Safety /// @@ -529,7 +529,7 @@ impl *mut T { } /// Calculates the offset from a pointer. `count` is in units of T; e.g. a - /// `count` of 3 represents a pointer offset of `3 * sizeof::()` bytes. + /// `count` of 3 represents a pointer offset of `3 * size_of::()` bytes. /// /// # Safety /// @@ -558,7 +558,7 @@ impl *mut T { /// Calculates the offset from a pointer using wrapping arithmetic. /// `count` is in units of T; e.g. a `count` of 3 represents a pointer - /// offset of `3 * sizeof::()` bytes. + /// offset of `3 * size_of::()` bytes. /// /// # Safety /// diff --git a/src/librustc/hir/def.rs b/src/librustc/hir/def.rs index aedb8fef2885c..7bab4a8d725dc 100644 --- a/src/librustc/hir/def.rs +++ b/src/librustc/hir/def.rs @@ -12,6 +12,7 @@ use hir::def_id::DefId; use util::nodemap::NodeMap; use syntax::ast; use syntax::ext::base::MacroKind; +use syntax_pos::Span; use hir; #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] @@ -116,6 +117,7 @@ pub type ExportMap = NodeMap>; pub struct Export { pub name: ast::Name, // The name of the target. pub def: Def, // The definition of the target. + pub span: Span, // The span of the target definition. } impl CtorKind { diff --git a/src/librustc/hir/def_id.rs b/src/librustc/hir/def_id.rs index cbf162cc1366e..e975211bbda4c 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc/hir/def_id.rs @@ -78,33 +78,86 @@ impl serialize::UseSpecializedDecodable for CrateNum { /// A DefIndex is an index into the hir-map for a crate, identifying a /// particular definition. It should really be considered an interned /// shorthand for a particular DefPath. +/// +/// At the moment we are allocating the numerical values of DefIndexes into two +/// ranges: the "low" range (starting at zero) and the "high" range (starting at +/// DEF_INDEX_HI_START). This allows us to allocate the DefIndexes of all +/// item-likes (Items, TraitItems, and ImplItems) into one of these ranges and +/// consequently use a simple array for lookup tables keyed by DefIndex and +/// known to be densely populated. This is especially important for the HIR map. +/// +/// Since the DefIndex is mostly treated as an opaque ID, you probably don't +/// have to care about these ranges. #[derive(Clone, Debug, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, RustcDecodable, Hash, Copy)] pub struct DefIndex(u32); impl DefIndex { + #[inline] pub fn new(x: usize) -> DefIndex { assert!(x < (u32::MAX as usize)); DefIndex(x as u32) } + #[inline] pub fn from_u32(x: u32) -> DefIndex { DefIndex(x) } + #[inline] pub fn as_usize(&self) -> usize { self.0 as usize } + #[inline] pub fn as_u32(&self) -> u32 { self.0 } + + #[inline] + pub fn address_space(&self) -> DefIndexAddressSpace { + if self.0 < DEF_INDEX_HI_START.0 { + DefIndexAddressSpace::Low + } else { + DefIndexAddressSpace::High + } + } + + /// Converts this DefIndex into a zero-based array index. + /// This index is the offset within the given "range" of the DefIndex, + /// that is, if the DefIndex is part of the "high" range, the resulting + /// index will be (DefIndex - DEF_INDEX_HI_START). + #[inline] + pub fn as_array_index(&self) -> usize { + (self.0 & !DEF_INDEX_HI_START.0) as usize + } } +/// The start of the "high" range of DefIndexes. +const DEF_INDEX_HI_START: DefIndex = DefIndex(1 << 31); + /// The crate root is always assigned index 0 by the AST Map code, /// thanks to `NodeCollector::new`. pub const CRATE_DEF_INDEX: DefIndex = DefIndex(0); +#[derive(Copy, Clone, Eq, PartialEq, Hash)] +pub enum DefIndexAddressSpace { + Low = 0, + High = 1, +} + +impl DefIndexAddressSpace { + #[inline] + pub fn index(&self) -> usize { + *self as usize + } + + #[inline] + pub fn start(&self) -> usize { + self.index() * DEF_INDEX_HI_START.as_usize() + } +} + /// A DefId identifies a particular *definition*, by combining a crate /// index and a def index. #[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, RustcDecodable, Hash, Copy)] diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index d966f4899e50f..a72821d313201 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -41,14 +41,16 @@ // in the HIR, especially for multiple identifiers. use hir; -use hir::map::{Definitions, DefKey}; +use hir::map::{Definitions, DefKey, REGULAR_SPACE}; use hir::map::definitions::DefPathData; -use hir::def_id::{DefIndex, DefId}; +use hir::def_id::{DefIndex, DefId, CRATE_DEF_INDEX}; use hir::def::{Def, PathResolution}; +use rustc_data_structures::indexed_vec::IndexVec; use session::Session; use util::nodemap::{DefIdMap, NodeMap}; use std::collections::BTreeMap; +use std::fmt::Debug; use std::iter; use std::mem; @@ -63,6 +65,8 @@ use syntax::util::small_vector::SmallVector; use syntax::visit::{self, Visitor}; use syntax_pos::Span; +const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF; + pub struct LoweringContext<'a> { crate_root: Option<&'static str>, // Use to assign ids to hir nodes that do not directly correspond to an ast node @@ -89,6 +93,10 @@ pub struct LoweringContext<'a> { is_in_loop_condition: bool, type_def_lifetime_params: DefIdMap, + + current_hir_id_owner: Vec<(DefIndex, u32)>, + item_local_id_counters: NodeMap, + node_id_to_hir_id: IndexVec, } pub trait Resolver { @@ -128,6 +136,9 @@ pub fn lower_crate(sess: &Session, loop_scopes: Vec::new(), is_in_loop_condition: false, type_def_lifetime_params: DefIdMap(), + current_hir_id_owner: vec![(CRATE_DEF_INDEX, 0)], + item_local_id_counters: NodeMap(), + node_id_to_hir_id: IndexVec::new(), }.lower_crate(krate) } @@ -152,6 +163,8 @@ impl<'a> LoweringContext<'a> { impl<'lcx, 'interner> Visitor<'lcx> for MiscCollector<'lcx, 'interner> { fn visit_item(&mut self, item: &'lcx Item) { + self.lctx.allocate_hir_id_counter(item.id, item); + match item.node { ItemKind::Struct(_, ref generics) | ItemKind::Union(_, ref generics) | @@ -166,6 +179,16 @@ impl<'a> LoweringContext<'a> { } visit::walk_item(self, item); } + + fn visit_trait_item(&mut self, item: &'lcx TraitItem) { + self.lctx.allocate_hir_id_counter(item.id, item); + visit::walk_trait_item(self, item); + } + + fn visit_impl_item(&mut self, item: &'lcx ImplItem) { + self.lctx.allocate_hir_id_counter(item.id, item); + visit::walk_impl_item(self, item); + } } struct ItemLowerer<'lcx, 'interner: 'lcx> { @@ -174,27 +197,43 @@ impl<'a> LoweringContext<'a> { impl<'lcx, 'interner> Visitor<'lcx> for ItemLowerer<'lcx, 'interner> { fn visit_item(&mut self, item: &'lcx Item) { - if let Some(hir_item) = self.lctx.lower_item(item) { - self.lctx.items.insert(item.id, hir_item); + let mut item_lowered = true; + self.lctx.with_hir_id_owner(item.id, |lctx| { + if let Some(hir_item) = lctx.lower_item(item) { + lctx.items.insert(item.id, hir_item); + } else { + item_lowered = false; + } + }); + + if item_lowered { visit::walk_item(self, item); } } fn visit_trait_item(&mut self, item: &'lcx TraitItem) { - let id = hir::TraitItemId { node_id: item.id }; - let hir_item = self.lctx.lower_trait_item(item); - self.lctx.trait_items.insert(id, hir_item); + self.lctx.with_hir_id_owner(item.id, |lctx| { + let id = hir::TraitItemId { node_id: item.id }; + let hir_item = lctx.lower_trait_item(item); + lctx.trait_items.insert(id, hir_item); + }); + visit::walk_trait_item(self, item); } fn visit_impl_item(&mut self, item: &'lcx ImplItem) { - let id = hir::ImplItemId { node_id: item.id }; - let hir_item = self.lctx.lower_impl_item(item); - self.lctx.impl_items.insert(id, hir_item); + self.lctx.with_hir_id_owner(item.id, |lctx| { + let id = hir::ImplItemId { node_id: item.id }; + let hir_item = lctx.lower_impl_item(item); + lctx.impl_items.insert(id, hir_item); + }); 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 }, c); visit::walk_crate(&mut ItemLowerer { lctx: &mut self }, c); @@ -202,6 +241,10 @@ impl<'a> LoweringContext<'a> { let attrs = self.lower_attrs(&c.attrs); let body_ids = body_ids(&self.bodies); + self.resolver + .definitions() + .init_node_id_to_hir_id_mapping(self.node_id_to_hir_id); + hir::Crate { module: module, attrs: attrs, @@ -217,6 +260,103 @@ impl<'a> LoweringContext<'a> { } } + fn allocate_hir_id_counter(&mut self, + owner: NodeId, + debug: &T) { + if self.item_local_id_counters.insert(owner, 0).is_some() { + bug!("Tried to allocate item_local_id_counter for {:?} twice", debug); + } + // Always allocate the first HirId for the owner itself + self.lower_node_id_with_owner(owner, owner); + } + + fn lower_node_id_generic(&mut self, + ast_node_id: NodeId, + alloc_hir_id: F) + -> NodeId + where F: FnOnce(&mut Self) -> hir::HirId + { + if ast_node_id == DUMMY_NODE_ID { + return ast_node_id; + } + + let min_size = ast_node_id.as_usize() + 1; + + if min_size > self.node_id_to_hir_id.len() { + self.node_id_to_hir_id.resize(min_size, hir::DUMMY_HIR_ID); + } + + if self.node_id_to_hir_id[ast_node_id] == hir::DUMMY_HIR_ID { + // Generate a new HirId + self.node_id_to_hir_id[ast_node_id] = alloc_hir_id(self); + } + + ast_node_id + } + + fn with_hir_id_owner(&mut self, owner: NodeId, f: F) + where F: FnOnce(&mut Self) + { + let counter = self.item_local_id_counters + .insert(owner, HIR_ID_COUNTER_LOCKED) + .unwrap(); + let def_index = self.resolver.definitions().opt_def_index(owner).unwrap(); + self.current_hir_id_owner.push((def_index, counter)); + f(self); + let (new_def_index, new_counter) = self.current_hir_id_owner.pop().unwrap(); + + debug_assert!(def_index == new_def_index); + debug_assert!(new_counter >= counter); + + let prev = self.item_local_id_counters.insert(owner, new_counter).unwrap(); + debug_assert!(prev == HIR_ID_COUNTER_LOCKED); + } + + /// This method allocates a new HirId for the given NodeId and stores it in + /// the LoweringContext's NodeId => HirId map. + /// Take care not to call this method if the resulting HirId is then not + /// actually used in the HIR, as that would trigger an assertion in the + /// HirIdValidator later on, which makes sure that all NodeIds got mapped + /// properly. Calling the method twice with the same NodeId is fine though. + fn lower_node_id(&mut self, ast_node_id: NodeId) -> NodeId { + self.lower_node_id_generic(ast_node_id, |this| { + let &mut (def_index, ref mut local_id_counter) = this.current_hir_id_owner + .last_mut() + .unwrap(); + let local_id = *local_id_counter; + *local_id_counter += 1; + hir::HirId { + owner: def_index, + local_id: hir::ItemLocalId(local_id), + } + }) + } + + fn lower_node_id_with_owner(&mut self, + ast_node_id: NodeId, + owner: NodeId) + -> NodeId { + self.lower_node_id_generic(ast_node_id, |this| { + let local_id_counter = this.item_local_id_counters + .get_mut(&owner) + .unwrap(); + let local_id = *local_id_counter; + + // We want to be sure not to modify the counter in the map while it + // is also on the stack. Otherwise we'll get lost updates when writing + // back from the stack to the map. + debug_assert!(local_id != HIR_ID_COUNTER_LOCKED); + + *local_id_counter += 1; + let def_index = this.resolver.definitions().opt_def_index(owner).unwrap(); + + hir::HirId { + owner: def_index, + local_id: hir::ItemLocalId(local_id), + } + }) + } + fn record_body(&mut self, value: hir::Expr, decl: Option<&FnDecl>) -> hir::BodyId { let body = hir::Body { @@ -230,8 +370,8 @@ impl<'a> LoweringContext<'a> { id } - fn next_id(&self) -> NodeId { - self.sess.next_node_id() + fn next_id(&mut self) -> NodeId { + self.lower_node_id(self.sess.next_node_id()) } fn expect_full_def(&mut self, id: NodeId) -> Def { @@ -363,15 +503,22 @@ impl<'a> LoweringContext<'a> { Some((id, label_ident)) => hir::Destination { ident: Some(label_ident), loop_id: if let Def::Label(loop_id) = self.expect_full_def(id) { - hir::LoopIdResult::Ok(loop_id) + hir::LoopIdResult::Ok(self.lower_node_id(loop_id)) } else { hir::LoopIdResult::Err(hir::LoopIdError::UnresolvedLabel) } }, - None => hir::Destination { - ident: None, - loop_id: self.loop_scopes.last().map(|innermost_loop_id| Ok(*innermost_loop_id)) - .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)).into() + None => { + let loop_id = self.loop_scopes + .last() + .map(|innermost_loop_id| *innermost_loop_id); + + hir::Destination { + ident: None, + loop_id: loop_id.map(|id| Ok(self.lower_node_id(id))) + .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)) + .into() + } } } } @@ -391,7 +538,7 @@ impl<'a> LoweringContext<'a> { fn lower_ty_binding(&mut self, b: &TypeBinding) -> hir::TypeBinding { hir::TypeBinding { - id: b.id, + id: self.lower_node_id(b.id), name: b.ident.name, ty: self.lower_ty(&b.ty), span: b.span, @@ -399,82 +546,87 @@ impl<'a> LoweringContext<'a> { } fn lower_ty(&mut self, t: &Ty) -> P { - P(hir::Ty { - id: t.id, - node: match t.node { - TyKind::Infer => hir::TyInfer, - TyKind::Slice(ref ty) => hir::TySlice(self.lower_ty(ty)), - TyKind::Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt)), - TyKind::Rptr(ref region, ref mt) => { - let span = Span { hi: t.span.lo, ..t.span }; - let lifetime = match *region { - Some(ref lt) => self.lower_lifetime(lt), - None => self.elided_lifetime(span) - }; - hir::TyRptr(lifetime, self.lower_mt(mt)) - } - TyKind::BareFn(ref f) => { - hir::TyBareFn(P(hir::BareFnTy { - lifetimes: self.lower_lifetime_defs(&f.lifetimes), - unsafety: self.lower_unsafety(f.unsafety), - abi: f.abi, - decl: self.lower_fn_decl(&f.decl), - })) - } - TyKind::Never => hir::TyNever, - TyKind::Tup(ref tys) => { - hir::TyTup(tys.iter().map(|ty| self.lower_ty(ty)).collect()) - } - TyKind::Paren(ref ty) => { - return self.lower_ty(ty); - } - TyKind::Path(ref qself, ref path) => { - let qpath = self.lower_qpath(t.id, qself, path, ParamMode::Explicit); - return self.ty_path(t.id, t.span, qpath); - } - TyKind::ImplicitSelf => { - hir::TyPath(hir::QPath::Resolved(None, P(hir::Path { - def: self.expect_full_def(t.id), - segments: hir_vec![hir::PathSegment { - name: keywords::SelfType.name(), - parameters: hir::PathParameters::none() - }], - span: t.span, - }))) - } - TyKind::Array(ref ty, ref length) => { - let length = self.lower_expr(length); - hir::TyArray(self.lower_ty(ty), - self.record_body(length, None)) - } - TyKind::Typeof(ref expr) => { - let expr = self.lower_expr(expr); - hir::TyTypeof(self.record_body(expr, None)) - } - TyKind::TraitObject(ref bounds) => { - let mut lifetime_bound = None; - let bounds = bounds.iter().filter_map(|bound| { - match *bound { - TraitTyParamBound(ref ty, TraitBoundModifier::None) => { - Some(self.lower_poly_trait_ref(ty)) - } - TraitTyParamBound(_, TraitBoundModifier::Maybe) => None, - RegionTyParamBound(ref lifetime) => { + let kind = match t.node { + TyKind::Infer => hir::TyInfer, + TyKind::Slice(ref ty) => hir::TySlice(self.lower_ty(ty)), + TyKind::Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt)), + TyKind::Rptr(ref region, ref mt) => { + let span = Span { hi: t.span.lo, ..t.span }; + let lifetime = match *region { + Some(ref lt) => self.lower_lifetime(lt), + None => self.elided_lifetime(span) + }; + hir::TyRptr(lifetime, self.lower_mt(mt)) + } + TyKind::BareFn(ref f) => { + hir::TyBareFn(P(hir::BareFnTy { + lifetimes: self.lower_lifetime_defs(&f.lifetimes), + unsafety: self.lower_unsafety(f.unsafety), + abi: f.abi, + decl: self.lower_fn_decl(&f.decl), + })) + } + TyKind::Never => hir::TyNever, + TyKind::Tup(ref tys) => { + hir::TyTup(tys.iter().map(|ty| self.lower_ty(ty)).collect()) + } + TyKind::Paren(ref ty) => { + return self.lower_ty(ty); + } + TyKind::Path(ref qself, ref path) => { + let id = self.lower_node_id(t.id); + let qpath = self.lower_qpath(t.id, qself, path, ParamMode::Explicit); + return self.ty_path(id, t.span, qpath); + } + TyKind::ImplicitSelf => { + hir::TyPath(hir::QPath::Resolved(None, P(hir::Path { + def: self.expect_full_def(t.id), + segments: hir_vec![hir::PathSegment { + name: keywords::SelfType.name(), + parameters: hir::PathParameters::none() + }], + span: t.span, + }))) + } + TyKind::Array(ref ty, ref length) => { + let length = self.lower_expr(length); + hir::TyArray(self.lower_ty(ty), + self.record_body(length, None)) + } + TyKind::Typeof(ref expr) => { + let expr = self.lower_expr(expr); + hir::TyTypeof(self.record_body(expr, None)) + } + TyKind::TraitObject(ref bounds) => { + let mut lifetime_bound = None; + let bounds = bounds.iter().filter_map(|bound| { + match *bound { + TraitTyParamBound(ref ty, TraitBoundModifier::None) => { + Some(self.lower_poly_trait_ref(ty)) + } + TraitTyParamBound(_, TraitBoundModifier::Maybe) => None, + RegionTyParamBound(ref lifetime) => { + if lifetime_bound.is_none() { lifetime_bound = Some(self.lower_lifetime(lifetime)); - None } + None } - }).collect(); - let lifetime_bound = lifetime_bound.unwrap_or_else(|| { - self.elided_lifetime(t.span) - }); - hir::TyTraitObject(bounds, lifetime_bound) - } - TyKind::ImplTrait(ref bounds) => { - hir::TyImplTrait(self.lower_bounds(bounds)) - } - TyKind::Mac(_) => panic!("TyMac should have been expanded by now."), - }, + } + }).collect(); + let lifetime_bound = lifetime_bound.unwrap_or_else(|| { + self.elided_lifetime(t.span) + }); + hir::TyTraitObject(bounds, lifetime_bound) + } + TyKind::ImplTrait(ref bounds) => { + hir::TyImplTrait(self.lower_bounds(bounds)) + } + TyKind::Mac(_) => panic!("TyMac should have been expanded by now."), + }; + + P(hir::Ty { + id: self.lower_node_id(t.id), + node: kind, span: t.span, }) } @@ -708,7 +860,7 @@ impl<'a> LoweringContext<'a> { fn lower_local(&mut self, l: &Local) -> P { P(hir::Local { - id: l.id, + id: self.lower_node_id(l.id), ty: l.ty.as_ref().map(|t| self.lower_ty(t)), pat: self.lower_pat(&l.pat), init: l.init.as_ref().map(|e| P(self.lower_expr(e))), @@ -726,7 +878,7 @@ impl<'a> LoweringContext<'a> { fn lower_arg(&mut self, arg: &Arg) -> hir::Arg { hir::Arg { - id: arg.id, + id: self.lower_node_id(arg.id), pat: self.lower_pat(&arg.pat), } } @@ -782,7 +934,7 @@ impl<'a> LoweringContext<'a> { } hir::TyParam { - id: tp.id, + id: self.lower_node_id(tp.id), name: name, bounds: bounds, default: tp.default.as_ref().map(|x| self.lower_ty(x)), @@ -800,7 +952,7 @@ impl<'a> LoweringContext<'a> { fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime { hir::Lifetime { - id: l.id, + id: self.lower_node_id(l.id), name: l.name, span: l.span, } @@ -872,7 +1024,7 @@ impl<'a> LoweringContext<'a> { fn lower_where_clause(&mut self, wc: &WhereClause) -> hir::WhereClause { hir::WhereClause { - id: wc.id, + id: self.lower_node_id(wc.id), predicates: wc.predicates .iter() .map(|predicate| self.lower_where_predicate(predicate)) @@ -911,7 +1063,7 @@ impl<'a> LoweringContext<'a> { ref rhs_ty, span}) => { hir::WherePredicate::EqPredicate(hir::WhereEqPredicate { - id: id, + id: self.lower_node_id(id), lhs_ty: self.lower_ty(lhs_ty), rhs_ty: self.lower_ty(rhs_ty), span: span, @@ -927,16 +1079,16 @@ impl<'a> LoweringContext<'a> { .enumerate() .map(|f| self.lower_struct_field(f)) .collect(), - id) + self.lower_node_id(id)) } VariantData::Tuple(ref fields, id) => { hir::VariantData::Tuple(fields.iter() .enumerate() .map(|f| self.lower_struct_field(f)) .collect(), - id) + self.lower_node_id(id)) } - VariantData::Unit(id) => hir::VariantData::Unit(id), + VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id)), } } @@ -947,7 +1099,7 @@ impl<'a> LoweringContext<'a> { }; hir::TraitRef { path: path, - ref_id: p.ref_id, + ref_id: self.lower_node_id(p.ref_id), } } @@ -962,9 +1114,9 @@ impl<'a> LoweringContext<'a> { fn lower_struct_field(&mut self, (index, f): (usize, &StructField)) -> hir::StructField { hir::StructField { span: f.span, - id: f.id, + id: self.lower_node_id(f.id), name: f.ident.map(|ident| ident.name).unwrap_or(Symbol::intern(&index.to_string())), - vis: self.lower_visibility(&f.vis), + vis: self.lower_visibility(&f.vis, None), ty: self.lower_ty(&f.ty), attrs: self.lower_attrs(&f.attrs), } @@ -993,17 +1145,22 @@ impl<'a> LoweringContext<'a> { fn lower_block(&mut self, b: &Block) -> P { let mut expr = None; - let mut stmts = b.stmts.iter().flat_map(|s| self.lower_stmt(s)).collect::>(); - if let Some(last) = stmts.pop() { - if let hir::StmtExpr(e, _) = last.node { - expr = Some(e); + let mut stmts = vec![]; + + for (index, stmt) in b.stmts.iter().enumerate() { + if index == b.stmts.len() - 1 { + if let StmtKind::Expr(ref e) = stmt.node { + expr = Some(P(self.lower_expr(e))); + } else { + stmts.extend(self.lower_stmt(stmt)); + } } else { - stmts.push(last); + stmts.extend(self.lower_stmt(stmt)); } } P(hir::Block { - id: b.id, + id: self.lower_node_id(b.id), stmts: stmts.into(), expr: expr, rules: self.lower_block_check_mode(&b.rules), @@ -1041,13 +1198,30 @@ impl<'a> LoweringContext<'a> { let mut path = self.lower_path_extra(import.id, path, suffix, ParamMode::Explicit, true); path.span = span; - self.items.insert(import.id, hir::Item { - id: import.id, - name: import.rename.unwrap_or(ident).name, - attrs: attrs.clone(), - node: hir::ItemUse(P(path), hir::UseKind::Single), - vis: vis.clone(), - span: span, + + self.allocate_hir_id_counter(import.id, import); + self.with_hir_id_owner(import.id, |this| { + let vis = match *vis { + hir::Visibility::Public => hir::Visibility::Public, + hir::Visibility::Crate => hir::Visibility::Crate, + hir::Visibility::Inherited => hir::Visibility::Inherited, + hir::Visibility::Restricted { ref path, id: _ } => { + hir::Visibility::Restricted { + path: path.clone(), + // We are allocating a new NodeId here + id: this.next_id(), + } + } + }; + + this.items.insert(import.id, hir::Item { + id: import.id, + name: import.rename.unwrap_or(ident).name, + attrs: attrs.clone(), + node: hir::ItemUse(P(path), hir::UseKind::Single), + vis: vis, + span: span, + }); }); } path @@ -1162,7 +1336,7 @@ impl<'a> LoweringContext<'a> { fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem { self.with_parent_def(i.id, |this| { hir::TraitItem { - id: i.id, + id: this.lower_node_id(i.id), name: i.ident.name, attrs: this.lower_attrs(&i.attrs), node: match i.node { @@ -1223,10 +1397,10 @@ impl<'a> LoweringContext<'a> { fn lower_impl_item(&mut self, i: &ImplItem) -> hir::ImplItem { self.with_parent_def(i.id, |this| { hir::ImplItem { - id: i.id, + id: this.lower_node_id(i.id), name: i.ident.name, attrs: this.lower_attrs(&i.attrs), - vis: this.lower_visibility(&i.vis), + vis: this.lower_visibility(&i.vis, None), defaultness: this.lower_defaultness(i.defaultness, true /* [1] */), node: match i.node { ImplItemKind::Const(ref ty, ref expr) => { @@ -1255,7 +1429,7 @@ impl<'a> LoweringContext<'a> { id: hir::ImplItemId { node_id: i.id }, name: i.ident.name, span: i.span, - vis: self.lower_visibility(&i.vis), + vis: self.lower_visibility(&i.vis, Some(i.id)), defaultness: self.lower_defaultness(i.defaultness, true /* [1] */), kind: match i.node { ImplItemKind::Const(..) => hir::AssociatedItemKind::Const, @@ -1294,7 +1468,6 @@ impl<'a> LoweringContext<'a> { pub fn lower_item(&mut self, i: &Item) -> Option { let mut name = i.ident.name; let attrs = self.lower_attrs(&i.attrs); - let mut vis = self.lower_visibility(&i.vis); if let ItemKind::MacroDef(ref tts) = i.node { if i.attrs.iter().any(|attr| attr.path == "macro_export") { self.exported_macros.push(hir::MacroDef { @@ -1304,12 +1477,13 @@ impl<'a> LoweringContext<'a> { return None; } + let mut vis = self.lower_visibility(&i.vis, None); let node = self.with_parent_def(i.id, |this| { this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node) }); Some(hir::Item { - id: i.id, + id: self.lower_node_id(i.id), name: name, attrs: attrs, node: node, @@ -1321,7 +1495,7 @@ impl<'a> LoweringContext<'a> { fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem { self.with_parent_def(i.id, |this| { hir::ForeignItem { - id: i.id, + id: this.lower_node_id(i.id), name: i.ident.name, attrs: this.lower_attrs(&i.attrs), node: match i.node { @@ -1334,7 +1508,7 @@ impl<'a> LoweringContext<'a> { hir::ForeignItemStatic(this.lower_ty(t), m) } }, - vis: this.lower_visibility(&i.vis), + vis: this.lower_visibility(&i.vis, None), span: i.span, } }) @@ -1400,7 +1574,7 @@ impl<'a> LoweringContext<'a> { fn lower_pat(&mut self, p: &Pat) -> P { P(hir::Pat { - id: p.id, + id: self.lower_node_id(p.id), node: match p.node { PatKind::Wild => hir::PatKind::Wild, PatKind::Ident(ref binding_mode, pth1, ref sub) => { @@ -1486,691 +1660,730 @@ impl<'a> LoweringContext<'a> { } fn lower_expr(&mut self, e: &Expr) -> hir::Expr { - hir::Expr { - id: e.id, - node: match e.node { - // Issue #22181: - // Eventually a desugaring for `box EXPR` - // (similar to the desugaring above for `in PLACE BLOCK`) - // should go here, desugaring - // + let kind = match e.node { + // Issue #22181: + // Eventually a desugaring for `box EXPR` + // (similar to the desugaring above for `in PLACE BLOCK`) + // should go here, desugaring + // + // to: + // + // let mut place = BoxPlace::make_place(); + // let raw_place = Place::pointer(&mut place); + // let value = $value; + // unsafe { + // ::std::ptr::write(raw_place, value); + // Boxed::finalize(place) + // } + // + // But for now there are type-inference issues doing that. + ExprKind::Box(ref inner) => { + hir::ExprBox(P(self.lower_expr(inner))) + } + + // Desugar ExprBox: `in (PLACE) EXPR` + ExprKind::InPlace(ref placer, ref value_expr) => { // to: // - // let mut place = BoxPlace::make_place(); + // let p = PLACE; + // let mut place = Placer::make_place(p); // let raw_place = Place::pointer(&mut place); - // let value = $value; - // unsafe { - // ::std::ptr::write(raw_place, value); - // Boxed::finalize(place) - // } - // - // But for now there are type-inference issues doing that. - ExprKind::Box(ref e) => { - hir::ExprBox(P(self.lower_expr(e))) - } - - // Desugar ExprBox: `in (PLACE) EXPR` - ExprKind::InPlace(ref placer, ref value_expr) => { - // to: - // - // let p = PLACE; - // let mut place = Placer::make_place(p); - // let raw_place = Place::pointer(&mut place); - // push_unsafe!({ - // std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR )); - // InPlace::finalize(place) - // }) - let placer_expr = P(self.lower_expr(placer)); - let value_expr = P(self.lower_expr(value_expr)); - - let placer_ident = self.str_to_ident("placer"); - let place_ident = self.str_to_ident("place"); - let p_ptr_ident = self.str_to_ident("p_ptr"); - - let make_place = ["ops", "Placer", "make_place"]; - let place_pointer = ["ops", "Place", "pointer"]; - let move_val_init = ["intrinsics", "move_val_init"]; - let inplace_finalize = ["ops", "InPlace", "finalize"]; - - let unstable_span = self.allow_internal_unstable("<-", e.span); - let make_call = |this: &mut LoweringContext, p, args| { - let path = P(this.expr_std_path(unstable_span, p, ThinVec::new())); - P(this.expr_call(e.span, path, args)) - }; + // push_unsafe!({ + // std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR )); + // InPlace::finalize(place) + // }) + let placer_expr = P(self.lower_expr(placer)); + let value_expr = P(self.lower_expr(value_expr)); + + let placer_ident = self.str_to_ident("placer"); + let place_ident = self.str_to_ident("place"); + let p_ptr_ident = self.str_to_ident("p_ptr"); + + let make_place = ["ops", "Placer", "make_place"]; + let place_pointer = ["ops", "Place", "pointer"]; + let move_val_init = ["intrinsics", "move_val_init"]; + let inplace_finalize = ["ops", "InPlace", "finalize"]; + + let unstable_span = self.allow_internal_unstable("<-", e.span); + let make_call = |this: &mut LoweringContext, p, args| { + let path = P(this.expr_std_path(unstable_span, p, ThinVec::new())); + P(this.expr_call(e.span, path, args)) + }; - let mk_stmt_let = |this: &mut LoweringContext, bind, expr| { - this.stmt_let(e.span, false, bind, expr) - }; + let mk_stmt_let = |this: &mut LoweringContext, bind, expr| { + this.stmt_let(e.span, false, bind, expr) + }; - let mk_stmt_let_mut = |this: &mut LoweringContext, bind, expr| { - this.stmt_let(e.span, true, bind, expr) - }; + let mk_stmt_let_mut = |this: &mut LoweringContext, bind, expr| { + this.stmt_let(e.span, true, bind, expr) + }; - // let placer = ; - let (s1, placer_binding) = { - mk_stmt_let(self, placer_ident, placer_expr) - }; + // let placer = ; + let (s1, placer_binding) = { + mk_stmt_let(self, placer_ident, placer_expr) + }; - // let mut place = Placer::make_place(placer); - let (s2, place_binding) = { - let placer = self.expr_ident(e.span, placer_ident, placer_binding); - let call = make_call(self, &make_place, hir_vec![placer]); - mk_stmt_let_mut(self, place_ident, call) - }; + // let mut place = Placer::make_place(placer); + let (s2, place_binding) = { + let placer = self.expr_ident(e.span, placer_ident, placer_binding); + let call = make_call(self, &make_place, hir_vec![placer]); + mk_stmt_let_mut(self, place_ident, call) + }; - // let p_ptr = Place::pointer(&mut place); - let (s3, p_ptr_binding) = { - let agent = P(self.expr_ident(e.span, place_ident, place_binding)); - let args = hir_vec![self.expr_mut_addr_of(e.span, agent)]; - let call = make_call(self, &place_pointer, args); - mk_stmt_let(self, p_ptr_ident, call) - }; + // let p_ptr = Place::pointer(&mut place); + let (s3, p_ptr_binding) = { + let agent = P(self.expr_ident(e.span, place_ident, place_binding)); + let args = hir_vec![self.expr_mut_addr_of(e.span, agent)]; + let call = make_call(self, &place_pointer, args); + mk_stmt_let(self, p_ptr_ident, call) + }; - // pop_unsafe!(EXPR)); - let pop_unsafe_expr = { - self.signal_block_expr(hir_vec![], - value_expr, - e.span, - hir::PopUnsafeBlock(hir::CompilerGenerated), - ThinVec::new()) - }; + // pop_unsafe!(EXPR)); + let pop_unsafe_expr = { + self.signal_block_expr(hir_vec![], + value_expr, + e.span, + hir::PopUnsafeBlock(hir::CompilerGenerated), + ThinVec::new()) + }; - // push_unsafe!({ - // std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR )); - // InPlace::finalize(place) - // }) - let expr = { - let ptr = self.expr_ident(e.span, p_ptr_ident, p_ptr_binding); - let call_move_val_init = - hir::StmtSemi( - make_call(self, &move_val_init, hir_vec![ptr, pop_unsafe_expr]), - self.next_id()); - let call_move_val_init = respan(e.span, call_move_val_init); - - let place = self.expr_ident(e.span, place_ident, place_binding); - let call = make_call(self, &inplace_finalize, hir_vec![place]); - P(self.signal_block_expr(hir_vec![call_move_val_init], - call, - e.span, - hir::PushUnsafeBlock(hir::CompilerGenerated), - ThinVec::new())) - }; + // push_unsafe!({ + // std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR )); + // InPlace::finalize(place) + // }) + let expr = { + let ptr = self.expr_ident(e.span, p_ptr_ident, p_ptr_binding); + let call_move_val_init = + hir::StmtSemi( + make_call(self, &move_val_init, hir_vec![ptr, pop_unsafe_expr]), + self.next_id()); + let call_move_val_init = respan(e.span, call_move_val_init); + + let place = self.expr_ident(e.span, place_ident, place_binding); + let call = make_call(self, &inplace_finalize, hir_vec![place]); + P(self.signal_block_expr(hir_vec![call_move_val_init], + call, + e.span, + hir::PushUnsafeBlock(hir::CompilerGenerated), + ThinVec::new())) + }; - let block = self.block_all(e.span, hir_vec![s1, s2, s3], Some(expr)); - // add the attributes to the outer returned expr node - return self.expr_block(P(block), e.attrs.clone()); - } + let block = self.block_all(e.span, hir_vec![s1, s2, s3], Some(expr)); + hir::ExprBlock(P(block)) + } - ExprKind::Array(ref exprs) => { - hir::ExprArray(exprs.iter().map(|x| self.lower_expr(x)).collect()) - } - ExprKind::Repeat(ref expr, ref count) => { - let expr = P(self.lower_expr(expr)); - let count = self.lower_expr(count); - hir::ExprRepeat(expr, self.record_body(count, None)) - } - ExprKind::Tup(ref elts) => { - hir::ExprTup(elts.iter().map(|x| self.lower_expr(x)).collect()) - } - ExprKind::Call(ref f, ref args) => { - let f = P(self.lower_expr(f)); - hir::ExprCall(f, args.iter().map(|x| self.lower_expr(x)).collect()) - } - ExprKind::MethodCall(i, ref tps, ref args) => { - let tps = tps.iter().map(|x| self.lower_ty(x)).collect(); - let args = args.iter().map(|x| self.lower_expr(x)).collect(); - hir::ExprMethodCall(respan(i.span, i.node.name), tps, args) - } - ExprKind::Binary(binop, ref lhs, ref rhs) => { - let binop = self.lower_binop(binop); - let lhs = P(self.lower_expr(lhs)); - let rhs = P(self.lower_expr(rhs)); - hir::ExprBinary(binop, lhs, rhs) - } - ExprKind::Unary(op, ref ohs) => { - let op = self.lower_unop(op); - let ohs = P(self.lower_expr(ohs)); - hir::ExprUnary(op, ohs) - } - ExprKind::Lit(ref l) => hir::ExprLit(P((**l).clone())), - ExprKind::Cast(ref expr, ref ty) => { - let expr = P(self.lower_expr(expr)); - hir::ExprCast(expr, self.lower_ty(ty)) - } - ExprKind::Type(ref expr, ref ty) => { - let expr = P(self.lower_expr(expr)); - hir::ExprType(expr, self.lower_ty(ty)) - } - ExprKind::AddrOf(m, ref ohs) => { - let m = self.lower_mutability(m); - let ohs = P(self.lower_expr(ohs)); - hir::ExprAddrOf(m, ohs) - } - // More complicated than you might expect because the else branch - // might be `if let`. - ExprKind::If(ref cond, ref blk, ref else_opt) => { - let else_opt = else_opt.as_ref().map(|els| { - match els.node { - ExprKind::IfLet(..) => { - // wrap the if-let expr in a block - let span = els.span; - let els = P(self.lower_expr(els)); - let id = self.next_id(); - let blk = P(hir::Block { - stmts: hir_vec![], - expr: Some(els), - id: id, - rules: hir::DefaultBlock, - span: span, - }); - P(self.expr_block(blk, ThinVec::new())) - } - _ => P(self.lower_expr(els)), + ExprKind::Array(ref exprs) => { + hir::ExprArray(exprs.iter().map(|x| self.lower_expr(x)).collect()) + } + ExprKind::Repeat(ref expr, ref count) => { + let expr = P(self.lower_expr(expr)); + let count = self.lower_expr(count); + hir::ExprRepeat(expr, self.record_body(count, None)) + } + ExprKind::Tup(ref elts) => { + hir::ExprTup(elts.iter().map(|x| self.lower_expr(x)).collect()) + } + ExprKind::Call(ref f, ref args) => { + let f = P(self.lower_expr(f)); + hir::ExprCall(f, args.iter().map(|x| self.lower_expr(x)).collect()) + } + ExprKind::MethodCall(i, ref tps, ref args) => { + let tps = tps.iter().map(|x| self.lower_ty(x)).collect(); + let args = args.iter().map(|x| self.lower_expr(x)).collect(); + hir::ExprMethodCall(respan(i.span, i.node.name), tps, args) + } + ExprKind::Binary(binop, ref lhs, ref rhs) => { + let binop = self.lower_binop(binop); + let lhs = P(self.lower_expr(lhs)); + let rhs = P(self.lower_expr(rhs)); + hir::ExprBinary(binop, lhs, rhs) + } + ExprKind::Unary(op, ref ohs) => { + let op = self.lower_unop(op); + let ohs = P(self.lower_expr(ohs)); + hir::ExprUnary(op, ohs) + } + ExprKind::Lit(ref l) => hir::ExprLit(P((**l).clone())), + ExprKind::Cast(ref expr, ref ty) => { + let expr = P(self.lower_expr(expr)); + hir::ExprCast(expr, self.lower_ty(ty)) + } + ExprKind::Type(ref expr, ref ty) => { + let expr = P(self.lower_expr(expr)); + hir::ExprType(expr, self.lower_ty(ty)) + } + ExprKind::AddrOf(m, ref ohs) => { + let m = self.lower_mutability(m); + let ohs = P(self.lower_expr(ohs)); + hir::ExprAddrOf(m, ohs) + } + // More complicated than you might expect because the else branch + // might be `if let`. + ExprKind::If(ref cond, ref blk, ref else_opt) => { + let else_opt = else_opt.as_ref().map(|els| { + match els.node { + ExprKind::IfLet(..) => { + // wrap the if-let expr in a block + let span = els.span; + let els = P(self.lower_expr(els)); + let id = self.next_id(); + let blk = P(hir::Block { + stmts: hir_vec![], + expr: Some(els), + id: id, + rules: hir::DefaultBlock, + span: span, + }); + P(self.expr_block(blk, ThinVec::new())) } - }); + _ => P(self.lower_expr(els)), + } + }); - hir::ExprIf(P(self.lower_expr(cond)), self.lower_block(blk), else_opt) - } - ExprKind::While(ref cond, ref body, opt_ident) => { - self.with_loop_scope(e.id, |this| - hir::ExprWhile( - this.with_loop_condition_scope(|this| P(this.lower_expr(cond))), - this.lower_block(body), - this.lower_opt_sp_ident(opt_ident))) - } - ExprKind::Loop(ref body, opt_ident) => { - self.with_loop_scope(e.id, |this| - hir::ExprLoop(this.lower_block(body), - this.lower_opt_sp_ident(opt_ident), - hir::LoopSource::Loop)) - } - ExprKind::Catch(ref body) => { - // FIXME(cramertj): Add catch to HIR - self.with_catch_scope(e.id, |this| hir::ExprBlock(this.lower_block(body))) - } - ExprKind::Match(ref expr, ref arms) => { - hir::ExprMatch(P(self.lower_expr(expr)), - arms.iter().map(|x| self.lower_arm(x)).collect(), - hir::MatchSource::Normal) - } - ExprKind::Closure(capture_clause, ref decl, ref body, fn_decl_span) => { - self.with_new_scopes(|this| { - this.with_parent_def(e.id, |this| { - let expr = this.lower_expr(body); - hir::ExprClosure(this.lower_capture_clause(capture_clause), - this.lower_fn_decl(decl), - this.record_body(expr, Some(decl)), - fn_decl_span) - }) + hir::ExprIf(P(self.lower_expr(cond)), self.lower_block(blk), else_opt) + } + ExprKind::While(ref cond, ref body, opt_ident) => { + self.with_loop_scope(e.id, |this| + hir::ExprWhile( + this.with_loop_condition_scope(|this| P(this.lower_expr(cond))), + this.lower_block(body), + this.lower_opt_sp_ident(opt_ident))) + } + ExprKind::Loop(ref body, opt_ident) => { + self.with_loop_scope(e.id, |this| + hir::ExprLoop(this.lower_block(body), + this.lower_opt_sp_ident(opt_ident), + hir::LoopSource::Loop)) + } + ExprKind::Catch(ref body) => { + // FIXME(cramertj): Add catch to HIR + self.with_catch_scope(e.id, |this| hir::ExprBlock(this.lower_block(body))) + } + ExprKind::Match(ref expr, ref arms) => { + hir::ExprMatch(P(self.lower_expr(expr)), + arms.iter().map(|x| self.lower_arm(x)).collect(), + hir::MatchSource::Normal) + } + ExprKind::Closure(capture_clause, ref decl, ref body, fn_decl_span) => { + self.with_new_scopes(|this| { + this.with_parent_def(e.id, |this| { + let expr = this.lower_expr(body); + hir::ExprClosure(this.lower_capture_clause(capture_clause), + this.lower_fn_decl(decl), + this.record_body(expr, Some(decl)), + fn_decl_span) }) - } - ExprKind::Block(ref blk) => hir::ExprBlock(self.lower_block(blk)), - ExprKind::Assign(ref el, ref er) => { - hir::ExprAssign(P(self.lower_expr(el)), P(self.lower_expr(er))) - } - ExprKind::AssignOp(op, ref el, ref er) => { - hir::ExprAssignOp(self.lower_binop(op), - P(self.lower_expr(el)), - P(self.lower_expr(er))) - } - ExprKind::Field(ref el, ident) => { - hir::ExprField(P(self.lower_expr(el)), respan(ident.span, ident.node.name)) - } - ExprKind::TupField(ref el, ident) => { - hir::ExprTupField(P(self.lower_expr(el)), ident) - } - ExprKind::Index(ref el, ref er) => { - hir::ExprIndex(P(self.lower_expr(el)), P(self.lower_expr(er))) - } - ExprKind::Range(ref e1, ref e2, lims) => { - fn make_struct(this: &mut LoweringContext, - ast_expr: &Expr, - path: &[&str], - fields: &[(&str, &P)]) -> hir::Expr { - let struct_path = &iter::once(&"ops").chain(path).map(|s| *s) - .collect::>(); - let unstable_span = this.allow_internal_unstable("...", ast_expr.span); - - if fields.len() == 0 { - this.expr_std_path(unstable_span, struct_path, - ast_expr.attrs.clone()) - } else { - let fields = fields.into_iter().map(|&(s, e)| { - let expr = P(this.lower_expr(&e)); - let unstable_span = this.allow_internal_unstable("...", e.span); - this.field(Symbol::intern(s), expr, unstable_span) - }).collect(); - let attrs = ast_expr.attrs.clone(); - - this.expr_std_struct(unstable_span, struct_path, fields, None, attrs) - } + }) + } + ExprKind::Block(ref blk) => hir::ExprBlock(self.lower_block(blk)), + ExprKind::Assign(ref el, ref er) => { + hir::ExprAssign(P(self.lower_expr(el)), P(self.lower_expr(er))) + } + ExprKind::AssignOp(op, ref el, ref er) => { + hir::ExprAssignOp(self.lower_binop(op), + P(self.lower_expr(el)), + P(self.lower_expr(er))) + } + ExprKind::Field(ref el, ident) => { + hir::ExprField(P(self.lower_expr(el)), respan(ident.span, ident.node.name)) + } + ExprKind::TupField(ref el, ident) => { + hir::ExprTupField(P(self.lower_expr(el)), ident) + } + ExprKind::Index(ref el, ref er) => { + hir::ExprIndex(P(self.lower_expr(el)), P(self.lower_expr(er))) + } + ExprKind::Range(ref e1, ref e2, lims) => { + fn make_struct(this: &mut LoweringContext, + ast_expr: &Expr, + path: &[&str], + fields: &[(&str, &P)]) -> hir::Expr { + let struct_path = &iter::once(&"ops").chain(path).map(|s| *s) + .collect::>(); + let unstable_span = this.allow_internal_unstable("...", ast_expr.span); + + if fields.len() == 0 { + this.expr_std_path(unstable_span, struct_path, + ast_expr.attrs.clone()) + } else { + let fields = fields.into_iter().map(|&(s, e)| { + let expr = P(this.lower_expr(&e)); + let unstable_span = this.allow_internal_unstable("...", e.span); + this.field(Symbol::intern(s), expr, unstable_span) + }).collect(); + let attrs = ast_expr.attrs.clone(); + + this.expr_std_struct(unstable_span, struct_path, fields, None, attrs) } + } - use syntax::ast::RangeLimits::*; + use syntax::ast::RangeLimits::*; - return match (e1, e2, lims) { - (&None, &None, HalfOpen) => - make_struct(self, e, &["RangeFull"], &[]), + return match (e1, e2, lims) { + (&None, &None, HalfOpen) => + make_struct(self, e, &["RangeFull"], &[]), - (&Some(ref e1), &None, HalfOpen) => - make_struct(self, e, &["RangeFrom"], - &[("start", e1)]), + (&Some(ref e1), &None, HalfOpen) => + make_struct(self, e, &["RangeFrom"], + &[("start", e1)]), - (&None, &Some(ref e2), HalfOpen) => - make_struct(self, e, &["RangeTo"], - &[("end", e2)]), + (&None, &Some(ref e2), HalfOpen) => + make_struct(self, e, &["RangeTo"], + &[("end", e2)]), - (&Some(ref e1), &Some(ref e2), HalfOpen) => - make_struct(self, e, &["Range"], - &[("start", e1), ("end", e2)]), + (&Some(ref e1), &Some(ref e2), HalfOpen) => + make_struct(self, e, &["Range"], + &[("start", e1), ("end", e2)]), - (&None, &Some(ref e2), Closed) => - make_struct(self, e, &["RangeToInclusive"], - &[("end", e2)]), + (&None, &Some(ref e2), Closed) => + make_struct(self, e, &["RangeToInclusive"], + &[("end", e2)]), - (&Some(ref e1), &Some(ref e2), Closed) => - make_struct(self, e, &["RangeInclusive", "NonEmpty"], - &[("start", e1), ("end", e2)]), + (&Some(ref e1), &Some(ref e2), Closed) => + make_struct(self, e, &["RangeInclusive", "NonEmpty"], + &[("start", e1), ("end", e2)]), - _ => panic!(self.diagnostic() - .span_fatal(e.span, "inclusive range with no end")), - }; - } - ExprKind::Path(ref qself, ref path) => { - hir::ExprPath(self.lower_qpath(e.id, qself, path, ParamMode::Optional)) - } - ExprKind::Break(opt_ident, ref opt_expr) => { - let label_result = if self.is_in_loop_condition && opt_ident.is_none() { + _ => panic!(self.diagnostic() + .span_fatal(e.span, "inclusive range with no end")), + }; + } + ExprKind::Path(ref qself, ref path) => { + hir::ExprPath(self.lower_qpath(e.id, qself, path, ParamMode::Optional)) + } + ExprKind::Break(opt_ident, ref opt_expr) => { + let label_result = if self.is_in_loop_condition && opt_ident.is_none() { + hir::Destination { + ident: opt_ident, + loop_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(), + } + } else { + self.lower_destination(opt_ident.map(|ident| (e.id, ident))) + }; + hir::ExprBreak( + label_result, + opt_expr.as_ref().map(|x| P(self.lower_expr(x)))) + } + ExprKind::Continue(opt_ident) => + hir::ExprAgain( + if self.is_in_loop_condition && opt_ident.is_none() { hir::Destination { ident: opt_ident, - loop_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(), + loop_id: Err( + hir::LoopIdError::UnlabeledCfInWhileCondition).into(), } } else { - self.lower_destination(opt_ident.map(|ident| (e.id, ident))) - }; - hir::ExprBreak( - label_result, - opt_expr.as_ref().map(|x| P(self.lower_expr(x)))) - } - ExprKind::Continue(opt_ident) => - hir::ExprAgain( - if self.is_in_loop_condition && opt_ident.is_none() { - hir::Destination { - ident: opt_ident, - loop_id: Err( - hir::LoopIdError::UnlabeledCfInWhileCondition).into(), - } + self.lower_destination(opt_ident.map( |ident| (e.id, ident))) + }), + ExprKind::Ret(ref e) => hir::ExprRet(e.as_ref().map(|x| P(self.lower_expr(x)))), + ExprKind::InlineAsm(ref asm) => { + let hir_asm = hir::InlineAsm { + inputs: asm.inputs.iter().map(|&(ref c, _)| c.clone()).collect(), + outputs: asm.outputs.iter().map(|out| { + hir::InlineAsmOutput { + constraint: out.constraint.clone(), + is_rw: out.is_rw, + is_indirect: out.is_indirect, + } + }).collect(), + asm: asm.asm.clone(), + asm_str_style: asm.asm_str_style, + clobbers: asm.clobbers.clone().into(), + volatile: asm.volatile, + alignstack: asm.alignstack, + dialect: asm.dialect, + expn_id: asm.expn_id, + }; + let outputs = + asm.outputs.iter().map(|out| self.lower_expr(&out.expr)).collect(); + let inputs = + asm.inputs.iter().map(|&(_, ref input)| self.lower_expr(input)).collect(); + hir::ExprInlineAsm(P(hir_asm), outputs, inputs) + } + ExprKind::Struct(ref path, ref fields, ref maybe_expr) => { + hir::ExprStruct(self.lower_qpath(e.id, &None, path, ParamMode::Optional), + fields.iter().map(|x| self.lower_field(x)).collect(), + maybe_expr.as_ref().map(|x| P(self.lower_expr(x)))) + } + ExprKind::Paren(ref ex) => { + let mut ex = self.lower_expr(ex); + // include parens in span, but only if it is a super-span. + if e.span.contains(ex.span) { + ex.span = e.span; + } + // merge attributes into the inner expression. + let mut attrs = e.attrs.clone(); + attrs.extend::>(ex.attrs.into()); + ex.attrs = attrs; + return ex; + } + + // Desugar ExprIfLet + // From: `if let = []` + ExprKind::IfLet(ref pat, ref sub_expr, ref body, ref else_opt) => { + // to: + // + // match { + // => , + // [_ if => ,] + // _ => [ | ()] + // } + + let mut arms = vec![]; + + // ` => ` + { + let body = self.lower_block(body); + let body_expr = P(self.expr_block(body, ThinVec::new())); + let pat = self.lower_pat(pat); + arms.push(self.arm(hir_vec![pat], body_expr)); + } + + // `[_ if => ,]` + // `_ => [ | ()]` + { + let mut current: Option<&Expr> = else_opt.as_ref().map(|p| &**p); + let mut else_exprs: Vec> = vec![current]; + + // First, we traverse the AST and recursively collect all + // `else` branches into else_exprs, e.g.: + // + // if let Some(_) = x { + // ... + // } else if ... { // Expr1 + // ... + // } else if ... { // Expr2 + // ... + // } else { // Expr3 + // ... + // } + // + // ... results in else_exprs = [Some(&Expr1), + // Some(&Expr2), + // Some(&Expr3)] + // + // Because there also the case there is no `else`, these + // entries can also be `None`, as in: + // + // if let Some(_) = x { + // ... + // } else if ... { // Expr1 + // ... + // } else if ... { // Expr2 + // ... + // } + // + // ... results in else_exprs = [Some(&Expr1), + // Some(&Expr2), + // None] + // + // The last entry in this list is always translated into + // the final "unguard" wildcard arm of the `match`. In the + // case of a `None`, it becomes `_ => ()`. + loop { + if let Some(e) = current { + // There is an else branch at this level + if let ExprKind::If(_, _, ref else_opt) = e.node { + // The else branch is again an if-expr + current = else_opt.as_ref().map(|p| &**p); + else_exprs.push(current); + } else { + // The last item in the list is not an if-expr, + // stop here + break + } } else { - self.lower_destination(opt_ident.map( |ident| (e.id, ident))) - }), - ExprKind::Ret(ref e) => hir::ExprRet(e.as_ref().map(|x| P(self.lower_expr(x)))), - ExprKind::InlineAsm(ref asm) => { - let hir_asm = hir::InlineAsm { - inputs: asm.inputs.iter().map(|&(ref c, _)| c.clone()).collect(), - outputs: asm.outputs.iter().map(|out| { - hir::InlineAsmOutput { - constraint: out.constraint.clone(), - is_rw: out.is_rw, - is_indirect: out.is_indirect, - } - }).collect(), - asm: asm.asm.clone(), - asm_str_style: asm.asm_str_style, - clobbers: asm.clobbers.clone().into(), - volatile: asm.volatile, - alignstack: asm.alignstack, - dialect: asm.dialect, - expn_id: asm.expn_id, - }; - let outputs = - asm.outputs.iter().map(|out| self.lower_expr(&out.expr)).collect(); - let inputs = - asm.inputs.iter().map(|&(_, ref input)| self.lower_expr(input)).collect(); - hir::ExprInlineAsm(P(hir_asm), outputs, inputs) - } - ExprKind::Struct(ref path, ref fields, ref maybe_expr) => { - hir::ExprStruct(self.lower_qpath(e.id, &None, path, ParamMode::Optional), - fields.iter().map(|x| self.lower_field(x)).collect(), - maybe_expr.as_ref().map(|x| P(self.lower_expr(x)))) - } - ExprKind::Paren(ref ex) => { - let mut ex = self.lower_expr(ex); - // include parens in span, but only if it is a super-span. - if e.span.contains(ex.span) { - ex.span = e.span; + // We have no more else branch + break + } } - // merge attributes into the inner expression. - let mut attrs = e.attrs.clone(); - attrs.extend::>(ex.attrs.into()); - ex.attrs = attrs; - return ex; - } - // Desugar ExprIfLet - // From: `if let = []` - ExprKind::IfLet(ref pat, ref sub_expr, ref body, ref else_opt) => { - // to: - // - // match { - // => , - // [_ if => ,] - // _ => [ | ()] - // } - - // ` => ` - let pat_arm = { - let body = self.lower_block(body); - let body_expr = P(self.expr_block(body, ThinVec::new())); - let pat = self.lower_pat(pat); - self.arm(hir_vec![pat], body_expr) - }; + // Now translate the list of nested else-branches into the + // arms of the match statement. + for else_expr in else_exprs { + if let Some(else_expr) = else_expr { + let (guard, body) = if let ExprKind::If(ref cond, + ref then, + _) = else_expr.node { + let then = self.lower_block(then); + (Some(cond), + self.expr_block(then, ThinVec::new())) + } else { + (None, + self.lower_expr(else_expr)) + }; - // `[_ if => ,]` - let mut else_opt = else_opt.as_ref().map(|e| P(self.lower_expr(e))); - let else_if_arms = { - let mut arms = vec![]; - loop { - let else_opt_continue = else_opt.and_then(|els| { - els.and_then(|els| { - match els.node { - // else if - hir::ExprIf(cond, then, else_opt) => { - let pat_under = self.pat_wild(e.span); - arms.push(hir::Arm { - attrs: hir_vec![], - pats: hir_vec![pat_under], - guard: Some(cond), - body: P(self.expr_block(then, ThinVec::new())), - }); - else_opt.map(|else_opt| (else_opt, true)) - } - _ => Some((P(els), false)), - } - }) + arms.push(hir::Arm { + attrs: hir_vec![], + pats: hir_vec![self.pat_wild(e.span)], + guard: guard.map(|e| P(self.lower_expr(e))), + body: P(body), }); - match else_opt_continue { - Some((e, true)) => { - else_opt = Some(e); - } - Some((e, false)) => { - else_opt = Some(e); - break; - } - None => { - else_opt = None; - break; - } - } + } else { + // There was no else-branch, push a noop + let pat_under = self.pat_wild(e.span); + let unit = self.expr_tuple(e.span, hir_vec![]); + arms.push(self.arm(hir_vec![pat_under], unit)); } - arms - }; + } + } - let contains_else_clause = else_opt.is_some(); + let contains_else_clause = else_opt.is_some(); - // `_ => [ | ()]` - let else_arm = { - let pat_under = self.pat_wild(e.span); - let else_expr = - else_opt.unwrap_or_else(|| self.expr_tuple(e.span, hir_vec![])); - self.arm(hir_vec![pat_under], else_expr) - }; + let sub_expr = P(self.lower_expr(sub_expr)); - let mut arms = Vec::with_capacity(else_if_arms.len() + 2); - arms.push(pat_arm); - arms.extend(else_if_arms); - arms.push(else_arm); - - let sub_expr = P(self.lower_expr(sub_expr)); - // add attributes to the outer returned expr node - return self.expr(e.span, - hir::ExprMatch(sub_expr, - arms.into(), - hir::MatchSource::IfLetDesugar { - contains_else_clause: contains_else_clause, - }), - e.attrs.clone()); - } + hir::ExprMatch( + sub_expr, + arms.into(), + hir::MatchSource::IfLetDesugar { + contains_else_clause: contains_else_clause, + }) + } - // Desugar ExprWhileLet - // From: `[opt_ident]: while let = ` - ExprKind::WhileLet(ref pat, ref sub_expr, ref body, opt_ident) => { - // to: - // - // [opt_ident]: loop { - // match { - // => , - // _ => break - // } - // } - - // Note that the block AND the condition are evaluated in the loop scope. - // This is done to allow `break` from inside the condition of the loop. - let (body, break_expr, sub_expr) = self.with_loop_scope(e.id, |this| ( - this.lower_block(body), - this.expr_break(e.span, ThinVec::new()), - this.with_loop_condition_scope(|this| P(this.lower_expr(sub_expr))), - )); - - // ` => ` - let pat_arm = { - let body_expr = P(self.expr_block(body, ThinVec::new())); - let pat = self.lower_pat(pat); - self.arm(hir_vec![pat], body_expr) - }; + // Desugar ExprWhileLet + // From: `[opt_ident]: while let = ` + ExprKind::WhileLet(ref pat, ref sub_expr, ref body, opt_ident) => { + // to: + // + // [opt_ident]: loop { + // match { + // => , + // _ => break + // } + // } + + // Note that the block AND the condition are evaluated in the loop scope. + // This is done to allow `break` from inside the condition of the loop. + let (body, break_expr, sub_expr) = self.with_loop_scope(e.id, |this| ( + this.lower_block(body), + this.expr_break(e.span, ThinVec::new()), + this.with_loop_condition_scope(|this| P(this.lower_expr(sub_expr))), + )); + + // ` => ` + let pat_arm = { + let body_expr = P(self.expr_block(body, ThinVec::new())); + let pat = self.lower_pat(pat); + self.arm(hir_vec![pat], body_expr) + }; - // `_ => break` - let break_arm = { - let pat_under = self.pat_wild(e.span); - self.arm(hir_vec![pat_under], break_expr) - }; + // `_ => break` + let break_arm = { + let pat_under = self.pat_wild(e.span); + self.arm(hir_vec![pat_under], break_expr) + }; - // `match { ... }` + // `match { ... }` + let arms = hir_vec![pat_arm, break_arm]; + let match_expr = self.expr(e.span, + hir::ExprMatch(sub_expr, + arms, + hir::MatchSource::WhileLetDesugar), + ThinVec::new()); + + // `[opt_ident]: loop { ... }` + let loop_block = P(self.block_expr(P(match_expr))); + let loop_expr = hir::ExprLoop(loop_block, self.lower_opt_sp_ident(opt_ident), + hir::LoopSource::WhileLet); + // add attributes to the outer returned expr node + loop_expr + } + + // Desugar ExprForLoop + // From: `[opt_ident]: for in ` + ExprKind::ForLoop(ref pat, ref head, ref body, opt_ident) => { + // to: + // + // { + // let result = match ::std::iter::IntoIterator::into_iter() { + // mut iter => { + // [opt_ident]: loop { + // match ::std::iter::Iterator::next(&mut iter) { + // ::std::option::Option::Some() => , + // ::std::option::Option::None => break + // } + // } + // } + // }; + // result + // } + + // expand + let head = self.lower_expr(head); + + let iter = self.str_to_ident("iter"); + + // `::std::option::Option::Some() => ` + let pat_arm = { + let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body)); + let body_expr = P(self.expr_block(body_block, ThinVec::new())); + let pat = self.lower_pat(pat); + let some_pat = self.pat_some(e.span, pat); + + self.arm(hir_vec![some_pat], body_expr) + }; + + // `::std::option::Option::None => break` + let break_arm = { + let break_expr = self.with_loop_scope(e.id, |this| + this.expr_break(e.span, ThinVec::new())); + let pat = self.pat_none(e.span); + self.arm(hir_vec![pat], break_expr) + }; + + // `mut iter` + let iter_pat = self.pat_ident_binding_mode(e.span, iter, + hir::BindByValue(hir::MutMutable)); + + // `match ::std::iter::Iterator::next(&mut iter) { ... }` + let match_expr = { + let iter = P(self.expr_ident(e.span, iter, iter_pat.id)); + let ref_mut_iter = self.expr_mut_addr_of(e.span, iter); + let next_path = &["iter", "Iterator", "next"]; + let next_path = P(self.expr_std_path(e.span, next_path, ThinVec::new())); + let next_expr = P(self.expr_call(e.span, next_path, + hir_vec![ref_mut_iter])); let arms = hir_vec![pat_arm, break_arm]; - let match_expr = self.expr(e.span, - hir::ExprMatch(sub_expr, - arms, - hir::MatchSource::WhileLetDesugar), - ThinVec::new()); - - // `[opt_ident]: loop { ... }` - let loop_block = P(self.block_expr(P(match_expr))); - let loop_expr = hir::ExprLoop(loop_block, self.lower_opt_sp_ident(opt_ident), - hir::LoopSource::WhileLet); - // add attributes to the outer returned expr node - let attrs = e.attrs.clone(); - return hir::Expr { id: e.id, node: loop_expr, span: e.span, attrs: attrs }; - } - // Desugar ExprForLoop - // From: `[opt_ident]: for in ` - ExprKind::ForLoop(ref pat, ref head, ref body, opt_ident) => { - // to: - // - // { - // let result = match ::std::iter::IntoIterator::into_iter() { - // mut iter => { - // [opt_ident]: loop { - // match ::std::iter::Iterator::next(&mut iter) { - // ::std::option::Option::Some() => , - // ::std::option::Option::None => break - // } - // } - // } - // }; - // result - // } - - // expand - let head = self.lower_expr(head); - - let iter = self.str_to_ident("iter"); - - // `::std::option::Option::Some() => ` - let pat_arm = { - let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body)); - let body_expr = P(self.expr_block(body_block, ThinVec::new())); - let pat = self.lower_pat(pat); - let some_pat = self.pat_some(e.span, pat); - - self.arm(hir_vec![some_pat], body_expr) - }; + P(self.expr(e.span, + hir::ExprMatch(next_expr, arms, + hir::MatchSource::ForLoopDesugar), + ThinVec::new())) + }; - // `::std::option::Option::None => break` - let break_arm = { - let break_expr = self.with_loop_scope(e.id, |this| - this.expr_break(e.span, ThinVec::new())); - let pat = self.pat_none(e.span); - self.arm(hir_vec![pat], break_expr) - }; + // `[opt_ident]: loop { ... }` + let loop_block = P(self.block_expr(match_expr)); + let loop_expr = hir::ExprLoop(loop_block, self.lower_opt_sp_ident(opt_ident), + hir::LoopSource::ForLoop); + let loop_expr = P(hir::Expr { + id: self.lower_node_id(e.id), + node: loop_expr, + span: e.span, + attrs: ThinVec::new(), + }); - // `mut iter` - let iter_pat = self.pat_ident_binding_mode(e.span, iter, - hir::BindByValue(hir::MutMutable)); - - // `match ::std::iter::Iterator::next(&mut iter) { ... }` - let match_expr = { - let iter = P(self.expr_ident(e.span, iter, iter_pat.id)); - let ref_mut_iter = self.expr_mut_addr_of(e.span, iter); - let next_path = &["iter", "Iterator", "next"]; - let next_path = P(self.expr_std_path(e.span, next_path, ThinVec::new())); - let next_expr = P(self.expr_call(e.span, next_path, - hir_vec![ref_mut_iter])); - let arms = hir_vec![pat_arm, break_arm]; - - P(self.expr(e.span, - hir::ExprMatch(next_expr, arms, - hir::MatchSource::ForLoopDesugar), - ThinVec::new())) - }; + // `mut iter => { ... }` + let iter_arm = self.arm(hir_vec![iter_pat], loop_expr); - // `[opt_ident]: loop { ... }` - let loop_block = P(self.block_expr(match_expr)); - let loop_expr = hir::ExprLoop(loop_block, self.lower_opt_sp_ident(opt_ident), - hir::LoopSource::ForLoop); - let loop_expr = P(hir::Expr { - id: e.id, - node: loop_expr, - span: e.span, - attrs: ThinVec::new(), - }); - - // `mut iter => { ... }` - let iter_arm = self.arm(hir_vec![iter_pat], loop_expr); - - // `match ::std::iter::IntoIterator::into_iter() { ... }` - let into_iter_expr = { - let into_iter_path = &["iter", "IntoIterator", "into_iter"]; - let into_iter = P(self.expr_std_path(e.span, into_iter_path, - ThinVec::new())); - P(self.expr_call(e.span, into_iter, hir_vec![head])) - }; + // `match ::std::iter::IntoIterator::into_iter() { ... }` + let into_iter_expr = { + let into_iter_path = &["iter", "IntoIterator", "into_iter"]; + let into_iter = P(self.expr_std_path(e.span, into_iter_path, + ThinVec::new())); + P(self.expr_call(e.span, into_iter, hir_vec![head])) + }; - let match_expr = P(self.expr_match(e.span, - into_iter_expr, - hir_vec![iter_arm], - hir::MatchSource::ForLoopDesugar)); - - // `{ let _result = ...; _result }` - // underscore prevents an unused_variables lint if the head diverges - let result_ident = self.str_to_ident("_result"); - let (let_stmt, let_stmt_binding) = - self.stmt_let(e.span, false, result_ident, match_expr); - - let result = P(self.expr_ident(e.span, result_ident, let_stmt_binding)); - let block = P(self.block_all(e.span, hir_vec![let_stmt], Some(result))); - // add the attributes to the outer returned expr node - return self.expr_block(block, e.attrs.clone()); - } + let match_expr = P(self.expr_match(e.span, + into_iter_expr, + hir_vec![iter_arm], + hir::MatchSource::ForLoopDesugar)); + + // `{ let _result = ...; _result }` + // underscore prevents an unused_variables lint if the head diverges + let result_ident = self.str_to_ident("_result"); + let (let_stmt, let_stmt_binding) = + self.stmt_let(e.span, false, result_ident, match_expr); + + let result = P(self.expr_ident(e.span, result_ident, let_stmt_binding)); + let block = P(self.block_all(e.span, hir_vec![let_stmt], Some(result))); + // add the attributes to the outer returned expr node + return self.expr_block(block, e.attrs.clone()); + } - // Desugar ExprKind::Try - // From: `?` - ExprKind::Try(ref sub_expr) => { - // to: - // - // match Carrier::translate() { - // Ok(val) => #[allow(unreachable_code)] val, - // Err(err) => #[allow(unreachable_code)] - // return Carrier::from_error(From::from(err)), - // } + // Desugar ExprKind::Try + // From: `?` + ExprKind::Try(ref sub_expr) => { + // to: + // + // match Carrier::translate() { + // Ok(val) => #[allow(unreachable_code)] val, + // Err(err) => #[allow(unreachable_code)] + // return Carrier::from_error(From::from(err)), + // } - // FIXME(cramertj): implement breaking to catch - if !self.catch_scopes.is_empty() { - bug!("`?` in catch scopes is unimplemented") - } + // FIXME(cramertj): implement breaking to catch + if !self.catch_scopes.is_empty() { + bug!("`?` in catch scopes is unimplemented") + } - let unstable_span = self.allow_internal_unstable("?", e.span); + let unstable_span = self.allow_internal_unstable("?", e.span); - // Carrier::translate() - let discr = { - // expand - let sub_expr = self.lower_expr(sub_expr); + // Carrier::translate() + let discr = { + // expand + let sub_expr = self.lower_expr(sub_expr); - let path = &["ops", "Carrier", "translate"]; - let path = P(self.expr_std_path(unstable_span, path, ThinVec::new())); - P(self.expr_call(e.span, path, hir_vec![sub_expr])) + let path = &["ops", "Carrier", "translate"]; + let path = P(self.expr_std_path(unstable_span, path, ThinVec::new())); + P(self.expr_call(e.span, path, hir_vec![sub_expr])) + }; + + // #[allow(unreachable_code)] + let attr = { + // allow(unreachable_code) + let allow = { + let allow_ident = self.str_to_ident("allow"); + let uc_ident = self.str_to_ident("unreachable_code"); + let uc_meta_item = attr::mk_spanned_word_item(e.span, uc_ident); + let uc_nested = NestedMetaItemKind::MetaItem(uc_meta_item); + let uc_spanned = respan(e.span, uc_nested); + attr::mk_spanned_list_item(e.span, allow_ident, vec![uc_spanned]) }; + attr::mk_spanned_attr_outer(e.span, attr::mk_attr_id(), allow) + }; + let attrs = vec![attr]; + + // Ok(val) => #[allow(unreachable_code)] val, + let ok_arm = { + let val_ident = self.str_to_ident("val"); + let val_pat = self.pat_ident(e.span, val_ident); + let val_expr = P(self.expr_ident_with_attrs(e.span, + val_ident, + val_pat.id, + ThinVec::from(attrs.clone()))); + let ok_pat = self.pat_ok(e.span, val_pat); + + self.arm(hir_vec![ok_pat], val_expr) + }; - // #[allow(unreachable_code)] - let attr = { - // allow(unreachable_code) - let allow = { - let allow_ident = self.str_to_ident("allow"); - let uc_ident = self.str_to_ident("unreachable_code"); - let uc_meta_item = attr::mk_spanned_word_item(e.span, uc_ident); - let uc_nested = NestedMetaItemKind::MetaItem(uc_meta_item); - let uc_spanned = respan(e.span, uc_nested); - attr::mk_spanned_list_item(e.span, allow_ident, vec![uc_spanned]) - }; - attr::mk_spanned_attr_outer(e.span, attr::mk_attr_id(), allow) + // Err(err) => #[allow(unreachable_code)] + // return Carrier::from_error(From::from(err)), + let err_arm = { + let err_ident = self.str_to_ident("err"); + let err_local = self.pat_ident(e.span, err_ident); + let from_expr = { + let path = &["convert", "From", "from"]; + let from = P(self.expr_std_path(e.span, path, ThinVec::new())); + let err_expr = self.expr_ident(e.span, err_ident, err_local.id); + + self.expr_call(e.span, from, hir_vec![err_expr]) }; - let attrs = vec![attr]; - - // Ok(val) => #[allow(unreachable_code)] val, - let ok_arm = { - let val_ident = self.str_to_ident("val"); - let val_pat = self.pat_ident(e.span, val_ident); - let val_expr = P(self.expr_ident_with_attrs(e.span, - val_ident, - val_pat.id, - ThinVec::from(attrs.clone()))); - let ok_pat = self.pat_ok(e.span, val_pat); - - self.arm(hir_vec![ok_pat], val_expr) + let from_err_expr = { + let path = &["ops", "Carrier", "from_error"]; + let from_err = P(self.expr_std_path(unstable_span, path, + ThinVec::new())); + P(self.expr_call(e.span, from_err, hir_vec![from_expr])) }; - // Err(err) => #[allow(unreachable_code)] - // return Carrier::from_error(From::from(err)), - let err_arm = { - let err_ident = self.str_to_ident("err"); - let err_local = self.pat_ident(e.span, err_ident); - let from_expr = { - let path = &["convert", "From", "from"]; - let from = P(self.expr_std_path(e.span, path, ThinVec::new())); - let err_expr = self.expr_ident(e.span, err_ident, err_local.id); - - self.expr_call(e.span, from, hir_vec![err_expr]) - }; - let from_err_expr = { - let path = &["ops", "Carrier", "from_error"]; - let from_err = P(self.expr_std_path(unstable_span, path, - ThinVec::new())); - P(self.expr_call(e.span, from_err, hir_vec![from_expr])) - }; + let ret_expr = P(self.expr(e.span, + hir::Expr_::ExprRet(Some(from_err_expr)), + ThinVec::from(attrs))); - let ret_expr = P(self.expr(e.span, - hir::Expr_::ExprRet(Some(from_err_expr)), - ThinVec::from(attrs))); + let err_pat = self.pat_err(e.span, err_local); + self.arm(hir_vec![err_pat], ret_expr) + }; - let err_pat = self.pat_err(e.span, err_local); - self.arm(hir_vec![err_pat], ret_expr) - }; + hir::ExprMatch(discr, + hir_vec![err_arm, ok_arm], + hir::MatchSource::TryDesugar) + } - return self.expr_match(e.span, discr, hir_vec![err_arm, ok_arm], - hir::MatchSource::TryDesugar); - } + ExprKind::Mac(_) => panic!("Shouldn't exist here"), + }; - ExprKind::Mac(_) => panic!("Shouldn't exist here"), - }, + + hir::Expr { + id: self.lower_node_id(e.id), + node: kind, span: e.span, attrs: e.attrs.clone(), } @@ -2182,7 +2395,7 @@ impl<'a> LoweringContext<'a> { node: hir::StmtDecl(P(Spanned { node: hir::DeclLocal(self.lower_local(l)), span: s.span, - }), s.id), + }), self.lower_node_id(s.id)), span: s.span, }, StmtKind::Item(ref it) => { @@ -2192,19 +2405,23 @@ impl<'a> LoweringContext<'a> { node: hir::StmtDecl(P(Spanned { node: hir::DeclItem(item_id), span: s.span, - }), id.take().unwrap_or_else(|| self.next_id())), + }), id.take() + .map(|id| self.lower_node_id(id)) + .unwrap_or_else(|| self.next_id())), span: s.span, }).collect(); } StmtKind::Expr(ref e) => { Spanned { - node: hir::StmtExpr(P(self.lower_expr(e)), s.id), + node: hir::StmtExpr(P(self.lower_expr(e)), + self.lower_node_id(s.id)), span: s.span, } } StmtKind::Semi(ref e) => { Spanned { - node: hir::StmtSemi(P(self.lower_expr(e)), s.id), + node: hir::StmtSemi(P(self.lower_expr(e)), + self.lower_node_id(s.id)), span: s.span, } } @@ -2219,14 +2436,26 @@ impl<'a> LoweringContext<'a> { } } - fn lower_visibility(&mut self, v: &Visibility) -> hir::Visibility { + /// If an `explicit_owner` is given, this method allocates the `HirId` in + /// the address space of that item instead of the item currently being + /// lowered. This can happen during `lower_impl_item_ref()` where we need to + /// lower a `Visibility` value although we haven't lowered the owning + /// `ImplItem` in question yet. + fn lower_visibility(&mut self, + v: &Visibility, + explicit_owner: Option) + -> hir::Visibility { match *v { Visibility::Public => hir::Public, Visibility::Crate(_) => hir::Visibility::Crate, Visibility::Restricted { ref path, id } => { hir::Visibility::Restricted { path: P(self.lower_path(id, path, ParamMode::Explicit, true)), - id: id + id: if let Some(owner) = explicit_owner { + self.lower_node_id_with_owner(id, owner) + } else { + self.lower_node_id(id) + } } } Visibility::Inherited => hir::Inherited, @@ -2460,7 +2689,10 @@ impl<'a> LoweringContext<'a> { let def_id = { let defs = self.resolver.definitions(); let def_path_data = DefPathData::Binding(name.as_str()); - let def_index = defs.create_def_with_parent(parent_def, id, def_path_data); + let def_index = defs.create_def_with_parent(parent_def, + id, + def_path_data, + REGULAR_SPACE); DefId::local(def_index) }; diff --git a/src/librustc/hir/map/def_collector.rs b/src/librustc/hir/map/def_collector.rs index f15e063e81e33..cae358a303e02 100644 --- a/src/librustc/hir/map/def_collector.rs +++ b/src/librustc/hir/map/def_collector.rs @@ -9,13 +9,15 @@ // except according to those terms. use hir::map::definitions::*; -use hir::def_id::{CRATE_DEF_INDEX, DefIndex}; +use hir::def_id::{CRATE_DEF_INDEX, DefIndex, DefIndexAddressSpace}; use syntax::ast::*; use syntax::ext::hygiene::Mark; use syntax::visit; use syntax::symbol::{Symbol, keywords}; +use hir::map::{ITEM_LIKE_SPACE, REGULAR_SPACE}; + /// Creates def ids for nodes in the AST. pub struct DefCollector<'a> { definitions: &'a mut Definitions, @@ -39,23 +41,31 @@ impl<'a> DefCollector<'a> { } pub fn collect_root(&mut self) { - let root = self.create_def_with_parent(None, CRATE_NODE_ID, DefPathData::CrateRoot); + let root = self.create_def_with_parent(None, + CRATE_NODE_ID, + DefPathData::CrateRoot, + ITEM_LIKE_SPACE); assert_eq!(root, CRATE_DEF_INDEX); self.parent_def = Some(root); } - fn create_def(&mut self, node_id: NodeId, data: DefPathData) -> DefIndex { + fn create_def(&mut self, + node_id: NodeId, + data: DefPathData, + address_space: DefIndexAddressSpace) + -> DefIndex { let parent_def = self.parent_def; debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def); - self.definitions.create_def_with_parent(parent_def, node_id, data) + self.definitions.create_def_with_parent(parent_def, node_id, data, address_space) } fn create_def_with_parent(&mut self, parent: Option, node_id: NodeId, - data: DefPathData) + data: DefPathData, + address_space: DefIndexAddressSpace) -> DefIndex { - self.definitions.create_def_with_parent(parent, node_id, data) + self.definitions.create_def_with_parent(parent, node_id, data, address_space) } pub fn with_parent(&mut self, parent_def: DefIndex, f: F) { @@ -76,7 +86,7 @@ impl<'a> DefCollector<'a> { _ => {} } - self.create_def(expr.id, DefPathData::Initializer); + self.create_def(expr.id, DefPathData::Initializer, REGULAR_SPACE); } fn visit_macro_invoc(&mut self, id: NodeId, const_expr: bool) { @@ -118,14 +128,16 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { ViewPathSimple(..) => {} ViewPathList(_, ref imports) => { for import in imports { - self.create_def(import.node.id, DefPathData::Misc); + self.create_def(import.node.id, + DefPathData::Misc, + ITEM_LIKE_SPACE); } } } DefPathData::Misc } }; - let def = self.create_def(i.id, def_data); + let def = self.create_def(i.id, def_data, ITEM_LIKE_SPACE); self.with_parent(def, |this| { match i.node { @@ -133,12 +145,15 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { for v in &enum_definition.variants { let variant_def_index = this.create_def(v.node.data.id(), - DefPathData::EnumVariant(v.node.name.name.as_str())); + DefPathData::EnumVariant(v.node.name.name.as_str()), + REGULAR_SPACE); this.with_parent(variant_def_index, |this| { for (index, field) in v.node.data.fields().iter().enumerate() { let name = field.ident.map(|ident| ident.name) .unwrap_or_else(|| Symbol::intern(&index.to_string())); - this.create_def(field.id, DefPathData::Field(name.as_str())); + this.create_def(field.id, + DefPathData::Field(name.as_str()), + REGULAR_SPACE); } if let Some(ref expr) = v.node.disr_expr { @@ -151,13 +166,14 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { // If this is a tuple-like struct, register the constructor. if !struct_def.is_struct() { this.create_def(struct_def.id(), - DefPathData::StructCtor); + DefPathData::StructCtor, + REGULAR_SPACE); } for (index, field) in struct_def.fields().iter().enumerate() { let name = field.ident.map(|ident| ident.name.as_str()) .unwrap_or(Symbol::intern(&index.to_string()).as_str()); - this.create_def(field.id, DefPathData::Field(name)); + this.create_def(field.id, DefPathData::Field(name), REGULAR_SPACE); } } _ => {} @@ -168,7 +184,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) { let def = self.create_def(foreign_item.id, - DefPathData::ValueNs(foreign_item.ident.name.as_str())); + DefPathData::ValueNs(foreign_item.ident.name.as_str()), + REGULAR_SPACE); self.with_parent(def, |this| { visit::walk_foreign_item(this, foreign_item); @@ -177,7 +194,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { fn visit_generics(&mut self, generics: &'a Generics) { for ty_param in generics.ty_params.iter() { - self.create_def(ty_param.id, DefPathData::TypeParam(ty_param.ident.name.as_str())); + self.create_def(ty_param.id, + DefPathData::TypeParam(ty_param.ident.name.as_str()), + REGULAR_SPACE); } visit::walk_generics(self, generics); @@ -191,7 +210,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id, false), }; - let def = self.create_def(ti.id, def_data); + let def = self.create_def(ti.id, def_data, ITEM_LIKE_SPACE); self.with_parent(def, |this| { if let TraitItemKind::Const(_, Some(ref expr)) = ti.node { this.visit_const_expr(expr); @@ -209,7 +228,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id, false), }; - let def = self.create_def(ii.id, def_data); + let def = self.create_def(ii.id, def_data, ITEM_LIKE_SPACE); self.with_parent(def, |this| { if let ImplItemKind::Const(_, ref expr) = ii.node { this.visit_const_expr(expr); @@ -225,7 +244,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { match pat.node { PatKind::Mac(..) => return self.visit_macro_invoc(pat.id, false), PatKind::Ident(_, id, _) => { - let def = self.create_def(pat.id, DefPathData::Binding(id.node.name.as_str())); + let def = self.create_def(pat.id, + DefPathData::Binding(id.node.name.as_str()), + REGULAR_SPACE); self.parent_def = Some(def); } _ => {} @@ -242,7 +263,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { ExprKind::Mac(..) => return self.visit_macro_invoc(expr.id, false), ExprKind::Repeat(_, ref count) => self.visit_const_expr(count), ExprKind::Closure(..) => { - let def = self.create_def(expr.id, DefPathData::ClosureExpr); + let def = self.create_def(expr.id, + DefPathData::ClosureExpr, + REGULAR_SPACE); self.parent_def = Some(def); } _ => {} @@ -257,7 +280,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { TyKind::Mac(..) => return self.visit_macro_invoc(ty.id, false), TyKind::Array(_, ref length) => self.visit_const_expr(length), TyKind::ImplTrait(..) => { - self.create_def(ty.id, DefPathData::ImplTrait); + self.create_def(ty.id, DefPathData::ImplTrait, REGULAR_SPACE); } TyKind::Typeof(ref expr) => self.visit_const_expr(expr), _ => {} @@ -266,7 +289,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { } fn visit_lifetime_def(&mut self, def: &'a LifetimeDef) { - self.create_def(def.lifetime.id, DefPathData::LifetimeDef(def.lifetime.name.as_str())); + self.create_def(def.lifetime.id, + DefPathData::LifetimeDef(def.lifetime.name.as_str()), + REGULAR_SPACE); } fn visit_stmt(&mut self, stmt: &'a Stmt) { diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index bf52a036cc8b6..809d5db3071d7 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -14,8 +14,10 @@ //! There are also some rather random cases (like const initializer //! expressions) that are mostly just leftovers. -use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE}; +use hir; +use hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE, DefIndexAddressSpace}; use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::indexed_vec::IndexVec; use rustc_data_structures::stable_hasher::StableHasher; use serialize::{Encodable, Decodable, Encoder, Decoder}; use std::fmt::Write; @@ -29,24 +31,44 @@ use util::nodemap::NodeMap; /// Internally the DefPathTable holds a tree of DefKeys, where each DefKey /// stores the DefIndex of its parent. /// There is one DefPathTable for each crate. -#[derive(Clone)] pub struct DefPathTable { - index_to_key: Vec, + index_to_key: [Vec; 2], key_to_index: FxHashMap, } +// Unfortunately we have to provide a manual impl of Clone because of the +// fixed-sized array field. +impl Clone for DefPathTable { + fn clone(&self) -> Self { + DefPathTable { + index_to_key: [self.index_to_key[0].clone(), + self.index_to_key[1].clone()], + key_to_index: self.key_to_index.clone(), + } + } +} + impl DefPathTable { - fn insert(&mut self, key: DefKey) -> DefIndex { - let index = DefIndex::new(self.index_to_key.len()); - debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index); - self.index_to_key.push(key.clone()); + + fn allocate(&mut self, + key: DefKey, + address_space: DefIndexAddressSpace) + -> DefIndex { + let index = { + let index_to_key = &mut self.index_to_key[address_space.index()]; + let index = DefIndex::new(index_to_key.len() + address_space.start()); + debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index); + index_to_key.push(key.clone()); + index + }; self.key_to_index.insert(key, index); index } #[inline(always)] pub fn def_key(&self, index: DefIndex) -> DefKey { - self.index_to_key[index.as_usize()].clone() + self.index_to_key[index.address_space().index()] + [index.as_array_index()].clone() } #[inline(always)] @@ -94,17 +116,28 @@ impl DefPathTable { impl Encodable for DefPathTable { fn encode(&self, s: &mut S) -> Result<(), S::Error> { - self.index_to_key.encode(s) + self.index_to_key[DefIndexAddressSpace::Low.index()].encode(s)?; + self.index_to_key[DefIndexAddressSpace::High.index()].encode(s) } } impl Decodable for DefPathTable { fn decode(d: &mut D) -> Result { - let index_to_key: Vec = Decodable::decode(d)?; - let key_to_index = index_to_key.iter() - .enumerate() - .map(|(index, key)| (key.clone(), DefIndex::new(index))) - .collect(); + let index_to_key_lo: Vec = Decodable::decode(d)?; + let index_to_key_high: Vec = Decodable::decode(d)?; + + let index_to_key = [index_to_key_lo, index_to_key_high]; + + let mut key_to_index = FxHashMap(); + + for space in &[DefIndexAddressSpace::Low, DefIndexAddressSpace::High] { + key_to_index.extend(index_to_key[space.index()] + .iter() + .enumerate() + .map(|(index, key)| (key.clone(), + DefIndex::new(index + space.start())))) + } + Ok(DefPathTable { index_to_key: index_to_key, key_to_index: key_to_index, @@ -116,11 +149,27 @@ impl Decodable for DefPathTable { /// The definition table containing node definitions. /// It holds the DefPathTable for local DefIds/DefPaths and it also stores a /// mapping from NodeIds to local DefIds. -#[derive(Clone)] pub struct Definitions { table: DefPathTable, node_to_def_index: NodeMap, - def_index_to_node: Vec, + def_index_to_node: [Vec; 2], + pub(super) node_to_hir_id: IndexVec, +} + +// Unfortunately we have to provide a manual impl of Clone because of the +// fixed-sized array field. +impl Clone for Definitions { + fn clone(&self) -> Self { + Definitions { + table: self.table.clone(), + node_to_def_index: self.node_to_def_index.clone(), + def_index_to_node: [ + self.def_index_to_node[0].clone(), + self.def_index_to_node[1].clone(), + ], + node_to_hir_id: self.node_to_hir_id.clone(), + } + } } /// A unique identifier that we can use to lookup a definition @@ -206,6 +255,23 @@ impl DefPath { s } + /// Returns a string representation of the DefPath without + /// the crate-prefix. This method is useful if you don't have + /// a TyCtxt available. + pub fn to_string_no_crate(&self) -> String { + let mut s = String::with_capacity(self.data.len() * 16); + + for component in &self.data { + write!(s, + "::{}[{}]", + component.data.as_interned_str(), + component.disambiguator) + .unwrap(); + } + + s + } + pub fn deterministic_hash(&self, tcx: TyCtxt) -> u64 { debug!("deterministic_hash({:?})", self); let mut state = StableHasher::new(); @@ -270,11 +336,12 @@ impl Definitions { pub fn new() -> Definitions { Definitions { table: DefPathTable { - index_to_key: vec![], + index_to_key: [vec![], vec![]], key_to_index: FxHashMap(), }, node_to_def_index: NodeMap(), - def_index_to_node: vec![], + def_index_to_node: [vec![], vec![]], + node_to_hir_id: IndexVec::new(), } } @@ -283,8 +350,9 @@ impl Definitions { } /// Get the number of definitions. - pub fn len(&self) -> usize { - self.def_index_to_node.len() + pub fn def_index_counts_lo_hi(&self) -> (usize, usize) { + (self.def_index_to_node[DefIndexAddressSpace::Low.index()].len(), + self.def_index_to_node[DefIndexAddressSpace::High.index()].len()) } pub fn def_key(&self, index: DefIndex) -> DefKey { @@ -318,8 +386,9 @@ impl Definitions { pub fn as_local_node_id(&self, def_id: DefId) -> Option { if def_id.krate == LOCAL_CRATE { - assert!(def_id.index.as_usize() < self.def_index_to_node.len()); - Some(self.def_index_to_node[def_id.index.as_usize()]) + let space_index = def_id.index.address_space().index(); + let array_index = def_id.index.as_array_index(); + Some(self.def_index_to_node[space_index][array_index]) } else { None } @@ -329,7 +398,9 @@ impl Definitions { pub fn create_def_with_parent(&mut self, parent: Option, node_id: ast::NodeId, - data: DefPathData) + data: DefPathData, + // is_owner: bool) + address_space: DefIndexAddressSpace) -> DefIndex { debug!("create_def_with_parent(parent={:?}, node_id={:?}, data={:?})", parent, node_id, data); @@ -359,14 +430,25 @@ impl Definitions { debug!("create_def_with_parent: after disambiguation, key = {:?}", key); // Create the definition. - let index = self.table.insert(key); + let index = self.table.allocate(key, address_space); + assert_eq!(index.as_array_index(), + self.def_index_to_node[address_space.index()].len()); + self.def_index_to_node[address_space.index()].push(node_id); + debug!("create_def_with_parent: def_index_to_node[{:?} <-> {:?}", index, node_id); self.node_to_def_index.insert(node_id, index); - assert_eq!(index.as_usize(), self.def_index_to_node.len()); - self.def_index_to_node.push(node_id); index } + + /// Initialize the ast::NodeId to HirId mapping once it has been generated during + /// AST to HIR lowering. + pub fn init_node_id_to_hir_id_mapping(&mut self, + mapping: IndexVec) { + assert!(self.node_to_hir_id.is_empty(), + "Trying initialize NodeId -> HirId mapping twice"); + self.node_to_hir_id = mapping; + } } impl DefPathData { diff --git a/src/librustc/hir/map/hir_id_validator.rs b/src/librustc/hir/map/hir_id_validator.rs new file mode 100644 index 0000000000000..b3cc0c542ef9d --- /dev/null +++ b/src/librustc/hir/map/hir_id_validator.rs @@ -0,0 +1,184 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX}; +use hir::{self, intravisit, HirId, ItemLocalId}; +use syntax::ast::NodeId; +use hir::itemlikevisit::ItemLikeVisitor; +use rustc_data_structures::fx::FxHashMap; + +pub fn check_crate<'hir>(hir_map: &hir::map::Map<'hir>) { + let mut outer_visitor = OuterVisitor { + hir_map: hir_map, + errors: vec![], + }; + + hir_map.dep_graph.with_ignore(|| { + hir_map.krate().visit_all_item_likes(&mut outer_visitor); + if !outer_visitor.errors.is_empty() { + let message = outer_visitor + .errors + .iter() + .fold(String::new(), |s1, s2| s1 + "\n" + s2); + bug!("{}", message); + } + }); +} + +struct HirIdValidator<'a, 'hir: 'a> { + hir_map: &'a hir::map::Map<'hir>, + owner_def_index: Option, + hir_ids_seen: FxHashMap, + errors: Vec, +} + +struct OuterVisitor<'a, 'hir: 'a> { + hir_map: &'a hir::map::Map<'hir>, + errors: Vec, +} + +impl<'a, 'hir: 'a> OuterVisitor<'a, 'hir> { + fn new_inner_visitor(&self, + hir_map: &'a hir::map::Map<'hir>) + -> HirIdValidator<'a, 'hir> { + HirIdValidator { + hir_map: hir_map, + owner_def_index: None, + hir_ids_seen: FxHashMap(), + errors: Vec::new(), + } + } +} + +impl<'a, 'hir: 'a> ItemLikeVisitor<'hir> for OuterVisitor<'a, 'hir> { + fn visit_item(&mut self, i: &'hir hir::Item) { + let mut inner_visitor = self.new_inner_visitor(self.hir_map); + inner_visitor.check(i.id, |this| intravisit::walk_item(this, i)); + self.errors.extend(inner_visitor.errors.drain(..)); + } + + fn visit_trait_item(&mut self, i: &'hir hir::TraitItem) { + let mut inner_visitor = self.new_inner_visitor(self.hir_map); + inner_visitor.check(i.id, |this| intravisit::walk_trait_item(this, i)); + self.errors.extend(inner_visitor.errors.drain(..)); + } + + fn visit_impl_item(&mut self, i: &'hir hir::ImplItem) { + let mut inner_visitor = self.new_inner_visitor(self.hir_map); + inner_visitor.check(i.id, |this| intravisit::walk_impl_item(this, i)); + self.errors.extend(inner_visitor.errors.drain(..)); + } +} + +impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> { + + fn check)>(&mut self, + node_id: NodeId, + walk: F) { + assert!(self.owner_def_index.is_none()); + let owner_def_index = self.hir_map.local_def_id(node_id).index; + self.owner_def_index = Some(owner_def_index); + walk(self); + + if owner_def_index == CRATE_DEF_INDEX { + return + } + + // There's always at least one entry for the owning item itself + let max = self.hir_ids_seen + .keys() + .map(|local_id| local_id.as_usize()) + .max() + .unwrap(); + + if max != self.hir_ids_seen.len() - 1 { + // Collect the missing ItemLocalIds + let missing: Vec<_> = (0 .. max + 1) + .filter(|&i| !self.hir_ids_seen.contains_key(&ItemLocalId(i as u32))) + .collect(); + + // Try to map those to something more useful + let mut missing_items = vec![]; + + for local_id in missing { + let hir_id = HirId { + owner: owner_def_index, + local_id: ItemLocalId(local_id as u32), + }; + + // We are already in ICE mode here, so doing a linear search + // should be fine. + let (node_id, _) = self.hir_map + .definitions() + .node_to_hir_id + .iter() + .enumerate() + .find(|&(_, &entry)| hir_id == entry) + .unwrap(); + let node_id = NodeId::new(node_id); + missing_items.push(format!("[local_id: {}, node:{}]", + local_id, + self.hir_map.node_to_string(node_id))); + } + + self.errors.push(format!( + "ItemLocalIds not assigned densely in {}. \ + Max ItemLocalId = {}, missing IDs = {:?}", + self.hir_map.def_path(DefId::local(owner_def_index)).to_string_no_crate(), + max, + missing_items)); + } + } +} + +impl<'a, 'hir: 'a> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { + + fn nested_visit_map<'this>(&'this mut self) + -> intravisit::NestedVisitorMap<'this, 'hir> { + intravisit::NestedVisitorMap::OnlyBodies(self.hir_map) + } + + fn visit_id(&mut self, node_id: NodeId) { + let owner = self.owner_def_index.unwrap(); + let stable_id = self.hir_map.definitions().node_to_hir_id[node_id]; + + if stable_id == hir::DUMMY_HIR_ID { + self.errors.push(format!("HirIdValidator: No HirId assigned for NodeId {}: {:?}", + node_id, + self.hir_map.node_to_string(node_id))); + } + + if owner != stable_id.owner { + self.errors.push(format!( + "HirIdValidator: The recorded owner of {} is {} instead of {}", + self.hir_map.node_to_string(node_id), + self.hir_map.def_path(DefId::local(stable_id.owner)).to_string_no_crate(), + self.hir_map.def_path(DefId::local(owner)).to_string_no_crate())); + } + + if let Some(prev) = self.hir_ids_seen.insert(stable_id.local_id, node_id) { + if prev != node_id { + self.errors.push(format!( + "HirIdValidator: Same HirId {}/{} assigned for nodes {} and {}", + self.hir_map.def_path(DefId::local(stable_id.owner)).to_string_no_crate(), + stable_id.local_id.as_usize(), + self.hir_map.node_to_string(prev), + self.hir_map.node_to_string(node_id))); + } + } + } + + fn visit_impl_item_ref(&mut self, _: &'hir hir::ImplItemRef) { + // Explicitly do nothing here. ImplItemRefs contain hir::Visibility + // values that actually belong to an ImplItem instead of the ItemImpl + // we are currently in. So for those it's correct that they have a + // different owner. + } +} diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 5d074903b2b99..583b3b848f30d 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -17,7 +17,7 @@ pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData, use dep_graph::{DepGraph, DepNode}; -use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex}; +use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex, DefIndexAddressSpace}; use syntax::abi::Abi; use syntax::ast::{self, Name, NodeId, CRATE_NODE_ID}; @@ -36,6 +36,10 @@ pub mod blocks; mod collector; mod def_collector; pub mod definitions; +mod hir_id_validator; + +pub const ITEM_LIKE_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::Low; +pub const REGULAR_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High; #[derive(Copy, Clone, Debug)] pub enum Node<'hir> { @@ -346,10 +350,6 @@ impl<'hir> Map<'hir> { } } - pub fn num_local_def_ids(&self) -> usize { - self.definitions.len() - } - pub fn definitions(&self) -> &Definitions { &self.definitions } @@ -964,13 +964,17 @@ pub fn map_crate<'hir>(forest: &'hir mut Forest, entries, vector_length, (entries as f64 / vector_length as f64) * 100.); } - Map { + let map = Map { forest: forest, dep_graph: forest.dep_graph.clone(), map: map, definitions: definitions, inlined_bodies: RefCell::new(DefIdMap()), - } + }; + + hir_id_validator::check_crate(&map); + + map } /// Identical to the `PpAnn` implementation for `hir::Crate`, diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 22bc28eb3fec0..a7b0fbedfc1da 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -30,7 +30,7 @@ pub use self::Visibility::{Public, Inherited}; pub use self::PathParameters::*; use hir::def::Def; -use hir::def_id::DefId; +use hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX}; use util::nodemap::{NodeMap, FxHashSet}; use syntax_pos::{Span, ExpnId, DUMMY_SP}; @@ -43,6 +43,8 @@ use syntax::symbol::{Symbol, keywords}; use syntax::tokenstream::TokenStream; use syntax::util::ThinVec; +use rustc_data_structures::indexed_vec; + use std::collections::BTreeMap; use std::fmt; @@ -73,6 +75,63 @@ pub mod pat_util; pub mod print; pub mod svh; +/// A HirId uniquely identifies a node in the HIR of then current crate. It is +/// composed of the `owner`, which is the DefIndex of the directly enclosing +/// hir::Item, hir::TraitItem, or hir::ImplItem (i.e. the closest "item-like"), +/// and the `local_id` which is unique within the given owner. +/// +/// This two-level structure makes for more stable values: One can move an item +/// around within the source code, or add or remove stuff before it, without +/// the local_id part of the HirId changing, which is a very useful property +/// incremental compilation where we have to persist things through changes to +/// the code base. +#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, + RustcEncodable, RustcDecodable)] +pub struct HirId { + pub owner: DefIndex, + pub local_id: ItemLocalId, +} + +/// An `ItemLocalId` uniquely identifies something within a given "item-like", +/// that is within a hir::Item, hir::TraitItem, or hir::ImplItem. There is no +/// guarantee that the numerical value of a given `ItemLocalId` corresponds to +/// the node's position within the owning item in any way, but there is a +/// guarantee that the `LocalItemId`s within an owner occupy a dense range of +/// integers starting at zero, so a mapping that maps all or most nodes within +/// an "item-like" to something else can be implement by a `Vec` instead of a +/// tree or hash map. +#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, + RustcEncodable, RustcDecodable)] +pub struct ItemLocalId(pub u32); + +impl ItemLocalId { + pub fn as_usize(&self) -> usize { + self.0 as usize + } +} + +impl indexed_vec::Idx for ItemLocalId { + fn new(idx: usize) -> Self { + debug_assert!((idx as u32) as usize == idx); + ItemLocalId(idx as u32) + } + + fn index(self) -> usize { + self.0 as usize + } +} + +/// The `HirId` corresponding to CRATE_NODE_ID and CRATE_DEF_INDEX +pub const CRATE_HIR_ID: HirId = HirId { + owner: CRATE_DEF_INDEX, + local_id: ItemLocalId(0) +}; + +pub const DUMMY_HIR_ID: HirId = HirId { + owner: CRATE_DEF_INDEX, + local_id: ItemLocalId(!0) +}; + #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] pub struct Lifetime { pub id: NodeId, diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index d7a765fb82215..62be2cdbcc42c 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -636,6 +636,8 @@ macro_rules! options { Some("either `panic` or `abort`"); pub const parse_sanitizer: Option<&'static str> = Some("one of: `address`, `leak`, `memory` or `thread`"); + pub const parse_optimization_fuel: Option<&'static str> = + Some("crate=integer"); } #[allow(dead_code)] @@ -772,6 +774,21 @@ macro_rules! options { } true } + + fn parse_optimization_fuel(slot: &mut Option<(String, u64)>, v: Option<&str>) -> bool { + match v { + None => false, + Some(s) => { + let parts = s.split('=').collect::>(); + if parts.len() != 2 { return false; } + let crate_name = parts[0].to_string(); + let fuel = parts[1].parse::(); + if fuel.is_err() { return false; } + *slot = Some((crate_name, fuel.unwrap())); + true + } + } + } } ) } @@ -974,6 +991,10 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, "pass `-install_name @rpath/...` to the macOS linker"), sanitizer: Option = (None, parse_sanitizer, [TRACKED], "Use a sanitizer"), + fuel: Option<(String, u64)> = (None, parse_optimization_fuel, [TRACKED], + "Set the optimization fuel quota for a crate."), + print_fuel: Option = (None, parse_opt_string, [TRACKED], + "Make Rustc print the total optimization fuel used by a crate."), } pub fn default_lib_output() -> CrateType { @@ -1766,11 +1787,13 @@ mod dep_tracking { impl_dep_tracking_hash_via_hash!(bool); impl_dep_tracking_hash_via_hash!(usize); + impl_dep_tracking_hash_via_hash!(u64); impl_dep_tracking_hash_via_hash!(String); impl_dep_tracking_hash_via_hash!(lint::Level); impl_dep_tracking_hash_via_hash!(Option); impl_dep_tracking_hash_via_hash!(Option); impl_dep_tracking_hash_via_hash!(Option); + impl_dep_tracking_hash_via_hash!(Option<(String, u64)>); impl_dep_tracking_hash_via_hash!(Option); impl_dep_tracking_hash_via_hash!(Option); impl_dep_tracking_hash_via_hash!(Option); @@ -1792,6 +1815,7 @@ mod dep_tracking { impl_dep_tracking_hash_for_sortable_vec_of!((String, lint::Level)); impl_dep_tracking_hash_for_sortable_vec_of!((String, Option, Option)); + impl_dep_tracking_hash_for_sortable_vec_of!((String, u64)); impl DepTrackingHash for SearchPaths { fn hash(&self, hasher: &mut DefaultHasher, _: ErrorOutputType) { let mut elems: Vec<_> = self diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 3ba82f34c3266..072301eeda5aa 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -123,6 +123,20 @@ pub struct Session { pub code_stats: RefCell, next_node_id: Cell, + + /// If -zfuel=crate=n is specified, Some(crate). + optimization_fuel_crate: Option, + /// If -zfuel=crate=n is specified, initially set to n. Otherwise 0. + optimization_fuel_limit: Cell, + /// We're rejecting all further optimizations. + out_of_fuel: Cell, + + // The next two are public because the driver needs to read them. + + /// If -zprint-fuel=crate, Some(crate). + pub print_fuel_crate: Option, + /// Always set to zero and incremented so that we can print fuel expended by a crate. + pub print_fuel: Cell, } pub struct PerfStats { @@ -504,6 +518,33 @@ impl Session { println!("Total time spent decoding DefPath tables: {}", duration_to_secs_str(self.perf_stats.decode_def_path_tables_time.get())); } + + /// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n. + /// This expends fuel if applicable, and records fuel if applicable. + pub fn consider_optimizing String>(&self, crate_name: &str, msg: T) -> bool { + let mut ret = true; + match self.optimization_fuel_crate { + Some(ref c) if c == crate_name => { + let fuel = self.optimization_fuel_limit.get(); + ret = fuel != 0; + if fuel == 0 && !self.out_of_fuel.get(){ + println!("optimization-fuel-exhausted: {}", msg()); + self.out_of_fuel.set(true); + } + else if fuel > 0{ + self.optimization_fuel_limit.set(fuel-1); + } + } + _ => {} + } + match self.print_fuel_crate { + Some(ref c) if c == crate_name=> { + self.print_fuel.set(self.print_fuel.get()+1); + }, + _ => {} + } + ret + } } pub fn build_session(sopts: config::Options, @@ -599,6 +640,12 @@ pub fn build_session_(sopts: config::Options, } ); + let optimization_fuel_crate = sopts.debugging_opts.fuel.as_ref().map(|i| i.0.clone()); + let optimization_fuel_limit = Cell::new(sopts.debugging_opts.fuel.as_ref() + .map(|i| i.1).unwrap_or(0)); + let print_fuel_crate = sopts.debugging_opts.print_fuel.clone(); + let print_fuel = Cell::new(0); + let sess = Session { dep_graph: dep_graph.clone(), target: target_cfg, @@ -640,6 +687,11 @@ pub fn build_session_(sopts: config::Options, decode_def_path_tables_time: Cell::new(Duration::from_secs(0)), }, code_stats: RefCell::new(CodeStats::new()), + optimization_fuel_crate: optimization_fuel_crate, + optimization_fuel_limit: optimization_fuel_limit, + print_fuel_crate: print_fuel_crate, + print_fuel: print_fuel, + out_of_fuel: Cell::new(false), }; init_llvm(&sess); diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index a0aeb4107c156..47801c3921d2a 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -728,6 +728,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { ast_ty_to_ty_cache: RefCell::new(NodeMap()), }, f) } + + pub fn consider_optimizing String>(&self, msg: T) -> bool { + let cname = self.crate_name(LOCAL_CRATE).as_str(); + self.sess.consider_optimizing(&cname, msg) + } } impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> { diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 123db6e89476c..ade67e3efbe07 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -555,7 +555,6 @@ enum StructKind { } impl<'a, 'gcx, 'tcx> Struct { - // FIXME(camlorn): reprs need a better representation to deal with multiple reprs on one type. fn new(dl: &TargetDataLayout, fields: &Vec<&'a Layout>, repr: &ReprOptions, kind: StructKind, scapegoat: Ty<'gcx>) -> Result> { @@ -573,12 +572,8 @@ impl<'a, 'gcx, 'tcx> Struct { // Neither do 1-member and 2-member structs. // In addition, code in trans assume that 2-element structs can become pairs. // It's easier to just short-circuit here. - let mut can_optimize = (fields.len() > 2 || StructKind::EnumVariant == kind) - && ! (repr.c || repr.packed); - - // Disable field reordering until we can decide what to do. - // The odd pattern here avoids a warning about the value never being read. - if can_optimize { can_optimize = false; } + let can_optimize = (fields.len() > 2 || StructKind::EnumVariant == kind) + && ! (repr.c || repr.packed || repr.linear || repr.simd); let (optimize, sort_ascending) = match kind { StructKind::AlwaysSizedUnivariant => (can_optimize, false), diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 3c37c7353d683..c2d977a793bba 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -1376,6 +1376,8 @@ pub struct ReprOptions { pub packed: bool, pub simd: bool, pub int: Option, + // Internal only for now. If true, don't reorder fields. + pub linear: bool, } impl ReprOptions { @@ -1398,6 +1400,9 @@ impl ReprOptions { ret.simd = true; } + // This is here instead of layout because the choice must make it into metadata. + ret.linear = !tcx.consider_optimizing(|| format!("Reorder fields of {:?}", + tcx.item_path_str(did))); ret } diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index 3f478d7c165d1..62c430dda327f 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -189,6 +189,13 @@ impl IndexVec { } } +impl IndexVec { + #[inline] + pub fn resize(&mut self, new_len: usize, value: T) { + self.raw.resize(new_len, value) + } +} + impl Index for IndexVec { type Output = T; diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 62d7512655728..d30183f01f54b 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -514,6 +514,14 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls { control.make_glob_map = resolve::MakeGlobMap::Yes; } + if sess.print_fuel_crate.is_some() { + control.compilation_done.callback = box |state| { + let sess = state.session; + println!("Fuel used by {}: {}", + sess.print_fuel_crate.as_ref().unwrap(), + sess.print_fuel.get()); + } + } control } } diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index b4b9966cbe47b..c2ad598b0c503 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -683,7 +683,7 @@ impl<'a, 'tcx> CrateMetadata { }, ext.kind() ); - callback(def::Export { name: name, def: def }); + callback(def::Export { name: name, def: def, span: DUMMY_SP }); } } return @@ -720,6 +720,7 @@ impl<'a, 'tcx> CrateMetadata { callback(def::Export { def: def, name: self.item_name(child_index), + span: self.entry(child_index).span.decode(self), }); } } @@ -732,12 +733,10 @@ impl<'a, 'tcx> CrateMetadata { } let def_key = self.def_key(child_index); + let span = child.span.decode(self); if let (Some(def), Some(name)) = (self.get_def(child_index), def_key.disambiguated_data.data.get_opt_name()) { - callback(def::Export { - def: def, - name: name, - }); + callback(def::Export { def: def, name: name, span: span }); // For non-reexport structs and variants add their constructors to children. // Reexport lists automatically contain constructors when necessary. match def { @@ -745,10 +744,7 @@ impl<'a, 'tcx> CrateMetadata { if let Some(ctor_def_id) = self.get_struct_ctor_def_id(child_index) { let ctor_kind = self.get_ctor_kind(child_index); let ctor_def = Def::StructCtor(ctor_def_id, ctor_kind); - callback(def::Export { - def: ctor_def, - name: name, - }); + callback(def::Export { def: ctor_def, name: name, span: span }); } } Def::Variant(def_id) => { @@ -756,10 +752,7 @@ impl<'a, 'tcx> CrateMetadata { // value namespace, they are reserved for possible future use. let ctor_kind = self.get_ctor_kind(child_index); let ctor_def = Def::VariantCtor(def_id, ctor_kind); - callback(def::Export { - def: ctor_def, - name: name, - }); + callback(def::Export { def: ctor_def, name: name, span: span }); } _ => {} } diff --git a/src/librustc_metadata/index.rs b/src/librustc_metadata/index.rs index db9fc870fa86a..970a401177ba5 100644 --- a/src/librustc_metadata/index.rs +++ b/src/librustc_metadata/index.rs @@ -10,7 +10,7 @@ use schema::*; -use rustc::hir::def_id::{DefId, DefIndex}; +use rustc::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace}; use std::io::{Cursor, Write}; use std::slice; use std::u32; @@ -23,12 +23,15 @@ use std::u32; /// appropriate spot by calling `record_position`. We should never /// visit the same index twice. pub struct Index { - positions: Vec, + positions: [Vec; 2] } impl Index { - pub fn new(max_index: usize) -> Index { - Index { positions: vec![u32::MAX; max_index] } + pub fn new((max_index_lo, max_index_hi): (usize, usize)) -> Index { + Index { + positions: [vec![u32::MAX; max_index_lo], + vec![u32::MAX; max_index_hi]], + } } pub fn record(&mut self, def_id: DefId, entry: Lazy) { @@ -37,24 +40,31 @@ impl Index { } pub fn record_index(&mut self, item: DefIndex, entry: Lazy) { - let item = item.as_usize(); - assert!(entry.position < (u32::MAX as usize)); let position = entry.position as u32; + let space_index = item.address_space().index(); + let array_index = item.as_array_index(); - assert!(self.positions[item] == u32::MAX, + assert!(self.positions[space_index][array_index] == u32::MAX, "recorded position for item {:?} twice, first at {:?} and now at {:?}", item, - self.positions[item], + self.positions[space_index][array_index], position); - self.positions[item] = position.to_le(); + self.positions[space_index][array_index] = position.to_le(); } pub fn write_index(&self, buf: &mut Cursor>) -> LazySeq { let pos = buf.position(); - buf.write_all(words_to_bytes(&self.positions)).unwrap(); - LazySeq::with_position_and_length(pos as usize, self.positions.len()) + + // First we write the length of the lower range ... + buf.write_all(words_to_bytes(&[self.positions[0].len() as u32])).unwrap(); + // ... then the values in the lower range ... + buf.write_all(words_to_bytes(&self.positions[0][..])).unwrap(); + // ... then the values in the higher range. + buf.write_all(words_to_bytes(&self.positions[1][..])).unwrap(); + LazySeq::with_position_and_length(pos as usize, + self.positions[0].len() + self.positions[1].len() + 1) } } @@ -70,7 +80,18 @@ impl<'tcx> LazySeq { index, words.len()); - let position = u32::from_le(words[index].get()); + let positions = match def_index.address_space() { + DefIndexAddressSpace::Low => &words[1..], + DefIndexAddressSpace::High => { + // This is a DefIndex in the higher range, so find out where + // that starts: + let lo_count = u32::from_le(words[0].get()) as usize; + &words[lo_count + 1 .. ] + } + }; + + let array_index = def_index.as_array_index(); + let position = u32::from_le(positions[array_index].get()); if position == u32::MAX { debug!("Index::lookup: position=u32::MAX"); None @@ -84,14 +105,26 @@ impl<'tcx> LazySeq { bytes: &'a [u8]) -> impl Iterator>)> + 'a { let words = &bytes_to_words(&bytes[self.position..])[..self.len]; - words.iter().map(|word| word.get()).enumerate().filter_map(|(index, position)| { - if position == u32::MAX { + let lo_count = u32::from_le(words[0].get()) as usize; + let lo = &words[1 .. lo_count + 1]; + let hi = &words[1 + lo_count ..]; + + lo.iter().map(|word| word.get()).enumerate().filter_map(|(index, pos)| { + if pos == u32::MAX { + None + } else { + let pos = u32::from_le(pos) as usize; + Some((DefIndex::new(index), Lazy::with_position(pos))) + } + }).chain(hi.iter().map(|word| word.get()).enumerate().filter_map(|(index, pos)| { + if pos == u32::MAX { None } else { - let position = u32::from_le(position) as usize; - Some((DefIndex::new(index), Lazy::with_position(position))) + let pos = u32::from_le(pos) as usize; + Some((DefIndex::new(index + DefIndexAddressSpace::High.start()), + Lazy::with_position(pos))) } - }) + })) } } diff --git a/src/librustc_metadata/index_builder.rs b/src/librustc_metadata/index_builder.rs index 2359c747d888d..a811f72bc956c 100644 --- a/src/librustc_metadata/index_builder.rs +++ b/src/librustc_metadata/index_builder.rs @@ -90,7 +90,7 @@ impl<'a, 'b, 'tcx> DerefMut for IndexBuilder<'a, 'b, 'tcx> { impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> { pub fn new(ecx: &'a mut EncodeContext<'b, 'tcx>) -> Self { IndexBuilder { - items: Index::new(ecx.tcx.hir.num_local_def_ids()), + items: Index::new(ecx.tcx.hir.definitions().def_index_counts_lo_hi()), ecx: ecx, } } diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index be905a9d0f94a..c33d5b9b6e16b 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -605,7 +605,7 @@ impl<'a> Resolver<'a> { let ident = Ident::with_empty_ctxt(name); let result = self.resolve_ident_in_module(module, ident, MacroNS, false, None); if let Ok(binding) = result { - self.macro_exports.push(Export { name: name, def: binding.def() }); + self.macro_exports.push(Export { name: name, def: binding.def(), span: span }); } else { span_err!(self.session, span, E0470, "reexported macro not found"); } diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index df3be8fa0f810..f72c2eadb04a8 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -653,7 +653,7 @@ impl<'a> Resolver<'a> { if attr::contains_name(&item.attrs, "macro_export") { let def = Def::Macro(def_id, MacroKind::Bang); - self.macro_exports.push(Export { name: ident.name, def: def }); + self.macro_exports.push(Export { name: ident.name, def: def, span: item.span }); } } diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index dbc8bca548b76..2f4ac12cd7363 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -21,7 +21,7 @@ use rustc::ty; use rustc::lint::builtin::PRIVATE_IN_PUBLIC; use rustc::hir::def_id::DefId; use rustc::hir::def::*; -use rustc::util::nodemap::FxHashSet; +use rustc::util::nodemap::FxHashMap; use syntax::ast::{Ident, NodeId}; use syntax::ext::base::Determinacy::{self, Determined, Undetermined}; @@ -763,10 +763,11 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { *module.globs.borrow_mut() = Vec::new(); let mut reexports = Vec::new(); + let mut exported_macro_names = FxHashMap(); if module as *const _ == self.graph_root as *const _ { - let mut exported_macro_names = FxHashSet(); - for export in mem::replace(&mut self.macro_exports, Vec::new()).into_iter().rev() { - if exported_macro_names.insert(export.name) { + let macro_exports = mem::replace(&mut self.macro_exports, Vec::new()); + for export in macro_exports.into_iter().rev() { + if exported_macro_names.insert(export.name, export.span).is_none() { reexports.push(export); } } @@ -786,7 +787,17 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { if !def.def_id().is_local() { self.session.cstore.export_macros(def.def_id().krate); } - reexports.push(Export { name: ident.name, def: def }); + if let Def::Macro(..) = def { + if let Some(&span) = exported_macro_names.get(&ident.name) { + let msg = + format!("a macro named `{}` has already been exported", ident); + self.session.struct_span_err(span, &msg) + .span_label(span, &format!("`{}` already exported", ident)) + .span_note(binding.span, "previous macro export here") + .emit(); + } + } + reexports.push(Export { name: ident.name, def: def, span: binding.span }); } } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 84fb69a7f1094..5619e8c13d2a0 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -23,6 +23,7 @@ use abi::Abi; use ext::hygiene::SyntaxContext; use print::pprust; use ptr::P; +use rustc_data_structures::indexed_vec; use symbol::{Symbol, keywords}; use tokenstream::{ThinTokenStream, TokenStream}; @@ -275,6 +276,16 @@ impl serialize::UseSpecializedDecodable for NodeId { } } +impl indexed_vec::Idx for NodeId { + fn new(idx: usize) -> Self { + NodeId::new(idx) + } + + fn index(self) -> usize { + self.as_usize() + } +} + /// Node id used to represent the root of the crate. pub const CRATE_NODE_ID: NodeId = NodeId(0); diff --git a/src/libsyntax/ext/placeholders.rs b/src/libsyntax/ext/placeholders.rs index f60b1d17a5e2f..f0e328a551d5f 100644 --- a/src/libsyntax/ext/placeholders.rs +++ b/src/libsyntax/ext/placeholders.rs @@ -178,17 +178,9 @@ impl<'a, 'b> Folder for PlaceholderExpander<'a, 'b> { block.stmts = block.stmts.move_flat_map(|mut stmt| { remaining_stmts -= 1; - match stmt.node { - // Avoid wasting a node id on a trailing expression statement, - // which shares a HIR node with the expression itself. - ast::StmtKind::Expr(ref expr) if remaining_stmts == 0 => stmt.id = expr.id, - - _ if self.monotonic => { - assert_eq!(stmt.id, ast::DUMMY_NODE_ID); - stmt.id = self.cx.resolver.next_node_id(); - } - - _ => {} + if self.monotonic { + assert_eq!(stmt.id, ast::DUMMY_NODE_ID); + stmt.id = self.cx.resolver.next_node_id(); } Some(stmt) diff --git a/src/test/compile-fail/duplicate-check-macro-exports.rs b/src/test/compile-fail/duplicate-check-macro-exports.rs new file mode 100644 index 0000000000000..53d7e54ee5b7b --- /dev/null +++ b/src/test/compile-fail/duplicate-check-macro-exports.rs @@ -0,0 +1,19 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(use_extern_macros)] + +pub use std::panic; //~ NOTE previous macro export here + +#[macro_export] +macro_rules! panic { () => {} } //~ ERROR a macro named `panic` has already been exported +//~| NOTE `panic` already exported + +fn main() {} diff --git a/src/test/compile-fail/region-bounds-on-objects-and-type-parameters.rs b/src/test/compile-fail/region-bounds-on-objects-and-type-parameters.rs index 503b577b1f1b4..fd8d5ff9e7ea8 100644 --- a/src/test/compile-fail/region-bounds-on-objects-and-type-parameters.rs +++ b/src/test/compile-fail/region-bounds-on-objects-and-type-parameters.rs @@ -18,7 +18,7 @@ trait SomeTrait { } // Bounds on object types: -struct Foo<'a,'b,'c> { //~ ERROR parameter `'b` is never used +struct Foo<'a,'b,'c> { //~ ERROR parameter `'c` is never used // All of these are ok, because we can derive exactly one bound: a: Box, b: Box>, diff --git a/src/test/run-pass/macro-nested_definition_issue-31946.rs b/src/test/run-pass/macro-nested_definition_issue-31946.rs new file mode 100644 index 0000000000000..84ff2dc4d0d61 --- /dev/null +++ b/src/test/run-pass/macro-nested_definition_issue-31946.rs @@ -0,0 +1,18 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + println!("{}", { + macro_rules! foo { + ($name:expr) => { concat!("hello ", $name) } + } + foo!("rust") + }); +} diff --git a/src/test/run-pass/optimization-fuel-0.rs b/src/test/run-pass/optimization-fuel-0.rs new file mode 100644 index 0000000000000..3832c040108f8 --- /dev/null +++ b/src/test/run-pass/optimization-fuel-0.rs @@ -0,0 +1,24 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_name="foo"] + +use std::mem::size_of; + +// compile-flags: -Z fuel=foo=0 + +struct S1(u8, u16, u8); +struct S2(u8, u16, u8); + +fn main() { + assert_eq!(size_of::(), 6); + assert_eq!(size_of::(), 6); +} + diff --git a/src/test/run-pass/optimization-fuel-1.rs b/src/test/run-pass/optimization-fuel-1.rs new file mode 100644 index 0000000000000..5f294e26aa53e --- /dev/null +++ b/src/test/run-pass/optimization-fuel-1.rs @@ -0,0 +1,26 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_name="foo"] + +use std::mem::size_of; + +// compile-flags: -Z fuel=foo=1 + +struct S1(u8, u16, u8); +struct S2(u8, u16, u8); + +fn main() { + let optimized = (size_of::() == 4) as usize + +(size_of::() == 4) as usize; + assert_eq!(optimized, 1); +} + + diff --git a/src/test/run-pass/type-sizes.rs b/src/test/run-pass/type-sizes.rs index bbb01eaaf46b9..9d1a3500a582a 100644 --- a/src/test/run-pass/type-sizes.rs +++ b/src/test/run-pass/type-sizes.rs @@ -31,6 +31,17 @@ enum e3 { a([u16; 0], u8), b } +struct ReorderedStruct { + a: u8, + b: u64, + c: u8 +} + +enum ReorderedEnum { + A(u8, u64, u8), + B(u8, u64, u8), +} + pub fn main() { assert_eq!(size_of::(), 1 as usize); assert_eq!(size_of::(), 4 as usize); @@ -54,4 +65,6 @@ pub fn main() { assert_eq!(size_of::(), 8 as usize); assert_eq!(size_of::(), 8 as usize); assert_eq!(size_of::(), 4 as usize); + assert_eq!(size_of::(), 16); + assert_eq!(size_of::(), 16); } diff --git a/src/test/ui/print-fuel/print-fuel.rs b/src/test/ui/print-fuel/print-fuel.rs new file mode 100644 index 0000000000000..0d9e243763f78 --- /dev/null +++ b/src/test/ui/print-fuel/print-fuel.rs @@ -0,0 +1,21 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_name="foo"] +#![allow(dead_code)] + +// compile-flags: -Z print-fuel=foo + +struct S1(u8, u16, u8); +struct S2(u8, u16, u8); +struct S3(u8, u16, u8); + +fn main() { +} diff --git a/src/test/ui/print-fuel/print-fuel.stdout b/src/test/ui/print-fuel/print-fuel.stdout new file mode 100644 index 0000000000000..cc88cc077bb21 --- /dev/null +++ b/src/test/ui/print-fuel/print-fuel.stdout @@ -0,0 +1 @@ +Fuel used by foo: 3 diff --git a/src/test/ui/print_type_sizes/nullable.stdout b/src/test/ui/print_type_sizes/nullable.stdout index dd999c4a5e4c7..830678f174f88 100644 --- a/src/test/ui/print_type_sizes/nullable.stdout +++ b/src/test/ui/print_type_sizes/nullable.stdout @@ -1,25 +1,22 @@ -print-type-size type: `IndirectNonZero`: 20 bytes, alignment: 4 bytes -print-type-size field `.pre`: 1 bytes -print-type-size padding: 3 bytes -print-type-size field `.nested`: 12 bytes, alignment: 4 bytes +print-type-size type: `IndirectNonZero`: 12 bytes, alignment: 4 bytes +print-type-size field `.nested`: 8 bytes print-type-size field `.post`: 2 bytes -print-type-size end padding: 2 bytes -print-type-size type: `MyOption>`: 20 bytes, alignment: 4 bytes -print-type-size variant `Some`: 20 bytes -print-type-size field `.0`: 20 bytes -print-type-size type: `EmbeddedDiscr`: 12 bytes, alignment: 4 bytes -print-type-size variant `Record`: 10 bytes -print-type-size field `.pre`: 1 bytes -print-type-size padding: 3 bytes -print-type-size field `.val`: 4 bytes, alignment: 4 bytes -print-type-size field `.post`: 2 bytes -print-type-size end padding: 2 bytes -print-type-size type: `NestedNonZero`: 12 bytes, alignment: 4 bytes print-type-size field `.pre`: 1 bytes -print-type-size padding: 3 bytes -print-type-size field `.val`: 4 bytes, alignment: 4 bytes +print-type-size end padding: 1 bytes +print-type-size type: `MyOption>`: 12 bytes, alignment: 4 bytes +print-type-size variant `Some`: 12 bytes +print-type-size field `.0`: 12 bytes +print-type-size type: `EmbeddedDiscr`: 8 bytes, alignment: 4 bytes +print-type-size variant `Record`: 7 bytes +print-type-size field `.val`: 4 bytes +print-type-size field `.post`: 2 bytes +print-type-size field `.pre`: 1 bytes +print-type-size end padding: 1 bytes +print-type-size type: `NestedNonZero`: 8 bytes, alignment: 4 bytes +print-type-size field `.val`: 4 bytes print-type-size field `.post`: 2 bytes -print-type-size end padding: 2 bytes +print-type-size field `.pre`: 1 bytes +print-type-size end padding: 1 bytes print-type-size type: `MyOption>`: 4 bytes, alignment: 4 bytes print-type-size variant `Some`: 4 bytes print-type-size field `.0`: 4 bytes diff --git a/src/test/ui/print_type_sizes/packed.stdout b/src/test/ui/print_type_sizes/packed.stdout index 1278a7d7c92c6..83fd333c9c7fc 100644 --- a/src/test/ui/print_type_sizes/packed.stdout +++ b/src/test/ui/print_type_sizes/packed.stdout @@ -1,13 +1,11 @@ -print-type-size type: `Padded`: 16 bytes, alignment: 4 bytes +print-type-size type: `Padded`: 12 bytes, alignment: 4 bytes +print-type-size field `.g`: 4 bytes +print-type-size field `.h`: 2 bytes print-type-size field `.a`: 1 bytes print-type-size field `.b`: 1 bytes -print-type-size padding: 2 bytes -print-type-size field `.g`: 4 bytes, alignment: 4 bytes print-type-size field `.c`: 1 bytes -print-type-size padding: 1 bytes -print-type-size field `.h`: 2 bytes, alignment: 2 bytes print-type-size field `.d`: 1 bytes -print-type-size end padding: 3 bytes +print-type-size end padding: 2 bytes print-type-size type: `Packed`: 10 bytes, alignment: 1 bytes print-type-size field `.a`: 1 bytes print-type-size field `.b`: 1 bytes diff --git a/src/test/ui/print_type_sizes/padding.stdout b/src/test/ui/print_type_sizes/padding.stdout index bb95f790bd9e4..0eaff7118b35c 100644 --- a/src/test/ui/print_type_sizes/padding.stdout +++ b/src/test/ui/print_type_sizes/padding.stdout @@ -1,10 +1,12 @@ print-type-size type: `E1`: 12 bytes, alignment: 4 bytes -print-type-size discriminant: 4 bytes -print-type-size variant `A`: 5 bytes -print-type-size field `.0`: 4 bytes +print-type-size discriminant: 1 bytes +print-type-size variant `A`: 7 bytes print-type-size field `.1`: 1 bytes -print-type-size variant `B`: 8 bytes -print-type-size field `.0`: 8 bytes +print-type-size padding: 2 bytes +print-type-size field `.0`: 4 bytes, alignment: 4 bytes +print-type-size variant `B`: 11 bytes +print-type-size padding: 3 bytes +print-type-size field `.0`: 8 bytes, alignment: 4 bytes print-type-size type: `E2`: 12 bytes, alignment: 4 bytes print-type-size discriminant: 1 bytes print-type-size variant `A`: 7 bytes @@ -15,7 +17,7 @@ print-type-size variant `B`: 11 bytes print-type-size padding: 3 bytes print-type-size field `.0`: 8 bytes, alignment: 4 bytes print-type-size type: `S`: 8 bytes, alignment: 4 bytes +print-type-size field `.g`: 4 bytes print-type-size field `.a`: 1 bytes print-type-size field `.b`: 1 bytes -print-type-size padding: 2 bytes -print-type-size field `.g`: 4 bytes, alignment: 4 bytes +print-type-size end padding: 2 bytes