2015-07-31 00:04:06 -07:00
|
|
|
// Copyright 2015 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 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-06-03 18:26:20 +02:00
|
|
|
//! Lowers the AST to the HIR.
|
|
|
|
//!
|
|
|
|
//! Since the AST and HIR are fairly similar, this is mostly a simple procedure,
|
|
|
|
//! much like a fold. Where lowering involves a bit more work things get more
|
|
|
|
//! interesting and there are some invariants you should know about. These mostly
|
|
|
|
//! concern spans and ids.
|
|
|
|
//!
|
|
|
|
//! Spans are assigned to AST nodes during parsing and then are modified during
|
|
|
|
//! expansion to indicate the origin of a node and the process it went through
|
|
|
|
//! being expanded. Ids are assigned to AST nodes just before lowering.
|
|
|
|
//!
|
|
|
|
//! For the simpler lowering steps, ids and spans should be preserved. Unlike
|
|
|
|
//! expansion we do not preserve the process of lowering in the spans, so spans
|
|
|
|
//! should not be modified here. When creating a new node (as opposed to
|
|
|
|
//! 'folding' an existing one), then you create a new id using `next_id()`.
|
|
|
|
//!
|
|
|
|
//! You must ensure that ids are unique. That means that you should only use the
|
|
|
|
//! id from an AST node in a single HIR node (you can assume that AST node ids
|
|
|
|
//! are unique). Every new node must have a unique id. Avoid cloning HIR nodes.
|
|
|
|
//! If you do, you must then set the new node's id to a fresh one.
|
|
|
|
//!
|
|
|
|
//! Spans are used for error messages and for tools to map semantics back to
|
|
|
|
//! source code. It is therefore not as important with spans as ids to be strict
|
|
|
|
//! about use (you can't break the compiler by screwing up a span). Obviously, a
|
|
|
|
//! HIR node can only have a single span. But multiple nodes can have the same
|
|
|
|
//! span and spans don't need to be kept in order, etc. Where code is preserved
|
|
|
|
//! by lowering, it should have the same span as in the AST. Where HIR nodes are
|
|
|
|
//! new it is probably best to give a span for the whole AST node being lowered.
|
|
|
|
//! All nodes should have real spans, don't use dummy spans. Tools are likely to
|
|
|
|
//! get confused if the spans from leaf AST nodes occur in multiple places
|
|
|
|
//! in the HIR, especially for multiple identifiers.
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-09-09 11:02:18 -07:00
|
|
|
use dep_graph::DepGraph;
|
2015-07-31 00:04:06 -07:00
|
|
|
use hir;
|
2017-10-15 13:43:06 -07:00
|
|
|
use hir::HirVec;
|
|
|
|
use hir::map::{Definitions, DefKey, DefPathData};
|
|
|
|
use hir::def_id::{DefIndex, DefId, CRATE_DEF_INDEX, DefIndexAddressSpace};
|
2016-09-15 00:51:46 +03:00
|
|
|
use hir::def::{Def, PathResolution};
|
2017-07-29 01:13:40 +03:00
|
|
|
use lint::builtin::PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES;
|
2017-09-05 16:48:24 +02:00
|
|
|
use middle::cstore::CrateStore;
|
2017-03-14 15:50:40 +01:00
|
|
|
use rustc_data_structures::indexed_vec::IndexVec;
|
2016-05-10 20:50:00 -04:00
|
|
|
use session::Session;
|
2017-07-29 01:13:40 +03:00
|
|
|
use util::common::FN_OUTPUT_NAME;
|
2017-03-24 23:03:15 +00:00
|
|
|
use util::nodemap::{DefIdMap, FxHashMap, NodeMap};
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-10-15 13:43:06 -07:00
|
|
|
use std::collections::{BTreeMap, HashSet};
|
2017-03-14 15:50:40 +01:00
|
|
|
use std::fmt::Debug;
|
2016-01-13 01:24:34 -05:00
|
|
|
use std::iter;
|
2017-02-15 14:52:27 -08:00
|
|
|
use std::mem;
|
2017-01-17 21:32:27 +08:00
|
|
|
use syntax::attr;
|
2015-07-31 00:04:06 -07:00
|
|
|
use syntax::ast::*;
|
2016-06-22 01:16:56 +00:00
|
|
|
use syntax::errors;
|
2017-03-17 04:04:41 +00:00
|
|
|
use syntax::ext::hygiene::{Mark, SyntaxContext};
|
2015-07-31 00:04:06 -07:00
|
|
|
use syntax::ptr::P;
|
2017-08-16 18:46:48 -05:00
|
|
|
use syntax::codemap::{self, respan, Spanned, CompilerDesugaringKind};
|
2015-09-28 15:00:15 +13:00
|
|
|
use syntax::std_inject;
|
2016-11-16 08:21:52 +00:00
|
|
|
use syntax::symbol::{Symbol, keywords};
|
2017-09-15 08:28:34 -07:00
|
|
|
use syntax::tokenstream::{TokenStream, TokenTree, Delimited};
|
2017-09-21 08:13:25 -07:00
|
|
|
use syntax::parse::token::Token;
|
2016-11-24 06:11:31 +02:00
|
|
|
use syntax::util::small_vector::SmallVector;
|
2015-11-17 17:32:12 -05:00
|
|
|
use syntax::visit::{self, Visitor};
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;
|
|
|
|
|
2015-09-30 16:17:37 +13:00
|
|
|
pub struct LoweringContext<'a> {
|
2015-09-28 15:00:15 +13:00
|
|
|
crate_root: Option<&'static str>,
|
2017-06-04 18:15:42 +02:00
|
|
|
|
2016-04-30 19:48:46 +00:00
|
|
|
// Use to assign ids to hir nodes that do not directly correspond to an ast node
|
2016-08-31 14:00:29 +03:00
|
|
|
sess: &'a Session,
|
2017-06-04 18:15:42 +02:00
|
|
|
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore: &'a CrateStore,
|
|
|
|
|
2016-04-18 10:30:55 +12:00
|
|
|
// As we walk the AST we must keep track of the current 'parent' def id (in
|
|
|
|
// the form of a DefIndex) so that if we create a new node which introduces
|
|
|
|
// a definition, then we can properly create the def id.
|
2016-05-09 07:59:19 +00:00
|
|
|
parent_def: Option<DefIndex>,
|
|
|
|
resolver: &'a mut Resolver,
|
2017-03-24 23:03:15 +00:00
|
|
|
name_map: FxHashMap<Ident, Name>,
|
2016-11-24 06:11:31 +02:00
|
|
|
|
|
|
|
/// The items being lowered are collected here.
|
|
|
|
items: BTreeMap<NodeId, hir::Item>,
|
|
|
|
|
2016-12-04 04:21:06 +02:00
|
|
|
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem>,
|
2016-11-24 06:11:31 +02:00
|
|
|
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem>,
|
2017-02-21 10:55:40 -05:00
|
|
|
bodies: BTreeMap<hir::BodyId, hir::Body>,
|
2017-03-05 05:15:58 +00:00
|
|
|
exported_macros: Vec<hir::MacroDef>,
|
2017-01-09 17:46:11 +02:00
|
|
|
|
2017-02-19 14:46:29 +02:00
|
|
|
trait_impls: BTreeMap<DefId, Vec<NodeId>>,
|
2017-10-09 13:59:20 -03:00
|
|
|
trait_auto_impl: BTreeMap<DefId, NodeId>,
|
2017-02-19 14:46:29 +02:00
|
|
|
|
2017-07-28 13:09:33 -07:00
|
|
|
is_generator: bool,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2017-02-17 15:12:47 -08:00
|
|
|
catch_scopes: Vec<NodeId>,
|
2017-02-15 14:52:27 -08:00
|
|
|
loop_scopes: Vec<NodeId>,
|
2017-02-15 23:28:59 -08:00
|
|
|
is_in_loop_condition: bool,
|
2017-11-10 12:33:05 -05:00
|
|
|
is_in_trait_impl: bool,
|
2017-02-15 14:52:27 -08:00
|
|
|
|
2017-11-16 22:59:45 -08:00
|
|
|
// Used to create lifetime definitions from in-band lifetime usages.
|
|
|
|
// e.g. `fn foo(x: &'x u8) -> &'x u8` to `fn foo<'x>(x: &'x u8) -> &'x u8`
|
|
|
|
// When a named lifetime is encountered in a function or impl header and
|
|
|
|
// has not been defined
|
|
|
|
// (i.e. it doesn't appear in the in_scope_lifetimes list), it is added
|
|
|
|
// to this list. The results of this list are then added to the list of
|
|
|
|
// lifetime definitions in the corresponding impl or function generics.
|
|
|
|
lifetimes_to_define: Vec<(Span, Name)>,
|
|
|
|
// Whether or not in-band lifetimes are being collected. This is used to
|
|
|
|
// indicate whether or not we're in a place where new lifetimes will result
|
|
|
|
// in in-band lifetime definitions, such a function or an impl header.
|
|
|
|
// This will always be false unless the `in_band_lifetimes` feature is
|
|
|
|
// enabled.
|
|
|
|
is_collecting_in_band_lifetimes: bool,
|
|
|
|
// Currently in-scope lifetimes defined in impl headers, fn headers, or HRTB.
|
|
|
|
// When `is_collectin_in_band_lifetimes` is true, each lifetime is checked
|
|
|
|
// against this list to see if it is already in-scope, or if a definition
|
|
|
|
// needs to be created for it.
|
|
|
|
in_scope_lifetimes: Vec<Name>,
|
|
|
|
|
2017-01-09 17:46:11 +02:00
|
|
|
type_def_lifetime_params: DefIdMap<usize>,
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
current_hir_id_owner: Vec<(DefIndex, u32)>,
|
|
|
|
item_local_id_counters: NodeMap<u32>,
|
|
|
|
node_id_to_hir_id: IndexVec<NodeId, hir::HirId>,
|
2016-05-02 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Resolver {
|
2017-06-03 18:26:20 +02:00
|
|
|
/// Resolve a hir path generated by the lowerer when expanding `for`, `if let`, etc.
|
2016-11-25 06:07:21 +00:00
|
|
|
fn resolve_hir_path(&mut self, path: &mut hir::Path, is_value: bool);
|
2016-05-02 23:26:18 +00:00
|
|
|
|
2017-06-03 18:26:20 +02:00
|
|
|
/// Obtain the resolution for a node id
|
2016-03-06 15:54:44 +03:00
|
|
|
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
|
|
|
|
|
2017-06-03 18:26:20 +02:00
|
|
|
/// We must keep the set of definitions up to date as we add nodes that weren't in the AST.
|
|
|
|
/// This should only return `None` during testing.
|
2016-08-29 05:12:03 +03:00
|
|
|
fn definitions(&mut self) -> &mut Definitions;
|
2015-09-25 16:03:28 +12:00
|
|
|
}
|
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
enum ImplTraitContext {
|
|
|
|
/// Treat `impl Trait` as shorthand for a new universal generic parameter.
|
|
|
|
/// Example: `fn foo(x: impl Debug)`, where `impl Debug` is conceptually
|
|
|
|
/// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
|
|
|
|
///
|
|
|
|
/// We store a DefId here so we can look up necessary information later
|
|
|
|
Universal(DefId),
|
|
|
|
|
|
|
|
/// Treat `impl Trait` as shorthand for a new universal existential parameter.
|
|
|
|
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
|
|
|
|
/// equivalent to a fresh existential parameter like `abstract type T; fn foo() -> T`.
|
|
|
|
Existential,
|
|
|
|
|
|
|
|
/// `impl Trait` is not accepted in this position.
|
|
|
|
Disallowed,
|
|
|
|
}
|
|
|
|
|
2016-05-10 20:50:00 -04:00
|
|
|
pub fn lower_crate(sess: &Session,
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore: &CrateStore,
|
2017-09-09 11:02:18 -07:00
|
|
|
dep_graph: &DepGraph,
|
2016-05-10 20:50:00 -04:00
|
|
|
krate: &Crate,
|
|
|
|
resolver: &mut Resolver)
|
2016-05-10 05:29:13 +00:00
|
|
|
-> hir::Crate {
|
2016-05-10 20:50:00 -04:00
|
|
|
// We're constructing the HIR here; we don't care what we will
|
|
|
|
// read, since we haven't even constructed the *input* to
|
|
|
|
// incr. comp. yet.
|
2017-09-09 11:02:18 -07:00
|
|
|
let _ignore = dep_graph.in_ignore();
|
2016-05-10 20:50:00 -04:00
|
|
|
|
2016-05-10 05:29:13 +00:00
|
|
|
LoweringContext {
|
2017-12-12 11:57:58 -08:00
|
|
|
crate_root: std_inject::injected_crate_name(),
|
2017-07-03 11:19:51 -07:00
|
|
|
sess,
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore,
|
2016-05-10 05:29:13 +00:00
|
|
|
parent_def: None,
|
2017-07-03 11:19:51 -07:00
|
|
|
resolver,
|
2017-03-24 23:03:15 +00:00
|
|
|
name_map: FxHashMap(),
|
2016-11-24 06:11:31 +02:00
|
|
|
items: BTreeMap::new(),
|
2016-12-04 04:21:06 +02:00
|
|
|
trait_items: BTreeMap::new(),
|
2016-11-24 06:11:31 +02:00
|
|
|
impl_items: BTreeMap::new(),
|
2017-02-21 10:55:40 -05:00
|
|
|
bodies: BTreeMap::new(),
|
2017-02-19 14:46:29 +02:00
|
|
|
trait_impls: BTreeMap::new(),
|
2017-10-09 13:59:20 -03:00
|
|
|
trait_auto_impl: BTreeMap::new(),
|
2017-03-05 05:15:58 +00:00
|
|
|
exported_macros: Vec::new(),
|
2017-02-17 15:12:47 -08:00
|
|
|
catch_scopes: Vec::new(),
|
2017-02-15 14:52:27 -08:00
|
|
|
loop_scopes: Vec::new(),
|
2017-02-15 23:28:59 -08:00
|
|
|
is_in_loop_condition: false,
|
2017-01-09 17:46:11 +02:00
|
|
|
type_def_lifetime_params: DefIdMap(),
|
2017-03-14 15:50:40 +01:00
|
|
|
current_hir_id_owner: vec![(CRATE_DEF_INDEX, 0)],
|
|
|
|
item_local_id_counters: NodeMap(),
|
|
|
|
node_id_to_hir_id: IndexVec::new(),
|
2017-07-28 13:09:33 -07:00
|
|
|
is_generator: false,
|
2017-11-10 12:33:05 -05:00
|
|
|
is_in_trait_impl: false,
|
2017-11-16 22:59:45 -08:00
|
|
|
lifetimes_to_define: Vec::new(),
|
|
|
|
is_collecting_in_band_lifetimes: false,
|
|
|
|
in_scope_lifetimes: Vec::new(),
|
2016-05-10 05:29:13 +00:00
|
|
|
}.lower_crate(krate)
|
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-10-17 06:02:23 +03:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
|
|
|
enum ParamMode {
|
|
|
|
/// Any path in a type context.
|
|
|
|
Explicit,
|
|
|
|
/// The `module::Type` in `module::Type::method` in an expression.
|
|
|
|
Optional
|
|
|
|
}
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
struct LoweredNodeId {
|
|
|
|
node_id: NodeId,
|
|
|
|
hir_id: hir::HirId,
|
|
|
|
}
|
|
|
|
|
2017-07-29 01:13:40 +03:00
|
|
|
enum ParenthesizedGenericArgs {
|
|
|
|
Ok,
|
|
|
|
Warn,
|
|
|
|
Err,
|
|
|
|
}
|
|
|
|
|
2016-05-10 05:29:13 +00:00
|
|
|
impl<'a> LoweringContext<'a> {
|
2016-11-24 06:11:31 +02:00
|
|
|
fn lower_crate(mut self, c: &Crate) -> hir::Crate {
|
2017-01-09 17:46:11 +02:00
|
|
|
/// Full-crate AST visitor that inserts into a fresh
|
|
|
|
/// `LoweringContext` any information that may be
|
|
|
|
/// needed from arbitrary locations in the crate.
|
|
|
|
/// E.g. The number of lifetime generic parameters
|
|
|
|
/// declared for every type and trait definition.
|
|
|
|
struct MiscCollector<'lcx, 'interner: 'lcx> {
|
|
|
|
lctx: &'lcx mut LoweringContext<'interner>,
|
|
|
|
}
|
2016-10-27 22:04:22 +02:00
|
|
|
|
2017-01-09 17:46:11 +02:00
|
|
|
impl<'lcx, 'interner> Visitor<'lcx> for MiscCollector<'lcx, 'interner> {
|
|
|
|
fn visit_item(&mut self, item: &'lcx Item) {
|
2017-03-14 15:50:40 +01:00
|
|
|
self.lctx.allocate_hir_id_counter(item.id, item);
|
|
|
|
|
2017-01-09 17:46:11 +02:00
|
|
|
match item.node {
|
|
|
|
ItemKind::Struct(_, ref generics) |
|
|
|
|
ItemKind::Union(_, ref generics) |
|
|
|
|
ItemKind::Enum(_, ref generics) |
|
|
|
|
ItemKind::Ty(_, ref generics) |
|
2017-10-12 09:51:31 -03:00
|
|
|
ItemKind::Trait(_, _, ref generics, ..) => {
|
2017-01-09 17:46:11 +02:00
|
|
|
let def_id = self.lctx.resolver.definitions().local_def_id(item.id);
|
|
|
|
let count = generics.lifetimes.len();
|
|
|
|
self.lctx.type_def_lifetime_params.insert(def_id, count);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
visit::walk_item(self, item);
|
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2016-10-27 22:04:22 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
struct ItemLowerer<'lcx, 'interner: 'lcx> {
|
|
|
|
lctx: &'lcx mut LoweringContext<'interner>,
|
|
|
|
}
|
|
|
|
|
2017-11-10 12:33:05 -05:00
|
|
|
impl<'lcx, 'interner> ItemLowerer<'lcx, 'interner> {
|
|
|
|
fn with_trait_impl_ref<F>(&mut self, trait_impl_ref: &Option<TraitRef>, f: F)
|
|
|
|
where F: FnOnce(&mut Self)
|
|
|
|
{
|
|
|
|
let old = self.lctx.is_in_trait_impl;
|
|
|
|
self.lctx.is_in_trait_impl = if let &None = trait_impl_ref {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
f(self);
|
|
|
|
self.lctx.is_in_trait_impl = old;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
impl<'lcx, 'interner> Visitor<'lcx> for ItemLowerer<'lcx, 'interner> {
|
|
|
|
fn visit_item(&mut self, item: &'lcx Item) {
|
2017-03-14 15:50:40 +01:00
|
|
|
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 {
|
2017-11-16 22:59:45 -08:00
|
|
|
let item_lifetimes = match self.lctx.items.get(&item.id).unwrap().node {
|
|
|
|
hir::Item_::ItemImpl(_,_,_,ref generics,..) |
|
|
|
|
hir::Item_::ItemTrait(_,_,ref generics,..) =>
|
|
|
|
generics.lifetimes.clone(),
|
|
|
|
_ => Vec::new().into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.lctx.with_parent_impl_lifetime_defs(&item_lifetimes, |this| {
|
|
|
|
let this = &mut ItemLowerer { lctx: this };
|
|
|
|
if let ItemKind::Impl(_,_,_,_,ref opt_trait_ref,_,_) = item.node {
|
|
|
|
this.with_trait_impl_ref(opt_trait_ref, |this| {
|
|
|
|
visit::walk_item(this, item)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
visit::walk_item(this, item);
|
|
|
|
}
|
|
|
|
});
|
2017-03-05 05:15:58 +00:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2016-11-02 18:25:31 -04:00
|
|
|
|
2016-12-04 04:21:06 +02:00
|
|
|
fn visit_trait_item(&mut self, item: &'lcx TraitItem) {
|
2017-03-14 15:50:40 +01:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2016-12-04 04:21:06 +02:00
|
|
|
visit::walk_trait_item(self, item);
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_impl_item(&mut self, item: &'lcx ImplItem) {
|
2017-03-14 15:50:40 +01:00
|
|
|
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);
|
|
|
|
});
|
2016-11-02 18:25:31 -04:00
|
|
|
visit::walk_impl_item(self, item);
|
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
self.lower_node_id(CRATE_NODE_ID);
|
|
|
|
debug_assert!(self.node_id_to_hir_id[CRATE_NODE_ID] == hir::CRATE_HIR_ID);
|
|
|
|
|
2017-01-09 17:46:11 +02:00
|
|
|
visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c);
|
|
|
|
visit::walk_crate(&mut ItemLowerer { lctx: &mut self }, c);
|
|
|
|
|
|
|
|
let module = self.lower_mod(&c.module);
|
|
|
|
let attrs = self.lower_attrs(&c.attrs);
|
2017-02-21 12:23:47 -05:00
|
|
|
let body_ids = body_ids(&self.bodies);
|
2017-01-09 17:46:11 +02:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
self.resolver
|
|
|
|
.definitions()
|
|
|
|
.init_node_id_to_hir_id_mapping(self.node_id_to_hir_id);
|
|
|
|
|
2017-01-09 17:46:11 +02:00
|
|
|
hir::Crate {
|
2017-07-03 11:19:51 -07:00
|
|
|
module,
|
|
|
|
attrs,
|
2017-01-09 17:46:11 +02:00
|
|
|
span: c.span,
|
2017-03-05 05:15:58 +00:00
|
|
|
exported_macros: hir::HirVec::from(self.exported_macros),
|
2017-01-09 17:46:11 +02:00
|
|
|
items: self.items,
|
|
|
|
trait_items: self.trait_items,
|
|
|
|
impl_items: self.impl_items,
|
|
|
|
bodies: self.bodies,
|
2017-07-03 11:19:51 -07:00
|
|
|
body_ids,
|
2017-02-19 14:46:29 +02:00
|
|
|
trait_impls: self.trait_impls,
|
2017-10-09 13:59:20 -03:00
|
|
|
trait_auto_impl: self.trait_auto_impl,
|
2017-01-09 17:46:11 +02:00
|
|
|
}
|
2016-10-27 22:04:22 +02:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
fn allocate_hir_id_counter<T: Debug>(&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<F>(&mut self,
|
|
|
|
ast_node_id: NodeId,
|
|
|
|
alloc_hir_id: F)
|
2017-08-04 09:49:40 +02:00
|
|
|
-> LoweredNodeId
|
2017-03-14 15:50:40 +01:00
|
|
|
where F: FnOnce(&mut Self) -> hir::HirId
|
|
|
|
{
|
|
|
|
if ast_node_id == DUMMY_NODE_ID {
|
2017-08-04 09:49:40 +02:00
|
|
|
return LoweredNodeId {
|
|
|
|
node_id: DUMMY_NODE_ID,
|
|
|
|
hir_id: hir::DUMMY_HIR_ID,
|
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
let existing_hir_id = self.node_id_to_hir_id[ast_node_id];
|
|
|
|
|
|
|
|
if existing_hir_id == hir::DUMMY_HIR_ID {
|
2017-03-14 15:50:40 +01:00
|
|
|
// Generate a new HirId
|
2017-08-04 09:49:40 +02:00
|
|
|
let hir_id = alloc_hir_id(self);
|
|
|
|
self.node_id_to_hir_id[ast_node_id] = hir_id;
|
|
|
|
LoweredNodeId {
|
|
|
|
node_id: ast_node_id,
|
|
|
|
hir_id,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LoweredNodeId {
|
|
|
|
node_id: ast_node_id,
|
|
|
|
hir_id: existing_hir_id,
|
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_hir_id_owner<F>(&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.
|
2017-08-04 09:49:40 +02:00
|
|
|
fn lower_node_id(&mut self, ast_node_id: NodeId) -> LoweredNodeId {
|
2017-03-14 15:50:40 +01:00
|
|
|
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)
|
2017-08-04 09:49:40 +02:00
|
|
|
-> LoweredNodeId {
|
2017-03-14 15:50:40 +01:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-20 22:46:11 +02:00
|
|
|
fn record_body(&mut self, value: hir::Expr, decl: Option<&FnDecl>)
|
|
|
|
-> hir::BodyId {
|
2016-12-21 12:32:59 +02:00
|
|
|
let body = hir::Body {
|
2016-12-20 22:46:11 +02:00
|
|
|
arguments: decl.map_or(hir_vec![], |decl| {
|
|
|
|
decl.inputs.iter().map(|x| self.lower_arg(x)).collect()
|
|
|
|
}),
|
2017-07-28 13:09:33 -07:00
|
|
|
is_generator: self.is_generator,
|
2017-07-03 11:19:51 -07:00
|
|
|
value,
|
2016-12-21 12:32:59 +02:00
|
|
|
};
|
|
|
|
let id = body.id();
|
|
|
|
self.bodies.insert(id, body);
|
2016-10-27 22:04:22 +02:00
|
|
|
id
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
fn next_id(&mut self) -> LoweredNodeId {
|
2017-03-14 15:50:40 +01:00
|
|
|
self.lower_node_id(self.sess.next_node_id())
|
2016-06-22 01:16:56 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
fn expect_full_def(&mut self, id: NodeId) -> Def {
|
|
|
|
self.resolver.get_resolution(id).map_or(Def::Err, |pr| {
|
2017-02-18 22:11:42 +03:00
|
|
|
if pr.unresolved_segments() != 0 {
|
2016-11-25 13:21:19 +02:00
|
|
|
bug!("path not fully resolved: {:?}", pr);
|
|
|
|
}
|
2017-02-18 22:11:42 +03:00
|
|
|
pr.base_def()
|
2016-11-25 13:21:19 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-22 01:16:56 +00:00
|
|
|
fn diagnostic(&self) -> &errors::Handler {
|
2016-08-31 14:00:29 +03:00
|
|
|
self.sess.diagnostic()
|
2015-09-28 15:00:15 +13:00
|
|
|
}
|
2015-10-07 08:26:22 +13:00
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
fn str_to_ident(&self, s: &'static str) -> Name {
|
2016-11-16 08:21:52 +00:00
|
|
|
Symbol::gensym(s)
|
2015-10-07 08:26:22 +13:00
|
|
|
}
|
2016-03-15 13:03:42 -04:00
|
|
|
|
2017-07-31 23:04:34 +03:00
|
|
|
fn allow_internal_unstable(&self, reason: CompilerDesugaringKind, span: Span) -> Span
|
2017-08-16 18:46:48 -05:00
|
|
|
{
|
2017-03-22 08:39:51 +00:00
|
|
|
let mark = Mark::fresh(Mark::root());
|
2017-03-17 04:04:41 +00:00
|
|
|
mark.set_expn_info(codemap::ExpnInfo {
|
2016-10-30 03:42:12 +03:00
|
|
|
call_site: span,
|
|
|
|
callee: codemap::NameAndSpan {
|
2017-08-12 19:43:43 -05:00
|
|
|
format: codemap::CompilerDesugaring(reason),
|
2016-10-30 03:42:12 +03:00
|
|
|
span: Some(span),
|
|
|
|
allow_internal_unstable: true,
|
2017-08-08 18:21:20 +03:00
|
|
|
allow_internal_unsafe: false,
|
2016-10-30 03:42:12 +03:00
|
|
|
},
|
|
|
|
});
|
2017-07-31 23:04:34 +03:00
|
|
|
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))
|
2016-10-30 03:42:12 +03:00
|
|
|
}
|
|
|
|
|
2017-11-16 22:59:45 -08:00
|
|
|
// Creates a new hir::LifetimeDef for every new lifetime encountered
|
|
|
|
// while evaluating `f`. Definitions are created with the parent provided.
|
|
|
|
// If no `parent_id` is provided, no definitions will be returned.
|
|
|
|
fn collect_in_band_lifetime_defs<T, F>(
|
|
|
|
&mut self,
|
|
|
|
parent_id: Option<DefId>,
|
|
|
|
f: F
|
|
|
|
) -> (Vec<hir::LifetimeDef>, T) where F: FnOnce(&mut LoweringContext) -> T
|
|
|
|
{
|
|
|
|
assert!(!self.is_collecting_in_band_lifetimes);
|
|
|
|
assert!(self.lifetimes_to_define.is_empty());
|
|
|
|
self.is_collecting_in_band_lifetimes = self.sess.features.borrow().in_band_lifetimes;
|
|
|
|
|
|
|
|
let res = f(self);
|
|
|
|
|
|
|
|
self.is_collecting_in_band_lifetimes = false;
|
|
|
|
|
|
|
|
let lifetimes_to_define = self.lifetimes_to_define.split_off(0);
|
|
|
|
|
|
|
|
let lifetime_defs = match parent_id {
|
|
|
|
Some(parent_id) => lifetimes_to_define.into_iter().map(|(span, name)| {
|
|
|
|
let def_node_id = self.next_id().node_id;
|
|
|
|
|
|
|
|
// Add a definition for the in-band lifetime def
|
|
|
|
self.resolver.definitions().create_def_with_parent(
|
|
|
|
parent_id.index,
|
|
|
|
def_node_id,
|
|
|
|
DefPathData::LifetimeDef(name.as_str()),
|
|
|
|
DefIndexAddressSpace::High,
|
|
|
|
Mark::root()
|
|
|
|
);
|
|
|
|
|
|
|
|
hir::LifetimeDef {
|
|
|
|
lifetime: hir::Lifetime {
|
|
|
|
id: def_node_id,
|
|
|
|
span,
|
|
|
|
name: hir::LifetimeName::Name(name),
|
|
|
|
},
|
|
|
|
bounds: Vec::new().into(),
|
|
|
|
pure_wrt_drop: false,
|
|
|
|
in_band: true,
|
|
|
|
}
|
|
|
|
}).collect(),
|
|
|
|
None => Vec::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
(lifetime_defs, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluates `f` with the lifetimes in `lt_defs` in-scope.
|
|
|
|
// This is used to track which lifetimes have already been defined, and
|
|
|
|
// which are new in-band lifetimes that need to have a definition created
|
|
|
|
// for them.
|
|
|
|
fn with_in_scope_lifetime_defs<T, F>(
|
|
|
|
&mut self,
|
|
|
|
lt_defs: &[LifetimeDef],
|
|
|
|
f: F
|
|
|
|
) -> T where F: FnOnce(&mut LoweringContext) -> T
|
|
|
|
{
|
|
|
|
let old_len = self.in_scope_lifetimes.len();
|
|
|
|
let lt_def_names = lt_defs.iter().map(|lt_def| lt_def.lifetime.ident.name);
|
|
|
|
self.in_scope_lifetimes.extend(lt_def_names);
|
|
|
|
|
|
|
|
let res = f(self);
|
|
|
|
|
|
|
|
self.in_scope_lifetimes.truncate(old_len);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same as the method above, but accepts `hir::LifetimeDef`s
|
|
|
|
// instead of `ast::LifetimeDef`s.
|
|
|
|
// This should only be used with generics that have already had their
|
|
|
|
// in-band lifetimes added. In practice, this means that this function is
|
|
|
|
// only used when lowering a child item of a trait or impl.
|
|
|
|
fn with_parent_impl_lifetime_defs<T, F>(
|
|
|
|
&mut self,
|
|
|
|
lt_defs: &[hir::LifetimeDef],
|
|
|
|
f: F
|
|
|
|
) -> T where F: FnOnce(&mut LoweringContext) -> T
|
|
|
|
{
|
|
|
|
let old_len = self.in_scope_lifetimes.len();
|
|
|
|
let lt_def_names = lt_defs.iter().map(|lt_def| lt_def.lifetime.name.name());
|
|
|
|
self.in_scope_lifetimes.extend(lt_def_names);
|
|
|
|
|
|
|
|
let res = f(self);
|
|
|
|
|
|
|
|
self.in_scope_lifetimes.truncate(old_len);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Appends in-band lifetime defs to the existing set of out-of-band lifetime defs.
|
|
|
|
// Evaluates all within the context of the out-of-band defs.
|
|
|
|
// If provided, `impl_item_id` is used to find the parent impls of impl items so
|
|
|
|
// that their generics are not duplicated.
|
|
|
|
fn add_in_band_lifetime_defs<F, T>(
|
|
|
|
&mut self,
|
|
|
|
generics: &Generics,
|
|
|
|
parent_id: Option<DefId>,
|
|
|
|
f: F
|
|
|
|
) -> (hir::Generics, T)
|
|
|
|
where F: FnOnce(&mut LoweringContext) -> T
|
|
|
|
{
|
|
|
|
let (in_band_defs, (mut lowered_generics, res)) =
|
|
|
|
self.with_in_scope_lifetime_defs(&generics.lifetimes, |this| {
|
|
|
|
this.collect_in_band_lifetime_defs(parent_id, |this| {
|
|
|
|
(this.lower_generics(generics), f(this))
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
lowered_generics.lifetimes =
|
|
|
|
lowered_generics.lifetimes
|
|
|
|
.iter().cloned()
|
|
|
|
.chain(in_band_defs.into_iter())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
(lowered_generics, res)
|
|
|
|
}
|
|
|
|
|
2017-02-17 15:12:47 -08:00
|
|
|
fn with_catch_scope<T, F>(&mut self, catch_id: NodeId, f: F) -> T
|
|
|
|
where F: FnOnce(&mut LoweringContext) -> T
|
|
|
|
{
|
|
|
|
let len = self.catch_scopes.len();
|
|
|
|
self.catch_scopes.push(catch_id);
|
|
|
|
|
|
|
|
let result = f(self);
|
|
|
|
assert_eq!(len + 1, self.catch_scopes.len(),
|
|
|
|
"catch scopes should be added and removed in stack order");
|
|
|
|
|
|
|
|
self.catch_scopes.pop().unwrap();
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2016-12-26 14:34:03 +01:00
|
|
|
fn lower_body<F>(&mut self, decl: Option<&FnDecl>, f: F) -> hir::BodyId
|
|
|
|
where F: FnOnce(&mut LoweringContext) -> hir::Expr
|
|
|
|
{
|
2017-07-28 13:09:33 -07:00
|
|
|
let prev = mem::replace(&mut self.is_generator, false);
|
2016-12-26 14:34:03 +01:00
|
|
|
let result = f(self);
|
|
|
|
let r = self.record_body(result, decl);
|
2017-07-11 12:57:05 -07:00
|
|
|
self.is_generator = prev;
|
|
|
|
return r
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 14:52:27 -08:00
|
|
|
fn with_loop_scope<T, F>(&mut self, loop_id: NodeId, f: F) -> T
|
|
|
|
where F: FnOnce(&mut LoweringContext) -> T
|
|
|
|
{
|
2017-02-15 23:28:59 -08:00
|
|
|
// We're no longer in the base loop's condition; we're in another loop.
|
|
|
|
let was_in_loop_condition = self.is_in_loop_condition;
|
|
|
|
self.is_in_loop_condition = false;
|
|
|
|
|
2017-02-15 14:52:27 -08:00
|
|
|
let len = self.loop_scopes.len();
|
|
|
|
self.loop_scopes.push(loop_id);
|
2017-02-15 23:28:59 -08:00
|
|
|
|
2017-02-15 14:52:27 -08:00
|
|
|
let result = f(self);
|
|
|
|
assert_eq!(len + 1, self.loop_scopes.len(),
|
|
|
|
"Loop scopes should be added and removed in stack order");
|
2017-02-15 23:28:59 -08:00
|
|
|
|
2017-02-15 14:52:27 -08:00
|
|
|
self.loop_scopes.pop().unwrap();
|
2017-02-15 23:28:59 -08:00
|
|
|
|
|
|
|
self.is_in_loop_condition = was_in_loop_condition;
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_loop_condition_scope<T, F>(&mut self, f: F) -> T
|
|
|
|
where F: FnOnce(&mut LoweringContext) -> T
|
|
|
|
{
|
|
|
|
let was_in_loop_condition = self.is_in_loop_condition;
|
|
|
|
self.is_in_loop_condition = true;
|
|
|
|
|
|
|
|
let result = f(self);
|
|
|
|
|
|
|
|
self.is_in_loop_condition = was_in_loop_condition;
|
|
|
|
|
2017-02-15 14:52:27 -08:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2017-02-17 15:12:47 -08:00
|
|
|
fn with_new_scopes<T, F>(&mut self, f: F) -> T
|
2017-02-15 14:52:27 -08:00
|
|
|
where F: FnOnce(&mut LoweringContext) -> T
|
|
|
|
{
|
2017-02-15 23:28:59 -08:00
|
|
|
let was_in_loop_condition = self.is_in_loop_condition;
|
|
|
|
self.is_in_loop_condition = false;
|
|
|
|
|
2017-02-17 15:12:47 -08:00
|
|
|
let catch_scopes = mem::replace(&mut self.catch_scopes, Vec::new());
|
2017-02-15 14:52:27 -08:00
|
|
|
let loop_scopes = mem::replace(&mut self.loop_scopes, Vec::new());
|
|
|
|
let result = f(self);
|
2017-02-17 15:12:47 -08:00
|
|
|
self.catch_scopes = catch_scopes;
|
|
|
|
self.loop_scopes = loop_scopes;
|
2017-02-15 23:28:59 -08:00
|
|
|
|
|
|
|
self.is_in_loop_condition = was_in_loop_condition;
|
|
|
|
|
2017-02-15 14:52:27 -08:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:59:19 +00:00
|
|
|
fn with_parent_def<T, F>(&mut self, parent_id: NodeId, f: F) -> T
|
|
|
|
where F: FnOnce(&mut LoweringContext) -> T
|
|
|
|
{
|
|
|
|
let old_def = self.parent_def;
|
2016-08-29 05:12:03 +03:00
|
|
|
self.parent_def = {
|
|
|
|
let defs = self.resolver.definitions();
|
|
|
|
Some(defs.opt_def_index(parent_id).unwrap())
|
2016-05-09 07:59:19 +00:00
|
|
|
};
|
2016-05-06 08:24:04 +00:00
|
|
|
|
2016-05-09 07:59:19 +00:00
|
|
|
let result = f(self);
|
2016-04-18 10:30:55 +12:00
|
|
|
|
2016-05-09 07:59:19 +00:00
|
|
|
self.parent_def = old_def;
|
2016-04-18 10:30:55 +12:00
|
|
|
result
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-01-09 17:46:11 +02:00
|
|
|
fn def_key(&mut self, id: DefId) -> DefKey {
|
|
|
|
if id.is_local() {
|
|
|
|
self.resolver.definitions().def_key(id.index)
|
|
|
|
} else {
|
2017-09-05 16:48:24 +02:00
|
|
|
self.cstore.def_key(id)
|
2017-01-09 17:46:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 23:03:15 +00:00
|
|
|
fn lower_ident(&mut self, ident: Ident) -> Name {
|
|
|
|
let ident = ident.modern();
|
|
|
|
if ident.ctxt == SyntaxContext::empty() {
|
|
|
|
return ident.name;
|
|
|
|
}
|
|
|
|
*self.name_map.entry(ident).or_insert_with(|| Symbol::from_ident(ident))
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:22:03 +02:00
|
|
|
fn lower_opt_sp_ident(&mut self, o_id: Option<Spanned<Ident>>) -> Option<Spanned<Name>> {
|
2016-06-11 00:12:39 +03:00
|
|
|
o_id.map(|sp_ident| respan(sp_ident.span, sp_ident.node.name))
|
2016-05-02 18:22:03 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 11:05:03 -08:00
|
|
|
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Spanned<Ident>)>)
|
2017-02-17 18:55:28 -08:00
|
|
|
-> hir::Destination
|
|
|
|
{
|
|
|
|
match destination {
|
2017-02-28 11:05:03 -08:00
|
|
|
Some((id, label_ident)) => {
|
|
|
|
let target = if let Def::Label(loop_id) = self.expect_full_def(id) {
|
2017-08-04 09:49:40 +02:00
|
|
|
hir::LoopIdResult::Ok(self.lower_node_id(loop_id).node_id)
|
2017-02-15 23:28:59 -08:00
|
|
|
} else {
|
|
|
|
hir::LoopIdResult::Err(hir::LoopIdError::UnresolvedLabel)
|
2017-02-28 11:05:03 -08:00
|
|
|
};
|
|
|
|
hir::Destination {
|
|
|
|
ident: Some(label_ident),
|
|
|
|
target_id: hir::ScopeTarget::Loop(target),
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
2017-02-15 14:52:27 -08:00
|
|
|
},
|
2017-03-14 15:50:40 +01:00
|
|
|
None => {
|
|
|
|
let loop_id = self.loop_scopes
|
|
|
|
.last()
|
|
|
|
.map(|innermost_loop_id| *innermost_loop_id);
|
|
|
|
|
|
|
|
hir::Destination {
|
|
|
|
ident: None,
|
|
|
|
target_id: hir::ScopeTarget::Loop(
|
2017-08-04 09:49:40 +02:00
|
|
|
loop_id.map(|id| Ok(self.lower_node_id(id).node_id))
|
2017-03-14 15:50:40 +01:00
|
|
|
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
|
|
|
|
.into())
|
|
|
|
}
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
2017-02-15 14:52:27 -08:00
|
|
|
}
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_attrs(&mut self, attrs: &Vec<Attribute>) -> hir::HirVec<Attribute> {
|
2017-09-15 08:28:34 -07:00
|
|
|
attrs.iter().map(|a| self.lower_attr(a)).collect::<Vec<_>>().into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_attr(&mut self, attr: &Attribute) -> Attribute {
|
|
|
|
Attribute {
|
|
|
|
id: attr.id,
|
|
|
|
style: attr.style,
|
|
|
|
path: attr.path.clone(),
|
|
|
|
tokens: self.lower_token_stream(attr.tokens.clone()),
|
|
|
|
is_sugared_doc: attr.is_sugared_doc,
|
|
|
|
span: attr.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_token_stream(&mut self, tokens: TokenStream) -> TokenStream {
|
2017-09-21 08:13:25 -07:00
|
|
|
tokens.into_trees()
|
|
|
|
.flat_map(|tree| self.lower_token_tree(tree).into_trees())
|
|
|
|
.collect()
|
2017-09-15 08:28:34 -07:00
|
|
|
}
|
|
|
|
|
2017-09-21 08:13:25 -07:00
|
|
|
fn lower_token_tree(&mut self, tree: TokenTree) -> TokenStream {
|
2017-09-15 08:28:34 -07:00
|
|
|
match tree {
|
|
|
|
TokenTree::Token(span, token) => {
|
|
|
|
self.lower_token(token, span)
|
|
|
|
}
|
|
|
|
TokenTree::Delimited(span, delimited) => {
|
|
|
|
TokenTree::Delimited(span, Delimited {
|
|
|
|
delim: delimited.delim,
|
|
|
|
tts: self.lower_token_stream(delimited.tts.into()).into(),
|
2017-09-21 08:13:25 -07:00
|
|
|
}).into()
|
2017-09-15 08:28:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-21 08:13:25 -07:00
|
|
|
fn lower_token(&mut self, token: Token, span: Span) -> TokenStream {
|
2017-09-15 08:28:34 -07:00
|
|
|
match token {
|
|
|
|
Token::Interpolated(_) => {}
|
2017-09-21 08:13:25 -07:00
|
|
|
other => return TokenTree::Token(span, other).into(),
|
2017-09-15 08:28:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let tts = token.interpolated_to_tokenstream(&self.sess.parse_sess, span);
|
2017-09-21 08:13:25 -07:00
|
|
|
self.lower_token_stream(tts)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {
|
|
|
|
hir::Arm {
|
|
|
|
attrs: self.lower_attrs(&arm.attrs),
|
|
|
|
pats: arm.pats.iter().map(|x| self.lower_pat(x)).collect(),
|
2016-10-28 21:16:44 +11:00
|
|
|
guard: arm.guard.as_ref().map(|ref x| P(self.lower_expr(x))),
|
|
|
|
body: P(self.lower_expr(&arm.body)),
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
fn lower_ty_binding(&mut self, b: &TypeBinding, itctx: ImplTraitContext) -> hir::TypeBinding {
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::TypeBinding {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: self.lower_node_id(b.id).node_id,
|
2017-03-24 23:03:15 +00:00
|
|
|
name: self.lower_ident(b.ident),
|
2017-11-10 12:23:59 -05:00
|
|
|
ty: self.lower_ty(&b.ty, itctx),
|
2016-05-10 01:11:59 +00:00
|
|
|
span: b.span,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext) -> P<hir::Ty> {
|
2017-03-14 15:50:40 +01:00
|
|
|
let kind = match t.node {
|
|
|
|
TyKind::Infer => hir::TyInfer,
|
2017-03-28 18:56:29 -07:00
|
|
|
TyKind::Err => hir::TyErr,
|
2017-11-10 12:23:59 -05:00
|
|
|
TyKind::Slice(ref ty) => hir::TySlice(self.lower_ty(ty, itctx)),
|
|
|
|
TyKind::Ptr(ref mt) => hir::TyPtr(self.lower_mt(mt, itctx)),
|
2017-03-14 15:50:40 +01:00
|
|
|
TyKind::Rptr(ref region, ref mt) => {
|
2017-07-31 23:04:34 +03:00
|
|
|
let span = t.span.with_hi(t.span.lo());
|
2017-03-14 15:50:40 +01:00
|
|
|
let lifetime = match *region {
|
|
|
|
Some(ref lt) => self.lower_lifetime(lt),
|
|
|
|
None => self.elided_lifetime(span)
|
|
|
|
};
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::TyRptr(lifetime, self.lower_mt(mt, itctx))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
TyKind::BareFn(ref f) => {
|
2017-11-16 22:59:45 -08:00
|
|
|
self.with_in_scope_lifetime_defs(&f.lifetimes, |this|
|
|
|
|
hir::TyBareFn(P(hir::BareFnTy {
|
|
|
|
lifetimes: this.lower_lifetime_defs(&f.lifetimes),
|
|
|
|
unsafety: this.lower_unsafety(f.unsafety),
|
|
|
|
abi: f.abi,
|
|
|
|
decl: this.lower_fn_decl(&f.decl, None, false),
|
|
|
|
arg_names: this.lower_fn_args_to_names(&f.decl),
|
|
|
|
})))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
TyKind::Never => hir::TyNever,
|
|
|
|
TyKind::Tup(ref tys) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::TyTup(tys.iter().map(|ty| self.lower_ty(ty, itctx)).collect())
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
TyKind::Paren(ref ty) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
return self.lower_ty(ty, itctx);
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
TyKind::Path(ref qself, ref path) => {
|
2017-09-13 18:29:59 +03:00
|
|
|
let id = self.lower_node_id(t.id);
|
2017-11-10 12:23:59 -05:00
|
|
|
let qpath = self.lower_qpath(t.id, qself, path, ParamMode::Explicit, itctx);
|
2017-03-14 15:50:40 +01:00
|
|
|
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),
|
2017-09-21 23:24:26 +03:00
|
|
|
segments: hir_vec![
|
|
|
|
hir::PathSegment::from_name(keywords::SelfType.name())
|
|
|
|
],
|
2017-03-14 15:50:40 +01:00
|
|
|
span: t.span,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
TyKind::Array(ref ty, ref length) => {
|
2017-07-20 05:17:07 +02:00
|
|
|
let length = self.lower_body(None, |this| this.lower_expr(length));
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::TyArray(self.lower_ty(ty, itctx), length)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
TyKind::Typeof(ref expr) => {
|
2017-07-20 05:17:07 +02:00
|
|
|
let expr = self.lower_body(None, |this| this.lower_expr(expr));
|
|
|
|
hir::TyTypeof(expr)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2017-10-10 17:33:19 +03:00
|
|
|
TyKind::TraitObject(ref bounds, ..) => {
|
2017-03-14 15:50:40 +01:00
|
|
|
let mut lifetime_bound = None;
|
|
|
|
let bounds = bounds.iter().filter_map(|bound| {
|
|
|
|
match *bound {
|
|
|
|
TraitTyParamBound(ref ty, TraitBoundModifier::None) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
Some(self.lower_poly_trait_ref(ty, itctx))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
TraitTyParamBound(_, TraitBoundModifier::Maybe) => None,
|
|
|
|
RegionTyParamBound(ref lifetime) => {
|
|
|
|
if lifetime_bound.is_none() {
|
2017-01-24 17:17:06 +02:00
|
|
|
lifetime_bound = Some(self.lower_lifetime(lifetime));
|
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
None
|
2017-01-24 17:17:06 +02:00
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
let lifetime_bound = lifetime_bound.unwrap_or_else(|| {
|
|
|
|
self.elided_lifetime(t.span)
|
|
|
|
});
|
|
|
|
hir::TyTraitObject(bounds, lifetime_bound)
|
|
|
|
}
|
|
|
|
TyKind::ImplTrait(ref bounds) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
use syntax::feature_gate::{emit_feature_err, GateIssue};
|
|
|
|
match itctx {
|
|
|
|
ImplTraitContext::Existential => {
|
2017-11-10 12:58:52 -05:00
|
|
|
let has_feature = self.sess.features.borrow().conservative_impl_trait;
|
|
|
|
if !t.span.allows_unstable() && !has_feature {
|
|
|
|
emit_feature_err(&self.sess.parse_sess, "conservative_impl_trait",
|
|
|
|
t.span, GateIssue::Language,
|
|
|
|
"`impl Trait` in return position is experimental");
|
|
|
|
}
|
2017-10-15 13:43:06 -07:00
|
|
|
let def_index = self.resolver.definitions().opt_def_index(t.id).unwrap();
|
|
|
|
let hir_bounds = self.lower_bounds(bounds, itctx);
|
|
|
|
let (lifetimes, lifetime_defs) =
|
|
|
|
self.lifetimes_from_impl_trait_bounds(def_index, &hir_bounds);
|
|
|
|
|
|
|
|
hir::TyImplTraitExistential(hir::ExistTy {
|
|
|
|
generics: hir::Generics {
|
|
|
|
lifetimes: lifetime_defs,
|
|
|
|
// Type parameters are taken from environment:
|
|
|
|
ty_params: Vec::new().into(),
|
|
|
|
where_clause: hir::WhereClause {
|
|
|
|
id: self.next_id().node_id,
|
|
|
|
predicates: Vec::new().into(),
|
|
|
|
},
|
|
|
|
span: t.span,
|
|
|
|
},
|
|
|
|
bounds: hir_bounds,
|
|
|
|
}, lifetimes)
|
2017-11-10 12:23:59 -05:00
|
|
|
},
|
|
|
|
ImplTraitContext::Universal(def_id) => {
|
2017-11-10 12:58:52 -05:00
|
|
|
let has_feature = self.sess.features.borrow().universal_impl_trait;
|
|
|
|
if !t.span.allows_unstable() && !has_feature {
|
|
|
|
emit_feature_err(&self.sess.parse_sess, "universal_impl_trait",
|
|
|
|
t.span, GateIssue::Language,
|
|
|
|
"`impl Trait` in argument position is experimental");
|
|
|
|
}
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::TyImplTraitUniversal(def_id, self.lower_bounds(bounds, itctx))
|
|
|
|
},
|
|
|
|
ImplTraitContext::Disallowed => {
|
|
|
|
span_err!(self.sess, t.span, E0562,
|
|
|
|
"`impl Trait` not allowed outside of function \
|
|
|
|
and inherent method return types");
|
|
|
|
hir::TyErr
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
TyKind::Mac(_) => panic!("TyMac should have been expanded by now."),
|
|
|
|
};
|
|
|
|
|
2017-09-13 18:29:59 +03:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(t.id);
|
2017-03-14 15:50:40 +01:00
|
|
|
P(hir::Ty {
|
2017-09-13 18:29:59 +03:00
|
|
|
id: node_id,
|
2017-03-14 15:50:40 +01:00
|
|
|
node: kind,
|
2016-05-10 01:11:59 +00:00
|
|
|
span: t.span,
|
2017-09-13 18:29:59 +03:00
|
|
|
hir_id,
|
2016-05-10 01:11:59 +00:00
|
|
|
})
|
2015-12-07 17:17:41 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-10-15 13:43:06 -07:00
|
|
|
fn lifetimes_from_impl_trait_bounds(
|
|
|
|
&mut self,
|
|
|
|
parent_index: DefIndex,
|
|
|
|
bounds: &hir::TyParamBounds
|
|
|
|
) -> (HirVec<hir::Lifetime>, HirVec<hir::LifetimeDef>) {
|
|
|
|
|
|
|
|
// This visitor walks over impl trait bounds and creates defs for all lifetimes which
|
|
|
|
// appear in the bounds, excluding lifetimes that are created within the bounds.
|
|
|
|
// e.g. 'a, 'b, but not 'c in `impl for<'c> SomeTrait<'a, 'b, 'c>`
|
|
|
|
struct ImplTraitLifetimeCollector<'r, 'a: 'r> {
|
|
|
|
context: &'r mut LoweringContext<'a>,
|
|
|
|
parent: DefIndex,
|
2017-12-09 21:54:58 -08:00
|
|
|
collect_elided_lifetimes: bool,
|
|
|
|
currently_bound_lifetimes: Vec<hir::LifetimeName>,
|
|
|
|
already_defined_lifetimes: HashSet<hir::LifetimeName>,
|
2017-10-15 13:43:06 -07:00
|
|
|
output_lifetimes: Vec<hir::Lifetime>,
|
|
|
|
output_lifetime_defs: Vec<hir::LifetimeDef>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'r, 'a: 'r, 'v> hir::intravisit::Visitor<'v> for ImplTraitLifetimeCollector<'r, 'a> {
|
|
|
|
fn nested_visit_map<'this>(&'this mut self)
|
|
|
|
-> hir::intravisit::NestedVisitorMap<'this, 'v> {
|
|
|
|
hir::intravisit::NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
|
2017-12-09 21:54:58 -08:00
|
|
|
fn visit_path_parameters(&mut self, span: Span, parameters: &'v hir::PathParameters) {
|
|
|
|
// Don't collect elided lifetimes used inside of `Fn()` syntax.
|
|
|
|
if parameters.parenthesized {
|
|
|
|
let old_collect_elided_lifetimes = self.collect_elided_lifetimes;
|
|
|
|
self.collect_elided_lifetimes = false;
|
|
|
|
hir::intravisit::walk_path_parameters(self, span, parameters);
|
|
|
|
self.collect_elided_lifetimes = old_collect_elided_lifetimes;
|
|
|
|
} else {
|
|
|
|
hir::intravisit::walk_path_parameters(self, span, parameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_ty(&mut self, t: &'v hir::Ty) {
|
|
|
|
// Don't collect elided lifetimes used inside of `fn()` syntax
|
|
|
|
if let &hir::Ty_::TyBareFn(_) = &t.node {
|
|
|
|
let old_collect_elided_lifetimes = self.collect_elided_lifetimes;
|
|
|
|
self.collect_elided_lifetimes = false;
|
|
|
|
hir::intravisit::walk_ty(self, t);
|
|
|
|
self.collect_elided_lifetimes = old_collect_elided_lifetimes;
|
|
|
|
} else {
|
|
|
|
hir::intravisit::walk_ty(self, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 13:43:06 -07:00
|
|
|
fn visit_poly_trait_ref(&mut self,
|
|
|
|
polytr: &'v hir::PolyTraitRef,
|
|
|
|
_: hir::TraitBoundModifier) {
|
|
|
|
let old_len = self.currently_bound_lifetimes.len();
|
|
|
|
|
|
|
|
// Record the introduction of 'a in `for<'a> ...`
|
|
|
|
for lt_def in &polytr.bound_lifetimes {
|
|
|
|
// Introduce lifetimes one at a time so that we can handle
|
2017-12-09 21:54:58 -08:00
|
|
|
// cases like `fn foo<'d>() -> impl for<'a, 'b: 'a, 'c: 'b + 'd>`
|
|
|
|
self.currently_bound_lifetimes.push(lt_def.lifetime.name);
|
2017-10-15 13:43:06 -07:00
|
|
|
|
|
|
|
// Visit the lifetime bounds
|
|
|
|
for lt_bound in <_def.bounds {
|
|
|
|
self.visit_lifetime(<_bound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hir::intravisit::walk_trait_ref(self, &polytr.trait_ref);
|
|
|
|
|
|
|
|
self.currently_bound_lifetimes.truncate(old_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
|
2017-12-09 21:54:58 -08:00
|
|
|
let name = match lifetime.name {
|
|
|
|
hir::LifetimeName::Implicit |
|
|
|
|
hir::LifetimeName::Underscore =>
|
|
|
|
if self.collect_elided_lifetimes {
|
|
|
|
// Use `'_` for both implicit and underscore lifetimes in
|
|
|
|
// `abstract type Foo<'_>: SomeTrait<'_>;`
|
|
|
|
hir::LifetimeName::Underscore
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name @ hir::LifetimeName::Name(_) => name,
|
|
|
|
hir::LifetimeName::Static => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
if !self.currently_bound_lifetimes.contains(&name) &&
|
|
|
|
!self.already_defined_lifetimes.contains(&name)
|
|
|
|
{
|
|
|
|
self.already_defined_lifetimes.insert(name);
|
|
|
|
|
|
|
|
self.output_lifetimes.push(hir::Lifetime {
|
|
|
|
id: self.context.next_id().node_id,
|
|
|
|
span: lifetime.span,
|
|
|
|
name,
|
|
|
|
});
|
|
|
|
|
|
|
|
let def_node_id = self.context.next_id().node_id;
|
|
|
|
self.context.resolver.definitions().create_def_with_parent(
|
|
|
|
self.parent,
|
|
|
|
def_node_id,
|
|
|
|
DefPathData::LifetimeDef(name.name().as_str()),
|
|
|
|
DefIndexAddressSpace::High,
|
|
|
|
Mark::root()
|
|
|
|
);
|
|
|
|
let def_lifetime = hir::Lifetime {
|
|
|
|
id: def_node_id,
|
|
|
|
span: lifetime.span,
|
|
|
|
name: name,
|
|
|
|
};
|
|
|
|
self.output_lifetime_defs.push(hir::LifetimeDef {
|
|
|
|
lifetime: def_lifetime,
|
|
|
|
bounds: Vec::new().into(),
|
|
|
|
pure_wrt_drop: false,
|
|
|
|
in_band: false,
|
|
|
|
});
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut lifetime_collector = ImplTraitLifetimeCollector {
|
|
|
|
context: self,
|
|
|
|
parent: parent_index,
|
2017-12-09 21:54:58 -08:00
|
|
|
collect_elided_lifetimes: true,
|
2017-10-15 13:43:06 -07:00
|
|
|
currently_bound_lifetimes: Vec::new(),
|
|
|
|
already_defined_lifetimes: HashSet::new(),
|
|
|
|
output_lifetimes: Vec::new(),
|
|
|
|
output_lifetime_defs: Vec::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
for bound in bounds {
|
|
|
|
hir::intravisit::walk_ty_param_bound(&mut lifetime_collector, &bound);
|
|
|
|
}
|
|
|
|
|
|
|
|
(
|
|
|
|
lifetime_collector.output_lifetimes.into(),
|
|
|
|
lifetime_collector.output_lifetime_defs.into()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_foreign_mod(&mut self, fm: &ForeignMod) -> hir::ForeignMod {
|
|
|
|
hir::ForeignMod {
|
|
|
|
abi: fm.abi,
|
|
|
|
items: fm.items.iter().map(|x| self.lower_foreign_item(x)).collect(),
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2015-12-01 20:38:40 +03:00
|
|
|
|
2017-03-15 21:27:40 -05:00
|
|
|
fn lower_global_asm(&mut self, ga: &GlobalAsm) -> P<hir::GlobalAsm> {
|
|
|
|
P(hir::GlobalAsm {
|
|
|
|
asm: ga.asm,
|
|
|
|
ctxt: ga.ctxt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_variant(&mut self, v: &Variant) -> hir::Variant {
|
|
|
|
Spanned {
|
|
|
|
node: hir::Variant_ {
|
|
|
|
name: v.node.name.name,
|
|
|
|
attrs: self.lower_attrs(&v.node.attrs),
|
|
|
|
data: self.lower_variant_data(&v.node.data),
|
2016-12-21 12:32:59 +02:00
|
|
|
disr_expr: v.node.disr_expr.as_ref().map(|e| {
|
2016-12-26 14:34:03 +01:00
|
|
|
self.lower_body(None, |this| this.lower_expr(e))
|
2016-12-21 12:32:59 +02:00
|
|
|
}),
|
2016-05-10 01:11:59 +00:00
|
|
|
},
|
|
|
|
span: v.span,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-10-27 05:17:42 +03:00
|
|
|
fn lower_qpath(&mut self,
|
|
|
|
id: NodeId,
|
|
|
|
qself: &Option<QSelf>,
|
|
|
|
p: &Path,
|
2017-11-10 12:23:59 -05:00
|
|
|
param_mode: ParamMode,
|
|
|
|
itctx: ImplTraitContext)
|
2016-10-27 05:17:42 +03:00
|
|
|
-> hir::QPath {
|
|
|
|
let qself_position = qself.as_ref().map(|q| q.position);
|
2017-11-10 12:23:59 -05:00
|
|
|
let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx));
|
2016-10-27 05:17:42 +03:00
|
|
|
|
|
|
|
let resolution = self.resolver.get_resolution(id)
|
|
|
|
.unwrap_or(PathResolution::new(Def::Err));
|
|
|
|
|
2017-02-18 22:11:42 +03:00
|
|
|
let proj_start = p.segments.len() - resolution.unresolved_segments();
|
2016-10-27 05:17:42 +03:00
|
|
|
let path = P(hir::Path {
|
2017-02-18 22:11:42 +03:00
|
|
|
def: resolution.base_def(),
|
2016-10-27 05:17:42 +03:00
|
|
|
segments: p.segments[..proj_start].iter().enumerate().map(|(i, segment)| {
|
|
|
|
let param_mode = match (qself_position, param_mode) {
|
|
|
|
(Some(j), ParamMode::Optional) if i < j => {
|
2016-10-17 06:02:23 +03:00
|
|
|
// This segment is part of the trait path in a
|
|
|
|
// qualified path - one of `a`, `b` or `Trait`
|
|
|
|
// in `<X as a::b::Trait>::T::U::method`.
|
|
|
|
ParamMode::Explicit
|
|
|
|
}
|
|
|
|
_ => param_mode
|
|
|
|
};
|
2017-01-09 17:46:11 +02:00
|
|
|
|
|
|
|
// Figure out if this is a type/trait segment,
|
|
|
|
// which may need lifetime elision performed.
|
|
|
|
let parent_def_id = |this: &mut Self, def_id: DefId| {
|
|
|
|
DefId {
|
|
|
|
krate: def_id.krate,
|
|
|
|
index: this.def_key(def_id).parent.expect("missing parent")
|
|
|
|
}
|
|
|
|
};
|
2017-02-18 22:11:42 +03:00
|
|
|
let type_def_id = match resolution.base_def() {
|
2017-01-09 17:46:11 +02:00
|
|
|
Def::AssociatedTy(def_id) if i + 2 == proj_start => {
|
|
|
|
Some(parent_def_id(self, def_id))
|
|
|
|
}
|
|
|
|
Def::Variant(def_id) if i + 1 == proj_start => {
|
|
|
|
Some(parent_def_id(self, def_id))
|
|
|
|
}
|
|
|
|
Def::Struct(def_id) |
|
|
|
|
Def::Union(def_id) |
|
|
|
|
Def::Enum(def_id) |
|
|
|
|
Def::TyAlias(def_id) |
|
|
|
|
Def::Trait(def_id) if i + 1 == proj_start => Some(def_id),
|
|
|
|
_ => None
|
|
|
|
};
|
2017-07-29 01:13:40 +03:00
|
|
|
let parenthesized_generic_args = match resolution.base_def() {
|
|
|
|
// `a::b::Trait(Args)`
|
|
|
|
Def::Trait(..) if i + 1 == proj_start => ParenthesizedGenericArgs::Ok,
|
|
|
|
// `a::b::Trait(Args)::TraitItem`
|
|
|
|
Def::Method(..) |
|
|
|
|
Def::AssociatedConst(..) |
|
|
|
|
Def::AssociatedTy(..) if i + 2 == proj_start => ParenthesizedGenericArgs::Ok,
|
|
|
|
// Avoid duplicated errors
|
|
|
|
Def::Err => ParenthesizedGenericArgs::Ok,
|
|
|
|
// An error
|
|
|
|
Def::Struct(..) | Def::Enum(..) | Def::Union(..) | Def::TyAlias(..) |
|
|
|
|
Def::Variant(..) if i + 1 == proj_start => ParenthesizedGenericArgs::Err,
|
|
|
|
// A warning for now, for compatibility reasons
|
|
|
|
_ => ParenthesizedGenericArgs::Warn,
|
|
|
|
};
|
2017-01-09 17:46:11 +02:00
|
|
|
|
|
|
|
let num_lifetimes = type_def_id.map_or(0, |def_id| {
|
|
|
|
if let Some(&n) = self.type_def_lifetime_params.get(&def_id) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
assert!(!def_id.is_local());
|
2017-11-07 15:14:32 +01:00
|
|
|
let n = self.cstore
|
|
|
|
.item_generics_cloned_untracked(def_id, self.sess)
|
|
|
|
.regions
|
|
|
|
.len();
|
2017-01-09 17:46:11 +02:00
|
|
|
self.type_def_lifetime_params.insert(def_id, n);
|
|
|
|
n
|
|
|
|
});
|
2017-07-29 01:13:40 +03:00
|
|
|
self.lower_path_segment(p.span, segment, param_mode, num_lifetimes,
|
2017-11-10 12:23:59 -05:00
|
|
|
parenthesized_generic_args, itctx)
|
2016-10-17 06:02:23 +03:00
|
|
|
}).collect(),
|
2016-05-10 01:11:59 +00:00
|
|
|
span: p.span,
|
2016-10-27 05:17:42 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Simple case, either no projections, or only fully-qualified.
|
|
|
|
// E.g. `std::mem::size_of` or `<I as Iterator>::Item`.
|
2017-02-18 22:11:42 +03:00
|
|
|
if resolution.unresolved_segments() == 0 {
|
2016-10-27 05:17:42 +03:00
|
|
|
return hir::QPath::Resolved(qself, path);
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2016-10-27 05:17:42 +03:00
|
|
|
|
|
|
|
// Create the innermost type that we're projecting from.
|
|
|
|
let mut ty = if path.segments.is_empty() {
|
|
|
|
// If the base path is empty that means there exists a
|
|
|
|
// syntactical `Self`, e.g. `&i32` in `<&i32>::clone`.
|
|
|
|
qself.expect("missing QSelf for <T>::...")
|
|
|
|
} else {
|
|
|
|
// Otherwise, the base path is an implicit `Self` type path,
|
|
|
|
// e.g. `Vec` in `Vec::new` or `<I as Iterator>::Item` in
|
|
|
|
// `<I as Iterator>::Item::default`.
|
2017-09-13 18:29:59 +03:00
|
|
|
let new_id = self.next_id();
|
2017-01-17 16:46:23 +02:00
|
|
|
self.ty_path(new_id, p.span, hir::QPath::Resolved(qself, path))
|
2016-10-27 05:17:42 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Anything after the base path are associated "extensions",
|
|
|
|
// out of which all but the last one are associated types,
|
|
|
|
// e.g. for `std::vec::Vec::<T>::IntoIter::Item::clone`:
|
|
|
|
// * base path is `std::vec::Vec<T>`
|
|
|
|
// * "extensions" are `IntoIter`, `Item` and `clone`
|
|
|
|
// * type nodes are:
|
|
|
|
// 1. `std::vec::Vec<T>` (created above)
|
|
|
|
// 2. `<std::vec::Vec<T>>::IntoIter`
|
|
|
|
// 3. `<<std::vec::Vec<T>>::IntoIter>::Item`
|
|
|
|
// * final path is `<<<std::vec::Vec<T>>::IntoIter>::Item>::clone`
|
|
|
|
for (i, segment) in p.segments.iter().enumerate().skip(proj_start) {
|
2017-07-29 01:13:40 +03:00
|
|
|
let segment = P(self.lower_path_segment(p.span, segment, param_mode, 0,
|
2017-11-10 12:23:59 -05:00
|
|
|
ParenthesizedGenericArgs::Warn,
|
|
|
|
itctx));
|
2016-10-27 05:17:42 +03:00
|
|
|
let qpath = hir::QPath::TypeRelative(ty, segment);
|
|
|
|
|
|
|
|
// It's finished, return the extension of the right node type.
|
|
|
|
if i == p.segments.len() - 1 {
|
|
|
|
return qpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the associated extension in another type node.
|
2017-09-13 18:29:59 +03:00
|
|
|
let new_id = self.next_id();
|
2017-01-17 16:46:23 +02:00
|
|
|
ty = self.ty_path(new_id, p.span, qpath);
|
2016-10-27 05:17:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should've returned in the for loop above.
|
|
|
|
span_bug!(p.span, "lower_qpath: no final extension segment in {}..{}",
|
|
|
|
proj_start, p.segments.len())
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-11-24 06:11:31 +02:00
|
|
|
fn lower_path_extra(&mut self,
|
2016-11-25 13:21:19 +02:00
|
|
|
id: NodeId,
|
2016-11-24 06:11:31 +02:00
|
|
|
p: &Path,
|
|
|
|
name: Option<Name>,
|
2016-12-05 03:51:11 +00:00
|
|
|
param_mode: ParamMode,
|
|
|
|
defaults_to_global: bool)
|
2016-11-24 06:11:31 +02:00
|
|
|
-> hir::Path {
|
2016-12-05 03:51:11 +00:00
|
|
|
let mut segments = p.segments.iter();
|
|
|
|
if defaults_to_global && p.is_global() {
|
|
|
|
segments.next();
|
|
|
|
}
|
|
|
|
|
2016-10-27 05:17:42 +03:00
|
|
|
hir::Path {
|
2016-11-25 13:21:19 +02:00
|
|
|
def: self.expect_full_def(id),
|
2016-12-05 03:51:11 +00:00
|
|
|
segments: segments.map(|segment| {
|
2017-07-29 01:13:40 +03:00
|
|
|
self.lower_path_segment(p.span, segment, param_mode, 0,
|
2017-11-10 12:23:59 -05:00
|
|
|
ParenthesizedGenericArgs::Err,
|
|
|
|
ImplTraitContext::Disallowed)
|
2017-09-21 23:24:26 +03:00
|
|
|
}).chain(name.map(|name| hir::PathSegment::from_name(name)))
|
|
|
|
.collect(),
|
2016-10-27 05:17:42 +03:00
|
|
|
span: p.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 06:11:31 +02:00
|
|
|
fn lower_path(&mut self,
|
2016-11-25 13:21:19 +02:00
|
|
|
id: NodeId,
|
2016-11-24 06:11:31 +02:00
|
|
|
p: &Path,
|
2016-12-05 03:51:11 +00:00
|
|
|
param_mode: ParamMode,
|
|
|
|
defaults_to_global: bool)
|
2016-11-24 06:11:31 +02:00
|
|
|
-> hir::Path {
|
2016-12-05 03:51:11 +00:00
|
|
|
self.lower_path_extra(id, p, None, param_mode, defaults_to_global)
|
2016-11-24 06:11:31 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 05:17:42 +03:00
|
|
|
fn lower_path_segment(&mut self,
|
2017-01-09 17:46:11 +02:00
|
|
|
path_span: Span,
|
2016-10-27 05:17:42 +03:00
|
|
|
segment: &PathSegment,
|
2017-01-09 17:46:11 +02:00
|
|
|
param_mode: ParamMode,
|
2017-07-29 01:13:40 +03:00
|
|
|
expected_lifetimes: usize,
|
2017-11-10 12:23:59 -05:00
|
|
|
parenthesized_generic_args: ParenthesizedGenericArgs,
|
|
|
|
itctx: ImplTraitContext)
|
2016-10-27 05:17:42 +03:00
|
|
|
-> hir::PathSegment {
|
2017-09-21 23:24:26 +03:00
|
|
|
let (mut parameters, infer_types) = if let Some(ref parameters) = segment.parameters {
|
2017-07-29 01:13:40 +03:00
|
|
|
let msg = "parenthesized parameters may only be used with a trait";
|
2016-12-10 06:45:58 +00:00
|
|
|
match **parameters {
|
|
|
|
PathParameters::AngleBracketed(ref data) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
self.lower_angle_bracketed_parameter_data(data, param_mode, itctx)
|
2016-12-10 06:45:58 +00:00
|
|
|
}
|
2017-07-29 01:13:40 +03:00
|
|
|
PathParameters::Parenthesized(ref data) => match parenthesized_generic_args {
|
2017-11-10 12:23:59 -05:00
|
|
|
ParenthesizedGenericArgs::Ok =>
|
2017-11-14 19:57:09 -05:00
|
|
|
self.lower_parenthesized_parameter_data(data),
|
2017-07-29 01:13:40 +03:00
|
|
|
ParenthesizedGenericArgs::Warn => {
|
|
|
|
self.sess.buffer_lint(PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
|
|
|
|
CRATE_NODE_ID, data.span, msg.into());
|
2017-09-21 23:24:26 +03:00
|
|
|
(hir::PathParameters::none(), true)
|
2017-07-29 01:13:40 +03:00
|
|
|
}
|
|
|
|
ParenthesizedGenericArgs::Err => {
|
|
|
|
struct_span_err!(self.sess, data.span, E0214, "{}", msg)
|
|
|
|
.span_label(data.span, "only traits may use parentheses").emit();
|
2017-09-21 23:24:26 +03:00
|
|
|
(hir::PathParameters::none(), true)
|
2017-07-29 01:13:40 +03:00
|
|
|
}
|
2016-12-10 06:45:58 +00:00
|
|
|
}
|
2016-10-17 06:02:23 +03:00
|
|
|
}
|
2016-12-10 06:45:58 +00:00
|
|
|
} else {
|
2017-11-10 12:23:59 -05:00
|
|
|
self.lower_angle_bracketed_parameter_data(&Default::default(), param_mode, itctx)
|
2016-10-27 05:17:42 +03:00
|
|
|
};
|
|
|
|
|
2017-07-29 01:13:40 +03:00
|
|
|
if !parameters.parenthesized && parameters.lifetimes.is_empty() {
|
|
|
|
parameters.lifetimes = (0..expected_lifetimes).map(|_| {
|
|
|
|
self.elided_lifetime(path_span)
|
|
|
|
}).collect();
|
2017-01-09 17:46:11 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 23:24:26 +03:00
|
|
|
hir::PathSegment::new(
|
|
|
|
self.lower_ident(segment.identifier),
|
2017-07-03 11:19:51 -07:00
|
|
|
parameters,
|
2017-09-21 23:24:26 +03:00
|
|
|
infer_types
|
|
|
|
)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_angle_bracketed_parameter_data(&mut self,
|
2016-10-17 06:02:23 +03:00
|
|
|
data: &AngleBracketedParameterData,
|
2017-11-10 12:23:59 -05:00
|
|
|
param_mode: ParamMode,
|
|
|
|
itctx: ImplTraitContext)
|
2017-09-21 23:24:26 +03:00
|
|
|
-> (hir::PathParameters, bool) {
|
2017-07-23 20:50:56 +03:00
|
|
|
let &AngleBracketedParameterData { ref lifetimes, ref types, ref bindings, .. } = data;
|
2017-09-21 23:24:26 +03:00
|
|
|
(hir::PathParameters {
|
2016-05-10 01:11:59 +00:00
|
|
|
lifetimes: self.lower_lifetimes(lifetimes),
|
2017-11-10 12:23:59 -05:00
|
|
|
types: types.iter().map(|ty| self.lower_ty(ty, itctx)).collect(),
|
|
|
|
bindings: bindings.iter().map(|b| self.lower_ty_binding(b, itctx)).collect(),
|
2017-07-29 01:13:40 +03:00
|
|
|
parenthesized: false,
|
2017-09-21 23:24:26 +03:00
|
|
|
}, types.is_empty() && param_mode == ParamMode::Optional)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_parenthesized_parameter_data(&mut self,
|
2017-11-14 19:57:09 -05:00
|
|
|
data: &ParenthesizedParameterData)
|
2017-09-21 23:24:26 +03:00
|
|
|
-> (hir::PathParameters, bool) {
|
2017-11-10 12:23:59 -05:00
|
|
|
const DISALLOWED: ImplTraitContext = ImplTraitContext::Disallowed;
|
2016-05-10 01:11:59 +00:00
|
|
|
let &ParenthesizedParameterData { ref inputs, ref output, span } = data;
|
2017-11-10 12:23:59 -05:00
|
|
|
let inputs = inputs.iter().map(|ty| self.lower_ty(ty, DISALLOWED)).collect();
|
2017-07-29 01:13:40 +03:00
|
|
|
let mk_tup = |this: &mut Self, tys, span| {
|
2017-09-13 18:29:59 +03:00
|
|
|
let LoweredNodeId { node_id, hir_id } = this.next_id();
|
|
|
|
P(hir::Ty { node: hir::TyTup(tys), id: node_id, hir_id, span })
|
2017-07-29 01:13:40 +03:00
|
|
|
};
|
|
|
|
|
2017-09-21 23:24:26 +03:00
|
|
|
(hir::PathParameters {
|
2017-07-29 01:13:40 +03:00
|
|
|
lifetimes: hir::HirVec::new(),
|
|
|
|
types: hir_vec![mk_tup(self, inputs, span)],
|
|
|
|
bindings: hir_vec![hir::TypeBinding {
|
|
|
|
id: self.next_id().node_id,
|
|
|
|
name: Symbol::intern(FN_OUTPUT_NAME),
|
2017-11-14 19:57:09 -05:00
|
|
|
ty: output.as_ref().map(|ty| self.lower_ty(&ty, DISALLOWED))
|
2017-07-29 01:13:40 +03:00
|
|
|
.unwrap_or_else(|| mk_tup(self, hir::HirVec::new(), span)),
|
|
|
|
span: output.as_ref().map_or(span, |ty| ty.span),
|
|
|
|
}],
|
|
|
|
parenthesized: true,
|
2017-09-21 23:24:26 +03:00
|
|
|
}, false)
|
2015-10-06 16:03:56 +13:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_local(&mut self, l: &Local) -> P<hir::Local> {
|
2017-08-07 14:43:43 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(l.id);
|
2016-05-10 01:11:59 +00:00
|
|
|
P(hir::Local {
|
2017-08-07 14:43:43 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-11-10 12:23:59 -05:00
|
|
|
ty: l.ty.as_ref().map(|t| self.lower_ty(t, ImplTraitContext::Disallowed)),
|
2016-05-10 01:11:59 +00:00
|
|
|
pat: self.lower_pat(&l.pat),
|
2016-10-28 21:16:44 +11:00
|
|
|
init: l.init.as_ref().map(|e| P(self.lower_expr(e))),
|
2016-05-10 01:11:59 +00:00
|
|
|
span: l.span,
|
|
|
|
attrs: l.attrs.clone(),
|
2017-05-27 20:20:17 +02:00
|
|
|
source: hir::LocalSource::Normal,
|
2016-05-10 01:11:59 +00:00
|
|
|
})
|
2015-10-06 16:03:56 +13:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_mutability(&mut self, m: Mutability) -> hir::Mutability {
|
|
|
|
match m {
|
|
|
|
Mutability::Mutable => hir::MutMutable,
|
|
|
|
Mutability::Immutable => hir::MutImmutable,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_arg(&mut self, arg: &Arg) -> hir::Arg {
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(arg.id);
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::Arg {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2016-05-10 01:11:59 +00:00
|
|
|
pat: self.lower_pat(&arg.pat),
|
|
|
|
}
|
2015-09-28 08:23:31 +13:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-12-20 22:46:11 +02:00
|
|
|
fn lower_fn_args_to_names(&mut self, decl: &FnDecl)
|
|
|
|
-> hir::HirVec<Spanned<Name>> {
|
|
|
|
decl.inputs.iter().map(|arg| {
|
|
|
|
match arg.pat.node {
|
|
|
|
PatKind::Ident(_, ident, None) => {
|
|
|
|
respan(ident.span, ident.node.name)
|
|
|
|
}
|
|
|
|
_ => respan(arg.pat.span, keywords::Invalid.name()),
|
|
|
|
}
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
|
2017-11-14 16:24:35 -05:00
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
fn lower_fn_decl(&mut self,
|
|
|
|
decl: &FnDecl,
|
|
|
|
fn_def_id: Option<DefId>,
|
|
|
|
impl_trait_return_allow: bool)
|
|
|
|
-> P<hir::FnDecl> {
|
2017-11-14 16:24:35 -05:00
|
|
|
// NOTE: The two last paramters here have to do with impl Trait. If fn_def_id is Some,
|
|
|
|
// then impl Trait arguments are lowered into generic paramters on the given
|
|
|
|
// fn_def_id, otherwise impl Trait is disallowed. (for now)
|
|
|
|
//
|
|
|
|
// Furthermore, if impl_trait_return_allow is true, then impl Trait may be used in
|
|
|
|
// return positions as well. This guards against trait declarations and their impls
|
|
|
|
// where impl Trait is disallowed. (again for now)
|
2016-05-10 01:11:59 +00:00
|
|
|
P(hir::FnDecl {
|
2017-11-10 12:23:59 -05:00
|
|
|
inputs: decl.inputs.iter()
|
|
|
|
.map(|arg| if let Some(def_id) = fn_def_id {
|
|
|
|
self.lower_ty(&arg.ty, ImplTraitContext::Universal(def_id))
|
|
|
|
} else {
|
|
|
|
self.lower_ty(&arg.ty, ImplTraitContext::Disallowed)
|
|
|
|
}).collect(),
|
2016-05-10 01:11:59 +00:00
|
|
|
output: match decl.output {
|
2017-11-14 16:24:35 -05:00
|
|
|
FunctionRetTy::Ty(ref ty) => match fn_def_id {
|
|
|
|
Some(_) if impl_trait_return_allow =>
|
|
|
|
hir::Return(self.lower_ty(ty, ImplTraitContext::Existential)),
|
2017-11-10 12:23:59 -05:00
|
|
|
_ => hir::Return(self.lower_ty(ty, ImplTraitContext::Disallowed)),
|
|
|
|
},
|
2016-05-10 01:11:59 +00:00
|
|
|
FunctionRetTy::Default(span) => hir::DefaultReturn(span),
|
|
|
|
},
|
|
|
|
variadic: decl.variadic,
|
2017-03-27 01:35:50 +03:00
|
|
|
has_implicit_self: decl.inputs.get(0).map_or(false, |arg| {
|
|
|
|
match arg.ty.node {
|
|
|
|
TyKind::ImplicitSelf => true,
|
|
|
|
TyKind::Rptr(_, ref mt) => mt.ty.node == TyKind::ImplicitSelf,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
})
|
2016-05-10 01:11:59 +00:00
|
|
|
})
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
fn lower_ty_param_bound(&mut self, tpb: &TyParamBound, itctx: ImplTraitContext)
|
|
|
|
-> hir::TyParamBound {
|
2016-05-10 01:11:59 +00:00
|
|
|
match *tpb {
|
|
|
|
TraitTyParamBound(ref ty, modifier) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::TraitTyParamBound(self.lower_poly_trait_ref(ty, itctx),
|
2016-05-10 01:11:59 +00:00
|
|
|
self.lower_trait_bound_modifier(modifier))
|
|
|
|
}
|
|
|
|
RegionTyParamBound(ref lifetime) => {
|
|
|
|
hir::RegionTyParamBound(self.lower_lifetime(lifetime))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-10-22 03:33:36 +03:00
|
|
|
fn lower_ty_param(&mut self, tp: &TyParam, add_bounds: &[TyParamBound]) -> hir::TyParam {
|
2017-03-24 23:03:15 +00:00
|
|
|
let mut name = self.lower_ident(tp.ident);
|
2016-09-22 20:03:28 +03:00
|
|
|
|
|
|
|
// Don't expose `Self` (recovered "keyword used as ident" parse error).
|
|
|
|
// `rustc::ty` expects `Self` to be only used for a trait's `Self`.
|
|
|
|
// Instead, use gensym("Self") to create a distinct name that looks the same.
|
2016-11-16 08:21:52 +00:00
|
|
|
if name == keywords::SelfType.name() {
|
|
|
|
name = Symbol::gensym("Self");
|
2016-09-22 20:03:28 +03:00
|
|
|
}
|
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
let itctx = ImplTraitContext::Universal(self.resolver.definitions().local_def_id(tp.id));
|
|
|
|
let mut bounds = self.lower_bounds(&tp.bounds, itctx);
|
2016-10-22 03:33:36 +03:00
|
|
|
if !add_bounds.is_empty() {
|
2017-11-10 12:23:59 -05:00
|
|
|
bounds = bounds.into_iter().chain(
|
|
|
|
self.lower_bounds(add_bounds, itctx).into_iter()
|
|
|
|
).collect();
|
2016-10-22 03:33:36 +03:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::TyParam {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: self.lower_node_id(tp.id).node_id,
|
2017-07-03 11:19:51 -07:00
|
|
|
name,
|
|
|
|
bounds,
|
2017-11-10 12:23:59 -05:00
|
|
|
default: tp.default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),
|
2016-05-10 01:11:59 +00:00
|
|
|
span: tp.span,
|
2016-10-11 16:06:43 +02:00
|
|
|
pure_wrt_drop: tp.attrs.iter().any(|attr| attr.check_name("may_dangle")),
|
2017-09-26 11:43:33 +02:00
|
|
|
synthetic: tp.attrs.iter()
|
|
|
|
.filter(|attr| attr.check_name("rustc_synthetic"))
|
|
|
|
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
|
|
|
|
.nth(0),
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-01-17 01:54:59 +03:00
|
|
|
fn lower_ty_params(&mut self, tps: &Vec<TyParam>, add_bounds: &NodeMap<Vec<TyParamBound>>)
|
2016-10-22 03:33:36 +03:00
|
|
|
-> hir::HirVec<hir::TyParam> {
|
|
|
|
tps.iter().map(|tp| {
|
|
|
|
self.lower_ty_param(tp, add_bounds.get(&tp.id).map_or(&[][..], |x| &x))
|
|
|
|
}).collect()
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
|
2017-11-16 22:59:45 -08:00
|
|
|
let name = match self.lower_ident(l.ident) {
|
|
|
|
x if x == "'_" => hir::LifetimeName::Underscore,
|
|
|
|
x if x == "'static" => hir::LifetimeName::Static,
|
|
|
|
name => {
|
|
|
|
if self.is_collecting_in_band_lifetimes &&
|
|
|
|
!self.in_scope_lifetimes.contains(&name) &&
|
|
|
|
self.lifetimes_to_define.iter()
|
|
|
|
.find(|&&(_, lt_name)| lt_name == name)
|
|
|
|
.is_none()
|
|
|
|
{
|
|
|
|
self.lifetimes_to_define.push((l.span, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
hir::LifetimeName::Name(name)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::Lifetime {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: self.lower_node_id(l.id).node_id,
|
2017-11-16 22:59:45 -08:00
|
|
|
name,
|
2016-05-10 01:11:59 +00:00
|
|
|
span: l.span,
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_lifetime_def(&mut self, l: &LifetimeDef) -> hir::LifetimeDef {
|
2017-11-16 22:59:45 -08:00
|
|
|
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
|
|
|
|
self.is_collecting_in_band_lifetimes = false;
|
|
|
|
|
|
|
|
let def = hir::LifetimeDef {
|
2016-05-10 01:11:59 +00:00
|
|
|
lifetime: self.lower_lifetime(&l.lifetime),
|
|
|
|
bounds: self.lower_lifetimes(&l.bounds),
|
2016-10-11 16:06:43 +02:00
|
|
|
pure_wrt_drop: l.attrs.iter().any(|attr| attr.check_name("may_dangle")),
|
2017-11-16 22:59:45 -08:00
|
|
|
in_band: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.is_collecting_in_band_lifetimes = was_collecting_in_band;
|
|
|
|
|
|
|
|
def
|
2015-10-25 18:33:51 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_lifetimes(&mut self, lts: &Vec<Lifetime>) -> hir::HirVec<hir::Lifetime> {
|
|
|
|
lts.iter().map(|l| self.lower_lifetime(l)).collect()
|
2015-10-06 16:03:56 +13:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_lifetime_defs(&mut self, lts: &Vec<LifetimeDef>) -> hir::HirVec<hir::LifetimeDef> {
|
|
|
|
lts.iter().map(|l| self.lower_lifetime_def(l)).collect()
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_generics(&mut self, g: &Generics) -> hir::Generics {
|
2016-10-22 03:33:36 +03:00
|
|
|
// Collect `?Trait` bounds in where clause and move them to parameter definitions.
|
|
|
|
let mut add_bounds = NodeMap();
|
|
|
|
for pred in &g.where_clause.predicates {
|
|
|
|
if let WherePredicate::BoundPredicate(ref bound_pred) = *pred {
|
|
|
|
'next_bound: for bound in &bound_pred.bounds {
|
|
|
|
if let TraitTyParamBound(_, TraitBoundModifier::Maybe) = *bound {
|
|
|
|
let report_error = |this: &mut Self| {
|
|
|
|
this.diagnostic().span_err(bound_pred.bounded_ty.span,
|
|
|
|
"`?Trait` bounds are only permitted at the \
|
|
|
|
point where a type parameter is declared");
|
|
|
|
};
|
|
|
|
// Check if the where clause type is a plain type parameter.
|
|
|
|
match bound_pred.bounded_ty.node {
|
|
|
|
TyKind::Path(None, ref path)
|
2016-12-05 03:51:11 +00:00
|
|
|
if path.segments.len() == 1 &&
|
|
|
|
bound_pred.bound_lifetimes.is_empty() => {
|
2016-10-22 03:33:36 +03:00
|
|
|
if let Some(Def::TyParam(def_id)) =
|
|
|
|
self.resolver.get_resolution(bound_pred.bounded_ty.id)
|
2017-02-18 22:11:42 +03:00
|
|
|
.map(|d| d.base_def()) {
|
2016-10-22 03:33:36 +03:00
|
|
|
if let Some(node_id) =
|
|
|
|
self.resolver.definitions().as_local_node_id(def_id) {
|
|
|
|
for ty_param in &g.ty_params {
|
|
|
|
if node_id == ty_param.id {
|
|
|
|
add_bounds.entry(ty_param.id).or_insert(Vec::new())
|
|
|
|
.push(bound.clone());
|
|
|
|
continue 'next_bound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
report_error(self)
|
|
|
|
}
|
|
|
|
_ => report_error(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::Generics {
|
2016-10-22 03:33:36 +03:00
|
|
|
ty_params: self.lower_ty_params(&g.ty_params, &add_bounds),
|
2016-05-10 01:11:59 +00:00
|
|
|
lifetimes: self.lower_lifetime_defs(&g.lifetimes),
|
|
|
|
where_clause: self.lower_where_clause(&g.where_clause),
|
2016-08-10 19:39:12 +02:00
|
|
|
span: g.span,
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-09-20 14:00:18 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_where_clause(&mut self, wc: &WhereClause) -> hir::WhereClause {
|
|
|
|
hir::WhereClause {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: self.lower_node_id(wc.id).node_id,
|
2016-05-10 01:11:59 +00:00
|
|
|
predicates: wc.predicates
|
|
|
|
.iter()
|
|
|
|
.map(|predicate| self.lower_where_predicate(predicate))
|
|
|
|
.collect(),
|
|
|
|
}
|
2015-10-06 16:03:56 +13:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate {
|
|
|
|
match *pred {
|
|
|
|
WherePredicate::BoundPredicate(WhereBoundPredicate{ ref bound_lifetimes,
|
|
|
|
ref bounded_ty,
|
|
|
|
ref bounds,
|
|
|
|
span}) => {
|
2017-11-16 22:59:45 -08:00
|
|
|
self.with_in_scope_lifetime_defs(bound_lifetimes, |this| {
|
|
|
|
hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
|
|
|
|
bound_lifetimes: this.lower_lifetime_defs(bound_lifetimes),
|
|
|
|
bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::Disallowed),
|
|
|
|
bounds: bounds.iter().filter_map(|bound| match *bound {
|
|
|
|
// Ignore `?Trait` bounds.
|
|
|
|
// Tthey were copied into type parameters already.
|
|
|
|
TraitTyParamBound(_, TraitBoundModifier::Maybe) => None,
|
|
|
|
_ => Some(this.lower_ty_param_bound(
|
|
|
|
bound, ImplTraitContext::Disallowed))
|
|
|
|
}).collect(),
|
|
|
|
span,
|
|
|
|
})
|
2016-05-10 01:11:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
WherePredicate::RegionPredicate(WhereRegionPredicate{ ref lifetime,
|
|
|
|
ref bounds,
|
|
|
|
span}) => {
|
|
|
|
hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2016-05-10 01:11:59 +00:00
|
|
|
lifetime: self.lower_lifetime(lifetime),
|
|
|
|
bounds: bounds.iter().map(|bound| self.lower_lifetime(bound)).collect(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
WherePredicate::EqPredicate(WhereEqPredicate{ id,
|
2017-01-16 21:32:13 +03:00
|
|
|
ref lhs_ty,
|
|
|
|
ref rhs_ty,
|
2016-05-10 01:11:59 +00:00
|
|
|
span}) => {
|
|
|
|
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: self.lower_node_id(id).node_id,
|
2017-11-10 12:23:59 -05:00
|
|
|
lhs_ty: self.lower_ty(lhs_ty, ImplTraitContext::Disallowed),
|
|
|
|
rhs_ty: self.lower_ty(rhs_ty, ImplTraitContext::Disallowed),
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2016-05-10 01:11:59 +00:00
|
|
|
})
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_variant_data(&mut self, vdata: &VariantData) -> hir::VariantData {
|
|
|
|
match *vdata {
|
|
|
|
VariantData::Struct(ref fields, id) => {
|
|
|
|
hir::VariantData::Struct(fields.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|f| self.lower_struct_field(f))
|
|
|
|
.collect(),
|
2017-08-04 09:49:40 +02:00
|
|
|
self.lower_node_id(id).node_id)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
VariantData::Tuple(ref fields, id) => {
|
|
|
|
hir::VariantData::Tuple(fields.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|f| self.lower_struct_field(f))
|
|
|
|
.collect(),
|
2017-08-04 09:49:40 +02:00
|
|
|
self.lower_node_id(id).node_id)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2017-08-04 09:49:40 +02:00
|
|
|
VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id).node_id),
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
fn lower_trait_ref(&mut self, p: &TraitRef, itctx: ImplTraitContext) -> hir::TraitRef {
|
|
|
|
let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) {
|
2017-01-09 17:46:11 +02:00
|
|
|
hir::QPath::Resolved(None, path) => path.and_then(|path| path),
|
|
|
|
qpath => bug!("lower_trait_ref: unexpected QPath `{:?}`", qpath)
|
|
|
|
};
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::TraitRef {
|
2017-07-03 11:19:51 -07:00
|
|
|
path,
|
2017-08-04 09:49:40 +02:00
|
|
|
ref_id: self.lower_node_id(p.ref_id).node_id,
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
fn lower_poly_trait_ref(&mut self,
|
|
|
|
p: &PolyTraitRef,
|
|
|
|
itctx: ImplTraitContext)
|
|
|
|
-> hir::PolyTraitRef {
|
2017-11-16 22:59:45 -08:00
|
|
|
let bound_lifetimes = self.lower_lifetime_defs(&p.bound_lifetimes);
|
|
|
|
let trait_ref = self.with_parent_impl_lifetime_defs(&bound_lifetimes,
|
|
|
|
|this| this.lower_trait_ref(&p.trait_ref, itctx));
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::PolyTraitRef {
|
2017-11-16 22:59:45 -08:00
|
|
|
bound_lifetimes,
|
|
|
|
trait_ref,
|
2016-05-10 01:11:59 +00:00
|
|
|
span: p.span,
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_struct_field(&mut self, (index, f): (usize, &StructField)) -> hir::StructField {
|
|
|
|
hir::StructField {
|
|
|
|
span: f.span,
|
2017-08-04 09:49:40 +02:00
|
|
|
id: self.lower_node_id(f.id).node_id,
|
2017-03-24 23:03:15 +00:00
|
|
|
name: self.lower_ident(match f.ident {
|
|
|
|
Some(ident) => ident,
|
|
|
|
// FIXME(jseyfried) positional field hygiene
|
2017-07-31 23:04:34 +03:00
|
|
|
None => Ident { name: Symbol::intern(&index.to_string()), ctxt: f.span.ctxt() },
|
2017-03-24 23:03:15 +00:00
|
|
|
}),
|
2017-03-14 15:50:40 +01:00
|
|
|
vis: self.lower_visibility(&f.vis, None),
|
2017-11-10 12:23:59 -05:00
|
|
|
ty: self.lower_ty(&f.ty, ImplTraitContext::Disallowed),
|
2016-05-10 01:11:59 +00:00
|
|
|
attrs: self.lower_attrs(&f.attrs),
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_field(&mut self, f: &Field) -> hir::Field {
|
|
|
|
hir::Field {
|
2017-03-24 23:03:15 +00:00
|
|
|
name: respan(f.ident.span, self.lower_ident(f.ident.node)),
|
2016-10-28 21:16:44 +11:00
|
|
|
expr: P(self.lower_expr(&f.expr)),
|
2016-05-10 01:11:59 +00:00
|
|
|
span: f.span,
|
2016-10-27 03:15:13 +03:00
|
|
|
is_shorthand: f.is_shorthand,
|
2016-04-18 10:30:55 +12:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext) -> hir::MutTy {
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::MutTy {
|
2017-11-10 12:23:59 -05:00
|
|
|
ty: self.lower_ty(&mt.ty, itctx),
|
2016-05-10 01:11:59 +00:00
|
|
|
mutbl: self.lower_mutability(mt.mutbl),
|
2016-04-18 10:30:55 +12:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
fn lower_bounds(&mut self, bounds: &[TyParamBound], itctx: ImplTraitContext)
|
|
|
|
-> hir::TyParamBounds {
|
|
|
|
bounds.iter().map(|bound| self.lower_ty_param_bound(bound, itctx)).collect()
|
2015-11-17 17:32:12 -05:00
|
|
|
}
|
|
|
|
|
2017-03-22 11:40:29 -04:00
|
|
|
fn lower_block(&mut self, b: &Block, targeted_by_break: bool) -> P<hir::Block> {
|
2016-06-23 09:51:18 +00:00
|
|
|
let mut expr = None;
|
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
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));
|
|
|
|
}
|
2016-06-23 09:51:18 +00:00
|
|
|
} else {
|
2017-03-14 15:50:40 +01:00
|
|
|
stmts.extend(self.lower_stmt(stmt));
|
2016-06-23 09:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(b.id);
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
P(hir::Block {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2016-06-23 09:51:18 +00:00
|
|
|
stmts: stmts.into(),
|
2017-07-03 11:19:51 -07:00
|
|
|
expr,
|
2016-05-10 01:11:59 +00:00
|
|
|
rules: self.lower_block_check_mode(&b.rules),
|
|
|
|
span: b.span,
|
2017-07-03 11:19:51 -07:00
|
|
|
targeted_by_break,
|
2016-05-10 01:11:59 +00:00
|
|
|
})
|
|
|
|
}
|
2015-11-17 17:32:12 -05:00
|
|
|
|
2016-11-24 06:11:31 +02:00
|
|
|
fn lower_item_kind(&mut self,
|
2016-11-25 13:21:19 +02:00
|
|
|
id: NodeId,
|
2016-11-24 06:11:31 +02:00
|
|
|
name: &mut Name,
|
|
|
|
attrs: &hir::HirVec<Attribute>,
|
|
|
|
vis: &mut hir::Visibility,
|
|
|
|
i: &ItemKind)
|
|
|
|
-> hir::Item_ {
|
2016-05-10 01:11:59 +00:00
|
|
|
match *i {
|
|
|
|
ItemKind::ExternCrate(string) => hir::ItemExternCrate(string),
|
2017-09-26 23:04:00 +02:00
|
|
|
ItemKind::Use(ref use_tree) => {
|
|
|
|
// Start with an empty prefix
|
|
|
|
let prefix = Path {
|
|
|
|
segments: vec![],
|
|
|
|
span: use_tree.span,
|
2016-11-24 06:11:31 +02:00
|
|
|
};
|
2017-09-26 23:04:00 +02:00
|
|
|
|
|
|
|
self.lower_use_tree(use_tree, &prefix, id, vis, name, attrs)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
ItemKind::Static(ref t, m, ref e) => {
|
2017-07-20 05:17:07 +02:00
|
|
|
let value = self.lower_body(None, |this| this.lower_expr(e));
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::ItemStatic(self.lower_ty(t, ImplTraitContext::Disallowed),
|
2016-05-10 01:11:59 +00:00
|
|
|
self.lower_mutability(m),
|
2017-07-20 05:17:07 +02:00
|
|
|
value)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
ItemKind::Const(ref t, ref e) => {
|
2017-07-20 05:17:07 +02:00
|
|
|
let value = self.lower_body(None, |this| this.lower_expr(e));
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::ItemConst(self.lower_ty(t, ImplTraitContext::Disallowed), value)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
let fn_def_id = self.resolver.definitions().opt_local_def_id(id);
|
2017-02-17 15:12:47 -08:00
|
|
|
self.with_new_scopes(|this| {
|
2016-12-26 14:34:03 +01:00
|
|
|
let body_id = this.lower_body(Some(decl), |this| {
|
|
|
|
let body = this.lower_block(body, false);
|
|
|
|
this.expr_block(body, ThinVec::new())
|
|
|
|
});
|
2017-11-16 22:59:45 -08:00
|
|
|
let (generics, fn_decl) =
|
|
|
|
this.add_in_band_lifetime_defs(generics, fn_def_id, |this|
|
|
|
|
this.lower_fn_decl(decl, fn_def_id, true));
|
|
|
|
|
|
|
|
hir::ItemFn(fn_decl,
|
2017-11-10 12:23:59 -05:00
|
|
|
this.lower_unsafety(unsafety),
|
|
|
|
this.lower_constness(constness),
|
|
|
|
abi,
|
2017-11-16 22:59:45 -08:00
|
|
|
generics,
|
2017-11-10 12:23:59 -05:00
|
|
|
body_id)
|
2017-02-15 14:52:27 -08:00
|
|
|
})
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
ItemKind::Mod(ref m) => hir::ItemMod(self.lower_mod(m)),
|
|
|
|
ItemKind::ForeignMod(ref nm) => hir::ItemForeignMod(self.lower_foreign_mod(nm)),
|
2017-03-15 21:27:40 -05:00
|
|
|
ItemKind::GlobalAsm(ref ga) => hir::ItemGlobalAsm(self.lower_global_asm(ga)),
|
2016-05-10 01:11:59 +00:00
|
|
|
ItemKind::Ty(ref t, ref generics) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::ItemTy(self.lower_ty(t, ImplTraitContext::Disallowed),
|
|
|
|
self.lower_generics(generics))
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
ItemKind::Enum(ref enum_definition, ref generics) => {
|
|
|
|
hir::ItemEnum(hir::EnumDef {
|
|
|
|
variants: enum_definition.variants
|
|
|
|
.iter()
|
|
|
|
.map(|x| self.lower_variant(x))
|
|
|
|
.collect(),
|
|
|
|
},
|
|
|
|
self.lower_generics(generics))
|
|
|
|
}
|
|
|
|
ItemKind::Struct(ref struct_def, ref generics) => {
|
|
|
|
let struct_def = self.lower_variant_data(struct_def);
|
|
|
|
hir::ItemStruct(struct_def, self.lower_generics(generics))
|
|
|
|
}
|
2016-08-09 01:18:47 +03:00
|
|
|
ItemKind::Union(ref vdata, ref generics) => {
|
|
|
|
let vdata = self.lower_variant_data(vdata);
|
|
|
|
hir::ItemUnion(vdata, self.lower_generics(generics))
|
|
|
|
}
|
2017-10-09 13:59:20 -03:00
|
|
|
ItemKind::AutoImpl(unsafety, ref trait_ref) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
let trait_ref = self.lower_trait_ref(trait_ref, ImplTraitContext::Disallowed);
|
2017-02-19 14:46:29 +02:00
|
|
|
|
|
|
|
if let Def::Trait(def_id) = trait_ref.path.def {
|
2017-10-09 13:59:20 -03:00
|
|
|
self.trait_auto_impl.insert(def_id, id);
|
2017-02-19 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
2017-10-09 13:59:20 -03:00
|
|
|
hir::ItemAutoImpl(self.lower_unsafety(unsafety),
|
2017-02-19 14:46:29 +02:00
|
|
|
trait_ref)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2016-11-18 17:14:42 +01:00
|
|
|
ItemKind::Impl(unsafety,
|
|
|
|
polarity,
|
|
|
|
defaultness,
|
2017-11-16 22:59:45 -08:00
|
|
|
ref ast_generics,
|
2016-11-18 17:14:42 +01:00
|
|
|
ref ifce,
|
|
|
|
ref ty,
|
|
|
|
ref impl_items) => {
|
2017-11-16 22:59:45 -08:00
|
|
|
let def_id = self.resolver.definitions().opt_local_def_id(id);
|
|
|
|
let (generics, (ifce, lowered_ty)) =
|
|
|
|
self.add_in_band_lifetime_defs(ast_generics, def_id, |this| {
|
|
|
|
let ifce = ifce.as_ref().map(|trait_ref| {
|
|
|
|
this.lower_trait_ref(trait_ref, ImplTraitContext::Disallowed)
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(ref trait_ref) = ifce {
|
|
|
|
if let Def::Trait(def_id) = trait_ref.path.def {
|
|
|
|
this.trait_impls.entry(def_id).or_insert(vec![]).push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let lowered_ty = this.lower_ty(ty, ImplTraitContext::Disallowed);
|
|
|
|
|
|
|
|
(ifce, lowered_ty)
|
|
|
|
});
|
|
|
|
|
|
|
|
let new_impl_items = self.with_in_scope_lifetime_defs(
|
|
|
|
&ast_generics.lifetimes, |this| {
|
|
|
|
impl_items.iter()
|
|
|
|
.map(|item| this.lower_impl_item_ref(item))
|
|
|
|
.collect()
|
2017-11-10 12:23:59 -05:00
|
|
|
});
|
2017-02-19 14:46:29 +02:00
|
|
|
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::ItemImpl(self.lower_unsafety(unsafety),
|
|
|
|
self.lower_impl_polarity(polarity),
|
2016-11-21 18:39:25 +00:00
|
|
|
self.lower_defaultness(defaultness, true /* [1] */),
|
2017-11-16 22:59:45 -08:00
|
|
|
generics,
|
2016-05-10 01:11:59 +00:00
|
|
|
ifce,
|
2017-11-16 22:59:45 -08:00
|
|
|
lowered_ty,
|
2016-05-10 01:11:59 +00:00
|
|
|
new_impl_items)
|
|
|
|
}
|
2017-10-12 09:51:31 -03:00
|
|
|
ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref items) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
let bounds = self.lower_bounds(bounds, ImplTraitContext::Disallowed);
|
2016-12-04 04:21:06 +02:00
|
|
|
let items = items.iter().map(|item| self.lower_trait_item_ref(item)).collect();
|
2017-10-12 09:51:31 -03:00
|
|
|
hir::ItemTrait(self.lower_is_auto(is_auto),
|
|
|
|
self.lower_unsafety(unsafety),
|
2016-05-10 01:11:59 +00:00
|
|
|
self.lower_generics(generics),
|
|
|
|
bounds,
|
|
|
|
items)
|
|
|
|
}
|
2017-11-21 08:03:02 -08:00
|
|
|
ItemKind::MacroDef(..) | ItemKind::Mac(..) => {
|
2017-11-21 06:49:15 -08:00
|
|
|
panic!("Shouldn't still be around")
|
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2017-04-24 09:56:54 +00:00
|
|
|
|
2017-04-26 03:17:48 +00:00
|
|
|
// [1] `defaultness.has_value()` is never called for an `impl`, always `true` in order to
|
2017-04-24 09:56:54 +00:00
|
|
|
// not cause an assertion failure inside the `lower_defaultness` function
|
2015-10-06 16:03:56 +13:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
fn lower_use_tree(&mut self,
|
|
|
|
tree: &UseTree,
|
|
|
|
prefix: &Path,
|
|
|
|
id: NodeId,
|
|
|
|
vis: &mut hir::Visibility,
|
|
|
|
name: &mut Name,
|
|
|
|
attrs: &hir::HirVec<Attribute>)
|
|
|
|
-> hir::Item_ {
|
|
|
|
let path = &tree.prefix;
|
|
|
|
|
|
|
|
match tree.kind {
|
|
|
|
UseTreeKind::Simple(ident) => {
|
|
|
|
*name = ident.name;
|
|
|
|
|
|
|
|
// First apply the prefix to the path
|
|
|
|
let mut path = Path {
|
|
|
|
segments: prefix.segments
|
|
|
|
.iter()
|
|
|
|
.chain(path.segments.iter())
|
|
|
|
.cloned()
|
|
|
|
.collect(),
|
|
|
|
span: path.span.to(prefix.span),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Correctly resolve `self` imports
|
|
|
|
if path.segments.last().unwrap().identifier.name == keywords::SelfValue.name() {
|
|
|
|
let _ = path.segments.pop();
|
|
|
|
if ident.name == keywords::SelfValue.name() {
|
|
|
|
*name = path.segments.last().unwrap().identifier.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = P(self.lower_path(id, &path, ParamMode::Explicit, true));
|
|
|
|
hir::ItemUse(path, hir::UseKind::Single)
|
|
|
|
}
|
|
|
|
UseTreeKind::Glob => {
|
|
|
|
let path = P(self.lower_path(id, &Path {
|
|
|
|
segments: prefix.segments
|
|
|
|
.iter()
|
|
|
|
.chain(path.segments.iter())
|
|
|
|
.cloned()
|
|
|
|
.collect(),
|
|
|
|
span: path.span,
|
|
|
|
}, ParamMode::Explicit, true));
|
|
|
|
hir::ItemUse(path, hir::UseKind::Glob)
|
|
|
|
}
|
|
|
|
UseTreeKind::Nested(ref trees) => {
|
|
|
|
let prefix = Path {
|
|
|
|
segments: prefix.segments
|
|
|
|
.iter()
|
|
|
|
.chain(path.segments.iter())
|
|
|
|
.cloned()
|
|
|
|
.collect(),
|
|
|
|
span: prefix.span.to(path.span),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add all the nested PathListItems in the HIR
|
|
|
|
for &(ref use_tree, id) in trees {
|
|
|
|
self.allocate_hir_id_counter(id, &use_tree);
|
|
|
|
let LoweredNodeId {
|
|
|
|
node_id: new_id,
|
|
|
|
hir_id: new_hir_id,
|
|
|
|
} = self.lower_node_id(id);
|
|
|
|
|
|
|
|
let mut vis = vis.clone();
|
|
|
|
let mut name = name.clone();
|
|
|
|
let item = self.lower_use_tree(
|
|
|
|
use_tree, &prefix, new_id, &mut vis, &mut name, &attrs,
|
|
|
|
);
|
|
|
|
|
|
|
|
self.with_hir_id_owner(new_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().node_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.items.insert(new_id, hir::Item {
|
|
|
|
id: new_id,
|
|
|
|
hir_id: new_hir_id,
|
|
|
|
name: name,
|
|
|
|
attrs: attrs.clone(),
|
|
|
|
node: item,
|
|
|
|
vis,
|
|
|
|
span: use_tree.span,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Privatize the degenerate import base, used only to check
|
|
|
|
// the stability of `use a::{};`, to avoid it showing up as
|
|
|
|
// a reexport by accident when `pub`, e.g. in documentation.
|
|
|
|
let path = P(self.lower_path(id, &prefix, ParamMode::Explicit, true));
|
|
|
|
*vis = hir::Inherited;
|
|
|
|
hir::ItemUse(path, hir::UseKind::ListStem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem {
|
|
|
|
self.with_parent_def(i.id, |this| {
|
2017-08-18 20:24:19 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
|
2017-11-10 12:23:59 -05:00
|
|
|
let fn_def_id = this.resolver.definitions().opt_local_def_id(node_id);
|
2017-08-18 20:24:19 +02:00
|
|
|
|
2017-11-16 22:59:45 -08:00
|
|
|
let (generics, node) = match i.node {
|
|
|
|
TraitItemKind::Const(ref ty, ref default) => {
|
|
|
|
(
|
|
|
|
this.lower_generics(&i.generics),
|
|
|
|
hir::TraitItemKind::Const(
|
|
|
|
this.lower_ty(ty, ImplTraitContext::Disallowed),
|
|
|
|
default.as_ref().map(|x| {
|
|
|
|
this.lower_body(None, |this| this.lower_expr(x))
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
TraitItemKind::Method(ref sig, None) => {
|
|
|
|
let names = this.lower_fn_args_to_names(&sig.decl);
|
|
|
|
this.add_in_band_lifetime_defs(&i.generics, fn_def_id, |this|
|
|
|
|
hir::TraitItemKind::Method(
|
|
|
|
this.lower_method_sig(sig, fn_def_id, false),
|
|
|
|
hir::TraitMethod::Required(names)))
|
|
|
|
}
|
|
|
|
TraitItemKind::Method(ref sig, Some(ref body)) => {
|
|
|
|
let body_id = this.lower_body(Some(&sig.decl), |this| {
|
|
|
|
let body = this.lower_block(body, false);
|
|
|
|
this.expr_block(body, ThinVec::new())
|
|
|
|
});
|
|
|
|
|
|
|
|
this.add_in_band_lifetime_defs(&i.generics, fn_def_id, |this|
|
|
|
|
hir::TraitItemKind::Method(
|
|
|
|
this.lower_method_sig(sig, fn_def_id, false),
|
|
|
|
hir::TraitMethod::Provided(body_id)))
|
|
|
|
}
|
|
|
|
TraitItemKind::Type(ref bounds, ref default) => {
|
|
|
|
(
|
|
|
|
this.lower_generics(&i.generics),
|
|
|
|
hir::TraitItemKind::Type(
|
|
|
|
this.lower_bounds(bounds, ImplTraitContext::Disallowed),
|
|
|
|
default.as_ref().map(|x| {
|
|
|
|
this.lower_ty(x, ImplTraitContext::Disallowed)
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
TraitItemKind::Macro(..) => panic!("Shouldn't exist any more"),
|
|
|
|
};
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::TraitItem {
|
2017-08-18 20:24:19 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-03-24 23:03:15 +00:00
|
|
|
name: this.lower_ident(i.ident),
|
2016-05-10 01:11:59 +00:00
|
|
|
attrs: this.lower_attrs(&i.attrs),
|
2017-11-16 22:59:45 -08:00
|
|
|
generics,
|
|
|
|
node,
|
2016-05-10 01:11:59 +00:00
|
|
|
span: i.span,
|
|
|
|
}
|
|
|
|
})
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-12-04 04:21:06 +02:00
|
|
|
fn lower_trait_item_ref(&mut self, i: &TraitItem) -> hir::TraitItemRef {
|
|
|
|
let (kind, has_default) = match i.node {
|
|
|
|
TraitItemKind::Const(_, ref default) => {
|
|
|
|
(hir::AssociatedItemKind::Const, default.is_some())
|
|
|
|
}
|
|
|
|
TraitItemKind::Type(_, ref default) => {
|
|
|
|
(hir::AssociatedItemKind::Type, default.is_some())
|
|
|
|
}
|
|
|
|
TraitItemKind::Method(ref sig, ref default) => {
|
|
|
|
(hir::AssociatedItemKind::Method {
|
|
|
|
has_self: sig.decl.has_self(),
|
|
|
|
}, default.is_some())
|
|
|
|
}
|
|
|
|
TraitItemKind::Macro(..) => unimplemented!(),
|
|
|
|
};
|
|
|
|
hir::TraitItemRef {
|
|
|
|
id: hir::TraitItemId { node_id: i.id },
|
2017-03-24 23:03:15 +00:00
|
|
|
name: self.lower_ident(i.ident),
|
2016-12-04 04:21:06 +02:00
|
|
|
span: i.span,
|
|
|
|
defaultness: self.lower_defaultness(Defaultness::Default, has_default),
|
2017-07-03 11:19:51 -07:00
|
|
|
kind,
|
2016-12-04 04:21:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_impl_item(&mut self, i: &ImplItem) -> hir::ImplItem {
|
|
|
|
self.with_parent_def(i.id, |this| {
|
2017-08-18 20:24:19 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
|
2017-11-10 12:23:59 -05:00
|
|
|
let fn_def_id = this.resolver.definitions().opt_local_def_id(node_id);
|
2017-08-18 20:24:19 +02:00
|
|
|
|
2017-11-16 22:59:45 -08:00
|
|
|
let (generics, node) = match i.node {
|
|
|
|
ImplItemKind::Const(ref ty, ref expr) => {
|
|
|
|
let body_id = this.lower_body(None, |this| this.lower_expr(expr));
|
|
|
|
(
|
|
|
|
this.lower_generics(&i.generics),
|
|
|
|
hir::ImplItemKind::Const(
|
|
|
|
this.lower_ty(ty, ImplTraitContext::Disallowed),
|
|
|
|
body_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ImplItemKind::Method(ref sig, ref body) => {
|
|
|
|
let body_id = this.lower_body(Some(&sig.decl), |this| {
|
|
|
|
let body = this.lower_block(body, false);
|
|
|
|
this.expr_block(body, ThinVec::new())
|
|
|
|
});
|
|
|
|
let impl_trait_return_allow = !this.is_in_trait_impl;
|
|
|
|
|
|
|
|
this.add_in_band_lifetime_defs(&i.generics, fn_def_id, |this|
|
|
|
|
hir::ImplItemKind::Method(
|
|
|
|
this.lower_method_sig(sig, fn_def_id, impl_trait_return_allow),
|
|
|
|
body_id))
|
|
|
|
}
|
|
|
|
ImplItemKind::Type(ref ty) => (
|
|
|
|
this.lower_generics(&i.generics),
|
|
|
|
hir::ImplItemKind::Type(
|
|
|
|
this.lower_ty(ty, ImplTraitContext::Disallowed)),
|
|
|
|
),
|
|
|
|
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
|
|
|
|
};
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::ImplItem {
|
2017-08-18 20:24:19 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-03-24 23:03:15 +00:00
|
|
|
name: this.lower_ident(i.ident),
|
2016-05-10 01:11:59 +00:00
|
|
|
attrs: this.lower_attrs(&i.attrs),
|
2017-11-16 22:59:45 -08:00
|
|
|
generics,
|
2017-03-14 15:50:40 +01:00
|
|
|
vis: this.lower_visibility(&i.vis, None),
|
2016-11-14 11:00:02 -05:00
|
|
|
defaultness: this.lower_defaultness(i.defaultness, true /* [1] */),
|
2017-11-16 22:59:45 -08:00
|
|
|
node,
|
2016-05-10 01:11:59 +00:00
|
|
|
span: i.span,
|
|
|
|
}
|
|
|
|
})
|
2016-11-14 11:00:02 -05:00
|
|
|
|
|
|
|
// [1] since `default impl` is not yet implemented, this is always true in impls
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-11-10 09:47:00 -05:00
|
|
|
fn lower_impl_item_ref(&mut self, i: &ImplItem) -> hir::ImplItemRef {
|
|
|
|
hir::ImplItemRef {
|
|
|
|
id: hir::ImplItemId { node_id: i.id },
|
2017-03-24 23:03:15 +00:00
|
|
|
name: self.lower_ident(i.ident),
|
2016-11-10 09:47:00 -05:00
|
|
|
span: i.span,
|
2017-03-14 15:50:40 +01:00
|
|
|
vis: self.lower_visibility(&i.vis, Some(i.id)),
|
2016-11-14 11:00:02 -05:00
|
|
|
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
|
2016-11-10 09:47:00 -05:00
|
|
|
kind: match i.node {
|
|
|
|
ImplItemKind::Const(..) => hir::AssociatedItemKind::Const,
|
|
|
|
ImplItemKind::Type(..) => hir::AssociatedItemKind::Type,
|
2017-11-10 12:23:59 -05:00
|
|
|
ImplItemKind::Method(ref sig, _) => {
|
|
|
|
hir::AssociatedItemKind::Method {
|
|
|
|
has_self: sig.decl.has_self(),
|
|
|
|
}
|
2016-11-10 09:47:00 -05:00
|
|
|
},
|
|
|
|
ImplItemKind::Macro(..) => unimplemented!(),
|
|
|
|
},
|
|
|
|
}
|
2016-11-14 11:00:02 -05:00
|
|
|
|
|
|
|
// [1] since `default impl` is not yet implemented, this is always true in impls
|
2016-11-02 18:25:31 -04:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_mod(&mut self, m: &Mod) -> hir::Mod {
|
|
|
|
hir::Mod {
|
|
|
|
inner: m.inner,
|
2016-11-24 06:11:31 +02:00
|
|
|
item_ids: m.items.iter().flat_map(|x| self.lower_item_id(x)).collect(),
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-11-24 06:11:31 +02:00
|
|
|
fn lower_item_id(&mut self, i: &Item) -> SmallVector<hir::ItemId> {
|
2017-03-05 05:15:58 +00:00
|
|
|
match i.node {
|
2017-09-26 23:04:00 +02:00
|
|
|
ItemKind::Use(ref use_tree) => {
|
|
|
|
let mut vec = SmallVector::one(hir::ItemId { id: i.id });
|
|
|
|
self.lower_item_id_use_tree(use_tree, &mut vec);
|
|
|
|
return vec;
|
2016-11-24 06:11:31 +02:00
|
|
|
}
|
2017-03-05 05:15:58 +00:00
|
|
|
ItemKind::MacroDef(..) => return SmallVector::new(),
|
|
|
|
_ => {}
|
2016-11-24 06:11:31 +02:00
|
|
|
}
|
|
|
|
SmallVector::one(hir::ItemId { id: i.id })
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
fn lower_item_id_use_tree(&self, tree: &UseTree, vec: &mut SmallVector<hir::ItemId>) {
|
|
|
|
match tree.kind {
|
|
|
|
UseTreeKind::Nested(ref nested_vec) => {
|
|
|
|
for &(ref nested, id) in nested_vec {
|
|
|
|
vec.push(hir::ItemId { id, });
|
|
|
|
self.lower_item_id_use_tree(nested, vec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UseTreeKind::Glob => {}
|
|
|
|
UseTreeKind::Simple(..) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 05:15:58 +00:00
|
|
|
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
|
2016-11-24 06:11:31 +02:00
|
|
|
let mut name = i.ident.name;
|
2017-03-25 01:46:38 +00:00
|
|
|
let mut vis = self.lower_visibility(&i.vis, None);
|
2016-11-24 06:11:31 +02:00
|
|
|
let attrs = self.lower_attrs(&i.attrs);
|
2017-03-18 01:55:51 +00:00
|
|
|
if let ItemKind::MacroDef(ref def) = i.node {
|
|
|
|
if !def.legacy || i.attrs.iter().any(|attr| attr.path == "macro_export") {
|
2017-09-15 08:28:34 -07:00
|
|
|
let body = self.lower_token_stream(def.stream());
|
2017-03-05 05:15:58 +00:00
|
|
|
self.exported_macros.push(hir::MacroDef {
|
2017-07-03 11:19:51 -07:00
|
|
|
name,
|
|
|
|
vis,
|
|
|
|
attrs,
|
2017-03-25 01:46:38 +00:00
|
|
|
id: i.id,
|
|
|
|
span: i.span,
|
2017-09-15 08:28:34 -07:00
|
|
|
body,
|
2017-03-25 01:46:38 +00:00
|
|
|
legacy: def.legacy,
|
2017-03-05 05:15:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
let node = self.with_parent_def(i.id, |this| {
|
2016-11-25 13:21:19 +02:00
|
|
|
this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node)
|
2016-05-10 01:11:59 +00:00
|
|
|
});
|
|
|
|
|
2017-08-18 20:24:19 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(i.id);
|
|
|
|
|
2017-03-05 05:15:58 +00:00
|
|
|
Some(hir::Item {
|
2017-08-18 20:24:19 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-07-03 11:19:51 -07:00
|
|
|
name,
|
|
|
|
attrs,
|
|
|
|
node,
|
|
|
|
vis,
|
2016-04-18 10:30:55 +12:00
|
|
|
span: i.span,
|
2017-03-05 05:15:58 +00:00
|
|
|
})
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem {
|
|
|
|
self.with_parent_def(i.id, |this| {
|
2017-11-16 22:59:45 -08:00
|
|
|
let node_id = this.lower_node_id(i.id).node_id;
|
|
|
|
let def_id = this.resolver.definitions().local_def_id(node_id);
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::ForeignItem {
|
2017-11-16 22:59:45 -08:00
|
|
|
id: node_id,
|
2016-05-10 01:11:59 +00:00
|
|
|
name: i.ident.name,
|
|
|
|
attrs: this.lower_attrs(&i.attrs),
|
|
|
|
node: match i.node {
|
|
|
|
ForeignItemKind::Fn(ref fdec, ref generics) => {
|
2017-11-14 16:24:35 -05:00
|
|
|
// Disallow impl Trait in foreign items
|
2017-11-16 22:59:45 -08:00
|
|
|
let (generics, (fn_dec, fn_args)) =
|
|
|
|
this.add_in_band_lifetime_defs(
|
|
|
|
generics,
|
|
|
|
Some(def_id),
|
|
|
|
|this| (
|
|
|
|
this.lower_fn_decl(fdec, None, false),
|
|
|
|
this.lower_fn_args_to_names(fdec)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
hir::ForeignItemFn(fn_dec, fn_args, generics)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
ForeignItemKind::Static(ref t, m) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::ForeignItemStatic(this.lower_ty(t, ImplTraitContext::Disallowed), m)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2017-09-03 19:53:58 +01:00
|
|
|
ForeignItemKind::Ty => {
|
|
|
|
hir::ForeignItemType
|
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
},
|
2017-03-14 15:50:40 +01:00
|
|
|
vis: this.lower_visibility(&i.vis, None),
|
2016-05-10 01:11:59 +00:00
|
|
|
span: i.span,
|
|
|
|
}
|
|
|
|
})
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-11-10 12:23:59 -05:00
|
|
|
fn lower_method_sig(&mut self,
|
|
|
|
sig: &MethodSig,
|
|
|
|
fn_def_id: Option<DefId>,
|
|
|
|
impl_trait_return_allow: bool)
|
|
|
|
-> hir::MethodSig {
|
2016-12-04 03:18:11 +02:00
|
|
|
hir::MethodSig {
|
2016-05-10 01:11:59 +00:00
|
|
|
abi: sig.abi,
|
|
|
|
unsafety: self.lower_unsafety(sig.unsafety),
|
|
|
|
constness: self.lower_constness(sig.constness),
|
2017-11-10 12:23:59 -05:00
|
|
|
decl: self.lower_fn_decl(&sig.decl, fn_def_id, impl_trait_return_allow),
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-10-12 11:18:55 -03:00
|
|
|
fn lower_is_auto(&mut self, a: IsAuto) -> hir::IsAuto {
|
|
|
|
match a {
|
2017-10-12 09:51:31 -03:00
|
|
|
IsAuto::Yes => hir::IsAuto::Yes,
|
|
|
|
IsAuto::No => hir::IsAuto::No,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_unsafety(&mut self, u: Unsafety) -> hir::Unsafety {
|
|
|
|
match u {
|
|
|
|
Unsafety::Unsafe => hir::Unsafety::Unsafe,
|
|
|
|
Unsafety::Normal => hir::Unsafety::Normal,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-08-10 16:20:12 -07:00
|
|
|
fn lower_constness(&mut self, c: Spanned<Constness>) -> hir::Constness {
|
|
|
|
match c.node {
|
2016-05-10 01:11:59 +00:00
|
|
|
Constness::Const => hir::Constness::Const,
|
|
|
|
Constness::NotConst => hir::Constness::NotConst,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_unop(&mut self, u: UnOp) -> hir::UnOp {
|
|
|
|
match u {
|
|
|
|
UnOp::Deref => hir::UnDeref,
|
|
|
|
UnOp::Not => hir::UnNot,
|
|
|
|
UnOp::Neg => hir::UnNeg,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_binop(&mut self, b: BinOp) -> hir::BinOp {
|
|
|
|
Spanned {
|
|
|
|
node: match b.node {
|
|
|
|
BinOpKind::Add => hir::BiAdd,
|
|
|
|
BinOpKind::Sub => hir::BiSub,
|
|
|
|
BinOpKind::Mul => hir::BiMul,
|
|
|
|
BinOpKind::Div => hir::BiDiv,
|
|
|
|
BinOpKind::Rem => hir::BiRem,
|
|
|
|
BinOpKind::And => hir::BiAnd,
|
|
|
|
BinOpKind::Or => hir::BiOr,
|
|
|
|
BinOpKind::BitXor => hir::BiBitXor,
|
|
|
|
BinOpKind::BitAnd => hir::BiBitAnd,
|
|
|
|
BinOpKind::BitOr => hir::BiBitOr,
|
|
|
|
BinOpKind::Shl => hir::BiShl,
|
|
|
|
BinOpKind::Shr => hir::BiShr,
|
|
|
|
BinOpKind::Eq => hir::BiEq,
|
|
|
|
BinOpKind::Lt => hir::BiLt,
|
|
|
|
BinOpKind::Le => hir::BiLe,
|
|
|
|
BinOpKind::Ne => hir::BiNe,
|
|
|
|
BinOpKind::Ge => hir::BiGe,
|
|
|
|
BinOpKind::Gt => hir::BiGt,
|
|
|
|
},
|
|
|
|
span: b.span,
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_pat(&mut self, p: &Pat) -> P<hir::Pat> {
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(p.id);
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
P(hir::Pat {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2016-05-10 01:11:59 +00:00
|
|
|
node: match p.node {
|
|
|
|
PatKind::Wild => hir::PatKind::Wild,
|
|
|
|
PatKind::Ident(ref binding_mode, pth1, ref sub) => {
|
2017-04-29 14:39:47 +03:00
|
|
|
match self.resolver.get_resolution(p.id).map(|d| d.base_def()) {
|
|
|
|
// `None` can occur in body-less function signatures
|
|
|
|
def @ None | def @ Some(Def::Local(_)) => {
|
|
|
|
let canonical_id = match def {
|
|
|
|
Some(Def::Local(id)) => id,
|
|
|
|
_ => p.id
|
|
|
|
};
|
|
|
|
hir::PatKind::Binding(self.lower_binding_mode(binding_mode),
|
|
|
|
canonical_id,
|
|
|
|
respan(pth1.span, pth1.node.name),
|
|
|
|
sub.as_ref().map(|x| self.lower_pat(x)))
|
2016-03-06 15:54:44 +03:00
|
|
|
}
|
2017-04-29 14:39:47 +03:00
|
|
|
Some(def) => {
|
|
|
|
hir::PatKind::Path(hir::QPath::Resolved(None, P(hir::Path {
|
|
|
|
span: pth1.span,
|
|
|
|
def,
|
|
|
|
segments: hir_vec![
|
|
|
|
hir::PathSegment::from_name(pth1.node.name)
|
|
|
|
],
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2016-10-28 21:16:44 +11:00
|
|
|
PatKind::Lit(ref e) => hir::PatKind::Lit(P(self.lower_expr(e))),
|
2016-09-15 00:51:46 +03:00
|
|
|
PatKind::TupleStruct(ref path, ref pats, ddpos) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
let qpath = self.lower_qpath(p.id, &None, path, ParamMode::Optional,
|
|
|
|
ImplTraitContext::Disallowed);
|
2016-10-27 05:17:42 +03:00
|
|
|
hir::PatKind::TupleStruct(qpath,
|
2016-10-17 06:02:23 +03:00
|
|
|
pats.iter().map(|x| self.lower_pat(x)).collect(),
|
|
|
|
ddpos)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2016-10-17 06:02:23 +03:00
|
|
|
PatKind::Path(ref qself, ref path) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::PatKind::Path(self.lower_qpath(p.id, qself, path, ParamMode::Optional,
|
|
|
|
ImplTraitContext::Disallowed))
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2016-10-27 05:17:42 +03:00
|
|
|
PatKind::Struct(ref path, ref fields, etc) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
let qpath = self.lower_qpath(p.id, &None, path, ParamMode::Optional,
|
|
|
|
ImplTraitContext::Disallowed);
|
2016-10-27 05:17:42 +03:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
let fs = fields.iter()
|
|
|
|
.map(|f| {
|
|
|
|
Spanned {
|
|
|
|
span: f.span,
|
|
|
|
node: hir::FieldPat {
|
2017-03-24 23:03:15 +00:00
|
|
|
name: self.lower_ident(f.node.ident),
|
2016-05-10 01:11:59 +00:00
|
|
|
pat: self.lower_pat(&f.node.pat),
|
|
|
|
is_shorthand: f.node.is_shorthand,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2016-10-27 05:17:42 +03:00
|
|
|
hir::PatKind::Struct(qpath, fs, etc)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2016-03-06 15:54:44 +03:00
|
|
|
PatKind::Tuple(ref elts, ddpos) => {
|
|
|
|
hir::PatKind::Tuple(elts.iter().map(|x| self.lower_pat(x)).collect(), ddpos)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
PatKind::Box(ref inner) => hir::PatKind::Box(self.lower_pat(inner)),
|
|
|
|
PatKind::Ref(ref inner, mutbl) => {
|
|
|
|
hir::PatKind::Ref(self.lower_pat(inner), self.lower_mutability(mutbl))
|
|
|
|
}
|
2017-01-10 22:13:53 +01:00
|
|
|
PatKind::Range(ref e1, ref e2, ref end) => {
|
|
|
|
hir::PatKind::Range(P(self.lower_expr(e1)),
|
|
|
|
P(self.lower_expr(e2)),
|
|
|
|
self.lower_range_end(end))
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2016-09-20 16:54:24 +02:00
|
|
|
PatKind::Slice(ref before, ref slice, ref after) => {
|
2016-09-20 02:14:46 +02:00
|
|
|
hir::PatKind::Slice(before.iter().map(|x| self.lower_pat(x)).collect(),
|
2016-05-10 01:11:59 +00:00
|
|
|
slice.as_ref().map(|x| self.lower_pat(x)),
|
|
|
|
after.iter().map(|x| self.lower_pat(x)).collect())
|
|
|
|
}
|
|
|
|
PatKind::Mac(_) => panic!("Shouldn't exist here"),
|
|
|
|
},
|
|
|
|
span: p.span,
|
|
|
|
})
|
|
|
|
}
|
2015-10-06 16:03:56 +13:00
|
|
|
|
2017-01-10 22:13:53 +01:00
|
|
|
fn lower_range_end(&mut self, e: &RangeEnd) -> hir::RangeEnd {
|
|
|
|
match *e {
|
2017-09-21 12:13:26 +02:00
|
|
|
RangeEnd::Included(_) => hir::RangeEnd::Included,
|
2017-01-10 22:13:53 +01:00
|
|
|
RangeEnd::Excluded => hir::RangeEnd::Excluded,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 21:16:44 +11:00
|
|
|
fn lower_expr(&mut self, e: &Expr) -> hir::Expr {
|
2017-03-14 15:50:40 +01:00
|
|
|
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) => {
|
2015-09-29 13:46:01 +13:00
|
|
|
// to:
|
|
|
|
//
|
2017-03-14 15:50:40 +01:00
|
|
|
// let p = PLACE;
|
|
|
|
// let mut place = Placer::make_place(p);
|
2015-09-29 13:46:01 +13:00
|
|
|
// let raw_place = Place::pointer(&mut place);
|
2017-03-14 15:50:40 +01:00
|
|
|
// 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"];
|
|
|
|
|
2017-08-16 18:46:48 -05:00
|
|
|
let unstable_span =
|
|
|
|
self.allow_internal_unstable(CompilerDesugaringKind::BackArrow, e.span);
|
2017-03-14 15:50:40 +01:00
|
|
|
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))
|
|
|
|
};
|
2015-09-28 17:24:42 +13:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
let mk_stmt_let = |this: &mut LoweringContext, bind, expr| {
|
|
|
|
this.stmt_let(e.span, false, bind, expr)
|
|
|
|
};
|
2016-01-13 01:24:34 -05:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
let mk_stmt_let_mut = |this: &mut LoweringContext, bind, expr| {
|
|
|
|
this.stmt_let(e.span, true, bind, expr)
|
|
|
|
};
|
2016-01-13 01:24:34 -05:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// let placer = <placer_expr> ;
|
|
|
|
let (s1, placer_binding) = {
|
|
|
|
mk_stmt_let(self, placer_ident, placer_expr)
|
|
|
|
};
|
2016-01-13 01:24:34 -05:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// 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)
|
|
|
|
};
|
2016-01-13 01:24:34 -05:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// 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)
|
|
|
|
};
|
2016-01-13 01:24:34 -05:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// pop_unsafe!(EXPR));
|
|
|
|
let pop_unsafe_expr = {
|
|
|
|
self.signal_block_expr(hir_vec![],
|
|
|
|
value_expr,
|
|
|
|
e.span,
|
|
|
|
hir::PopUnsafeBlock(hir::CompilerGenerated),
|
|
|
|
ThinVec::new())
|
|
|
|
};
|
2016-01-13 01:24:34 -05:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// 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]),
|
2017-08-04 09:49:40 +02:00
|
|
|
self.next_id().node_id);
|
2017-03-14 15:50:40 +01:00
|
|
|
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()))
|
|
|
|
};
|
2016-01-13 01:24:34 -05:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
let block = self.block_all(e.span, hir_vec![s1, s2, s3], Some(expr));
|
|
|
|
hir::ExprBlock(P(block))
|
|
|
|
}
|
2016-01-13 01:24:34 -05:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
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));
|
2017-07-20 05:17:07 +02:00
|
|
|
let count = self.lower_body(None, |this| this.lower_expr(count));
|
|
|
|
hir::ExprRepeat(expr, count)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
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())
|
|
|
|
}
|
2017-07-07 02:39:55 +03:00
|
|
|
ExprKind::MethodCall(ref seg, ref args) => {
|
2017-07-29 01:13:40 +03:00
|
|
|
let hir_seg = self.lower_path_segment(e.span, seg, ParamMode::Optional, 0,
|
2017-11-10 12:23:59 -05:00
|
|
|
ParenthesizedGenericArgs::Err,
|
|
|
|
ImplTraitContext::Disallowed);
|
2017-03-14 15:50:40 +01:00
|
|
|
let args = args.iter().map(|x| self.lower_expr(x)).collect();
|
2017-07-07 15:57:51 +03:00
|
|
|
hir::ExprMethodCall(hir_seg, seg.span, args)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
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));
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::ExprCast(expr, self.lower_ty(ty, ImplTraitContext::Disallowed))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
ExprKind::Type(ref expr, ref ty) => {
|
|
|
|
let expr = P(self.lower_expr(expr));
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::ExprType(expr, self.lower_ty(ty, ImplTraitContext::Disallowed))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
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));
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId {
|
|
|
|
node_id,
|
|
|
|
hir_id,
|
|
|
|
} = self.next_id();
|
2017-03-14 15:50:40 +01:00
|
|
|
let blk = P(hir::Block {
|
|
|
|
stmts: hir_vec![],
|
|
|
|
expr: Some(els),
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-03-14 15:50:40 +01:00
|
|
|
rules: hir::DefaultBlock,
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2017-03-22 11:40:29 -04:00
|
|
|
targeted_by_break: false,
|
2017-03-14 15:50:40 +01:00
|
|
|
});
|
|
|
|
P(self.expr_block(blk, ThinVec::new()))
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
_ => P(self.lower_expr(els)),
|
|
|
|
}
|
|
|
|
});
|
2016-01-13 01:24:34 -05:00
|
|
|
|
2017-03-22 11:40:29 -04:00
|
|
|
let then_blk = self.lower_block(blk, false);
|
2017-03-09 09:53:00 -05:00
|
|
|
let then_expr = self.expr_block(then_blk, ThinVec::new());
|
|
|
|
|
|
|
|
hir::ExprIf(P(self.lower_expr(cond)), P(then_expr), else_opt)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
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))),
|
2017-03-22 11:40:29 -04:00
|
|
|
this.lower_block(body, false),
|
2017-03-14 15:50:40 +01:00
|
|
|
this.lower_opt_sp_ident(opt_ident)))
|
|
|
|
}
|
|
|
|
ExprKind::Loop(ref body, opt_ident) => {
|
|
|
|
self.with_loop_scope(e.id, |this|
|
2017-03-22 11:40:29 -04:00
|
|
|
hir::ExprLoop(this.lower_block(body, false),
|
2017-03-14 15:50:40 +01:00
|
|
|
this.lower_opt_sp_ident(opt_ident),
|
|
|
|
hir::LoopSource::Loop))
|
|
|
|
}
|
|
|
|
ExprKind::Catch(ref body) => {
|
2017-03-22 11:40:29 -04:00
|
|
|
self.with_catch_scope(body.id, |this|
|
|
|
|
hir::ExprBlock(this.lower_block(body, true)))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
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| {
|
2017-07-28 13:09:33 -07:00
|
|
|
let mut is_generator = false;
|
2016-12-26 14:34:03 +01:00
|
|
|
let body_id = this.lower_body(Some(decl), |this| {
|
|
|
|
let e = this.lower_expr(body);
|
2017-07-28 13:09:33 -07:00
|
|
|
is_generator = this.is_generator;
|
2016-12-26 14:34:03 +01:00
|
|
|
e
|
|
|
|
});
|
2017-07-28 13:09:33 -07:00
|
|
|
if is_generator && !decl.inputs.is_empty() {
|
2017-08-14 19:38:32 -07:00
|
|
|
span_err!(this.sess, fn_decl_span, E0628,
|
2017-07-20 08:06:21 +02:00
|
|
|
"generators cannot have explicit arguments");
|
2017-07-20 05:17:07 +02:00
|
|
|
this.sess.abort_if_errors();
|
2017-07-06 22:08:45 +02:00
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
hir::ExprClosure(this.lower_capture_clause(capture_clause),
|
2017-11-10 12:23:59 -05:00
|
|
|
this.lower_fn_decl(decl, None, false),
|
2016-12-26 14:34:03 +01:00
|
|
|
body_id,
|
|
|
|
fn_decl_span,
|
2017-07-28 13:09:33 -07:00
|
|
|
is_generator)
|
2016-05-10 01:11:59 +00:00
|
|
|
})
|
2017-03-14 15:50:40 +01:00
|
|
|
})
|
|
|
|
}
|
2017-03-22 11:40:29 -04:00
|
|
|
ExprKind::Block(ref blk) => hir::ExprBlock(self.lower_block(blk, false)),
|
2017-03-14 15:50:40 +01:00
|
|
|
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) => {
|
2017-03-24 23:03:15 +00:00
|
|
|
hir::ExprField(P(self.lower_expr(el)),
|
|
|
|
respan(ident.span, self.lower_ident(ident.node)))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
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) => {
|
|
|
|
use syntax::ast::RangeLimits::*;
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2017-04-23 21:14:32 -07:00
|
|
|
let path = match (e1, e2, lims) {
|
|
|
|
(&None, &None, HalfOpen) => "RangeFull",
|
|
|
|
(&Some(..), &None, HalfOpen) => "RangeFrom",
|
|
|
|
(&None, &Some(..), HalfOpen) => "RangeTo",
|
|
|
|
(&Some(..), &Some(..), HalfOpen) => "Range",
|
|
|
|
(&None, &Some(..), Closed) => "RangeToInclusive",
|
|
|
|
(&Some(..), &Some(..), Closed) => "RangeInclusive",
|
2017-03-23 19:26:38 +02:00
|
|
|
(_, &None, Closed) =>
|
|
|
|
panic!(self.diagnostic().span_fatal(
|
|
|
|
e.span, "inclusive range with no end")),
|
|
|
|
};
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2017-03-23 19:26:38 +02:00
|
|
|
let fields =
|
|
|
|
e1.iter().map(|e| ("start", e)).chain(e2.iter().map(|e| ("end", e)))
|
|
|
|
.map(|(s, e)| {
|
|
|
|
let expr = P(self.lower_expr(&e));
|
2017-08-16 18:46:48 -05:00
|
|
|
let unstable_span =
|
|
|
|
self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
|
2017-03-23 19:26:38 +02:00
|
|
|
self.field(Symbol::intern(s), expr, unstable_span)
|
|
|
|
}).collect::<P<[hir::Field]>>();
|
|
|
|
|
|
|
|
let is_unit = fields.is_empty();
|
2017-08-16 18:46:48 -05:00
|
|
|
let unstable_span =
|
|
|
|
self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
|
2017-03-23 19:26:38 +02:00
|
|
|
let struct_path =
|
2017-04-23 21:14:32 -07:00
|
|
|
iter::once("ops").chain(iter::once(path))
|
2017-03-23 19:26:38 +02:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let struct_path = self.std_path(unstable_span, &struct_path, is_unit);
|
|
|
|
let struct_path = hir::QPath::Resolved(None, P(struct_path));
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(e.id);
|
|
|
|
|
2017-03-23 19:26:38 +02:00
|
|
|
return hir::Expr {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-03-23 19:26:38 +02:00
|
|
|
node: if is_unit {
|
|
|
|
hir::ExprPath(struct_path)
|
|
|
|
} else {
|
|
|
|
hir::ExprStruct(struct_path, fields, None)
|
|
|
|
},
|
|
|
|
span: unstable_span,
|
|
|
|
attrs: e.attrs.clone(),
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
ExprKind::Path(ref qself, ref path) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::ExprPath(self.lower_qpath(e.id, qself, path, ParamMode::Optional,
|
|
|
|
ImplTraitContext::Disallowed))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
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,
|
|
|
|
target_id: hir::ScopeTarget::Loop(
|
|
|
|
Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.lower_loop_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() {
|
2017-02-17 18:55:28 -08:00
|
|
|
hir::Destination {
|
2017-02-15 23:28:59 -08:00
|
|
|
ident: opt_ident,
|
2017-03-14 15:50:40 +01:00
|
|
|
target_id: hir::ScopeTarget::Loop(Err(
|
|
|
|
hir::LoopIdError::UnlabeledCfInWhileCondition).into()),
|
2017-02-15 23:28:59 -08:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-14 15:50:40 +01:00
|
|
|
self.lower_loop_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,
|
2017-03-17 04:04:41 +00:00
|
|
|
ctxt: asm.ctxt,
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
|
|
|
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) => {
|
2017-11-10 12:23:59 -05:00
|
|
|
hir::ExprStruct(self.lower_qpath(e.id, &None, path, ParamMode::Optional,
|
|
|
|
ImplTraitContext::Disallowed),
|
2017-03-14 15:50:40 +01:00
|
|
|
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::<Vec<_>>(ex.attrs.into());
|
|
|
|
ex.attrs = attrs;
|
|
|
|
return ex;
|
|
|
|
}
|
|
|
|
|
2016-12-26 14:34:03 +01:00
|
|
|
ExprKind::Yield(ref opt_expr) => {
|
2017-07-28 13:09:33 -07:00
|
|
|
self.is_generator = true;
|
2016-12-26 14:34:03 +01:00
|
|
|
let expr = opt_expr.as_ref().map(|x| self.lower_expr(x)).unwrap_or_else(|| {
|
|
|
|
self.expr(e.span, hir::ExprTup(hir_vec![]), ThinVec::new())
|
|
|
|
});
|
2017-07-10 21:11:31 +02:00
|
|
|
hir::ExprYield(P(expr))
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// Desugar ExprIfLet
|
|
|
|
// From: `if let <pat> = <sub_expr> <body> [<else_opt>]`
|
|
|
|
ExprKind::IfLet(ref pat, ref sub_expr, ref body, ref else_opt) => {
|
|
|
|
// to:
|
|
|
|
//
|
|
|
|
// match <sub_expr> {
|
|
|
|
// <pat> => <body>,
|
|
|
|
// _ => [<else_opt> | ()]
|
|
|
|
// }
|
|
|
|
|
|
|
|
let mut arms = vec![];
|
|
|
|
|
|
|
|
// `<pat> => <body>`
|
|
|
|
{
|
2017-03-22 11:40:29 -04:00
|
|
|
let body = self.lower_block(body, false);
|
2017-03-14 15:50:40 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2017-04-15 10:14:09 +05:30
|
|
|
// _ => [<else_opt>|()]
|
2017-03-14 15:50:40 +01:00
|
|
|
{
|
2017-04-15 12:17:25 +05:30
|
|
|
let wildcard_arm: Option<&Expr> = else_opt.as_ref().map(|p| &**p);
|
|
|
|
let wildcard_pattern = self.pat_wild(e.span);
|
|
|
|
let body = if let Some(else_expr) = wildcard_arm {
|
2017-04-15 17:40:54 +05:30
|
|
|
P(self.lower_expr(else_expr))
|
2017-04-15 17:35:30 +05:30
|
|
|
} else {
|
2017-04-15 17:40:54 +05:30
|
|
|
self.expr_tuple(e.span, hir_vec![])
|
2017-04-15 17:35:30 +05:30
|
|
|
};
|
2017-04-15 12:17:25 +05:30
|
|
|
arms.push(self.arm(hir_vec![wildcard_pattern], body));
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2015-09-28 17:24:42 +13:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
let contains_else_clause = else_opt.is_some();
|
2015-09-28 17:24:42 +13:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
let sub_expr = P(self.lower_expr(sub_expr));
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
hir::ExprMatch(
|
|
|
|
sub_expr,
|
|
|
|
arms.into(),
|
|
|
|
hir::MatchSource::IfLetDesugar {
|
2017-07-03 11:19:51 -07:00
|
|
|
contains_else_clause,
|
2017-03-14 15:50:40 +01:00
|
|
|
})
|
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// Desugar ExprWhileLet
|
|
|
|
// From: `[opt_ident]: while let <pat> = <sub_expr> <body>`
|
|
|
|
ExprKind::WhileLet(ref pat, ref sub_expr, ref body, opt_ident) => {
|
|
|
|
// to:
|
|
|
|
//
|
|
|
|
// [opt_ident]: loop {
|
|
|
|
// match <sub_expr> {
|
|
|
|
// <pat> => <body>,
|
|
|
|
// _ => 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| (
|
2017-03-22 11:40:29 -04:00
|
|
|
this.lower_block(body, false),
|
2017-03-14 15:50:40 +01:00
|
|
|
this.expr_break(e.span, ThinVec::new()),
|
|
|
|
this.with_loop_condition_scope(|this| P(this.lower_expr(sub_expr))),
|
|
|
|
));
|
|
|
|
|
|
|
|
// `<pat> => <body>`
|
|
|
|
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)
|
|
|
|
};
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// `_ => break`
|
|
|
|
let break_arm = {
|
|
|
|
let pat_under = self.pat_wild(e.span);
|
|
|
|
self.arm(hir_vec![pat_under], break_expr)
|
|
|
|
};
|
2016-05-02 00:02:38 +00:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// `match <sub_expr> { ... }`
|
|
|
|
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
|
|
|
|
}
|
2016-05-02 23:26:18 +00:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// Desugar ExprForLoop
|
|
|
|
// From: `[opt_ident]: for <pat> in <head> <body>`
|
|
|
|
ExprKind::ForLoop(ref pat, ref head, ref body, opt_ident) => {
|
|
|
|
// to:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// let result = match ::std::iter::IntoIterator::into_iter(<head>) {
|
|
|
|
// mut iter => {
|
|
|
|
// [opt_ident]: loop {
|
2017-06-23 18:23:23 +02:00
|
|
|
// let mut __next;
|
2017-06-13 18:36:01 +02:00
|
|
|
// match ::std::iter::Iterator::next(&mut iter) {
|
2017-06-23 18:23:23 +02:00
|
|
|
// ::std::option::Option::Some(val) => __next = val,
|
2017-03-14 15:50:40 +01:00
|
|
|
// ::std::option::Option::None => break
|
2017-05-27 20:20:17 +02:00
|
|
|
// };
|
2017-06-23 18:23:23 +02:00
|
|
|
// let <pat> = __next;
|
2017-06-13 18:36:01 +02:00
|
|
|
// StmtExpr(<body>);
|
2017-03-14 15:50:40 +01:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// result
|
|
|
|
// }
|
|
|
|
|
|
|
|
// expand <head>
|
|
|
|
let head = self.lower_expr(head);
|
|
|
|
|
|
|
|
let iter = self.str_to_ident("iter");
|
|
|
|
|
2017-06-23 18:23:23 +02:00
|
|
|
let next_ident = self.str_to_ident("__next");
|
2017-06-17 14:46:37 +02:00
|
|
|
let next_pat = self.pat_ident_binding_mode(e.span,
|
|
|
|
next_ident,
|
2017-07-21 19:29:43 -04:00
|
|
|
hir::BindingAnnotation::Mutable);
|
2017-06-14 13:36:30 +02:00
|
|
|
|
2017-06-13 18:36:01 +02:00
|
|
|
// `::std::option::Option::Some(val) => next = val`
|
2017-03-14 15:50:40 +01:00
|
|
|
let pat_arm = {
|
2017-05-27 20:20:17 +02:00
|
|
|
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(e.span, val_ident, val_pat.id));
|
2017-06-13 18:36:01 +02:00
|
|
|
let next_expr = P(self.expr_ident(e.span, next_ident, next_pat.id));
|
2017-06-14 13:36:30 +02:00
|
|
|
let assign = P(self.expr(e.span,
|
|
|
|
hir::ExprAssign(next_expr, val_expr),
|
|
|
|
ThinVec::new()));
|
2017-05-27 20:20:17 +02:00
|
|
|
let some_pat = self.pat_some(e.span, val_pat);
|
2017-06-13 18:36:01 +02:00
|
|
|
self.arm(hir_vec![some_pat], assign)
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
2016-05-02 00:02:38 +00:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// `::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)
|
|
|
|
};
|
2016-05-02 00:02:38 +00:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// `mut iter`
|
2017-07-21 19:29:43 -04:00
|
|
|
let iter_pat = self.pat_ident_binding_mode(e.span,
|
|
|
|
iter,
|
|
|
|
hir::BindingAnnotation::Mutable);
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
// `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];
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
P(self.expr(e.span,
|
|
|
|
hir::ExprMatch(next_expr, arms,
|
|
|
|
hir::MatchSource::ForLoopDesugar),
|
|
|
|
ThinVec::new()))
|
|
|
|
};
|
2017-08-04 09:49:40 +02:00
|
|
|
let match_stmt = respan(e.span, hir::StmtExpr(match_expr, self.next_id().node_id));
|
2017-06-13 18:36:01 +02:00
|
|
|
|
|
|
|
let next_expr = P(self.expr_ident(e.span, next_ident, next_pat.id));
|
2017-06-14 13:36:30 +02:00
|
|
|
|
2017-06-23 18:23:23 +02:00
|
|
|
// `let mut __next`
|
2017-06-13 18:36:01 +02:00
|
|
|
let next_let = self.stmt_let_pat(e.span,
|
|
|
|
None,
|
|
|
|
next_pat,
|
|
|
|
hir::LocalSource::ForLoopDesugar);
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2017-06-23 18:23:23 +02:00
|
|
|
// `let <pat> = __next`
|
2017-05-27 20:20:17 +02:00
|
|
|
let pat = self.lower_pat(pat);
|
|
|
|
let pat_let = self.stmt_let_pat(e.span,
|
2017-06-13 18:36:01 +02:00
|
|
|
Some(next_expr),
|
2017-05-27 20:20:17 +02:00
|
|
|
pat,
|
|
|
|
hir::LocalSource::ForLoopDesugar);
|
|
|
|
|
|
|
|
let body_block = self.with_loop_scope(e.id,
|
|
|
|
|this| this.lower_block(body, false));
|
|
|
|
let body_expr = P(self.expr_block(body_block, ThinVec::new()));
|
2017-08-04 09:49:40 +02:00
|
|
|
let body_stmt = respan(e.span, hir::StmtExpr(body_expr, self.next_id().node_id));
|
2017-05-27 20:20:17 +02:00
|
|
|
|
2017-06-14 13:36:30 +02:00
|
|
|
let loop_block = P(self.block_all(e.span,
|
|
|
|
hir_vec![next_let,
|
|
|
|
match_stmt,
|
|
|
|
pat_let,
|
|
|
|
body_stmt],
|
|
|
|
None));
|
2017-05-27 20:20:17 +02:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// `[opt_ident]: loop { ... }`
|
|
|
|
let loop_expr = hir::ExprLoop(loop_block, self.lower_opt_sp_ident(opt_ident),
|
|
|
|
hir::LoopSource::ForLoop);
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(e.id);
|
2017-03-14 15:50:40 +01:00
|
|
|
let loop_expr = P(hir::Expr {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-03-14 15:50:40 +01:00
|
|
|
node: loop_expr,
|
|
|
|
span: e.span,
|
|
|
|
attrs: ThinVec::new(),
|
|
|
|
});
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// `mut iter => { ... }`
|
|
|
|
let iter_arm = self.arm(hir_vec![iter_pat], loop_expr);
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
|
|
|
|
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]))
|
|
|
|
};
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
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());
|
|
|
|
}
|
2017-02-17 15:12:47 -08:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// Desugar ExprKind::Try
|
|
|
|
// From: `<expr>?`
|
|
|
|
ExprKind::Try(ref sub_expr) => {
|
|
|
|
// to:
|
|
|
|
//
|
2017-05-07 00:14:04 -07:00
|
|
|
// match Try::into_result(<expr>) {
|
2017-03-14 15:50:40 +01:00
|
|
|
// Ok(val) => #[allow(unreachable_code)] val,
|
|
|
|
// Err(err) => #[allow(unreachable_code)]
|
|
|
|
// // If there is an enclosing `catch {...}`
|
2017-05-07 00:14:04 -07:00
|
|
|
// break 'catch_target Try::from_error(From::from(err)),
|
2017-03-14 15:50:40 +01:00
|
|
|
// // Otherwise
|
2017-05-07 00:14:04 -07:00
|
|
|
// return Try::from_error(From::from(err)),
|
2017-03-14 15:50:40 +01:00
|
|
|
// }
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2017-08-16 18:46:48 -05:00
|
|
|
let unstable_span =
|
|
|
|
self.allow_internal_unstable(CompilerDesugaringKind::QuestionMark, e.span);
|
2016-05-03 13:01:54 +12:00
|
|
|
|
2017-05-07 00:14:04 -07:00
|
|
|
// Try::into_result(<expr>)
|
2017-03-14 15:50:40 +01:00
|
|
|
let discr = {
|
|
|
|
// expand <expr>
|
|
|
|
let sub_expr = self.lower_expr(sub_expr);
|
|
|
|
|
2017-05-07 00:14:04 -07:00
|
|
|
let path = &["ops", "Try", "into_result"];
|
2017-03-14 15:50:40 +01:00
|
|
|
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])
|
2016-05-03 13:01:54 +12:00
|
|
|
};
|
2017-03-14 15:50:40 +01:00
|
|
|
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)
|
|
|
|
};
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
// Err(err) => #[allow(unreachable_code)]
|
2017-10-15 21:18:08 -07:00
|
|
|
// return Try::from_error(From::from(err)),
|
2017-03-14 15:50:40 +01:00
|
|
|
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])
|
2017-01-21 00:08:37 +08:00
|
|
|
};
|
2017-03-14 15:50:40 +01:00
|
|
|
let from_err_expr = {
|
2017-05-07 00:14:04 -07:00
|
|
|
let path = &["ops", "Try", "from_error"];
|
2017-03-14 15:50:40 +01:00
|
|
|
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]))
|
2016-02-28 17:38:48 -05:00
|
|
|
};
|
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
let thin_attrs = ThinVec::from(attrs);
|
|
|
|
let catch_scope = self.catch_scopes.last().map(|x| *x);
|
|
|
|
let ret_expr = if let Some(catch_node) = catch_scope {
|
|
|
|
P(self.expr(
|
|
|
|
e.span,
|
|
|
|
hir::ExprBreak(
|
|
|
|
hir::Destination {
|
|
|
|
ident: None,
|
|
|
|
target_id: hir::ScopeTarget::Block(catch_node),
|
|
|
|
},
|
|
|
|
Some(from_err_expr)
|
|
|
|
),
|
|
|
|
thin_attrs))
|
|
|
|
} else {
|
|
|
|
P(self.expr(e.span,
|
|
|
|
hir::Expr_::ExprRet(Some(from_err_expr)),
|
|
|
|
thin_attrs))
|
|
|
|
};
|
2016-05-03 13:01:54 +12:00
|
|
|
|
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
let err_pat = self.pat_err(e.span, err_local);
|
|
|
|
self.arm(hir_vec![err_pat], ret_expr)
|
|
|
|
};
|
2016-05-02 00:02:38 +00:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
hir::ExprMatch(discr,
|
|
|
|
hir_vec![err_arm, ok_arm],
|
|
|
|
hir::MatchSource::TryDesugar)
|
|
|
|
}
|
2016-02-28 17:38:48 -05:00
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
ExprKind::Mac(_) => panic!("Shouldn't exist here"),
|
|
|
|
};
|
|
|
|
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(e.id);
|
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
hir::Expr {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-03-14 15:50:40 +01:00
|
|
|
node: kind,
|
2016-05-10 01:11:59 +00:00
|
|
|
span: e.span,
|
|
|
|
attrs: e.attrs.clone(),
|
2016-10-28 21:16:44 +11:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2016-11-24 06:11:31 +02:00
|
|
|
fn lower_stmt(&mut self, s: &Stmt) -> SmallVector<hir::Stmt> {
|
|
|
|
SmallVector::one(match s.node {
|
2016-06-17 02:30:01 +00:00
|
|
|
StmtKind::Local(ref l) => Spanned {
|
|
|
|
node: hir::StmtDecl(P(Spanned {
|
|
|
|
node: hir::DeclLocal(self.lower_local(l)),
|
2016-05-10 01:11:59 +00:00
|
|
|
span: s.span,
|
2017-08-04 09:49:40 +02:00
|
|
|
}), self.lower_node_id(s.id).node_id),
|
2016-06-17 02:30:01 +00:00
|
|
|
span: s.span,
|
|
|
|
},
|
2016-11-24 06:11:31 +02:00
|
|
|
StmtKind::Item(ref it) => {
|
|
|
|
// Can only use the ID once.
|
|
|
|
let mut id = Some(s.id);
|
|
|
|
return self.lower_item_id(it).into_iter().map(|item_id| Spanned {
|
|
|
|
node: hir::StmtDecl(P(Spanned {
|
|
|
|
node: hir::DeclItem(item_id),
|
|
|
|
span: s.span,
|
2017-03-14 15:50:40 +01:00
|
|
|
}), id.take()
|
2017-08-04 09:49:40 +02:00
|
|
|
.map(|id| self.lower_node_id(id).node_id)
|
|
|
|
.unwrap_or_else(|| self.next_id().node_id)),
|
2016-06-17 02:30:01 +00:00
|
|
|
span: s.span,
|
2016-11-24 06:11:31 +02:00
|
|
|
}).collect();
|
|
|
|
}
|
2016-06-17 02:30:01 +00:00
|
|
|
StmtKind::Expr(ref e) => {
|
2016-05-10 01:11:59 +00:00
|
|
|
Spanned {
|
2017-03-14 15:50:40 +01:00
|
|
|
node: hir::StmtExpr(P(self.lower_expr(e)),
|
2017-08-04 09:49:40 +02:00
|
|
|
self.lower_node_id(s.id).node_id),
|
2016-05-10 01:11:59 +00:00
|
|
|
span: s.span,
|
|
|
|
}
|
2015-12-07 17:17:41 +03:00
|
|
|
}
|
2016-06-17 02:30:01 +00:00
|
|
|
StmtKind::Semi(ref e) => {
|
2016-05-10 01:11:59 +00:00
|
|
|
Spanned {
|
2017-03-14 15:50:40 +01:00
|
|
|
node: hir::StmtSemi(P(self.lower_expr(e)),
|
2017-08-04 09:49:40 +02:00
|
|
|
self.lower_node_id(s.id).node_id),
|
2016-05-10 01:11:59 +00:00
|
|
|
span: s.span,
|
|
|
|
}
|
2015-12-07 17:17:41 +03:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
StmtKind::Mac(..) => panic!("Shouldn't exist here"),
|
2016-11-24 06:11:31 +02:00
|
|
|
})
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_capture_clause(&mut self, c: CaptureBy) -> hir::CaptureClause {
|
|
|
|
match c {
|
|
|
|
CaptureBy::Value => hir::CaptureByValue,
|
|
|
|
CaptureBy::Ref => hir::CaptureByRef,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
/// 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<NodeId>)
|
|
|
|
-> hir::Visibility {
|
2016-05-10 01:11:59 +00:00
|
|
|
match *v {
|
|
|
|
Visibility::Public => hir::Public,
|
2017-10-19 14:43:47 -07:00
|
|
|
Visibility::Crate(..) => hir::Visibility::Crate,
|
2016-10-17 06:02:23 +03:00
|
|
|
Visibility::Restricted { ref path, id } => {
|
|
|
|
hir::Visibility::Restricted {
|
2016-12-05 03:51:11 +00:00
|
|
|
path: P(self.lower_path(id, path, ParamMode::Explicit, true)),
|
2017-03-14 15:50:40 +01:00
|
|
|
id: if let Some(owner) = explicit_owner {
|
2017-08-04 09:49:40 +02:00
|
|
|
self.lower_node_id_with_owner(id, owner).node_id
|
2017-03-14 15:50:40 +01:00
|
|
|
} else {
|
2017-08-04 09:49:40 +02:00
|
|
|
self.lower_node_id(id).node_id
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2016-10-17 06:02:23 +03:00
|
|
|
}
|
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
Visibility::Inherited => hir::Inherited,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-11-14 11:00:02 -05:00
|
|
|
fn lower_defaultness(&mut self, d: Defaultness, has_value: bool) -> hir::Defaultness {
|
2016-05-10 01:11:59 +00:00
|
|
|
match d {
|
2016-11-14 11:00:02 -05:00
|
|
|
Defaultness::Default => hir::Defaultness::Default { has_value: has_value },
|
|
|
|
Defaultness::Final => {
|
|
|
|
assert!(has_value);
|
|
|
|
hir::Defaultness::Final
|
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-12-18 14:38:28 -08:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode {
|
|
|
|
match *b {
|
|
|
|
BlockCheckMode::Default => hir::DefaultBlock,
|
|
|
|
BlockCheckMode::Unsafe(u) => hir::UnsafeBlock(self.lower_unsafe_source(u)),
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-07-21 19:29:43 -04:00
|
|
|
fn lower_binding_mode(&mut self, b: &BindingMode) -> hir::BindingAnnotation {
|
2016-05-10 01:11:59 +00:00
|
|
|
match *b {
|
2017-07-21 19:29:43 -04:00
|
|
|
BindingMode::ByValue(Mutability::Immutable) =>
|
|
|
|
hir::BindingAnnotation::Unannotated,
|
|
|
|
BindingMode::ByRef(Mutability::Immutable) => hir::BindingAnnotation::Ref,
|
|
|
|
BindingMode::ByValue(Mutability::Mutable) => hir::BindingAnnotation::Mutable,
|
|
|
|
BindingMode::ByRef(Mutability::Mutable) => hir::BindingAnnotation::RefMut,
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource {
|
|
|
|
match u {
|
|
|
|
CompilerGenerated => hir::CompilerGenerated,
|
|
|
|
UserProvided => hir::UserProvided,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_impl_polarity(&mut self, i: ImplPolarity) -> hir::ImplPolarity {
|
|
|
|
match i {
|
|
|
|
ImplPolarity::Positive => hir::ImplPolarity::Positive,
|
|
|
|
ImplPolarity::Negative => hir::ImplPolarity::Negative,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_trait_bound_modifier(&mut self, f: TraitBoundModifier) -> hir::TraitBoundModifier {
|
|
|
|
match f {
|
|
|
|
TraitBoundModifier::None => hir::TraitBoundModifier::None,
|
|
|
|
TraitBoundModifier::Maybe => hir::TraitBoundModifier::Maybe,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
// Helper methods for building HIR.
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
fn arm(&mut self, pats: hir::HirVec<P<hir::Pat>>, expr: P<hir::Expr>) -> hir::Arm {
|
|
|
|
hir::Arm {
|
|
|
|
attrs: hir_vec![],
|
2017-07-03 11:19:51 -07:00
|
|
|
pats,
|
2016-05-10 01:15:11 +00:00
|
|
|
guard: None,
|
|
|
|
body: expr,
|
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
fn field(&mut self, name: Name, expr: P<hir::Expr>, span: Span) -> hir::Field {
|
|
|
|
hir::Field {
|
|
|
|
name: Spanned {
|
|
|
|
node: name,
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2016-05-10 01:15:11 +00:00
|
|
|
},
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
|
|
|
expr,
|
2016-10-27 03:15:13 +03:00
|
|
|
is_shorthand: false,
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2016-01-13 01:24:34 -05:00
|
|
|
}
|
|
|
|
|
2016-06-18 04:01:57 +00:00
|
|
|
fn expr_break(&mut self, span: Span, attrs: ThinVec<Attribute>) -> P<hir::Expr> {
|
2017-02-28 11:05:03 -08:00
|
|
|
let expr_break = hir::ExprBreak(self.lower_loop_destination(None), None);
|
2017-02-15 14:52:27 -08:00
|
|
|
P(self.expr(span, expr_break, attrs))
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-10-28 21:16:44 +11:00
|
|
|
fn expr_call(&mut self, span: Span, e: P<hir::Expr>, args: hir::HirVec<hir::Expr>)
|
|
|
|
-> hir::Expr {
|
2016-06-18 04:01:57 +00:00
|
|
|
self.expr(span, hir::ExprCall(e, args), ThinVec::new())
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-10-28 21:16:44 +11:00
|
|
|
fn expr_ident(&mut self, span: Span, id: Name, binding: NodeId) -> hir::Expr {
|
2017-01-17 21:32:27 +08:00
|
|
|
self.expr_ident_with_attrs(span, id, binding, ThinVec::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_ident_with_attrs(&mut self, span: Span,
|
|
|
|
id: Name,
|
|
|
|
binding: NodeId,
|
|
|
|
attrs: ThinVec<Attribute>) -> hir::Expr {
|
2016-11-25 13:21:19 +02:00
|
|
|
let expr_path = hir::ExprPath(hir::QPath::Resolved(None, P(hir::Path {
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2017-04-29 14:39:47 +03:00
|
|
|
def: Def::Local(binding),
|
2016-11-25 13:21:19 +02:00
|
|
|
segments: hir_vec![hir::PathSegment::from_name(id)],
|
|
|
|
})));
|
2016-05-06 08:24:04 +00:00
|
|
|
|
2017-01-17 21:32:27 +08:00
|
|
|
self.expr(span, expr_path, attrs)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-10-28 21:16:44 +11:00
|
|
|
fn expr_mut_addr_of(&mut self, span: Span, e: P<hir::Expr>) -> hir::Expr {
|
2016-06-18 04:01:57 +00:00
|
|
|
self.expr(span, hir::ExprAddrOf(hir::MutMutable, e), ThinVec::new())
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
fn expr_std_path(&mut self,
|
|
|
|
span: Span,
|
|
|
|
components: &[&str],
|
|
|
|
attrs: ThinVec<Attribute>)
|
2016-10-30 03:42:12 +03:00
|
|
|
-> hir::Expr {
|
2016-11-25 13:21:19 +02:00
|
|
|
let path = self.std_path(span, components, true);
|
2016-11-29 00:33:59 +00:00
|
|
|
self.expr(span, hir::ExprPath(hir::QPath::Resolved(None, P(path))), attrs)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
fn expr_match(&mut self,
|
|
|
|
span: Span,
|
|
|
|
arg: P<hir::Expr>,
|
|
|
|
arms: hir::HirVec<hir::Arm>,
|
2016-06-18 04:01:57 +00:00
|
|
|
source: hir::MatchSource)
|
2016-10-28 21:16:44 +11:00
|
|
|
-> hir::Expr {
|
2016-06-18 04:01:57 +00:00
|
|
|
self.expr(span, hir::ExprMatch(arg, arms, source), ThinVec::new())
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-10-28 21:16:44 +11:00
|
|
|
fn expr_block(&mut self, b: P<hir::Block>, attrs: ThinVec<Attribute>) -> hir::Expr {
|
2016-05-10 01:15:11 +00:00
|
|
|
self.expr(b.span, hir::ExprBlock(b), attrs)
|
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-10-28 21:16:44 +11:00
|
|
|
fn expr_tuple(&mut self, sp: Span, exprs: hir::HirVec<hir::Expr>) -> P<hir::Expr> {
|
|
|
|
P(self.expr(sp, hir::ExprTup(exprs), ThinVec::new()))
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 17:24:42 +13:00
|
|
|
|
2016-10-28 21:16:44 +11:00
|
|
|
fn expr(&mut self, span: Span, node: hir::Expr_, attrs: ThinVec<Attribute>) -> hir::Expr {
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
2016-10-28 21:16:44 +11:00
|
|
|
hir::Expr {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-07-03 11:19:51 -07:00
|
|
|
node,
|
|
|
|
span,
|
|
|
|
attrs,
|
2016-10-28 21:16:44 +11:00
|
|
|
}
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2016-01-13 01:24:34 -05:00
|
|
|
|
2017-05-27 20:20:17 +02:00
|
|
|
fn stmt_let_pat(&mut self,
|
|
|
|
sp: Span,
|
2017-06-13 18:36:01 +02:00
|
|
|
ex: Option<P<hir::Expr>>,
|
2017-05-27 20:20:17 +02:00
|
|
|
pat: P<hir::Pat>,
|
|
|
|
source: hir::LocalSource)
|
|
|
|
-> hir::Stmt {
|
2017-08-07 14:43:43 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
let local = P(hir::Local {
|
2017-07-03 11:19:51 -07:00
|
|
|
pat,
|
2016-05-10 01:15:11 +00:00
|
|
|
ty: None,
|
2017-06-13 18:36:01 +02:00
|
|
|
init: ex,
|
2017-08-07 14:43:43 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2016-05-10 01:15:11 +00:00
|
|
|
span: sp,
|
2016-06-18 04:01:57 +00:00
|
|
|
attrs: ThinVec::new(),
|
2017-05-27 20:20:17 +02:00
|
|
|
source,
|
2016-05-10 01:15:11 +00:00
|
|
|
});
|
|
|
|
let decl = respan(sp, hir::DeclLocal(local));
|
2017-08-04 09:49:40 +02:00
|
|
|
respan(sp, hir::StmtDecl(P(decl), self.next_id().node_id))
|
2017-05-27 20:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn stmt_let(&mut self, sp: Span, mutbl: bool, ident: Name, ex: P<hir::Expr>)
|
|
|
|
-> (hir::Stmt, NodeId) {
|
|
|
|
let pat = if mutbl {
|
2017-07-21 19:29:43 -04:00
|
|
|
self.pat_ident_binding_mode(sp, ident, hir::BindingAnnotation::Mutable)
|
2017-05-27 20:20:17 +02:00
|
|
|
} else {
|
|
|
|
self.pat_ident(sp, ident)
|
|
|
|
};
|
|
|
|
let pat_id = pat.id;
|
2017-06-13 18:36:01 +02:00
|
|
|
(self.stmt_let_pat(sp, Some(ex), pat, hir::LocalSource::Normal), pat_id)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-10-28 21:16:44 +11:00
|
|
|
fn block_expr(&mut self, expr: P<hir::Expr>) -> hir::Block {
|
2016-05-10 01:15:11 +00:00
|
|
|
self.block_all(expr.span, hir::HirVec::new(), Some(expr))
|
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
fn block_all(&mut self, span: Span, stmts: hir::HirVec<hir::Stmt>, expr: Option<P<hir::Expr>>)
|
2016-10-28 21:16:44 +11:00
|
|
|
-> hir::Block {
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
|
|
|
|
2016-10-28 21:16:44 +11:00
|
|
|
hir::Block {
|
2017-07-03 11:19:51 -07:00
|
|
|
stmts,
|
|
|
|
expr,
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2016-05-10 01:15:11 +00:00
|
|
|
rules: hir::DefaultBlock,
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2017-03-22 11:40:29 -04:00
|
|
|
targeted_by_break: false,
|
2016-10-28 21:16:44 +11:00
|
|
|
}
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
fn pat_ok(&mut self, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
|
2016-11-25 13:21:19 +02:00
|
|
|
self.pat_std_enum(span, &["result", "Result", "Ok"], hir_vec![pat])
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
fn pat_err(&mut self, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
|
2016-11-25 13:21:19 +02:00
|
|
|
self.pat_std_enum(span, &["result", "Result", "Err"], hir_vec![pat])
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2016-02-28 17:38:48 -05:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
fn pat_some(&mut self, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
|
2016-11-25 13:21:19 +02:00
|
|
|
self.pat_std_enum(span, &["option", "Option", "Some"], hir_vec![pat])
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2016-02-28 17:38:48 -05:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
fn pat_none(&mut self, span: Span) -> P<hir::Pat> {
|
2016-11-25 13:21:19 +02:00
|
|
|
self.pat_std_enum(span, &["option", "Option", "None"], hir_vec![])
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
fn pat_std_enum(&mut self,
|
|
|
|
span: Span,
|
|
|
|
components: &[&str],
|
|
|
|
subpats: hir::HirVec<P<hir::Pat>>)
|
|
|
|
-> P<hir::Pat> {
|
|
|
|
let path = self.std_path(span, components, true);
|
2016-10-27 05:17:42 +03:00
|
|
|
let qpath = hir::QPath::Resolved(None, P(path));
|
2016-05-10 01:15:11 +00:00
|
|
|
let pt = if subpats.is_empty() {
|
2016-10-27 05:17:42 +03:00
|
|
|
hir::PatKind::Path(qpath)
|
2016-05-10 01:15:11 +00:00
|
|
|
} else {
|
2016-10-27 05:17:42 +03:00
|
|
|
hir::PatKind::TupleStruct(qpath, subpats, None)
|
2016-05-10 01:15:11 +00:00
|
|
|
};
|
2016-11-29 00:33:59 +00:00
|
|
|
self.pat(span, pt)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-03-06 15:54:44 +03:00
|
|
|
fn pat_ident(&mut self, span: Span, name: Name) -> P<hir::Pat> {
|
2017-07-21 19:29:43 -04:00
|
|
|
self.pat_ident_binding_mode(span, name, hir::BindingAnnotation::Unannotated)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2017-07-21 19:29:43 -04:00
|
|
|
fn pat_ident_binding_mode(&mut self, span: Span, name: Name, bm: hir::BindingAnnotation)
|
2016-05-10 01:15:11 +00:00
|
|
|
-> P<hir::Pat> {
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
2015-09-28 17:24:42 +13:00
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
P(hir::Pat {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2016-11-25 13:21:19 +02:00
|
|
|
node: hir::PatKind::Binding(bm,
|
2017-04-29 14:39:47 +03:00
|
|
|
node_id,
|
2016-11-25 13:21:19 +02:00
|
|
|
Spanned {
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2016-11-25 13:21:19 +02:00
|
|
|
node: name,
|
|
|
|
},
|
|
|
|
None),
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2016-11-25 13:21:19 +02:00
|
|
|
})
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
fn pat_wild(&mut self, span: Span) -> P<hir::Pat> {
|
|
|
|
self.pat(span, hir::PatKind::Wild)
|
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
fn pat(&mut self, span: Span, pat: hir::PatKind) -> P<hir::Pat> {
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
2016-05-10 01:15:11 +00:00
|
|
|
P(hir::Pat {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2016-05-10 01:15:11 +00:00
|
|
|
node: pat,
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2016-05-10 01:15:11 +00:00
|
|
|
})
|
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-11-25 13:21:19 +02:00
|
|
|
/// Given suffix ["b","c","d"], returns path `::std::b::c::d` when
|
|
|
|
/// `fld.cx.use_std`, and `::core::b::c::d` otherwise.
|
|
|
|
/// The path is also resolved according to `is_value`.
|
|
|
|
fn std_path(&mut self, span: Span, components: &[&str], is_value: bool) -> hir::Path {
|
|
|
|
let mut path = hir::Path {
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2016-11-25 13:21:19 +02:00
|
|
|
def: Def::Err,
|
2016-12-05 03:51:11 +00:00
|
|
|
segments: iter::once(keywords::CrateRoot.name()).chain({
|
|
|
|
self.crate_root.into_iter().chain(components.iter().cloned()).map(Symbol::intern)
|
|
|
|
}).map(hir::PathSegment::from_name).collect(),
|
2016-11-25 13:21:19 +02:00
|
|
|
};
|
2016-05-10 01:15:11 +00:00
|
|
|
|
2016-11-25 06:07:21 +00:00
|
|
|
self.resolver.resolve_hir_path(&mut path, is_value);
|
2016-11-25 13:21:19 +02:00
|
|
|
path
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signal_block_expr(&mut self,
|
|
|
|
stmts: hir::HirVec<hir::Stmt>,
|
|
|
|
expr: P<hir::Expr>,
|
|
|
|
span: Span,
|
|
|
|
rule: hir::BlockCheckMode,
|
2016-06-18 04:01:57 +00:00
|
|
|
attrs: ThinVec<Attribute>)
|
2016-10-28 21:16:44 +11:00
|
|
|
-> hir::Expr {
|
2017-08-04 09:49:40 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
let block = P(hir::Block {
|
|
|
|
rules: rule,
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2017-08-04 09:49:40 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-07-03 11:19:51 -07:00
|
|
|
stmts,
|
2016-05-10 01:15:11 +00:00
|
|
|
expr: Some(expr),
|
2017-03-22 11:40:29 -04:00
|
|
|
targeted_by_break: false,
|
2016-05-10 01:15:11 +00:00
|
|
|
});
|
|
|
|
self.expr_block(block, attrs)
|
|
|
|
}
|
2016-05-03 13:01:54 +12:00
|
|
|
|
2017-09-13 18:29:59 +03:00
|
|
|
fn ty_path(&mut self, id: LoweredNodeId, span: Span, qpath: hir::QPath) -> P<hir::Ty> {
|
2017-01-17 16:46:23 +02:00
|
|
|
let mut id = id;
|
|
|
|
let node = match qpath {
|
|
|
|
hir::QPath::Resolved(None, path) => {
|
|
|
|
// Turn trait object paths into `TyTraitObject` instead.
|
|
|
|
if let Def::Trait(_) = path.def {
|
2017-01-24 17:17:06 +02:00
|
|
|
let principal = hir::PolyTraitRef {
|
2017-01-17 16:46:23 +02:00
|
|
|
bound_lifetimes: hir_vec![],
|
|
|
|
trait_ref: hir::TraitRef {
|
|
|
|
path: path.and_then(|path| path),
|
2017-09-13 18:29:59 +03:00
|
|
|
ref_id: id.node_id,
|
2017-01-17 16:46:23 +02:00
|
|
|
},
|
|
|
|
span,
|
2017-01-24 17:17:06 +02:00
|
|
|
};
|
2017-01-17 16:46:23 +02:00
|
|
|
|
|
|
|
// The original ID is taken by the `PolyTraitRef`,
|
|
|
|
// so the `Ty` itself needs a different one.
|
2017-09-13 18:29:59 +03:00
|
|
|
id = self.next_id();
|
2017-01-17 16:46:23 +02:00
|
|
|
|
2017-01-24 17:17:06 +02:00
|
|
|
hir::TyTraitObject(hir_vec![principal], self.elided_lifetime(span))
|
2017-01-17 16:46:23 +02:00
|
|
|
} else {
|
|
|
|
hir::TyPath(hir::QPath::Resolved(None, path))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => hir::TyPath(qpath)
|
|
|
|
};
|
2017-09-13 18:29:59 +03:00
|
|
|
P(hir::Ty { id: id.node_id, hir_id: id.hir_id, node, span })
|
2016-10-27 05:17:42 +03:00
|
|
|
}
|
2017-01-09 17:46:11 +02:00
|
|
|
|
|
|
|
fn elided_lifetime(&mut self, span: Span) -> hir::Lifetime {
|
|
|
|
hir::Lifetime {
|
2017-08-04 09:49:40 +02:00
|
|
|
id: self.next_id().node_id,
|
2017-07-03 11:19:51 -07:00
|
|
|
span,
|
2017-09-19 16:36:54 -07:00
|
|
|
name: hir::LifetimeName::Implicit,
|
2017-01-09 17:46:11 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-29 13:17:46 +13:00
|
|
|
}
|
2017-02-21 12:23:47 -05:00
|
|
|
|
|
|
|
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
|
|
|
|
// Sorting by span ensures that we get things in order within a
|
|
|
|
// file, and also puts the files in a sensible order.
|
|
|
|
let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
|
|
|
|
body_ids.sort_by_key(|b| bodies[b].value.span);
|
|
|
|
body_ids
|
|
|
|
}
|