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
|
2019-02-08 14:53:55 +01:00
|
|
|
//! concern spans and IDs.
|
2017-06-03 18:26:20 +02:00
|
|
|
//!
|
|
|
|
//! 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
|
2019-02-08 14:53:55 +01:00
|
|
|
//! being expanded. IDs are assigned to AST nodes just before lowering.
|
2017-06-03 18:26:20 +02:00
|
|
|
//!
|
2019-02-08 14:53:55 +01:00
|
|
|
//! For the simpler lowering steps, IDs and spans should be preserved. Unlike
|
2017-06-03 18:26:20 +02:00
|
|
|
//! 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
|
2019-02-08 14:53:55 +01:00
|
|
|
//! 'folding' an existing one), then you create a new ID using `next_id()`.
|
2017-06-03 18:26:20 +02:00
|
|
|
//!
|
2019-02-08 14:53:55 +01:00
|
|
|
//! 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.
|
2017-06-03 18:26:20 +02:00
|
|
|
//!
|
|
|
|
//! Spans are used for error messages and for tools to map semantics back to
|
2019-02-08 14:53:55 +01:00
|
|
|
//! source code. It is therefore not as important with spans as IDs to be strict
|
2017-06-03 18:26:20 +02:00
|
|
|
//! 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
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::dep_graph::DepGraph;
|
|
|
|
use crate::hir::{self, ParamName};
|
|
|
|
use crate::hir::HirVec;
|
|
|
|
use crate::hir::map::{DefKey, DefPathData, Definitions};
|
|
|
|
use crate::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CRATE_DEF_INDEX};
|
|
|
|
use crate::hir::def::{Def, PathResolution, PerNS};
|
|
|
|
use crate::hir::GenericArg;
|
|
|
|
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
|
2018-07-17 00:30:53 -07:00
|
|
|
ELIDED_LIFETIMES_IN_PATHS};
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::middle::cstore::CrateStore;
|
|
|
|
use crate::session::Session;
|
|
|
|
use crate::session::config::nightly_options;
|
|
|
|
use crate::util::common::FN_OUTPUT_NAME;
|
|
|
|
use crate::util::nodemap::{DefIdMap, NodeMap};
|
2019-02-09 11:24:02 +09:00
|
|
|
use errors::Applicability;
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
use rustc_data_structures::indexed_vec::IndexVec;
|
|
|
|
use rustc_data_structures::thin_vec::ThinVec;
|
|
|
|
use rustc_data_structures::sync::Lrc;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
use std::collections::{BTreeSet, BTreeMap};
|
2017-03-14 15:50:40 +01:00
|
|
|
use std::fmt::Debug;
|
2017-02-15 14:52:27 -08:00
|
|
|
use std::mem;
|
2018-08-30 11:42:16 +02:00
|
|
|
use smallvec::SmallVec;
|
2017-01-17 21:32:27 +08:00
|
|
|
use syntax::attr;
|
2018-02-23 17:48:54 +00:00
|
|
|
use syntax::ast;
|
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};
|
2017-12-15 12:27:20 -08:00
|
|
|
use syntax::print::pprust;
|
2015-07-31 00:04:06 -07:00
|
|
|
use syntax::ptr::P;
|
2018-08-18 12:14:03 +02:00
|
|
|
use syntax::source_map::{self, respan, CompilerDesugaringKind, Spanned};
|
2015-09-28 15:00:15 +13:00
|
|
|
use syntax::std_inject;
|
2018-03-21 04:24:27 -04:00
|
|
|
use syntax::symbol::{keywords, Symbol};
|
2018-11-30 10:02:04 +11:00
|
|
|
use syntax::tokenstream::{TokenStream, TokenTree};
|
2017-09-21 08:13:25 -07:00
|
|
|
use syntax::parse::token::Token;
|
2015-11-17 17:32:12 -05:00
|
|
|
use syntax::visit::{self, Visitor};
|
2018-07-22 12:19:34 -07:00
|
|
|
use syntax_pos::{Span, MultiSpan};
|
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
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Used 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
|
|
|
|
2018-07-11 12:05:10 +02:00
|
|
|
cstore: &'a dyn CrateStore,
|
2017-09-05 16:48:24 +02:00
|
|
|
|
2018-07-11 12:05:10 +02:00
|
|
|
resolver: &'a mut dyn Resolver,
|
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
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
modules: BTreeMap<NodeId, hir::ModuleItems>,
|
|
|
|
|
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
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
/// What to do when we encounter either an "anonymous lifetime
|
|
|
|
/// reference". The term "anonymous" is meant to encompass both
|
|
|
|
/// `'_` lifetimes as well as fully elided cases where nothing is
|
|
|
|
/// written at all (e.g., `&T` or `std::cell::Ref<T>`).
|
|
|
|
anonymous_lifetime_mode: AnonymousLifetimeMode,
|
|
|
|
|
2017-11-16 22:59:45 -08:00
|
|
|
// Used to create lifetime definitions from in-band lifetime usages.
|
2018-11-27 02:59:49 +00:00
|
|
|
// e.g., `fn foo(x: &'x u8) -> &'x u8` to `fn foo<'x>(x: &'x u8) -> &'x u8`
|
2017-11-16 22:59:45 -08:00
|
|
|
// When a named lifetime is encountered in a function or impl header and
|
|
|
|
// has not been defined
|
2018-11-27 02:59:49 +00:00
|
|
|
// (i.e., it doesn't appear in the in_scope_lifetimes list), it is added
|
2017-11-16 22:59:45 -08:00
|
|
|
// to this list. The results of this list are then added to the list of
|
|
|
|
// lifetime definitions in the corresponding impl or function generics.
|
2018-06-01 23:23:48 +01:00
|
|
|
lifetimes_to_define: Vec<(Span, ParamName)>,
|
2018-03-21 16:30:09 -04:00
|
|
|
|
2017-11-16 22:59:45 -08:00
|
|
|
// 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
|
2018-10-03 00:27:12 -07:00
|
|
|
// in in-band lifetime definitions, such a function or an impl header,
|
|
|
|
// including implicit lifetimes from `impl_header_lifetime_elision`.
|
2017-11-16 22:59:45 -08:00
|
|
|
is_collecting_in_band_lifetimes: bool,
|
2018-03-21 16:59:28 -04:00
|
|
|
|
2017-11-16 22:59:45 -08:00
|
|
|
// 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.
|
2018-06-09 23:25:33 +03:00
|
|
|
in_scope_lifetimes: Vec<Ident>,
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
current_module: NodeId,
|
|
|
|
|
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 {
|
2018-09-06 19:24:33 +12:00
|
|
|
/// Resolve a path generated by the lowerer when expanding `for`, `if let`, etc.
|
|
|
|
fn resolve_hir_path(
|
|
|
|
&mut self,
|
|
|
|
path: &ast::Path,
|
|
|
|
is_value: bool,
|
|
|
|
) -> hir::Path;
|
2016-05-02 23:26:18 +00:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Obtain the resolution for a `NodeId`.
|
2016-03-06 15:54:44 +03:00
|
|
|
fn get_resolution(&mut self, id: NodeId) -> Option<PathResolution>;
|
|
|
|
|
2018-06-13 11:44:06 -05:00
|
|
|
/// Obtain the possible resolutions for the given `use` statement.
|
|
|
|
fn get_import(&mut self, id: NodeId) -> PerNS<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;
|
2017-12-25 11:37:55 +05:30
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Given suffix `["b", "c", "d"]`, creates a HIR path for `[::crate_root]::b::c::d` and
|
|
|
|
/// resolves it based on `is_value`.
|
2018-03-21 04:24:27 -04:00
|
|
|
fn resolve_str_path(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
crate_root: Option<&str>,
|
|
|
|
components: &[&str],
|
|
|
|
is_value: bool,
|
|
|
|
) -> hir::Path;
|
2015-09-25 16:03:28 +12:00
|
|
|
}
|
|
|
|
|
2018-06-19 13:47:53 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum ImplTraitContext<'a> {
|
2017-11-10 12:23:59 -05:00
|
|
|
/// 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)`.
|
2018-06-19 13:47:53 +02:00
|
|
|
///
|
2018-08-19 03:40:50 +01:00
|
|
|
/// Newly generated parameters should be inserted into the given `Vec`.
|
2018-06-19 13:47:53 +02:00
|
|
|
Universal(&'a mut Vec<hir::GenericParam>),
|
2017-11-10 12:23:59 -05:00
|
|
|
|
2018-08-19 03:40:50 +01:00
|
|
|
/// Treat `impl Trait` as shorthand for a new existential parameter.
|
2017-11-10 12:23:59 -05:00
|
|
|
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
|
2018-08-19 03:40:50 +01:00
|
|
|
/// equivalent to a fresh existential parameter like `existential type T; fn foo() -> T`.
|
2018-05-22 14:31:56 +02:00
|
|
|
///
|
2018-08-19 03:40:50 +01:00
|
|
|
/// We optionally store a `DefId` for the parent item here so we can look up necessary
|
|
|
|
/// information later. It is `None` when no information about the context should be stored,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// e.g., for consts and statics.
|
2018-08-19 03:40:50 +01:00
|
|
|
Existential(Option<DefId>),
|
2017-11-10 12:23:59 -05:00
|
|
|
|
|
|
|
/// `impl Trait` is not accepted in this position.
|
2018-09-27 22:34:19 +01:00
|
|
|
Disallowed(ImplTraitPosition),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Position in which `impl Trait` is disallowed. Used for error reporting.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
enum ImplTraitPosition {
|
|
|
|
Binding,
|
|
|
|
Other,
|
2017-11-10 12:23:59 -05:00
|
|
|
}
|
|
|
|
|
2018-06-19 13:47:53 +02:00
|
|
|
impl<'a> ImplTraitContext<'a> {
|
2018-09-27 22:34:19 +01:00
|
|
|
#[inline]
|
|
|
|
fn disallowed() -> Self {
|
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::Other)
|
|
|
|
}
|
|
|
|
|
2018-06-19 13:47:53 +02:00
|
|
|
fn reborrow(&'b mut self) -> ImplTraitContext<'b> {
|
|
|
|
use self::ImplTraitContext::*;
|
|
|
|
match self {
|
|
|
|
Universal(params) => Universal(params),
|
|
|
|
Existential(did) => Existential(*did),
|
2018-09-27 22:34:19 +01:00
|
|
|
Disallowed(pos) => Disallowed(*pos),
|
2018-06-19 13:47:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
pub fn lower_crate(
|
|
|
|
sess: &Session,
|
2018-07-11 12:05:10 +02:00
|
|
|
cstore: &dyn CrateStore,
|
2018-03-21 04:24:27 -04:00
|
|
|
dep_graph: &DepGraph,
|
|
|
|
krate: &Crate,
|
2018-07-11 12:05:10 +02:00
|
|
|
resolver: &mut dyn Resolver,
|
2018-03-21 04:24:27 -04: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-12-28 06:05:45 +01:00
|
|
|
dep_graph.assert_ignored();
|
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,
|
2017-07-03 11:19:51 -07:00
|
|
|
resolver,
|
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(),
|
2018-06-06 22:13:52 +02:00
|
|
|
modules: 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,
|
2018-03-21 17:12:39 -04:00
|
|
|
anonymous_lifetime_mode: AnonymousLifetimeMode::PassThrough,
|
2018-07-21 22:15:11 +03:00
|
|
|
type_def_lifetime_params: Default::default(),
|
2018-06-06 22:13:52 +02:00
|
|
|
current_module: CRATE_NODE_ID,
|
2017-03-14 15:50:40 +01:00
|
|
|
current_hir_id_owner: vec![(CRATE_DEF_INDEX, 0)],
|
2018-07-21 22:15:11 +03:00
|
|
|
item_local_id_counters: Default::default(),
|
2017-03-14 15:50:40 +01:00
|
|
|
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
|
|
|
|
2018-03-21 01:58:25 +03:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
2016-10-17 06:02:23 +03:00
|
|
|
enum ParamMode {
|
|
|
|
/// Any path in a type context.
|
|
|
|
Explicit,
|
|
|
|
/// The `module::Type` in `module::Type::method` in an expression.
|
2018-03-21 04:24:27 -04:00
|
|
|
Optional,
|
2016-10-17 06:02:23 +03:00
|
|
|
}
|
|
|
|
|
2018-05-22 14:31:56 +02:00
|
|
|
#[derive(Debug)]
|
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,
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
/// What to do when we encounter an **anonymous** lifetime
|
2019-02-08 14:53:55 +01:00
|
|
|
/// reference. Anonymous lifetime references come in two flavors. You
|
2018-03-21 17:12:39 -04:00
|
|
|
/// have implicit, or fully elided, references to lifetimes, like the
|
|
|
|
/// one in `&T` or `Ref<T>`, and you have `'_` lifetimes, like `&'_ T`
|
2019-02-08 14:53:55 +01:00
|
|
|
/// or `Ref<'_, T>`. These often behave the same, but not always:
|
2018-03-21 17:12:39 -04:00
|
|
|
///
|
|
|
|
/// - certain usages of implicit references are deprecated, like
|
|
|
|
/// `Ref<T>`, and we sometimes just give hard errors in those cases
|
|
|
|
/// as well.
|
|
|
|
/// - for object bounds there is a difference: `Box<dyn Foo>` is not
|
|
|
|
/// the same as `Box<dyn Foo + '_>`.
|
|
|
|
///
|
|
|
|
/// We describe the effects of the various modes in terms of three cases:
|
|
|
|
///
|
|
|
|
/// - **Modern** -- includes all uses of `'_`, but also the lifetime arg
|
|
|
|
/// of a `&` (e.g., the missing lifetime in something like `&T`)
|
|
|
|
/// - **Dyn Bound** -- if you have something like `Box<dyn Foo>`,
|
|
|
|
/// there is an elided lifetime bound (`Box<dyn Foo + 'X>`). These
|
|
|
|
/// elided bounds follow special rules. Note that this only covers
|
|
|
|
/// cases where *nothing* is written; the `'_` in `Box<dyn Foo +
|
|
|
|
/// '_>` is a case of "modern" elision.
|
|
|
|
/// - **Deprecated** -- this coverse cases like `Ref<T>`, where the lifetime
|
|
|
|
/// parameter to ref is completely elided. `Ref<'_, T>` would be the modern,
|
|
|
|
/// non-deprecated equivalent.
|
|
|
|
///
|
|
|
|
/// Currently, the handling of lifetime elision is somewhat spread out
|
|
|
|
/// between HIR lowering and -- as described below -- the
|
|
|
|
/// `resolve_lifetime` module. Often we "fallthrough" to that code by generating
|
|
|
|
/// an "elided" or "underscore" lifetime name. In the future, we probably want to move
|
|
|
|
/// everything into HIR lowering.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum AnonymousLifetimeMode {
|
|
|
|
/// For **Modern** cases, create a new anonymous region parameter
|
|
|
|
/// and reference that.
|
|
|
|
///
|
|
|
|
/// For **Dyn Bound** cases, pass responsibility to
|
|
|
|
/// `resolve_lifetime` code.
|
|
|
|
///
|
|
|
|
/// For **Deprecated** cases, report an error.
|
|
|
|
CreateParameter,
|
|
|
|
|
2018-10-11 15:51:44 -04:00
|
|
|
/// Give a hard error when either `&` or `'_` is written. Used to
|
|
|
|
/// rule out things like `where T: Foo<'_>`. Does not imply an
|
|
|
|
/// error on default object bounds (e.g., `Box<dyn Foo>`).
|
|
|
|
ReportError,
|
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
/// Pass responsibility to `resolve_lifetime` code for all cases.
|
|
|
|
PassThrough,
|
|
|
|
}
|
|
|
|
|
2018-08-30 11:42:16 +02:00
|
|
|
struct ImplTraitTypeIdVisitor<'a> { ids: &'a mut SmallVec<[hir::ItemId; 1]> }
|
2018-08-19 03:40:50 +01:00
|
|
|
|
|
|
|
impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
|
|
|
|
fn visit_ty(&mut self, ty: &'a Ty) {
|
|
|
|
match ty.node {
|
|
|
|
| TyKind::Typeof(_)
|
|
|
|
| TyKind::BareFn(_)
|
|
|
|
=> return,
|
|
|
|
|
|
|
|
TyKind::ImplTrait(id, _) => self.ids.push(hir::ItemId { id }),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
visit::walk_ty(self, ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_path_segment(
|
|
|
|
&mut self,
|
|
|
|
path_span: Span,
|
|
|
|
path_segment: &'v PathSegment,
|
|
|
|
) {
|
|
|
|
if let Some(ref p) = path_segment.args {
|
|
|
|
if let GenericArgs::Parenthesized(_) = **p {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
visit::walk_path_segment(self, path_span, path_segment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2018-11-27 02:59:49 +00:00
|
|
|
/// needed from arbitrary locations in the crate,
|
|
|
|
/// e.g., the number of lifetime generic parameters
|
2017-01-09 17:46:11 +02:00
|
|
|
/// 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 {
|
2018-03-21 04:24:27 -04:00
|
|
|
ItemKind::Struct(_, ref generics)
|
|
|
|
| ItemKind::Union(_, ref generics)
|
|
|
|
| ItemKind::Enum(_, ref generics)
|
|
|
|
| ItemKind::Ty(_, ref generics)
|
2018-07-03 19:38:14 +02:00
|
|
|
| ItemKind::Existential(_, ref generics)
|
2018-03-21 04:24:27 -04: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);
|
2018-03-21 04:24:27 -04:00
|
|
|
let count = generics
|
|
|
|
.params
|
|
|
|
.iter()
|
2018-05-26 23:21:08 +01:00
|
|
|
.filter(|param| match param.kind {
|
2018-05-27 20:07:09 +01:00
|
|
|
ast::GenericParamKind::Lifetime { .. } => true,
|
2018-05-26 23:21:08 +01:00
|
|
|
_ => false,
|
|
|
|
})
|
2017-10-16 21:07:26 +02:00
|
|
|
.count();
|
2017-01-09 17:46:11 +02:00
|
|
|
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)
|
2018-03-21 04:24:27 -04:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self),
|
2017-11-10 12:33:05 -05:00
|
|
|
{
|
|
|
|
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> {
|
2018-06-06 22:13:52 +02:00
|
|
|
fn visit_mod(&mut self, m: &'lcx Mod, _s: Span, _attrs: &[Attribute], n: NodeId) {
|
2019-01-11 04:58:46 +01:00
|
|
|
self.lctx.modules.insert(n, hir::ModuleItems {
|
|
|
|
items: BTreeSet::new(),
|
|
|
|
trait_items: BTreeSet::new(),
|
|
|
|
impl_items: BTreeSet::new(),
|
|
|
|
});
|
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
let old = self.lctx.current_module;
|
|
|
|
self.lctx.current_module = n;
|
|
|
|
visit::walk_mod(self, m);
|
|
|
|
self.lctx.current_module = old;
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
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) {
|
2018-06-06 22:13:52 +02:00
|
|
|
lctx.insert_item(item.id, hir_item);
|
2017-03-14 15:50:40 +01:00
|
|
|
} else {
|
|
|
|
item_lowered = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if item_lowered {
|
2018-06-14 12:05:47 +01:00
|
|
|
let item_generics = match self.lctx.items.get(&item.id).unwrap().node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Impl(_, _, _, ref generics, ..)
|
|
|
|
| hir::ItemKind::Trait(_, _, ref generics, ..) => {
|
2018-05-27 16:56:01 +01:00
|
|
|
generics.params.clone()
|
2018-03-21 04:24:27 -04:00
|
|
|
}
|
2018-05-27 16:56:01 +01:00
|
|
|
_ => HirVec::new(),
|
2017-11-16 22:59:45 -08:00
|
|
|
};
|
|
|
|
|
2018-06-14 12:05:47 +01:00
|
|
|
self.lctx.with_parent_impl_lifetime_defs(&item_generics, |this| {
|
2018-05-26 13:11:39 +01:00
|
|
|
let this = &mut ItemLowerer { lctx: this };
|
2018-09-24 04:25:23 +01:00
|
|
|
if let ItemKind::Impl(.., ref opt_trait_ref, _, _) = item.node {
|
2018-05-26 13:11:39 +01:00
|
|
|
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);
|
2018-06-06 22:13:52 +02:00
|
|
|
lctx.modules.get_mut(&lctx.current_module).unwrap().trait_items.insert(id);
|
2017-03-14 15:50:40 +01:00
|
|
|
});
|
|
|
|
|
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);
|
2018-06-06 22:13:52 +02:00
|
|
|
lctx.modules.get_mut(&lctx.current_module).unwrap().impl_items.insert(id);
|
2017-03-14 15:50:40 +01:00
|
|
|
});
|
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,
|
2018-06-06 22:13:52 +02:00
|
|
|
modules: self.modules,
|
2017-01-09 17:46:11 +02:00
|
|
|
}
|
2016-10-27 22:04:22 +02:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
fn insert_item(&mut self, id: NodeId, item: hir::Item) {
|
|
|
|
self.items.insert(id, item);
|
|
|
|
self.modules.get_mut(&self.current_module).unwrap().items.insert(id);
|
|
|
|
}
|
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
fn allocate_hir_id_counter<T: Debug>(&mut self, owner: NodeId, debug: &T) -> LoweredNodeId {
|
2017-03-14 15:50:40 +01:00
|
|
|
if self.item_local_id_counters.insert(owner, 0).is_some() {
|
2018-03-21 04:24:27 -04:00
|
|
|
bug!(
|
|
|
|
"Tried to allocate item_local_id_counter for {:?} twice",
|
|
|
|
debug
|
|
|
|
);
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Always allocate the first `HirId` for the owner itself.
|
2018-06-06 15:50:59 -07:00
|
|
|
self.lower_node_id_with_owner(owner, owner)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_node_id_generic<F>(&mut self, ast_node_id: NodeId, alloc_hir_id: F) -> LoweredNodeId
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> hir::HirId,
|
2017-03-14 15:50:40 +01:00
|
|
|
{
|
|
|
|
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,
|
2018-03-21 04:24:27 -04:00
|
|
|
};
|
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 {
|
2018-11-27 02:59:49 +00: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 14:31:56 +02:00
|
|
|
fn with_hir_id_owner<F, T>(&mut self, owner: NodeId, f: F) -> T
|
2018-03-21 04:24:27 -04:00
|
|
|
where
|
2018-05-22 14:31:56 +02:00
|
|
|
F: FnOnce(&mut Self) -> T,
|
2017-03-14 15:50:40 +01:00
|
|
|
{
|
|
|
|
let counter = self.item_local_id_counters
|
2018-03-21 04:24:27 -04:00
|
|
|
.insert(owner, HIR_ID_COUNTER_LOCKED)
|
2018-06-06 15:50:59 -07:00
|
|
|
.unwrap_or_else(|| panic!("No item_local_id_counters entry for {:?}", owner));
|
2017-03-14 15:50:40 +01:00
|
|
|
let def_index = self.resolver.definitions().opt_def_index(owner).unwrap();
|
|
|
|
self.current_hir_id_owner.push((def_index, counter));
|
2018-05-22 14:31:56 +02:00
|
|
|
let ret = f(self);
|
2017-03-14 15:50:40 +01:00
|
|
|
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);
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
let prev = self.item_local_id_counters
|
|
|
|
.insert(owner, new_counter)
|
|
|
|
.unwrap();
|
2017-03-14 15:50:40 +01:00
|
|
|
debug_assert!(prev == HIR_ID_COUNTER_LOCKED);
|
2018-05-22 14:31:56 +02:00
|
|
|
ret
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// 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
|
2017-03-14 15:50:40 +01:00
|
|
|
/// actually used in the HIR, as that would trigger an assertion in the
|
2018-11-27 02:59:49 +00:00
|
|
|
/// `HirIdValidator` later on, which makes sure that all `NodeId`s 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| {
|
2018-03-21 04:24:27 -04:00
|
|
|
let &mut (def_index, ref mut local_id_counter) =
|
|
|
|
this.current_hir_id_owner.last_mut().unwrap();
|
2017-03-14 15:50:40 +01:00
|
|
|
let local_id = *local_id_counter;
|
|
|
|
*local_id_counter += 1;
|
|
|
|
hir::HirId {
|
|
|
|
owner: def_index,
|
2018-11-07 11:01:18 +01:00
|
|
|
local_id: hir::ItemLocalId::from_u32(local_id),
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_node_id_with_owner(&mut self, ast_node_id: NodeId, owner: NodeId) -> LoweredNodeId {
|
2017-03-14 15:50:40 +01:00
|
|
|
self.lower_node_id_generic(ast_node_id, |this| {
|
2018-05-22 14:31:56 +02:00
|
|
|
let local_id_counter = this
|
|
|
|
.item_local_id_counters
|
|
|
|
.get_mut(&owner)
|
|
|
|
.expect("called lower_node_id_with_owner before allocate_hir_id_counter");
|
2017-03-14 15:50:40 +01:00
|
|
|
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;
|
2018-05-22 14:31:56 +02:00
|
|
|
let def_index = this
|
|
|
|
.resolver
|
|
|
|
.definitions()
|
|
|
|
.opt_def_index(owner)
|
|
|
|
.expect("You forgot to call `create_def_with_parent` or are lowering node ids \
|
|
|
|
that do not belong to the current owner");
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
hir::HirId {
|
|
|
|
owner: def_index,
|
2018-11-07 11:01:18 +01:00
|
|
|
local_id: hir::ItemLocalId::from_u32(local_id),
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04: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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-12 12:31:11 +02:00
|
|
|
fn expect_full_def_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Def> {
|
2018-06-15 14:47:07 -05:00
|
|
|
self.resolver.get_import(id).present_items().map(|pr| {
|
2018-06-13 11:44:06 -05:00
|
|
|
if pr.unresolved_segments() != 0 {
|
|
|
|
bug!("path not fully resolved: {:?}", pr);
|
|
|
|
}
|
|
|
|
pr.base_def()
|
2018-06-15 14:47:07 -05:00
|
|
|
})
|
2018-06-13 11:44:06 -05: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
|
|
|
|
2018-06-10 17:40:45 +03:00
|
|
|
fn str_to_ident(&self, s: &'static str) -> Ident {
|
|
|
|
Ident::with_empty_ctxt(Symbol::gensym(s))
|
2015-10-07 08:26:22 +13:00
|
|
|
}
|
2016-03-15 13:03:42 -04:00
|
|
|
|
2019-02-04 11:24:55 +01:00
|
|
|
/// Reuses the span but adds information like the kind of the desugaring and features that are
|
|
|
|
/// allowed inside this span.
|
|
|
|
fn mark_span_with_reason(
|
2019-02-03 12:55:00 +01:00
|
|
|
&self,
|
|
|
|
reason: CompilerDesugaringKind,
|
|
|
|
span: Span,
|
2019-02-08 10:21:21 +01:00
|
|
|
allow_internal_unstable: Option<Lrc<[Symbol]>>,
|
2019-02-03 12:55:00 +01:00
|
|
|
) -> Span {
|
2017-03-22 08:39:51 +00:00
|
|
|
let mark = Mark::fresh(Mark::root());
|
2018-08-18 12:14:03 +02:00
|
|
|
mark.set_expn_info(source_map::ExpnInfo {
|
2016-10-30 03:42:12 +03:00
|
|
|
call_site: span,
|
2018-06-23 21:41:39 +03:00
|
|
|
def_site: Some(span),
|
2018-08-18 12:14:03 +02:00
|
|
|
format: source_map::CompilerDesugaring(reason),
|
2019-02-03 12:55:00 +01:00
|
|
|
allow_internal_unstable,
|
2018-06-23 21:41:39 +03:00
|
|
|
allow_internal_unsafe: false,
|
2018-06-11 14:21:36 +03:00
|
|
|
local_inner_macros: false,
|
2018-08-18 12:14:03 +02:00
|
|
|
edition: source_map::hygiene::default_edition(),
|
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
|
|
|
}
|
|
|
|
|
2018-05-30 10:27:53 -04:00
|
|
|
fn with_anonymous_lifetime_mode<R>(
|
|
|
|
&mut self,
|
|
|
|
anonymous_lifetime_mode: AnonymousLifetimeMode,
|
|
|
|
op: impl FnOnce(&mut Self) -> R,
|
|
|
|
) -> R {
|
|
|
|
let old_anonymous_lifetime_mode = self.anonymous_lifetime_mode;
|
|
|
|
self.anonymous_lifetime_mode = anonymous_lifetime_mode;
|
|
|
|
let result = op(self);
|
|
|
|
self.anonymous_lifetime_mode = old_anonymous_lifetime_mode;
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
/// Creates a new hir::GenericParam for every new lifetime and
|
|
|
|
/// type parameter encountered while evaluating `f`. Definitions
|
|
|
|
/// are created with the parent provided. If no `parent_id` is
|
|
|
|
/// provided, no definitions will be returned.
|
|
|
|
///
|
|
|
|
/// Presuming that in-band lifetimes are enabled, then
|
|
|
|
/// `self.anonymous_lifetime_mode` will be updated to match the
|
|
|
|
/// argument while `f` is running (and restored afterwards).
|
|
|
|
fn collect_in_band_defs<T, F>(
|
|
|
|
&mut self,
|
|
|
|
parent_id: DefId,
|
|
|
|
anonymous_lifetime_mode: AnonymousLifetimeMode,
|
|
|
|
f: F,
|
|
|
|
) -> (Vec<hir::GenericParam>, T)
|
2018-03-21 04:24:27 -04:00
|
|
|
where
|
2018-08-29 22:02:42 -07:00
|
|
|
F: FnOnce(&mut LoweringContext<'_>) -> (Vec<hir::GenericParam>, T),
|
2017-11-16 22:59:45 -08:00
|
|
|
{
|
|
|
|
assert!(!self.is_collecting_in_band_lifetimes);
|
|
|
|
assert!(self.lifetimes_to_define.is_empty());
|
2018-03-21 17:12:39 -04:00
|
|
|
let old_anonymous_lifetime_mode = self.anonymous_lifetime_mode;
|
|
|
|
|
2018-10-03 00:27:12 -07:00
|
|
|
self.anonymous_lifetime_mode = anonymous_lifetime_mode;
|
|
|
|
self.is_collecting_in_band_lifetimes = true;
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2018-06-19 13:47:53 +02:00
|
|
|
let (in_band_ty_params, res) = f(self);
|
2017-11-16 22:59:45 -08:00
|
|
|
|
|
|
|
self.is_collecting_in_band_lifetimes = false;
|
2018-03-21 17:12:39 -04:00
|
|
|
self.anonymous_lifetime_mode = old_anonymous_lifetime_mode;
|
2017-11-16 22:59:45 -08:00
|
|
|
|
|
|
|
let lifetimes_to_define = self.lifetimes_to_define.split_off(0);
|
|
|
|
|
2018-03-21 04:45:21 -04:00
|
|
|
let params = lifetimes_to_define
|
|
|
|
.into_iter()
|
2018-03-21 16:30:09 -04:00
|
|
|
.map(|(span, hir_name)| {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
2018-03-21 04:45:21 -04:00
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
// Get the name we'll use to make the def-path. Note
|
|
|
|
// that collisions are ok here and this shouldn't
|
|
|
|
// really show up for end-user.
|
2018-09-22 01:45:42 -07:00
|
|
|
let (str_name, kind) = match hir_name {
|
|
|
|
ParamName::Plain(ident) => (
|
|
|
|
ident.as_interned_str(),
|
|
|
|
hir::LifetimeParamKind::InBand,
|
|
|
|
),
|
|
|
|
ParamName::Fresh(_) => (
|
|
|
|
keywords::UnderscoreLifetime.name().as_interned_str(),
|
|
|
|
hir::LifetimeParamKind::Elided,
|
|
|
|
),
|
2018-10-11 15:51:44 -04:00
|
|
|
ParamName::Error => (
|
|
|
|
keywords::UnderscoreLifetime.name().as_interned_str(),
|
|
|
|
hir::LifetimeParamKind::Error,
|
|
|
|
),
|
2018-03-21 16:30:09 -04:00
|
|
|
};
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Add a definition for the in-band lifetime def.
|
2018-03-21 04:45:21 -04:00
|
|
|
self.resolver.definitions().create_def_with_parent(
|
|
|
|
parent_id.index,
|
2019-02-02 15:40:08 +01:00
|
|
|
node_id,
|
2018-06-28 00:12:17 +03:00
|
|
|
DefPathData::LifetimeParam(str_name),
|
2018-03-21 04:45:21 -04:00
|
|
|
DefIndexAddressSpace::High,
|
|
|
|
Mark::root(),
|
|
|
|
span,
|
|
|
|
);
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2018-05-26 00:27:54 +01:00
|
|
|
hir::GenericParam {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-06-01 23:23:48 +01:00
|
|
|
name: hir_name,
|
2018-06-14 11:42:12 +01:00
|
|
|
attrs: hir_vec![],
|
|
|
|
bounds: hir_vec![],
|
2018-05-26 00:27:54 +01:00
|
|
|
span,
|
2018-03-21 04:45:21 -04:00
|
|
|
pure_wrt_drop: false,
|
2018-09-22 01:45:42 -07:00
|
|
|
kind: hir::GenericParamKind::Lifetime { kind }
|
2018-06-01 23:23:48 +01:00
|
|
|
}
|
2018-03-21 04:45:21 -04:00
|
|
|
})
|
2018-05-26 00:27:54 +01:00
|
|
|
.chain(in_band_ty_params.into_iter())
|
2018-03-21 04:45:21 -04:00
|
|
|
.collect();
|
2017-10-16 21:07:26 +02:00
|
|
|
|
|
|
|
(params, res)
|
2017-11-16 22:59:45 -08:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:57:57 -04:00
|
|
|
/// When there is a reference to some lifetime `'a`, and in-band
|
|
|
|
/// lifetimes are enabled, then we want to push that lifetime into
|
|
|
|
/// the vector of names to define later. In that case, it will get
|
|
|
|
/// added to the appropriate generics.
|
2018-06-09 23:25:33 +03:00
|
|
|
fn maybe_collect_in_band_lifetime(&mut self, ident: Ident) {
|
2018-03-21 04:57:57 -04:00
|
|
|
if !self.is_collecting_in_band_lifetimes {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-03 00:08:44 -07:00
|
|
|
if !self.sess.features_untracked().in_band_lifetimes {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-09 23:25:33 +03:00
|
|
|
if self.in_scope_lifetimes.contains(&ident.modern()) {
|
2018-03-21 04:57:57 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-28 00:12:17 +03:00
|
|
|
let hir_name = ParamName::Plain(ident);
|
2018-03-21 16:30:09 -04:00
|
|
|
|
2018-06-09 23:25:33 +03:00
|
|
|
if self.lifetimes_to_define.iter()
|
2018-06-28 00:12:17 +03:00
|
|
|
.any(|(_, lt_name)| lt_name.modern() == hir_name.modern()) {
|
2018-03-21 04:57:57 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-09 23:25:33 +03:00
|
|
|
self.lifetimes_to_define.push((ident.span, hir_name));
|
2018-03-21 04:57:57 -04:00
|
|
|
}
|
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
/// When we have either an elided or `'_` lifetime in an impl
|
2018-10-11 15:51:44 -04:00
|
|
|
/// header, we convert it to an in-band lifetime.
|
2018-06-01 23:23:48 +01:00
|
|
|
fn collect_fresh_in_band_lifetime(&mut self, span: Span) -> ParamName {
|
2018-03-21 17:12:39 -04:00
|
|
|
assert!(self.is_collecting_in_band_lifetimes);
|
|
|
|
let index = self.lifetimes_to_define.len();
|
2018-06-01 23:23:48 +01:00
|
|
|
let hir_name = ParamName::Fresh(index);
|
2018-03-21 17:12:39 -04:00
|
|
|
self.lifetimes_to_define.push((span, hir_name));
|
|
|
|
hir_name
|
|
|
|
}
|
|
|
|
|
2018-05-26 19:16:21 +01:00
|
|
|
// Evaluates `f` with the lifetimes in `params` in-scope.
|
2017-11-16 22:59:45 -08:00
|
|
|
// 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.
|
2018-07-27 16:50:28 +02:00
|
|
|
fn with_in_scope_lifetime_defs<T, F>(&mut self, params: &[GenericParam], f: F) -> T
|
2018-03-21 04:24:27 -04:00
|
|
|
where
|
2018-08-29 22:02:42 -07:00
|
|
|
F: FnOnce(&mut LoweringContext<'_>) -> T,
|
2017-11-16 22:59:45 -08:00
|
|
|
{
|
|
|
|
let old_len = self.in_scope_lifetimes.len();
|
2018-05-27 16:56:01 +01:00
|
|
|
let lt_def_names = params.iter().filter_map(|param| match param.kind {
|
2018-06-09 23:25:33 +03:00
|
|
|
GenericParamKind::Lifetime { .. } => Some(param.ident.modern()),
|
2018-05-27 16:56:01 +01:00
|
|
|
_ => None,
|
|
|
|
});
|
2017-11-16 22:59:45 -08:00
|
|
|
self.in_scope_lifetimes.extend(lt_def_names);
|
|
|
|
|
|
|
|
let res = f(self);
|
|
|
|
|
|
|
|
self.in_scope_lifetimes.truncate(old_len);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2018-05-26 19:16:21 +01:00
|
|
|
// Same as the method above, but accepts `hir::GenericParam`s
|
|
|
|
// instead of `ast::GenericParam`s.
|
2017-11-16 22:59:45 -08:00
|
|
|
// 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.
|
2018-05-27 16:56:01 +01:00
|
|
|
fn with_parent_impl_lifetime_defs<T, F>(&mut self,
|
|
|
|
params: &HirVec<hir::GenericParam>,
|
|
|
|
f: F
|
|
|
|
) -> T where
|
2018-08-29 22:02:42 -07:00
|
|
|
F: FnOnce(&mut LoweringContext<'_>) -> T,
|
2017-11-16 22:59:45 -08:00
|
|
|
{
|
|
|
|
let old_len = self.in_scope_lifetimes.len();
|
2018-05-27 16:56:01 +01:00
|
|
|
let lt_def_names = params.iter().filter_map(|param| match param.kind {
|
2018-06-09 23:25:33 +03:00
|
|
|
hir::GenericParamKind::Lifetime { .. } => Some(param.name.ident().modern()),
|
2018-05-27 16:56:01 +01:00
|
|
|
_ => None,
|
|
|
|
});
|
2017-11-16 22:59:45 -08:00
|
|
|
self.in_scope_lifetimes.extend(lt_def_names);
|
|
|
|
|
|
|
|
let res = f(self);
|
|
|
|
|
|
|
|
self.in_scope_lifetimes.truncate(old_len);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
/// Appends in-band lifetime defs and argument-position `impl
|
|
|
|
/// Trait` defs to the existing set of generics.
|
|
|
|
///
|
|
|
|
/// Presuming that in-band lifetimes are enabled, then
|
|
|
|
/// `self.anonymous_lifetime_mode` will be updated to match the
|
|
|
|
/// argument while `f` is running (and restored afterwards).
|
2017-12-15 12:27:20 -08:00
|
|
|
fn add_in_band_defs<F, T>(
|
2017-11-16 22:59:45 -08:00
|
|
|
&mut self,
|
|
|
|
generics: &Generics,
|
2018-03-21 04:45:21 -04:00
|
|
|
parent_id: DefId,
|
2018-03-21 17:12:39 -04:00
|
|
|
anonymous_lifetime_mode: AnonymousLifetimeMode,
|
2018-03-21 04:24:27 -04:00
|
|
|
f: F,
|
2017-11-16 22:59:45 -08:00
|
|
|
) -> (hir::Generics, T)
|
2018-03-21 04:24:27 -04:00
|
|
|
where
|
2018-08-29 22:02:42 -07:00
|
|
|
F: FnOnce(&mut LoweringContext<'_>, &mut Vec<hir::GenericParam>) -> T,
|
2017-11-16 22:59:45 -08:00
|
|
|
{
|
2018-03-21 04:24:27 -04:00
|
|
|
let (in_band_defs, (mut lowered_generics, res)) = self.with_in_scope_lifetime_defs(
|
2018-05-27 16:56:01 +01:00
|
|
|
&generics.params,
|
2018-03-21 04:24:27 -04:00
|
|
|
|this| {
|
2018-03-21 17:12:39 -04:00
|
|
|
this.collect_in_band_defs(parent_id, anonymous_lifetime_mode, |this| {
|
2018-06-19 13:47:53 +02:00
|
|
|
let mut params = Vec::new();
|
|
|
|
let generics = this.lower_generics(
|
|
|
|
generics,
|
|
|
|
ImplTraitContext::Universal(&mut params),
|
|
|
|
);
|
|
|
|
let res = f(this, &mut params);
|
|
|
|
(params, (generics, res))
|
2018-03-21 04:24:27 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
);
|
2017-12-15 12:27:20 -08:00
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
lowered_generics.params = lowered_generics
|
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.chain(in_band_defs)
|
|
|
|
.collect();
|
2017-11-16 22:59:45 -08:00
|
|
|
|
|
|
|
(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
|
2018-03-21 04:24:27 -04:00
|
|
|
where
|
2018-08-29 22:02:42 -07:00
|
|
|
F: FnOnce(&mut LoweringContext<'_>) -> T,
|
2017-02-17 15:12:47 -08:00
|
|
|
{
|
|
|
|
let len = self.catch_scopes.len();
|
|
|
|
self.catch_scopes.push(catch_id);
|
|
|
|
|
|
|
|
let result = f(self);
|
2018-03-21 04:24:27 -04:00
|
|
|
assert_eq!(
|
|
|
|
len + 1,
|
|
|
|
self.catch_scopes.len(),
|
|
|
|
"catch scopes should be added and removed in stack order"
|
|
|
|
);
|
2017-02-17 15:12:47 -08:00
|
|
|
|
|
|
|
self.catch_scopes.pop().unwrap();
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
fn make_async_expr(
|
|
|
|
&mut self,
|
|
|
|
capture_clause: CaptureBy,
|
|
|
|
closure_node_id: NodeId,
|
|
|
|
ret_ty: Option<&Ty>,
|
2018-08-29 22:02:42 -07:00
|
|
|
body: impl FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
|
2018-07-11 20:05:29 +08:00
|
|
|
) -> hir::ExprKind {
|
2018-06-06 15:50:59 -07:00
|
|
|
let prev_is_generator = mem::replace(&mut self.is_generator, true);
|
|
|
|
let body_expr = body(self);
|
|
|
|
let span = body_expr.span;
|
|
|
|
let output = match ret_ty {
|
|
|
|
Some(ty) => FunctionRetTy::Ty(P(ty.clone())),
|
|
|
|
None => FunctionRetTy::Default(span),
|
|
|
|
};
|
|
|
|
let decl = FnDecl {
|
|
|
|
inputs: vec![],
|
|
|
|
output,
|
|
|
|
variadic: false
|
|
|
|
};
|
|
|
|
let body_id = self.record_body(body_expr, Some(&decl));
|
|
|
|
self.is_generator = prev_is_generator;
|
|
|
|
|
|
|
|
let capture_clause = self.lower_capture_clause(capture_clause);
|
|
|
|
let closure_hir_id = self.lower_node_id(closure_node_id).hir_id;
|
2018-06-26 11:56:24 +02:00
|
|
|
let decl = self.lower_fn_decl(&decl, None, /* impl trait allowed */ false, None);
|
2018-06-06 15:50:59 -07:00
|
|
|
let generator = hir::Expr {
|
|
|
|
id: closure_node_id,
|
|
|
|
hir_id: closure_hir_id,
|
2018-07-11 20:05:29 +08:00
|
|
|
node: hir::ExprKind::Closure(capture_clause, decl, body_id, span,
|
2018-06-06 15:50:59 -07:00
|
|
|
Some(hir::GeneratorMovability::Static)),
|
|
|
|
span,
|
|
|
|
attrs: ThinVec::new(),
|
|
|
|
};
|
|
|
|
|
2019-02-04 11:24:55 +01:00
|
|
|
let unstable_span = self.mark_span_with_reason(
|
2019-02-03 12:55:00 +01:00
|
|
|
CompilerDesugaringKind::Async,
|
|
|
|
span,
|
2019-02-07 14:19:06 +01:00
|
|
|
Some(vec![
|
2019-02-03 12:55:00 +01:00
|
|
|
Symbol::intern("gen_future"),
|
2019-02-07 14:19:06 +01:00
|
|
|
].into()),
|
2019-02-03 12:55:00 +01:00
|
|
|
);
|
2018-06-06 15:50:59 -07:00
|
|
|
let gen_future = self.expr_std_path(
|
2018-06-22 16:08:07 -07:00
|
|
|
unstable_span, &["future", "from_generator"], None, ThinVec::new());
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Call(P(gen_future), hir_vec![generator])
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
|
2016-12-26 14:34:03 +01:00
|
|
|
fn lower_body<F>(&mut self, decl: Option<&FnDecl>, f: F) -> hir::BodyId
|
2018-03-21 04:24:27 -04:00
|
|
|
where
|
2018-08-29 22:02:42 -07:00
|
|
|
F: FnOnce(&mut LoweringContext<'_>) -> hir::Expr,
|
2016-12-26 14:34:03 +01:00
|
|
|
{
|
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;
|
2018-03-21 04:24:27 -04:00
|
|
|
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
|
2018-03-21 04:24:27 -04:00
|
|
|
where
|
2018-08-29 22:02:42 -07:00
|
|
|
F: FnOnce(&mut LoweringContext<'_>) -> T,
|
2017-02-15 14:52:27 -08:00
|
|
|
{
|
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);
|
2018-03-21 04:24:27 -04:00
|
|
|
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
|
2018-03-21 04:24:27 -04:00
|
|
|
where
|
2018-08-29 22:02:42 -07:00
|
|
|
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 = 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
|
2018-03-21 04:24:27 -04:00
|
|
|
where
|
2018-08-29 22:02:42 -07:00
|
|
|
F: FnOnce(&mut LoweringContext<'_>) -> T,
|
2017-02-15 14:52:27 -08:00
|
|
|
{
|
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());
|
2018-08-19 03:40:50 +01:00
|
|
|
let ret = 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;
|
|
|
|
|
2018-08-19 03:40:50 +01:00
|
|
|
ret
|
2017-02-15 14:52:27 -08: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 01:44:32 +03:00
|
|
|
fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
|
2018-03-21 04:24:27 -04:00
|
|
|
label.map(|label| hir::Label {
|
2018-06-10 19:33:30 +03:00
|
|
|
ident: label.ident,
|
2018-03-21 04:24:27 -04:00
|
|
|
})
|
2016-05-02 18:22:03 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
|
2018-07-20 15:59:00 +02:00
|
|
|
let target_id = match destination {
|
|
|
|
Some((id, _)) => {
|
|
|
|
if let Def::Label(loop_id) = self.expect_full_def(id) {
|
|
|
|
Ok(self.lower_node_id(loop_id).node_id)
|
|
|
|
} else {
|
|
|
|
Err(hir::LoopIdError::UnresolvedLabel)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
2018-07-20 15:59:00 +02:00
|
|
|
None => {
|
|
|
|
self.loop_scopes
|
|
|
|
.last()
|
2018-09-12 12:31:11 +02:00
|
|
|
.cloned()
|
2018-07-20 15:59:00 +02:00
|
|
|
.map(|id| Ok(self.lower_node_id(id).node_id))
|
|
|
|
.unwrap_or(Err(hir::LoopIdError::OutsideLoopScope))
|
|
|
|
.into()
|
|
|
|
}
|
2018-07-01 18:31:07 +01:00
|
|
|
};
|
|
|
|
hir::Destination {
|
|
|
|
label: self.lower_label(destination.map(|(_, label)| label)),
|
|
|
|
target_id,
|
2017-02-15 14:52:27 -08:00
|
|
|
}
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 23:11:27 +01:00
|
|
|
fn lower_attrs(&mut self, attrs: &[Attribute]) -> hir::HirVec<Attribute> {
|
2018-03-21 04:24:27 -04:00
|
|
|
attrs
|
|
|
|
.iter()
|
|
|
|
.map(|a| self.lower_attr(a))
|
2018-11-05 15:30:04 +01:00
|
|
|
.collect()
|
2017-09-15 08:28:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_attr(&mut self, attr: &Attribute) -> Attribute {
|
2018-10-11 21:15:18 +13:00
|
|
|
// Note that we explicitly do not walk the path. Since we don't really
|
|
|
|
// lower attributes (we use the AST version) there is nowhere to keep
|
2018-11-27 02:59:49 +00:00
|
|
|
// the `HirId`s. We don't actually need HIR version of attributes anyway.
|
2017-09-15 08:28:34 -07:00
|
|
|
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 {
|
2018-03-21 04:24:27 -04:00
|
|
|
tokens
|
|
|
|
.into_trees()
|
2017-09-21 08:13:25 -07:00
|
|
|
.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 {
|
2018-03-21 04:24:27 -04:00
|
|
|
TokenTree::Token(span, token) => self.lower_token(token, span),
|
2018-11-30 10:02:04 +11:00
|
|
|
TokenTree::Delimited(span, delim, tts) => TokenTree::Delimited(
|
2018-03-21 04:24:27 -04:00
|
|
|
span,
|
2018-11-30 10:02:04 +11:00
|
|
|
delim,
|
|
|
|
self.lower_token_stream(tts.into()).into(),
|
2018-03-21 04:24:27 -04: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(),
|
2018-08-30 12:18:11 +08:00
|
|
|
guard: match arm.guard {
|
|
|
|
Some(Guard::If(ref x)) => Some(hir::Guard::If(P(self.lower_expr(x)))),
|
|
|
|
_ => None,
|
|
|
|
},
|
2016-10-28 21:16:44 +11:00
|
|
|
body: P(self.lower_expr(&arm.body)),
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-08-29 22:02:42 -07:00
|
|
|
fn lower_ty_binding(&mut self, b: &TypeBinding,
|
|
|
|
itctx: ImplTraitContext<'_>) -> hir::TypeBinding {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(b.id);
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::TypeBinding {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-06-10 15:55:48 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-13 11:32:37 +00:00
|
|
|
fn lower_generic_arg(&mut self,
|
2018-05-27 20:07:09 +01:00
|
|
|
arg: &ast::GenericArg,
|
2018-08-29 22:02:42 -07:00
|
|
|
itctx: ImplTraitContext<'_>)
|
2018-02-23 17:48:54 +00:00
|
|
|
-> hir::GenericArg {
|
2018-05-27 16:56:01 +01:00
|
|
|
match arg {
|
2018-05-27 20:07:09 +01:00
|
|
|
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(<)),
|
2018-06-20 19:05:27 +02:00
|
|
|
ast::GenericArg::Type(ty) => GenericArg::Type(self.lower_ty_direct(&ty, itctx)),
|
2019-02-05 16:52:02 +01:00
|
|
|
ast::GenericArg::Const(ct) => {
|
|
|
|
// FIXME(const_generics): const generics are not yet defined in the HIR.
|
|
|
|
self.sess.struct_span_err(
|
|
|
|
ct.value.span,
|
|
|
|
"const generics in any position are currently unsupported",
|
|
|
|
).emit();
|
|
|
|
self.sess.abort_if_errors();
|
|
|
|
bug!();
|
|
|
|
}
|
2018-02-08 08:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:02:42 -07:00
|
|
|
fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext<'_>) -> P<hir::Ty> {
|
2018-06-20 19:05:27 +02:00
|
|
|
P(self.lower_ty_direct(t, itctx))
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:02:42 -07:00
|
|
|
fn lower_ty_direct(&mut self, t: &Ty, mut itctx: ImplTraitContext<'_>) -> hir::Ty {
|
2017-03-14 15:50:40 +01:00
|
|
|
let kind = match t.node {
|
2018-07-11 22:41:03 +08:00
|
|
|
TyKind::Infer => hir::TyKind::Infer,
|
|
|
|
TyKind::Err => hir::TyKind::Err,
|
|
|
|
TyKind::Slice(ref ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
|
|
|
|
TyKind::Ptr(ref mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
|
2017-03-14 15:50:40 +01:00
|
|
|
TyKind::Rptr(ref region, ref mt) => {
|
2018-10-17 21:07:31 -07:00
|
|
|
let span = self.sess.source_map().next_point(t.span.shrink_to_lo());
|
2017-03-14 15:50:40 +01:00
|
|
|
let lifetime = match *region {
|
|
|
|
Some(ref lt) => self.lower_lifetime(lt),
|
2018-03-21 16:59:28 -04:00
|
|
|
None => self.elided_ref_lifetime(span),
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Rptr(lifetime, self.lower_mt(mt, itctx))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
TyKind::BareFn(ref f) => self.with_in_scope_lifetime_defs(
|
2018-05-27 16:56:01 +01:00
|
|
|
&f.generic_params,
|
2018-03-21 04:24:27 -04:00
|
|
|
|this| {
|
2018-05-30 10:40:42 -04:00
|
|
|
this.with_anonymous_lifetime_mode(
|
|
|
|
AnonymousLifetimeMode::PassThrough,
|
|
|
|
|this| {
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::BareFn(P(hir::BareFnTy {
|
2018-05-30 10:40:42 -04:00
|
|
|
generic_params: this.lower_generic_params(
|
|
|
|
&f.generic_params,
|
2018-07-21 22:15:11 +03:00
|
|
|
&NodeMap::default(),
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::disallowed(),
|
2018-05-30 10:40:42 -04:00
|
|
|
),
|
|
|
|
unsafety: this.lower_unsafety(f.unsafety),
|
|
|
|
abi: f.abi,
|
2018-06-26 11:56:24 +02:00
|
|
|
decl: this.lower_fn_decl(&f.decl, None, false, None),
|
2018-05-30 10:40:42 -04:00
|
|
|
arg_names: this.lower_fn_args_to_names(&f.decl),
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
)
|
2018-03-21 04:24:27 -04:00
|
|
|
},
|
|
|
|
),
|
2018-07-11 22:41:03 +08:00
|
|
|
TyKind::Never => hir::TyKind::Never,
|
2017-03-14 15:50:40 +01:00
|
|
|
TyKind::Tup(ref tys) => {
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Tup(tys.iter().map(|ty| {
|
2018-06-19 13:47:53 +02:00
|
|
|
self.lower_ty_direct(ty, itctx.reborrow())
|
|
|
|
}).collect())
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
TyKind::Paren(ref ty) => {
|
2018-06-20 19:05:27 +02:00
|
|
|
return self.lower_ty_direct(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);
|
2018-02-22 22:44:44 -08:00
|
|
|
let ty = self.ty_path(id, t.span, qpath);
|
2018-07-11 22:41:03 +08:00
|
|
|
if let hir::TyKind::TraitObject(..) = ty.node {
|
2018-02-23 13:52:28 -08:00
|
|
|
self.maybe_lint_bare_trait(t.span, t.id, qself.is_none() && path.is_global());
|
2018-02-22 22:44:44 -08:00
|
|
|
}
|
|
|
|
return ty;
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
TyKind::ImplicitSelf => hir::TyKind::Path(hir::QPath::Resolved(
|
2018-03-21 04:24:27 -04:00
|
|
|
None,
|
|
|
|
P(hir::Path {
|
2017-03-14 15:50:40 +01:00
|
|
|
def: self.expect_full_def(t.id),
|
2018-12-02 03:35:55 +03:00
|
|
|
segments: hir_vec![hir::PathSegment::from_ident(keywords::SelfUpper.ident())],
|
2017-03-14 15:50:40 +01:00
|
|
|
span: t.span,
|
2018-03-21 04:24:27 -04:00
|
|
|
}),
|
|
|
|
)),
|
2017-03-14 15:50:40 +01:00
|
|
|
TyKind::Array(ref ty, ref length) => {
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Array(self.lower_ty(ty, itctx), self.lower_anon_const(length))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
TyKind::Typeof(ref expr) => {
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Typeof(self.lower_anon_const(expr))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-02-22 22:44:44 -08:00
|
|
|
TyKind::TraitObject(ref bounds, kind) => {
|
2017-03-14 15:50:40 +01:00
|
|
|
let mut lifetime_bound = None;
|
2018-03-21 04:24:27 -04:00
|
|
|
let bounds = bounds
|
|
|
|
.iter()
|
|
|
|
.filter_map(|bound| match *bound {
|
2018-06-14 12:23:46 +01:00
|
|
|
GenericBound::Trait(ref ty, TraitBoundModifier::None) => {
|
2018-06-19 13:47:53 +02:00
|
|
|
Some(self.lower_poly_trait_ref(ty, itctx.reborrow()))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-06-14 12:23:46 +01:00
|
|
|
GenericBound::Trait(_, TraitBoundModifier::Maybe) => None,
|
|
|
|
GenericBound::Outlives(ref lifetime) => {
|
2017-03-14 15:50:40 +01:00
|
|
|
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
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
})
|
|
|
|
.collect();
|
2018-03-21 16:59:28 -04:00
|
|
|
let lifetime_bound =
|
|
|
|
lifetime_bound.unwrap_or_else(|| self.elided_dyn_bound(t.span));
|
2018-02-22 22:44:44 -08:00
|
|
|
if kind != TraitObjectSyntax::Dyn {
|
2018-02-23 13:52:28 -08:00
|
|
|
self.maybe_lint_bare_trait(t.span, t.id, false);
|
2018-02-22 22:44:44 -08:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::TraitObject(bounds, lifetime_bound)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-06-20 10:44:31 +02:00
|
|
|
TyKind::ImplTrait(def_node_id, ref bounds) => {
|
2017-12-15 12:27:20 -08:00
|
|
|
let span = t.span;
|
2017-11-10 12:23:59 -05:00
|
|
|
match itctx {
|
2018-05-22 14:31:56 +02:00
|
|
|
ImplTraitContext::Existential(fn_def_id) => {
|
2018-06-06 15:50:59 -07:00
|
|
|
self.lower_existential_impl_trait(
|
2018-06-20 10:44:31 +02:00
|
|
|
span, fn_def_id, def_node_id,
|
2018-06-18 16:23:13 +02:00
|
|
|
|this| this.lower_param_bounds(bounds, itctx),
|
|
|
|
)
|
2018-03-21 04:24:27 -04:00
|
|
|
}
|
2018-06-19 13:47:53 +02:00
|
|
|
ImplTraitContext::Universal(in_band_ty_params) => {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id: _, hir_id } = self.lower_node_id(def_node_id);
|
2018-11-27 02:59:49 +00:00
|
|
|
// Add a definition for the in-band `Param`.
|
2018-06-20 10:59:24 +02:00
|
|
|
let def_index = self
|
|
|
|
.resolver
|
|
|
|
.definitions()
|
|
|
|
.opt_def_index(def_node_id)
|
|
|
|
.unwrap();
|
2017-12-15 12:27:20 -08:00
|
|
|
|
2018-06-19 13:47:53 +02:00
|
|
|
let hir_bounds = self.lower_param_bounds(
|
|
|
|
bounds,
|
|
|
|
ImplTraitContext::Universal(in_band_ty_params),
|
|
|
|
);
|
2018-11-27 02:59:49 +00:00
|
|
|
// Set the name to `impl Bound1 + Bound2`.
|
2018-06-10 19:33:30 +03:00
|
|
|
let ident = Ident::from_str(&pprust::ty_to_string(t)).with_span_pos(span);
|
2018-06-19 13:47:53 +02:00
|
|
|
in_band_ty_params.push(hir::GenericParam {
|
2017-12-15 12:27:20 -08:00
|
|
|
id: def_node_id,
|
2019-02-02 15:40:08 +01:00
|
|
|
hir_id,
|
2018-06-28 00:12:17 +03:00
|
|
|
name: ParamName::Plain(ident),
|
2017-12-15 12:27:20 -08:00
|
|
|
pure_wrt_drop: false,
|
2018-06-14 11:42:12 +01:00
|
|
|
attrs: hir_vec![],
|
2018-05-28 13:33:28 +01:00
|
|
|
bounds: hir_bounds,
|
2018-06-28 00:12:17 +03:00
|
|
|
span,
|
2018-05-26 00:27:54 +01:00
|
|
|
kind: hir::GenericParamKind::Type {
|
|
|
|
default: None,
|
|
|
|
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
|
|
|
|
}
|
2017-12-15 12:27:20 -08:00
|
|
|
});
|
|
|
|
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Path(hir::QPath::Resolved(
|
2018-03-21 04:24:27 -04:00
|
|
|
None,
|
|
|
|
P(hir::Path {
|
|
|
|
span,
|
2018-08-22 02:13:31 +01:00
|
|
|
def: Def::TyParam(DefId::local(def_index)),
|
2018-06-10 17:40:45 +03:00
|
|
|
segments: hir_vec![hir::PathSegment::from_ident(ident)],
|
2018-03-21 04:24:27 -04:00
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::Disallowed(pos) => {
|
2018-09-08 07:52:03 +01:00
|
|
|
let allowed_in = if self.sess.features_untracked()
|
|
|
|
.impl_trait_in_bindings {
|
|
|
|
"bindings or function and inherent method return types"
|
|
|
|
} else {
|
|
|
|
"function and inherent method return types"
|
|
|
|
};
|
2018-09-27 22:34:19 +01:00
|
|
|
let mut err = struct_span_err!(
|
2018-03-21 04:24:27 -04:00
|
|
|
self.sess,
|
|
|
|
t.span,
|
|
|
|
E0562,
|
2018-09-08 07:52:03 +01:00
|
|
|
"`impl Trait` not allowed outside of {}",
|
|
|
|
allowed_in,
|
2018-03-21 04:24:27 -04:00
|
|
|
);
|
2018-09-27 22:34:19 +01:00
|
|
|
if pos == ImplTraitPosition::Binding &&
|
|
|
|
nightly_options::is_nightly_build() {
|
|
|
|
help!(err,
|
|
|
|
"add #![feature(impl_trait_in_bindings)] to the crate attributes \
|
|
|
|
to enable");
|
|
|
|
}
|
|
|
|
err.emit();
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::TyKind::Err
|
2017-11-10 12:23:59 -05:00
|
|
|
}
|
|
|
|
}
|
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);
|
2018-06-20 19:05:27 +02:00
|
|
|
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,
|
2018-06-20 19:05:27 +02:00
|
|
|
}
|
2015-12-07 17:17:41 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
fn lower_existential_impl_trait(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
2018-08-19 03:40:50 +01:00
|
|
|
fn_def_id: Option<DefId>,
|
2018-06-18 16:23:13 +02:00
|
|
|
exist_ty_node_id: NodeId,
|
2018-08-29 22:02:42 -07:00
|
|
|
lower_bounds: impl FnOnce(&mut LoweringContext<'_>) -> hir::GenericBounds,
|
2018-07-11 22:41:03 +08:00
|
|
|
) -> hir::TyKind {
|
2018-06-06 15:50:59 -07:00
|
|
|
// Make sure we know that some funky desugaring has been going on here.
|
|
|
|
// This is a first: there is code in other places like for loop
|
|
|
|
// desugaring that explicitly states that we don't want to track that.
|
|
|
|
// Not tracking it makes lints in rustc and clippy very fragile as
|
|
|
|
// frequently opened issues show.
|
2019-02-04 11:24:55 +01:00
|
|
|
let exist_ty_span = self.mark_span_with_reason(
|
2018-06-06 15:50:59 -07:00
|
|
|
CompilerDesugaringKind::ExistentialReturnType,
|
|
|
|
span,
|
2019-02-07 14:19:06 +01:00
|
|
|
None,
|
2018-06-06 15:50:59 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
let exist_ty_def_index = self
|
|
|
|
.resolver
|
|
|
|
.definitions()
|
2018-06-20 10:59:24 +02:00
|
|
|
.opt_def_index(exist_ty_node_id)
|
|
|
|
.unwrap();
|
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
self.allocate_hir_id_counter(exist_ty_node_id, &"existential impl trait");
|
|
|
|
|
|
|
|
let hir_bounds = self.with_hir_id_owner(exist_ty_node_id, lower_bounds);
|
|
|
|
|
|
|
|
let (lifetimes, lifetime_defs) = self.lifetimes_from_impl_trait_bounds(
|
|
|
|
exist_ty_node_id,
|
|
|
|
exist_ty_def_index,
|
|
|
|
&hir_bounds,
|
|
|
|
);
|
|
|
|
|
|
|
|
self.with_hir_id_owner(exist_ty_node_id, |lctx| {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = lctx.next_id();
|
2018-07-11 23:36:06 +08:00
|
|
|
let exist_ty_item_kind = hir::ItemKind::Existential(hir::ExistTy {
|
2018-06-06 15:50:59 -07:00
|
|
|
generics: hir::Generics {
|
|
|
|
params: lifetime_defs,
|
|
|
|
where_clause: hir::WhereClause {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-06-06 15:50:59 -07:00
|
|
|
predicates: Vec::new().into(),
|
|
|
|
},
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
bounds: hir_bounds,
|
2018-08-19 03:40:50 +01:00
|
|
|
impl_trait_fn: fn_def_id,
|
2018-06-06 15:50:59 -07:00
|
|
|
});
|
|
|
|
let exist_ty_id = lctx.lower_node_id(exist_ty_node_id);
|
2018-11-27 02:59:49 +00:00
|
|
|
// Generate an `existential type Foo: Trait;` declaration.
|
2018-06-06 15:50:59 -07:00
|
|
|
trace!("creating existential type with id {:#?}", exist_ty_id);
|
|
|
|
|
|
|
|
trace!("exist ty def index: {:#?}", exist_ty_def_index);
|
|
|
|
let exist_ty_item = hir::Item {
|
|
|
|
id: exist_ty_id.node_id,
|
|
|
|
hir_id: exist_ty_id.hir_id,
|
2018-12-01 02:47:08 +00:00
|
|
|
ident: keywords::Invalid.ident(),
|
2018-06-06 15:50:59 -07:00
|
|
|
attrs: Default::default(),
|
|
|
|
node: exist_ty_item_kind,
|
2018-07-01 11:05:10 -07:00
|
|
|
vis: respan(span.shrink_to_lo(), hir::VisibilityKind::Inherited),
|
2018-06-06 15:50:59 -07:00
|
|
|
span: exist_ty_span,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Insert the item into the global list. This usually happens
|
|
|
|
// automatically for all AST items. But this existential type item
|
|
|
|
// does not actually exist in the AST.
|
2018-06-06 22:13:52 +02:00
|
|
|
lctx.insert_item(exist_ty_id.node_id, exist_ty_item);
|
2018-06-06 15:50:59 -07:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
|
2018-10-02 10:54:34 +02:00
|
|
|
hir::TyKind::Def(hir::ItemId { id: exist_ty_id.node_id }, lifetimes)
|
2018-06-06 15:50:59 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-15 13:43:06 -07:00
|
|
|
fn lifetimes_from_impl_trait_bounds(
|
|
|
|
&mut self,
|
2018-05-22 14:31:56 +02:00
|
|
|
exist_ty_id: NodeId,
|
2017-10-15 13:43:06 -07:00
|
|
|
parent_index: DefIndex,
|
2018-06-14 12:08:58 +01:00
|
|
|
bounds: &hir::GenericBounds,
|
2018-06-29 10:58:17 +02:00
|
|
|
) -> (HirVec<hir::GenericArg>, HirVec<hir::GenericParam>) {
|
2017-10-15 13:43:06 -07:00
|
|
|
// 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.
|
2018-11-27 02:59:49 +00:00
|
|
|
// E.g., `'a`, `'b`, but not `'c` in `impl for<'c> SomeTrait<'a, 'b, 'c>`.
|
2017-10-15 13:43:06 -07:00
|
|
|
struct ImplTraitLifetimeCollector<'r, 'a: 'r> {
|
|
|
|
context: &'r mut LoweringContext<'a>,
|
|
|
|
parent: DefIndex,
|
2018-05-22 14:31:56 +02:00
|
|
|
exist_ty_id: NodeId,
|
2017-12-09 21:54:58 -08:00
|
|
|
collect_elided_lifetimes: bool,
|
|
|
|
currently_bound_lifetimes: Vec<hir::LifetimeName>,
|
2018-08-18 13:55:43 +03:00
|
|
|
already_defined_lifetimes: FxHashSet<hir::LifetimeName>,
|
2018-06-29 10:58:17 +02:00
|
|
|
output_lifetimes: Vec<hir::GenericArg>,
|
2017-10-16 21:07:26 +02:00
|
|
|
output_lifetime_params: Vec<hir::GenericParam>,
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'r, 'a: 'r, 'v> hir::intravisit::Visitor<'v> for ImplTraitLifetimeCollector<'r, 'a> {
|
2018-03-21 04:24:27 -04:00
|
|
|
fn nested_visit_map<'this>(
|
|
|
|
&'this mut self,
|
|
|
|
) -> hir::intravisit::NestedVisitorMap<'this, 'v> {
|
2017-10-15 13:43:06 -07:00
|
|
|
hir::intravisit::NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
|
2018-02-13 11:32:37 +00:00
|
|
|
fn visit_generic_args(&mut self, span: Span, parameters: &'v hir::GenericArgs) {
|
2017-12-09 21:54:58 -08:00
|
|
|
// 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;
|
2018-02-13 11:32:37 +00:00
|
|
|
hir::intravisit::walk_generic_args(self, span, parameters);
|
2017-12-09 21:54:58 -08:00
|
|
|
self.collect_elided_lifetimes = old_collect_elided_lifetimes;
|
|
|
|
} else {
|
2018-02-13 11:32:37 +00:00
|
|
|
hir::intravisit::walk_generic_args(self, span, parameters);
|
2017-12-09 21:54:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_ty(&mut self, t: &'v hir::Ty) {
|
2018-11-27 02:59:49 +00:00
|
|
|
// Don't collect elided lifetimes used inside of `fn()` syntax.
|
2018-07-11 22:41:03 +08:00
|
|
|
if let hir::TyKind::BareFn(_) = t.node {
|
2017-12-09 21:54:58 -08:00
|
|
|
let old_collect_elided_lifetimes = self.collect_elided_lifetimes;
|
|
|
|
self.collect_elided_lifetimes = false;
|
2018-05-30 14:36:53 +03:00
|
|
|
|
|
|
|
// Record the "stack height" of `for<'a>` lifetime bindings
|
|
|
|
// to be able to later fully undo their introduction.
|
|
|
|
let old_len = self.currently_bound_lifetimes.len();
|
2017-12-09 21:54:58 -08:00
|
|
|
hir::intravisit::walk_ty(self, t);
|
2018-05-30 14:36:53 +03:00
|
|
|
self.currently_bound_lifetimes.truncate(old_len);
|
|
|
|
|
2017-12-09 21:54:58 -08:00
|
|
|
self.collect_elided_lifetimes = old_collect_elided_lifetimes;
|
|
|
|
} else {
|
2018-06-27 11:24:16 +02:00
|
|
|
hir::intravisit::walk_ty(self, t)
|
2017-12-09 21:54:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn visit_poly_trait_ref(
|
|
|
|
&mut self,
|
2018-05-30 14:36:53 +03:00
|
|
|
trait_ref: &'v hir::PolyTraitRef,
|
|
|
|
modifier: hir::TraitBoundModifier,
|
2018-03-21 04:24:27 -04:00
|
|
|
) {
|
2018-05-30 14:36:53 +03:00
|
|
|
// Record the "stack height" of `for<'a>` lifetime bindings
|
|
|
|
// to be able to later fully undo their introduction.
|
2017-10-15 13:43:06 -07:00
|
|
|
let old_len = self.currently_bound_lifetimes.len();
|
2018-05-30 14:36:53 +03:00
|
|
|
hir::intravisit::walk_poly_trait_ref(self, trait_ref, modifier);
|
|
|
|
self.currently_bound_lifetimes.truncate(old_len);
|
|
|
|
}
|
2017-10-15 13:43:06 -07:00
|
|
|
|
2018-05-30 14:36:53 +03:00
|
|
|
fn visit_generic_param(&mut self, param: &'v hir::GenericParam) {
|
2018-11-27 02:59:49 +00:00
|
|
|
// Record the introduction of 'a in `for<'a> ...`.
|
2018-06-01 23:23:48 +01:00
|
|
|
if let hir::GenericParamKind::Lifetime { .. } = param.kind {
|
2018-05-30 14:36:53 +03:00
|
|
|
// Introduce lifetimes one at a time so that we can handle
|
2018-11-27 02:59:49 +00:00
|
|
|
// cases like `fn foo<'d>() -> impl for<'a, 'b: 'a, 'c: 'b + 'd>`.
|
2018-06-01 23:23:48 +01:00
|
|
|
let lt_name = hir::LifetimeName::Param(param.name);
|
2018-05-28 14:55:23 +01:00
|
|
|
self.currently_bound_lifetimes.push(lt_name);
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:36:53 +03:00
|
|
|
hir::intravisit::walk_generic_param(self, param);
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
|
2017-12-09 21:54:58 -08:00
|
|
|
let name = match lifetime.name {
|
2018-03-21 04:24:27 -04:00
|
|
|
hir::LifetimeName::Implicit | hir::LifetimeName::Underscore => {
|
2017-12-09 21:54:58 -08:00
|
|
|
if self.collect_elided_lifetimes {
|
|
|
|
// Use `'_` for both implicit and underscore lifetimes in
|
2018-11-27 02:59:49 +00:00
|
|
|
// `abstract type Foo<'_>: SomeTrait<'_>;`.
|
2017-12-09 21:54:58 -08:00
|
|
|
hir::LifetimeName::Underscore
|
|
|
|
} else {
|
2018-03-21 04:24:27 -04:00
|
|
|
return;
|
2017-12-09 21:54:58 -08:00
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
}
|
2018-06-01 23:23:48 +01:00
|
|
|
hir::LifetimeName::Param(_) => lifetime.name,
|
2018-10-11 15:51:44 -04:00
|
|
|
hir::LifetimeName::Error | hir::LifetimeName::Static => return,
|
2017-12-09 21:54:58 -08:00
|
|
|
};
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
if !self.currently_bound_lifetimes.contains(&name)
|
2018-06-01 23:23:48 +01:00
|
|
|
&& !self.already_defined_lifetimes.contains(&name) {
|
2017-12-09 21:54:58 -08:00
|
|
|
self.already_defined_lifetimes.insert(name);
|
|
|
|
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.context.next_id();
|
2018-06-29 10:58:17 +02:00
|
|
|
self.output_lifetimes.push(hir::GenericArg::Lifetime(hir::Lifetime {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2017-12-09 21:54:58 -08:00
|
|
|
span: lifetime.span,
|
|
|
|
name,
|
2018-06-29 10:58:17 +02:00
|
|
|
}));
|
2017-12-09 21:54:58 -08:00
|
|
|
|
2018-05-22 14:31:56 +02:00
|
|
|
// We need to manually create the ids here, because the
|
|
|
|
// definitions will go into the explicit `existential type`
|
|
|
|
// declaration and thus need to have their owner set to that item
|
|
|
|
let def_node_id = self.context.sess.next_node_id();
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id: _, hir_id } =
|
|
|
|
self.context.lower_node_id_with_owner(def_node_id, self.exist_ty_id);
|
2017-12-09 21:54:58 -08:00
|
|
|
self.context.resolver.definitions().create_def_with_parent(
|
|
|
|
self.parent,
|
|
|
|
def_node_id,
|
2018-06-09 23:25:33 +03:00
|
|
|
DefPathData::LifetimeParam(name.ident().as_interned_str()),
|
2017-12-09 21:54:58 -08:00
|
|
|
DefIndexAddressSpace::High,
|
2018-02-17 13:22:58 +01:00
|
|
|
Mark::root(),
|
2018-03-21 04:24:27 -04:00
|
|
|
lifetime.span,
|
2017-12-09 21:54:58 -08:00
|
|
|
);
|
2018-05-26 00:27:54 +01:00
|
|
|
|
2018-09-22 01:45:42 -07:00
|
|
|
let (name, kind) = match name {
|
|
|
|
hir::LifetimeName::Underscore => (
|
|
|
|
hir::ParamName::Plain(keywords::UnderscoreLifetime.ident()),
|
|
|
|
hir::LifetimeParamKind::Elided,
|
|
|
|
),
|
|
|
|
hir::LifetimeName::Param(param_name) => (
|
|
|
|
param_name,
|
|
|
|
hir::LifetimeParamKind::Explicit,
|
|
|
|
),
|
2018-06-01 23:23:48 +01:00
|
|
|
_ => bug!("expected LifetimeName::Param or ParamName::Plain"),
|
|
|
|
};
|
|
|
|
|
2018-05-26 00:27:54 +01:00
|
|
|
self.output_lifetime_params.push(hir::GenericParam {
|
2017-12-09 21:54:58 -08:00
|
|
|
id: def_node_id,
|
2019-02-02 15:40:08 +01:00
|
|
|
hir_id,
|
2018-06-01 23:23:48 +01:00
|
|
|
name,
|
2017-12-09 21:54:58 -08:00
|
|
|
span: lifetime.span,
|
2018-05-26 00:27:54 +01:00
|
|
|
pure_wrt_drop: false,
|
2018-06-14 11:42:12 +01:00
|
|
|
attrs: hir_vec![],
|
|
|
|
bounds: hir_vec![],
|
2018-09-22 01:45:42 -07:00
|
|
|
kind: hir::GenericParamKind::Lifetime { kind }
|
2018-05-26 00:27:54 +01:00
|
|
|
});
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut lifetime_collector = ImplTraitLifetimeCollector {
|
|
|
|
context: self,
|
|
|
|
parent: parent_index,
|
2018-05-22 14:31:56 +02:00
|
|
|
exist_ty_id,
|
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(),
|
2018-08-18 13:55:43 +03:00
|
|
|
already_defined_lifetimes: FxHashSet::default(),
|
2017-10-15 13:43:06 -07:00
|
|
|
output_lifetimes: Vec::new(),
|
2017-10-16 21:07:26 +02:00
|
|
|
output_lifetime_params: Vec::new(),
|
2017-10-15 13:43:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
for bound in bounds {
|
2018-05-28 15:23:16 +01:00
|
|
|
hir::intravisit::walk_param_bound(&mut lifetime_collector, &bound);
|
2017-10-15 13:43:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
(
|
|
|
|
lifetime_collector.output_lifetimes.into(),
|
2018-03-21 04:24:27 -04:00
|
|
|
lifetime_collector.output_lifetime_params.into(),
|
2017-10-15 13:43:06 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_foreign_mod(&mut self, fm: &ForeignMod) -> hir::ForeignMod {
|
|
|
|
hir::ForeignMod {
|
|
|
|
abi: fm.abi,
|
2018-03-21 04:24:27 -04:00
|
|
|
items: fm.items
|
|
|
|
.iter()
|
|
|
|
.map(|x| self.lower_foreign_item(x))
|
|
|
|
.collect(),
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
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 {
|
2018-07-11 19:18:57 +08:00
|
|
|
node: hir::VariantKind {
|
2018-12-01 02:47:08 +00:00
|
|
|
ident: v.node.ident,
|
2016-05-10 01:11:59 +00:00
|
|
|
attrs: self.lower_attrs(&v.node.attrs),
|
|
|
|
data: self.lower_variant_data(&v.node.data),
|
2018-05-17 21:28:50 +03:00
|
|
|
disr_expr: v.node.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
|
2016-05-10 01:11:59 +00:00
|
|
|
},
|
|
|
|
span: v.span,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_qpath(
|
|
|
|
&mut self,
|
|
|
|
id: NodeId,
|
|
|
|
qself: &Option<QSelf>,
|
|
|
|
p: &Path,
|
|
|
|
param_mode: ParamMode,
|
2018-08-29 22:02:42 -07:00
|
|
|
mut itctx: ImplTraitContext<'_>,
|
2018-03-21 04:24:27 -04:00
|
|
|
) -> hir::QPath {
|
2016-10-27 05:17:42 +03:00
|
|
|
let qself_position = qself.as_ref().map(|q| q.position);
|
2018-06-19 13:47:53 +02:00
|
|
|
let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx.reborrow()));
|
2016-10-27 05:17:42 +03:00
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
let resolution = self.resolver
|
|
|
|
.get_resolution(id)
|
2018-10-12 16:16:00 +02:00
|
|
|
.unwrap_or_else(|| PathResolution::new(Def::Err));
|
2016-10-27 05:17:42 +03:00
|
|
|
|
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(),
|
2018-03-21 04:24:27 -04: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 => {
|
|
|
|
// 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
|
|
|
|
2018-03-21 04:24:27 -04: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 {
|
2017-01-09 17:46:11 +02:00
|
|
|
krate: def_id.krate,
|
2018-03-21 04:24:27 -04:00
|
|
|
index: this.def_key(def_id).parent.expect("missing parent"),
|
|
|
|
};
|
|
|
|
let type_def_id = match resolution.base_def() {
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
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
|
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Avoid duplicated errors.
|
2018-03-21 04:24:27 -04:00
|
|
|
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
|
|
|
|
2018-03-21 04:24:27 -04: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());
|
2018-02-25 00:31:26 +00:00
|
|
|
let item_generics =
|
|
|
|
self.cstore.item_generics_cloned_untracked(def_id, self.sess);
|
2018-02-13 11:32:37 +00:00
|
|
|
let n = item_generics.own_counts().lifetimes;
|
2018-03-21 04:24:27 -04:00
|
|
|
self.type_def_lifetime_params.insert(def_id, n);
|
|
|
|
n
|
|
|
|
});
|
|
|
|
self.lower_path_segment(
|
|
|
|
p.span,
|
|
|
|
segment,
|
|
|
|
param_mode,
|
|
|
|
num_lifetimes,
|
|
|
|
parenthesized_generic_args,
|
2018-06-19 13:47:53 +02:00
|
|
|
itctx.reborrow(),
|
2018-10-11 21:15:18 +13:00
|
|
|
None,
|
2018-03-21 04:24:27 -04: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.
|
2018-11-27 02:59:49 +00:00
|
|
|
// 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
|
2018-11-27 02:59:49 +00:00
|
|
|
// syntactical `Self`, e.g., `&i32` in `<&i32>::clone`.
|
2016-10-27 05:17:42 +03:00
|
|
|
qself.expect("missing QSelf for <T>::...")
|
|
|
|
} else {
|
|
|
|
// Otherwise, the base path is an implicit `Self` type path,
|
2018-11-27 02:59:49 +00:00
|
|
|
// e.g., `Vec` in `Vec::new` or `<I as Iterator>::Item` in
|
2016-10-27 05:17:42 +03:00
|
|
|
// `<I as Iterator>::Item::default`.
|
2017-09-13 18:29:59 +03:00
|
|
|
let new_id = self.next_id();
|
2018-06-20 19:05:27 +02:00
|
|
|
P(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,
|
2018-11-27 02:59:49 +00:00
|
|
|
// e.g., for `std::vec::Vec::<T>::IntoIter::Item::clone`:
|
2016-10-27 05:17:42 +03:00
|
|
|
// * 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) {
|
2018-03-21 04:24:27 -04:00
|
|
|
let segment = P(self.lower_path_segment(
|
|
|
|
p.span,
|
|
|
|
segment,
|
|
|
|
param_mode,
|
|
|
|
0,
|
|
|
|
ParenthesizedGenericArgs::Warn,
|
2018-06-19 13:47:53 +02:00
|
|
|
itctx.reborrow(),
|
2018-10-11 21:15:18 +13:00
|
|
|
None,
|
2018-03-21 04:24:27 -04:00
|
|
|
));
|
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();
|
2018-06-20 19:05:27 +02:00
|
|
|
ty = P(self.ty_path(new_id, p.span, qpath));
|
2016-10-27 05:17:42 +03:00
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// We should've returned in the for loop above.
|
2018-03-21 04:24:27 -04:00
|
|
|
span_bug!(
|
|
|
|
p.span,
|
|
|
|
"lower_qpath: no final extension segment in {}..{}",
|
|
|
|
proj_start,
|
|
|
|
p.segments.len()
|
|
|
|
)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_path_extra(
|
|
|
|
&mut self,
|
2018-06-13 11:44:06 -05:00
|
|
|
def: Def,
|
2018-03-21 04:24:27 -04:00
|
|
|
p: &Path,
|
|
|
|
param_mode: ParamMode,
|
2018-10-11 21:15:18 +13:00
|
|
|
explicit_owner: Option<NodeId>,
|
2018-03-21 04:24:27 -04:00
|
|
|
) -> hir::Path {
|
2016-10-27 05:17:42 +03:00
|
|
|
hir::Path {
|
2018-06-13 11:44:06 -05:00
|
|
|
def,
|
2018-03-21 04:24:27 -04:00
|
|
|
segments: p.segments
|
|
|
|
.iter()
|
|
|
|
.map(|segment| {
|
|
|
|
self.lower_path_segment(
|
|
|
|
p.span,
|
|
|
|
segment,
|
|
|
|
param_mode,
|
|
|
|
0,
|
|
|
|
ParenthesizedGenericArgs::Err,
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::disallowed(),
|
2018-10-11 21:15:18 +13:00
|
|
|
explicit_owner,
|
2018-03-21 04:24:27 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
2016-10-27 05:17:42 +03:00
|
|
|
span: p.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_path(&mut self, id: NodeId, p: &Path, param_mode: ParamMode) -> hir::Path {
|
2018-06-13 11:44:06 -05:00
|
|
|
let def = self.expect_full_def(id);
|
2018-10-31 11:05:58 +13:00
|
|
|
self.lower_path_extra(def, p, param_mode, None)
|
2016-11-24 06:11:31 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_path_segment(
|
|
|
|
&mut self,
|
|
|
|
path_span: Span,
|
|
|
|
segment: &PathSegment,
|
|
|
|
param_mode: ParamMode,
|
|
|
|
expected_lifetimes: usize,
|
|
|
|
parenthesized_generic_args: ParenthesizedGenericArgs,
|
2018-08-29 22:02:42 -07:00
|
|
|
itctx: ImplTraitContext<'_>,
|
2018-10-11 21:15:18 +13:00
|
|
|
explicit_owner: Option<NodeId>,
|
2018-03-21 04:24:27 -04:00
|
|
|
) -> hir::PathSegment {
|
2018-02-23 17:48:54 +00:00
|
|
|
let (mut generic_args, infer_types) = if let Some(ref generic_args) = segment.args {
|
2019-01-20 02:45:38 -08:00
|
|
|
let msg = "parenthesized type parameters may only be used with a `Fn` trait";
|
2018-02-13 11:32:37 +00:00
|
|
|
match **generic_args {
|
|
|
|
GenericArgs::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
|
|
|
}
|
2018-02-13 11:32:37 +00:00
|
|
|
GenericArgs::Parenthesized(ref data) => match parenthesized_generic_args {
|
2018-03-21 04:24:27 -04:00
|
|
|
ParenthesizedGenericArgs::Ok => self.lower_parenthesized_parameter_data(data),
|
2017-07-29 01:13:40 +03:00
|
|
|
ParenthesizedGenericArgs::Warn => {
|
2018-03-21 04:24:27 -04:00
|
|
|
self.sess.buffer_lint(
|
|
|
|
PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
|
|
|
|
CRATE_NODE_ID,
|
|
|
|
data.span,
|
|
|
|
msg.into(),
|
|
|
|
);
|
2018-02-13 11:32:37 +00:00
|
|
|
(hir::GenericArgs::none(), true)
|
2017-07-29 01:13:40 +03:00
|
|
|
}
|
|
|
|
ParenthesizedGenericArgs::Err => {
|
2019-01-19 19:25:28 -08:00
|
|
|
let mut err = struct_span_err!(self.sess, data.span, E0214, "{}", msg);
|
2019-01-20 02:45:38 -08:00
|
|
|
err.span_label(data.span, "only `Fn` traits may use parentheses");
|
2019-01-19 19:25:28 -08:00
|
|
|
if let Ok(snippet) = self.sess.source_map().span_to_snippet(data.span) {
|
2019-01-20 02:45:38 -08:00
|
|
|
// Do not suggest going from `Trait()` to `Trait<>`
|
|
|
|
if data.inputs.len() > 0 {
|
2019-01-25 16:03:27 -05:00
|
|
|
err.span_suggestion(
|
2019-01-20 02:45:38 -08:00
|
|
|
data.span,
|
|
|
|
"use angle brackets instead",
|
|
|
|
format!("<{}>", &snippet[1..snippet.len() - 1]),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
2019-01-19 19:25:28 -08:00
|
|
|
};
|
|
|
|
err.emit();
|
2019-01-19 18:44:20 -08:00
|
|
|
(self.lower_angle_bracketed_parameter_data(
|
|
|
|
&data.as_angle_bracketed_args(),
|
|
|
|
param_mode,
|
|
|
|
itctx).0,
|
|
|
|
false)
|
2017-07-29 01:13:40 +03:00
|
|
|
}
|
2018-03-21 04:24:27 -04: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
|
|
|
};
|
|
|
|
|
2018-05-27 01:43:03 +01:00
|
|
|
let has_lifetimes = generic_args.args.iter().any(|arg| match arg {
|
|
|
|
GenericArg::Lifetime(_) => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
2018-07-17 00:30:53 -07:00
|
|
|
let first_generic_span = generic_args.args.iter().map(|a| a.span())
|
|
|
|
.chain(generic_args.bindings.iter().map(|b| b.span)).next();
|
2018-05-27 01:43:03 +01:00
|
|
|
if !generic_args.parenthesized && !has_lifetimes {
|
2018-02-23 17:48:54 +00:00
|
|
|
generic_args.args =
|
|
|
|
self.elided_path_lifetimes(path_span, expected_lifetimes)
|
|
|
|
.into_iter()
|
|
|
|
.map(|lt| GenericArg::Lifetime(lt))
|
|
|
|
.chain(generic_args.args.into_iter())
|
2018-07-17 00:30:53 -07:00
|
|
|
.collect();
|
|
|
|
if expected_lifetimes > 0 && param_mode == ParamMode::Explicit {
|
|
|
|
let anon_lt_suggestion = vec!["'_"; expected_lifetimes].join(", ");
|
|
|
|
let no_ty_args = generic_args.args.len() == expected_lifetimes;
|
|
|
|
let no_bindings = generic_args.bindings.is_empty();
|
|
|
|
let (incl_angl_brckt, insertion_span, suggestion) = if no_ty_args && no_bindings {
|
|
|
|
// If there are no (non-implicit) generic args or associated-type
|
2018-11-27 02:59:49 +00:00
|
|
|
// bindings, our suggestion includes the angle brackets.
|
2018-07-17 00:30:53 -07:00
|
|
|
(true, path_span.shrink_to_hi(), format!("<{}>", anon_lt_suggestion))
|
|
|
|
} else {
|
|
|
|
// Otherwise—sorry, this is kind of gross—we need to infer the
|
2018-11-27 02:59:49 +00:00
|
|
|
// place to splice in the `'_, ` from the generics that do exist.
|
2018-07-17 00:30:53 -07:00
|
|
|
let first_generic_span = first_generic_span
|
|
|
|
.expect("already checked that type args or bindings exist");
|
|
|
|
(false, first_generic_span.shrink_to_lo(), format!("{}, ", anon_lt_suggestion))
|
|
|
|
};
|
|
|
|
self.sess.buffer_lint_with_diagnostic(
|
|
|
|
ELIDED_LIFETIMES_IN_PATHS,
|
|
|
|
CRATE_NODE_ID,
|
|
|
|
path_span,
|
|
|
|
"hidden lifetime parameters in types are deprecated",
|
|
|
|
builtin::BuiltinLintDiagnostics::ElidedLifetimesInPaths(
|
|
|
|
expected_lifetimes, path_span, incl_angl_brckt, insertion_span, suggestion
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-01-09 17:46:11 +02:00
|
|
|
}
|
|
|
|
|
2018-09-06 19:24:33 +12:00
|
|
|
let def = self.expect_full_def(segment.id);
|
2018-10-11 21:15:18 +13:00
|
|
|
let id = if let Some(owner) = explicit_owner {
|
|
|
|
self.lower_node_id_with_owner(segment.id, owner)
|
|
|
|
} else {
|
|
|
|
self.lower_node_id(segment.id)
|
|
|
|
};
|
2018-11-21 13:35:54 -05:00
|
|
|
debug!(
|
|
|
|
"lower_path_segment: ident={:?} original-id={:?} new-id={:?}",
|
|
|
|
segment.ident, segment.id, id,
|
|
|
|
);
|
2018-10-11 21:15:18 +13:00
|
|
|
|
2017-09-21 23:24:26 +03:00
|
|
|
hir::PathSegment::new(
|
2018-06-10 17:40:45 +03:00
|
|
|
segment.ident,
|
2018-10-11 21:15:18 +13:00
|
|
|
Some(id.node_id),
|
2019-02-02 15:40:08 +01:00
|
|
|
Some(id.hir_id),
|
2018-09-06 19:24:33 +12:00
|
|
|
Some(def),
|
2018-02-13 11:32:37 +00:00
|
|
|
generic_args,
|
2018-05-16 12:57:45 +01:00
|
|
|
infer_types,
|
2017-09-21 23:24:26 +03:00
|
|
|
)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_angle_bracketed_parameter_data(
|
|
|
|
&mut self,
|
2018-02-23 17:48:54 +00:00
|
|
|
data: &AngleBracketedArgs,
|
2018-03-21 04:24:27 -04:00
|
|
|
param_mode: ParamMode,
|
2018-08-29 22:02:42 -07:00
|
|
|
mut itctx: ImplTraitContext<'_>,
|
2018-02-13 11:32:37 +00:00
|
|
|
) -> (hir::GenericArgs, bool) {
|
2018-02-23 17:48:54 +00:00
|
|
|
let &AngleBracketedArgs { ref args, ref bindings, .. } = data;
|
2018-05-26 23:54:48 +01:00
|
|
|
let has_types = args.iter().any(|arg| match arg {
|
2018-05-27 20:07:09 +01:00
|
|
|
ast::GenericArg::Type(_) => true,
|
2018-05-26 23:54:48 +01:00
|
|
|
_ => false,
|
|
|
|
});
|
2018-02-13 11:32:37 +00:00
|
|
|
(hir::GenericArgs {
|
2018-06-19 13:47:53 +02:00
|
|
|
args: args.iter().map(|a| self.lower_generic_arg(a, itctx.reborrow())).collect(),
|
|
|
|
bindings: bindings.iter().map(|b| self.lower_ty_binding(b, itctx.reborrow())).collect(),
|
2018-02-08 08:58:13 +00:00
|
|
|
parenthesized: false,
|
|
|
|
},
|
2018-05-27 01:43:03 +01:00
|
|
|
!has_types && param_mode == ParamMode::Optional)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_parenthesized_parameter_data(
|
|
|
|
&mut self,
|
2019-01-21 08:09:56 +01:00
|
|
|
data: &ParenthesizedArgs,
|
2018-05-31 15:52:17 +01:00
|
|
|
) -> (hir::GenericArgs, bool) {
|
2018-05-30 10:27:53 -04:00
|
|
|
// Switch to `PassThrough` mode for anonymous lifetimes: this
|
|
|
|
// means that we permit things like `&Ref<T>`, where `Ref` has
|
|
|
|
// a hidden lifetime parameter. This is needed for backwards
|
|
|
|
// compatibility, even in contexts like an impl header where
|
|
|
|
// we generally don't permit such things (see #51008).
|
|
|
|
self.with_anonymous_lifetime_mode(
|
|
|
|
AnonymousLifetimeMode::PassThrough,
|
|
|
|
|this| {
|
2019-01-21 08:09:56 +01:00
|
|
|
let &ParenthesizedArgs { ref inputs, ref output, span } = data;
|
2018-09-27 22:34:19 +01:00
|
|
|
let inputs = inputs
|
|
|
|
.iter()
|
|
|
|
.map(|ty| this.lower_ty_direct(ty, ImplTraitContext::disallowed()))
|
|
|
|
.collect();
|
2018-05-30 10:27:53 -04:00
|
|
|
let mk_tup = |this: &mut Self, tys, span| {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = this.next_id();
|
2018-07-11 22:41:03 +08:00
|
|
|
hir::Ty { node: hir::TyKind::Tup(tys), id: node_id, hir_id, span }
|
2018-05-30 10:27:53 -04:00
|
|
|
};
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = this.next_id();
|
2017-07-29 01:13:40 +03:00
|
|
|
|
2018-05-30 10:27:53 -04:00
|
|
|
(
|
2018-02-13 11:32:37 +00:00
|
|
|
hir::GenericArgs {
|
2018-05-31 15:52:17 +01:00
|
|
|
args: hir_vec![GenericArg::Type(mk_tup(this, inputs, span))],
|
2018-05-30 10:27:53 -04:00
|
|
|
bindings: hir_vec![
|
|
|
|
hir::TypeBinding {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-06-10 15:55:48 +03:00
|
|
|
ident: Ident::from_str(FN_OUTPUT_NAME),
|
2018-05-30 10:27:53 -04:00
|
|
|
ty: output
|
|
|
|
.as_ref()
|
2018-09-27 22:34:19 +01:00
|
|
|
.map(|ty| this.lower_ty(&ty, ImplTraitContext::disallowed()))
|
2018-06-20 19:05:27 +02:00
|
|
|
.unwrap_or_else(|| P(mk_tup(this, hir::HirVec::new(), span))),
|
2018-05-30 10:27:53 -04:00
|
|
|
span: output.as_ref().map_or(span, |ty| ty.span),
|
|
|
|
}
|
|
|
|
],
|
|
|
|
parenthesized: true,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
)
|
2015-10-06 16:03:56 +13:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-01-17 10:39:24 +11:00
|
|
|
fn lower_local(&mut self, l: &Local) -> (hir::Local, SmallVec<[hir::ItemId; 1]>) {
|
2017-08-07 14:43:43 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(l.id);
|
2018-08-30 11:42:16 +02:00
|
|
|
let mut ids = SmallVec::<[hir::ItemId; 1]>::new();
|
2018-08-19 03:40:50 +01:00
|
|
|
if self.sess.features_untracked().impl_trait_in_bindings {
|
|
|
|
if let Some(ref ty) = l.ty {
|
|
|
|
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
|
|
|
|
visitor.visit_ty(ty);
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 04:25:23 +01:00
|
|
|
let parent_def_id = DefId::local(self.current_hir_id_owner.last().unwrap().0);
|
2019-01-17 10:39:24 +11:00
|
|
|
(hir::Local {
|
2017-08-07 14:43:43 +02:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-03-21 04:24:27 -04:00
|
|
|
ty: l.ty
|
|
|
|
.as_ref()
|
2018-08-19 03:40:50 +01:00
|
|
|
.map(|t| self.lower_ty(t,
|
|
|
|
if self.sess.features_untracked().impl_trait_in_bindings {
|
2018-09-24 04:25:23 +01:00
|
|
|
ImplTraitContext::Existential(Some(parent_def_id))
|
2018-08-19 03:40:50 +01:00
|
|
|
} else {
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
|
2018-08-19 03:40:50 +01:00
|
|
|
}
|
|
|
|
)),
|
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,
|
2019-01-17 10:39:24 +11:00
|
|
|
}, ids)
|
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
|
|
|
|
2018-06-10 19:33:30 +03:00
|
|
|
fn lower_fn_args_to_names(&mut self, decl: &FnDecl) -> hir::HirVec<Ident> {
|
2018-03-21 04:24:27 -04:00
|
|
|
decl.inputs
|
|
|
|
.iter()
|
|
|
|
.map(|arg| match arg.pat.node {
|
2018-06-10 19:33:30 +03:00
|
|
|
PatKind::Ident(_, ident, _) => ident,
|
|
|
|
_ => Ident::new(keywords::Invalid.name(), arg.pat.span),
|
2018-03-21 04:24:27 -04:00
|
|
|
})
|
|
|
|
.collect()
|
2016-12-20 22:46:11 +02:00
|
|
|
}
|
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
// Lowers a function declaration.
|
|
|
|
//
|
|
|
|
// decl: the unlowered (ast) function declaration.
|
|
|
|
// fn_def_id: if `Some`, impl Trait arguments are lowered into generic parameters on the
|
|
|
|
// given DefId, otherwise impl Trait is disallowed. Must be `Some` if
|
2018-06-26 11:56:24 +02:00
|
|
|
// make_ret_async is also `Some`.
|
2018-06-06 15:50:59 -07:00
|
|
|
// impl_trait_return_allow: determines whether impl Trait can be used in return position.
|
|
|
|
// This guards against trait declarations and implementations where impl Trait is
|
|
|
|
// disallowed.
|
2018-06-26 11:56:24 +02:00
|
|
|
// make_ret_async: if `Some`, converts `-> T` into `-> impl Future<Output = T>` in the
|
2018-06-27 11:24:50 +02:00
|
|
|
// return type. This is used for `async fn` declarations. The `NodeId` is the id of the
|
|
|
|
// return type impl Trait item.
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_fn_decl(
|
|
|
|
&mut self,
|
|
|
|
decl: &FnDecl,
|
2018-06-19 13:47:53 +02:00
|
|
|
mut in_band_ty_params: Option<(DefId, &mut Vec<hir::GenericParam>)>,
|
2018-03-21 04:24:27 -04:00
|
|
|
impl_trait_return_allow: bool,
|
2018-06-26 11:56:24 +02:00
|
|
|
make_ret_async: Option<NodeId>,
|
2018-03-21 04:24:27 -04:00
|
|
|
) -> P<hir::FnDecl> {
|
2018-06-06 15:50:59 -07:00
|
|
|
let inputs = decl.inputs
|
|
|
|
.iter()
|
|
|
|
.map(|arg| {
|
2018-06-19 13:47:53 +02:00
|
|
|
if let Some((_, ref mut ibty)) = in_band_ty_params {
|
|
|
|
self.lower_ty_direct(&arg.ty, ImplTraitContext::Universal(ibty))
|
2018-06-06 15:50:59 -07:00
|
|
|
} else {
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_ty_direct(&arg.ty, ImplTraitContext::disallowed())
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<HirVec<_>>();
|
|
|
|
|
2018-06-26 11:56:24 +02:00
|
|
|
let output = if let Some(ret_id) = make_ret_async {
|
2018-06-06 15:50:59 -07:00
|
|
|
self.lower_async_fn_ret_ty(
|
2018-06-19 13:47:53 +02:00
|
|
|
&inputs,
|
|
|
|
&decl.output,
|
|
|
|
in_band_ty_params.expect("make_ret_async but no fn_def_id").0,
|
|
|
|
ret_id,
|
|
|
|
)
|
2018-06-06 15:50:59 -07:00
|
|
|
} else {
|
|
|
|
match decl.output {
|
2018-06-19 13:47:53 +02:00
|
|
|
FunctionRetTy::Ty(ref ty) => match in_band_ty_params {
|
|
|
|
Some((def_id, _)) if impl_trait_return_allow => {
|
2018-09-27 22:34:19 +01:00
|
|
|
hir::Return(self.lower_ty(ty,
|
|
|
|
ImplTraitContext::Existential(Some(def_id))))
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
hir::Return(self.lower_ty(ty, ImplTraitContext::disallowed()))
|
2018-03-21 04:24:27 -04:00
|
|
|
}
|
2017-11-10 12:23:59 -05:00
|
|
|
},
|
2016-05-10 01:11:59 +00:00
|
|
|
FunctionRetTy::Default(span) => hir::DefaultReturn(span),
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
P(hir::FnDecl {
|
|
|
|
inputs,
|
|
|
|
output,
|
2016-05-10 01:11:59 +00:00
|
|
|
variadic: decl.variadic,
|
2018-10-01 17:46:04 +02:00
|
|
|
implicit_self: decl.inputs.get(0).map_or(
|
|
|
|
hir::ImplicitSelfKind::None,
|
|
|
|
|arg| {
|
|
|
|
let is_mutable_pat = match arg.pat.node {
|
|
|
|
PatKind::Ident(BindingMode::ByValue(mt), _, _) |
|
|
|
|
PatKind::Ident(BindingMode::ByRef(mt), _, _) =>
|
|
|
|
mt == Mutability::Mutable,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
match arg.ty.node {
|
|
|
|
TyKind::ImplicitSelf if is_mutable_pat => hir::ImplicitSelfKind::Mut,
|
|
|
|
TyKind::ImplicitSelf => hir::ImplicitSelfKind::Imm,
|
|
|
|
// Given we are only considering `ImplicitSelf` types, we needn't consider
|
|
|
|
// the case where we have a mutable pattern to a reference as that would
|
|
|
|
// no longer be an `ImplicitSelf`.
|
|
|
|
TyKind::Rptr(_, ref mt) if mt.ty.node.is_implicit_self() &&
|
|
|
|
mt.mutbl == ast::Mutability::Mutable =>
|
|
|
|
hir::ImplicitSelfKind::MutRef,
|
|
|
|
TyKind::Rptr(_, ref mt) if mt.ty.node.is_implicit_self() =>
|
|
|
|
hir::ImplicitSelfKind::ImmRef,
|
|
|
|
_ => hir::ImplicitSelfKind::None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2016-05-10 01:11:59 +00:00
|
|
|
})
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2018-06-06 15:50:59 -07:00
|
|
|
// Transform `-> T` into `-> impl Future<Output = T>` for `async fn`
|
|
|
|
//
|
|
|
|
// fn_span: the span of the async function declaration. Used for error reporting.
|
|
|
|
// inputs: lowered types of arguments to the function. Used to collect lifetimes.
|
|
|
|
// output: unlowered output type (`T` in `-> T`)
|
|
|
|
// fn_def_id: DefId of the parent function. Used to create child impl trait definition.
|
|
|
|
fn lower_async_fn_ret_ty(
|
|
|
|
&mut self,
|
2018-06-20 19:05:27 +02:00
|
|
|
inputs: &[hir::Ty],
|
2018-06-06 15:50:59 -07:00
|
|
|
output: &FunctionRetTy,
|
|
|
|
fn_def_id: DefId,
|
2018-06-26 11:56:24 +02:00
|
|
|
return_impl_trait_id: NodeId,
|
2018-06-06 15:50:59 -07:00
|
|
|
) -> hir::FunctionRetTy {
|
|
|
|
// Get lifetimes used in the input arguments to the function. Our output type must also
|
2018-11-27 02:59:49 +00:00
|
|
|
// have the same lifetime.
|
|
|
|
// FIXME(cramertj): multiple different lifetimes are not allowed because
|
|
|
|
// `impl Trait + 'a + 'b` doesn't allow for capture `'a` and `'b` where neither is a subset
|
|
|
|
// of the other. We really want some new lifetime that is a subset of all input lifetimes,
|
|
|
|
// but that doesn't exist at the moment.
|
2018-06-06 15:50:59 -07:00
|
|
|
|
|
|
|
struct AsyncFnLifetimeCollector<'r, 'a: 'r> {
|
|
|
|
context: &'r mut LoweringContext<'a>,
|
2018-11-27 02:59:49 +00:00
|
|
|
// Lifetimes bound by HRTB.
|
2018-06-06 15:50:59 -07:00
|
|
|
currently_bound_lifetimes: Vec<hir::LifetimeName>,
|
|
|
|
// Whether to count elided lifetimes.
|
|
|
|
// Disabled inside of `Fn` or `fn` syntax.
|
|
|
|
collect_elided_lifetimes: bool,
|
|
|
|
// The lifetime found.
|
|
|
|
// Multiple different or elided lifetimes cannot appear in async fn for now.
|
|
|
|
output_lifetime: Option<(hir::LifetimeName, Span)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'r, 'a: 'r, 'v> hir::intravisit::Visitor<'v> for AsyncFnLifetimeCollector<'r, 'a> {
|
|
|
|
fn nested_visit_map<'this>(
|
|
|
|
&'this mut self,
|
|
|
|
) -> hir::intravisit::NestedVisitorMap<'this, 'v> {
|
|
|
|
hir::intravisit::NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
|
2018-06-21 23:24:51 -07:00
|
|
|
fn visit_generic_args(&mut self, span: Span, parameters: &'v hir::GenericArgs) {
|
2018-06-06 15:50:59 -07:00
|
|
|
// 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;
|
2018-06-21 23:24:51 -07:00
|
|
|
hir::intravisit::walk_generic_args(self, span, parameters);
|
2018-06-06 15:50:59 -07:00
|
|
|
self.collect_elided_lifetimes = old_collect_elided_lifetimes;
|
|
|
|
} else {
|
2018-06-21 23:24:51 -07:00
|
|
|
hir::intravisit::walk_generic_args(self, span, parameters);
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_ty(&mut self, t: &'v hir::Ty) {
|
2018-11-27 02:59:49 +00:00
|
|
|
// Don't collect elided lifetimes used inside of `fn()` syntax.
|
2018-07-11 22:41:03 +08:00
|
|
|
if let &hir::TyKind::BareFn(_) = &t.node {
|
2018-06-06 15:50:59 -07:00
|
|
|
let old_collect_elided_lifetimes = self.collect_elided_lifetimes;
|
|
|
|
self.collect_elided_lifetimes = false;
|
|
|
|
|
|
|
|
// Record the "stack height" of `for<'a>` lifetime bindings
|
|
|
|
// to be able to later fully undo their introduction.
|
|
|
|
let old_len = self.currently_bound_lifetimes.len();
|
|
|
|
hir::intravisit::walk_ty(self, t);
|
|
|
|
self.currently_bound_lifetimes.truncate(old_len);
|
|
|
|
|
|
|
|
self.collect_elided_lifetimes = old_collect_elided_lifetimes;
|
|
|
|
} else {
|
|
|
|
hir::intravisit::walk_ty(self, t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_poly_trait_ref(
|
|
|
|
&mut self,
|
|
|
|
trait_ref: &'v hir::PolyTraitRef,
|
|
|
|
modifier: hir::TraitBoundModifier,
|
|
|
|
) {
|
|
|
|
// Record the "stack height" of `for<'a>` lifetime bindings
|
|
|
|
// to be able to later fully undo their introduction.
|
|
|
|
let old_len = self.currently_bound_lifetimes.len();
|
|
|
|
hir::intravisit::walk_poly_trait_ref(self, trait_ref, modifier);
|
|
|
|
self.currently_bound_lifetimes.truncate(old_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_generic_param(&mut self, param: &'v hir::GenericParam) {
|
2018-06-21 23:24:51 -07:00
|
|
|
// Record the introduction of 'a in `for<'a> ...`
|
|
|
|
if let hir::GenericParamKind::Lifetime { .. } = param.kind {
|
2018-06-06 15:50:59 -07:00
|
|
|
// Introduce lifetimes one at a time so that we can handle
|
|
|
|
// cases like `fn foo<'d>() -> impl for<'a, 'b: 'a, 'c: 'b + 'd>`
|
2018-06-21 23:24:51 -07:00
|
|
|
let lt_name = hir::LifetimeName::Param(param.name);
|
|
|
|
self.currently_bound_lifetimes.push(lt_name);
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
hir::intravisit::walk_generic_param(self, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 23:24:51 -07:00
|
|
|
hir::LifetimeName::Param(_) => lifetime.name,
|
2018-10-11 15:51:44 -04:00
|
|
|
hir::LifetimeName::Error | hir::LifetimeName::Static => return,
|
2018-06-06 15:50:59 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if !self.currently_bound_lifetimes.contains(&name) {
|
|
|
|
if let Some((current_lt_name, current_lt_span)) = self.output_lifetime {
|
|
|
|
// We don't currently have a reliable way to desugar `async fn` with
|
|
|
|
// multiple potentially unrelated input lifetimes into
|
|
|
|
// `-> impl Trait + 'lt`, so we report an error in this case.
|
|
|
|
if current_lt_name != name {
|
|
|
|
struct_span_err!(
|
|
|
|
self.context.sess,
|
2018-07-22 12:19:34 -07:00
|
|
|
MultiSpan::from_spans(vec![current_lt_span, lifetime.span]),
|
2018-06-23 20:14:04 +02:00
|
|
|
E0709,
|
2018-06-06 15:50:59 -07:00
|
|
|
"multiple different lifetimes used in arguments of `async fn`",
|
|
|
|
)
|
|
|
|
.span_label(current_lt_span, "first lifetime here")
|
|
|
|
.span_label(lifetime.span, "different lifetime here")
|
|
|
|
.help("`async fn` can only accept borrowed values \
|
2018-06-18 16:57:14 -07:00
|
|
|
with identical lifetimes")
|
2018-06-06 15:50:59 -07:00
|
|
|
.emit()
|
|
|
|
} else if current_lt_name.is_elided() && name.is_elided() {
|
|
|
|
struct_span_err!(
|
|
|
|
self.context.sess,
|
2018-07-22 12:19:34 -07:00
|
|
|
MultiSpan::from_spans(vec![current_lt_span, lifetime.span]),
|
2018-06-23 20:14:04 +02:00
|
|
|
E0707,
|
2018-06-06 15:50:59 -07:00
|
|
|
"multiple elided lifetimes used in arguments of `async fn`",
|
|
|
|
)
|
|
|
|
.span_label(current_lt_span, "first lifetime here")
|
|
|
|
.span_label(lifetime.span, "different lifetime here")
|
|
|
|
.help("consider giving these arguments named lifetimes")
|
|
|
|
.emit()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.output_lifetime = Some((name, lifetime.span));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let bound_lifetime = {
|
|
|
|
let mut lifetime_collector = AsyncFnLifetimeCollector {
|
|
|
|
context: self,
|
|
|
|
currently_bound_lifetimes: Vec::new(),
|
|
|
|
collect_elided_lifetimes: true,
|
|
|
|
output_lifetime: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
for arg in inputs {
|
|
|
|
hir::intravisit::walk_ty(&mut lifetime_collector, arg);
|
|
|
|
}
|
|
|
|
lifetime_collector.output_lifetime
|
|
|
|
};
|
|
|
|
|
2018-06-22 16:08:07 -07:00
|
|
|
let span = match output {
|
|
|
|
FunctionRetTy::Ty(ty) => ty.span,
|
|
|
|
FunctionRetTy::Default(span) => *span,
|
2018-06-06 15:50:59 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let impl_trait_ty = self.lower_existential_impl_trait(
|
2018-08-19 03:40:50 +01:00
|
|
|
span, Some(fn_def_id), return_impl_trait_id, |this| {
|
2018-06-06 15:50:59 -07:00
|
|
|
let output_ty = match output {
|
2018-09-08 07:52:03 +01:00
|
|
|
FunctionRetTy::Ty(ty) => {
|
2018-09-24 04:25:23 +01:00
|
|
|
this.lower_ty(ty, ImplTraitContext::Existential(Some(fn_def_id)))
|
2018-09-08 07:52:03 +01:00
|
|
|
}
|
2018-06-06 15:50:59 -07:00
|
|
|
FunctionRetTy::Default(span) => {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = this.next_id();
|
|
|
|
P(hir::Ty {
|
|
|
|
id: node_id,
|
2019-02-02 15:40:08 +01:00
|
|
|
hir_id,
|
2018-07-11 22:41:03 +08:00
|
|
|
node: hir::TyKind::Tup(hir_vec![]),
|
2018-06-06 15:50:59 -07:00
|
|
|
span: *span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-21 22:24:24 -07:00
|
|
|
// "<Output = T>"
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = this.next_id();
|
2018-06-21 23:24:51 -07:00
|
|
|
let future_params = P(hir::GenericArgs {
|
|
|
|
args: hir_vec![],
|
2018-06-21 22:24:24 -07:00
|
|
|
bindings: hir_vec![hir::TypeBinding {
|
2018-06-28 00:12:17 +03:00
|
|
|
ident: Ident::from_str(FN_OUTPUT_NAME),
|
2018-06-21 22:24:24 -07:00
|
|
|
ty: output_ty,
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-06-21 22:24:24 -07:00
|
|
|
span,
|
|
|
|
}],
|
|
|
|
parenthesized: false,
|
|
|
|
});
|
|
|
|
|
2018-06-21 23:24:51 -07:00
|
|
|
let future_path =
|
2018-06-21 22:24:24 -07:00
|
|
|
this.std_path(span, &["future", "Future"], Some(future_params), false);
|
2018-06-06 15:50:59 -07:00
|
|
|
|
2018-07-31 10:43:51 -06:00
|
|
|
let LoweredNodeId { node_id, hir_id } = this.next_id();
|
2018-06-06 15:50:59 -07:00
|
|
|
let mut bounds = vec![
|
2018-06-21 23:24:51 -07:00
|
|
|
hir::GenericBound::Trait(
|
2018-06-06 15:50:59 -07:00
|
|
|
hir::PolyTraitRef {
|
|
|
|
trait_ref: hir::TraitRef {
|
|
|
|
path: future_path,
|
2018-07-31 10:43:51 -06:00
|
|
|
ref_id: node_id,
|
|
|
|
hir_ref_id: hir_id,
|
2018-06-06 15:50:59 -07:00
|
|
|
},
|
|
|
|
bound_generic_params: hir_vec![],
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
hir::TraitBoundModifier::None
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
if let Some((name, span)) = bound_lifetime {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = this.next_id();
|
2018-06-21 23:24:51 -07:00
|
|
|
bounds.push(hir::GenericBound::Outlives(
|
2019-02-02 15:40:08 +01:00
|
|
|
hir::Lifetime { id: node_id, hir_id, name, span }));
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
hir::HirVec::from(bounds)
|
|
|
|
});
|
|
|
|
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
|
|
|
let impl_trait_ty = P(hir::Ty {
|
|
|
|
id: node_id,
|
|
|
|
node: impl_trait_ty,
|
|
|
|
span,
|
|
|
|
hir_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
hir::FunctionRetTy::Return(impl_trait_ty)
|
|
|
|
}
|
|
|
|
|
2018-05-28 13:33:28 +01:00
|
|
|
fn lower_param_bound(
|
2018-03-21 04:24:27 -04:00
|
|
|
&mut self,
|
2018-06-14 12:08:58 +01:00
|
|
|
tpb: &GenericBound,
|
2018-08-29 22:02:42 -07:00
|
|
|
itctx: ImplTraitContext<'_>,
|
2018-06-14 12:08:58 +01:00
|
|
|
) -> hir::GenericBound {
|
2016-05-10 01:11:59 +00:00
|
|
|
match *tpb {
|
2018-10-11 15:51:44 -04:00
|
|
|
GenericBound::Trait(ref ty, modifier) => {
|
|
|
|
hir::GenericBound::Trait(
|
|
|
|
self.lower_poly_trait_ref(ty, itctx),
|
|
|
|
self.lower_trait_bound_modifier(modifier),
|
2018-10-11 14:05:10 -04:00
|
|
|
)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2018-06-14 12:08:58 +01:00
|
|
|
GenericBound::Outlives(ref lifetime) => {
|
|
|
|
hir::GenericBound::Outlives(self.lower_lifetime(lifetime))
|
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_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
|
2018-03-19 03:54:56 +03:00
|
|
|
let span = l.ident.span;
|
2018-06-28 00:12:17 +03:00
|
|
|
match l.ident {
|
2018-06-09 23:25:33 +03:00
|
|
|
ident if ident.name == keywords::StaticLifetime.name() =>
|
|
|
|
self.new_named_lifetime(l.id, span, hir::LifetimeName::Static),
|
|
|
|
ident if ident.name == keywords::UnderscoreLifetime.name() =>
|
|
|
|
match self.anonymous_lifetime_mode {
|
|
|
|
AnonymousLifetimeMode::CreateParameter => {
|
|
|
|
let fresh_name = self.collect_fresh_in_band_lifetime(span);
|
|
|
|
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(fresh_name))
|
|
|
|
}
|
2018-03-21 17:12:39 -04:00
|
|
|
|
2018-06-09 23:25:33 +03:00
|
|
|
AnonymousLifetimeMode::PassThrough => {
|
|
|
|
self.new_named_lifetime(l.id, span, hir::LifetimeName::Underscore)
|
|
|
|
}
|
2018-10-11 15:51:44 -04:00
|
|
|
|
|
|
|
AnonymousLifetimeMode::ReportError => self.new_error_lifetime(Some(l.id), span),
|
2018-06-09 23:25:33 +03:00
|
|
|
},
|
|
|
|
ident => {
|
|
|
|
self.maybe_collect_in_band_lifetime(ident);
|
|
|
|
let param_name = ParamName::Plain(ident);
|
2018-06-01 23:23:48 +01:00
|
|
|
self.new_named_lifetime(l.id, span, hir::LifetimeName::Param(param_name))
|
2017-11-16 22:59:45 -08:00
|
|
|
}
|
2018-03-21 17:12:39 -04:00
|
|
|
}
|
|
|
|
}
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
fn new_named_lifetime(
|
|
|
|
&mut self,
|
|
|
|
id: NodeId,
|
|
|
|
span: Span,
|
|
|
|
name: hir::LifetimeName,
|
|
|
|
) -> hir::Lifetime {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(id);
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::Lifetime {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-03-21 17:12:39 -04:00
|
|
|
span,
|
|
|
|
name: name,
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 16:56:01 +01:00
|
|
|
fn lower_generic_params(
|
|
|
|
&mut self,
|
2018-07-27 16:50:28 +02:00
|
|
|
params: &[GenericParam],
|
2018-06-14 12:08:58 +01:00
|
|
|
add_bounds: &NodeMap<Vec<GenericBound>>,
|
2018-08-29 22:02:42 -07:00
|
|
|
mut itctx: ImplTraitContext<'_>,
|
2018-05-27 16:56:01 +01:00
|
|
|
) -> hir::HirVec<hir::GenericParam> {
|
2018-06-19 13:47:53 +02:00
|
|
|
params.iter().map(|param| {
|
|
|
|
self.lower_generic_param(param, add_bounds, itctx.reborrow())
|
|
|
|
}).collect()
|
2018-05-27 16:56:01 +01:00
|
|
|
}
|
|
|
|
|
2018-05-26 00:27:54 +01:00
|
|
|
fn lower_generic_param(&mut self,
|
2018-05-27 20:07:09 +01:00
|
|
|
param: &GenericParam,
|
2018-06-14 12:08:58 +01:00
|
|
|
add_bounds: &NodeMap<Vec<GenericBound>>,
|
2018-08-29 22:02:42 -07:00
|
|
|
mut itctx: ImplTraitContext<'_>)
|
2018-05-26 00:27:54 +01:00
|
|
|
-> hir::GenericParam {
|
2018-10-11 15:51:44 -04:00
|
|
|
let mut bounds = self.with_anonymous_lifetime_mode(
|
|
|
|
AnonymousLifetimeMode::ReportError,
|
|
|
|
|this| this.lower_param_bounds(¶m.bounds, itctx.reborrow()),
|
|
|
|
);
|
|
|
|
|
2019-02-05 16:52:02 +01:00
|
|
|
let (name, kind) = match param.kind {
|
2018-05-30 16:49:39 +01:00
|
|
|
GenericParamKind::Lifetime => {
|
2018-05-26 00:27:54 +01:00
|
|
|
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
|
|
|
|
self.is_collecting_in_band_lifetimes = false;
|
|
|
|
|
2018-10-11 15:51:44 -04:00
|
|
|
let lt = self.with_anonymous_lifetime_mode(
|
|
|
|
AnonymousLifetimeMode::ReportError,
|
|
|
|
|this| this.lower_lifetime(&Lifetime { id: param.id, ident: param.ident }),
|
|
|
|
);
|
2018-06-01 23:23:48 +01:00
|
|
|
let param_name = match lt.name {
|
|
|
|
hir::LifetimeName::Param(param_name) => param_name,
|
2018-10-11 15:51:44 -04:00
|
|
|
hir::LifetimeName::Implicit
|
|
|
|
| hir::LifetimeName::Underscore
|
|
|
|
| hir::LifetimeName::Static => hir::ParamName::Plain(lt.name.ident()),
|
|
|
|
hir::LifetimeName::Error => ParamName::Error,
|
2018-06-01 23:23:48 +01:00
|
|
|
};
|
2019-02-05 16:52:02 +01:00
|
|
|
|
|
|
|
let kind = hir::GenericParamKind::Lifetime {
|
|
|
|
kind: hir::LifetimeParamKind::Explicit
|
2018-05-26 00:27:54 +01:00
|
|
|
};
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2018-05-26 00:27:54 +01:00
|
|
|
self.is_collecting_in_band_lifetimes = was_collecting_in_band;
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2019-02-05 16:52:02 +01:00
|
|
|
(param_name, kind)
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
2018-05-28 13:33:28 +01:00
|
|
|
GenericParamKind::Type { ref default, .. } => {
|
2018-05-26 00:27:54 +01: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`.
|
2018-11-27 02:59:49 +00:00
|
|
|
// Instead, use `gensym("Self")` to create a distinct name that looks the same.
|
2018-12-02 03:35:55 +03:00
|
|
|
let ident = if param.ident.name == keywords::SelfUpper.name() {
|
2018-06-28 00:12:17 +03:00
|
|
|
param.ident.gensym()
|
|
|
|
} else {
|
|
|
|
param.ident
|
|
|
|
};
|
2018-05-26 00:27:54 +01:00
|
|
|
|
2018-05-26 19:16:21 +01:00
|
|
|
let add_bounds = add_bounds.get(¶m.id).map_or(&[][..], |x| &x);
|
2018-05-26 00:27:54 +01:00
|
|
|
if !add_bounds.is_empty() {
|
2018-06-19 13:47:53 +02:00
|
|
|
let params = self.lower_param_bounds(add_bounds, itctx.reborrow()).into_iter();
|
2018-05-27 16:56:01 +01:00
|
|
|
bounds = bounds.into_iter()
|
2018-06-19 13:47:53 +02:00
|
|
|
.chain(params)
|
2018-05-27 16:56:01 +01:00
|
|
|
.collect();
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2019-02-05 16:52:02 +01:00
|
|
|
let kind = hir::GenericParamKind::Type {
|
|
|
|
default: default.as_ref().map(|x| {
|
|
|
|
self.lower_ty(x, ImplTraitContext::disallowed())
|
|
|
|
}),
|
|
|
|
synthetic: param.attrs.iter()
|
|
|
|
.filter(|attr| attr.check_name("rustc_synthetic"))
|
|
|
|
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
|
|
|
|
.next(),
|
|
|
|
};
|
|
|
|
|
|
|
|
(hir::ParamName::Plain(ident), kind)
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
2019-02-05 16:52:02 +01:00
|
|
|
GenericParamKind::Const { .. } => {
|
|
|
|
// FIXME(const_generics): const generics are not yet defined in the HIR.
|
|
|
|
self.sess.struct_span_err(
|
|
|
|
param.ident.span,
|
|
|
|
"const generics in any position are currently unsupported",
|
|
|
|
).emit();
|
|
|
|
self.sess.abort_if_errors();
|
|
|
|
bug!();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(param.id);
|
|
|
|
|
|
|
|
hir::GenericParam {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
|
|
|
name,
|
|
|
|
span: param.ident.span,
|
|
|
|
pure_wrt_drop: attr::contains_name(¶m.attrs, "may_dangle"),
|
|
|
|
attrs: self.lower_attrs(¶m.attrs),
|
|
|
|
bounds,
|
|
|
|
kind,
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
2015-10-25 18:33:51 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2018-05-26 19:16:21 +01:00
|
|
|
fn lower_generics(
|
|
|
|
&mut self,
|
|
|
|
generics: &Generics,
|
2018-08-29 22:02:42 -07:00
|
|
|
itctx: ImplTraitContext<'_>)
|
2018-05-26 19:16:21 +01:00
|
|
|
-> hir::Generics
|
|
|
|
{
|
2016-10-22 03:33:36 +03:00
|
|
|
// Collect `?Trait` bounds in where clause and move them to parameter definitions.
|
2018-11-27 02:59:49 +00:00
|
|
|
// FIXME: this could probably be done with less rightward drift. Also looks like two control
|
2017-10-16 21:07:26 +02:00
|
|
|
// paths where report_error is called are also the only paths that advance to after
|
|
|
|
// the match statement, so the error reporting could probably just be moved there.
|
2018-07-21 22:15:11 +03:00
|
|
|
let mut add_bounds: NodeMap<Vec<_>> = Default::default();
|
2018-05-26 19:16:21 +01:00
|
|
|
for pred in &generics.where_clause.predicates {
|
2016-10-22 03:33:36 +03:00
|
|
|
if let WherePredicate::BoundPredicate(ref bound_pred) = *pred {
|
|
|
|
'next_bound: for bound in &bound_pred.bounds {
|
2018-06-14 12:08:58 +01:00
|
|
|
if let GenericBound::Trait(_, TraitBoundModifier::Maybe) = *bound {
|
2016-10-22 03:33:36 +03:00
|
|
|
let report_error = |this: &mut Self| {
|
2018-03-21 04:24:27 -04:00
|
|
|
this.diagnostic().span_err(
|
|
|
|
bound_pred.bounded_ty.span,
|
|
|
|
"`?Trait` bounds are only permitted at the \
|
|
|
|
point where a type parameter is declared",
|
|
|
|
);
|
2016-10-22 03:33:36 +03:00
|
|
|
};
|
|
|
|
// Check if the where clause type is a plain type parameter.
|
|
|
|
match bound_pred.bounded_ty.node {
|
|
|
|
TyKind::Path(None, ref path)
|
2018-03-21 04:24:27 -04:00
|
|
|
if path.segments.len() == 1
|
|
|
|
&& bound_pred.bound_generic_params.is_empty() =>
|
|
|
|
{
|
2018-08-22 02:13:31 +01:00
|
|
|
if let Some(Def::TyParam(def_id)) = self.resolver
|
2018-03-21 04:24:27 -04:00
|
|
|
.get_resolution(bound_pred.bounded_ty.id)
|
|
|
|
.map(|d| d.base_def())
|
|
|
|
{
|
2016-10-22 03:33:36 +03:00
|
|
|
if let Some(node_id) =
|
2018-03-21 04:24:27 -04:00
|
|
|
self.resolver.definitions().as_local_node_id(def_id)
|
|
|
|
{
|
2018-05-26 19:16:21 +01:00
|
|
|
for param in &generics.params {
|
|
|
|
match param.kind {
|
2018-05-27 20:07:09 +01:00
|
|
|
GenericParamKind::Type { .. } => {
|
2018-05-26 19:16:21 +01:00
|
|
|
if node_id == param.id {
|
|
|
|
add_bounds.entry(param.id)
|
2018-07-21 22:43:31 +03:00
|
|
|
.or_default()
|
2018-05-26 19:16:21 +01:00
|
|
|
.push(bound.clone());
|
|
|
|
continue 'next_bound;
|
|
|
|
}
|
2017-10-16 21:07:26 +02:00
|
|
|
}
|
2018-05-26 19:16:21 +01:00
|
|
|
_ => {}
|
2016-10-22 03:33:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
report_error(self)
|
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
_ => report_error(self),
|
2016-10-22 03:33:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::Generics {
|
2018-05-26 19:16:21 +01:00
|
|
|
params: self.lower_generic_params(&generics.params, &add_bounds, itctx),
|
|
|
|
where_clause: self.lower_where_clause(&generics.where_clause),
|
|
|
|
span: generics.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 {
|
2018-10-11 15:51:44 -04:00
|
|
|
self.with_anonymous_lifetime_mode(
|
|
|
|
AnonymousLifetimeMode::ReportError,
|
|
|
|
|this| {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(wc.id);
|
|
|
|
|
2018-10-11 15:51:44 -04:00
|
|
|
hir::WhereClause {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-10-11 15:51:44 -04:00
|
|
|
predicates: wc.predicates
|
|
|
|
.iter()
|
|
|
|
.map(|predicate| this.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 {
|
2018-03-21 04:24:27 -04:00
|
|
|
WherePredicate::BoundPredicate(WhereBoundPredicate {
|
|
|
|
ref bound_generic_params,
|
|
|
|
ref bounded_ty,
|
|
|
|
ref bounds,
|
|
|
|
span,
|
|
|
|
}) => {
|
2017-10-16 21:07:26 +02:00
|
|
|
self.with_in_scope_lifetime_defs(
|
2018-05-27 16:56:01 +01:00
|
|
|
&bound_generic_params,
|
2017-10-16 21:07:26 +02:00
|
|
|
|this| {
|
|
|
|
hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
|
2018-03-21 04:24:27 -04:00
|
|
|
bound_generic_params: this.lower_generic_params(
|
|
|
|
bound_generic_params,
|
2018-07-21 22:15:11 +03:00
|
|
|
&NodeMap::default(),
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::disallowed(),
|
2018-03-21 04:24:27 -04:00
|
|
|
),
|
2018-09-27 22:34:19 +01:00
|
|
|
bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::disallowed()),
|
2018-03-21 04:24:27 -04:00
|
|
|
bounds: bounds
|
|
|
|
.iter()
|
|
|
|
.filter_map(|bound| match *bound {
|
|
|
|
// Ignore `?Trait` bounds.
|
2018-11-27 02:59:49 +00:00
|
|
|
// They were copied into type parameters already.
|
2018-06-14 12:08:58 +01:00
|
|
|
GenericBound::Trait(_, TraitBoundModifier::Maybe) => None,
|
2018-05-28 13:33:28 +01:00
|
|
|
_ => Some(this.lower_param_bound(
|
2018-03-21 04:24:27 -04:00
|
|
|
bound,
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::disallowed(),
|
2018-03-21 04:24:27 -04:00
|
|
|
)),
|
|
|
|
})
|
|
|
|
.collect(),
|
2017-10-16 21:07:26 +02:00
|
|
|
span,
|
|
|
|
})
|
2018-03-21 04:24:27 -04:00
|
|
|
},
|
2017-10-16 21:07:26 +02:00
|
|
|
)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
WherePredicate::RegionPredicate(WhereRegionPredicate {
|
|
|
|
ref lifetime,
|
|
|
|
ref bounds,
|
|
|
|
span,
|
|
|
|
}) => hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
|
|
|
|
span,
|
|
|
|
lifetime: self.lower_lifetime(lifetime),
|
2018-09-27 22:34:19 +01:00
|
|
|
bounds: self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
|
2018-03-21 04:24:27 -04:00
|
|
|
}),
|
|
|
|
WherePredicate::EqPredicate(WhereEqPredicate {
|
|
|
|
id,
|
|
|
|
ref lhs_ty,
|
|
|
|
ref rhs_ty,
|
|
|
|
span,
|
2019-02-02 15:40:08 +01:00
|
|
|
}) => {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(id);
|
|
|
|
|
|
|
|
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
|
|
|
lhs_ty: self.lower_ty(lhs_ty, ImplTraitContext::disallowed()),
|
|
|
|
rhs_ty: self.lower_ty(rhs_ty, ImplTraitContext::disallowed()),
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
},
|
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 {
|
2019-02-02 15:40:08 +01:00
|
|
|
VariantData::Struct(ref fields, id) => {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(id);
|
|
|
|
|
|
|
|
hir::VariantData::Struct(
|
|
|
|
fields
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|f| self.lower_struct_field(f))
|
|
|
|
.collect(),
|
|
|
|
node_id,
|
|
|
|
hir_id,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
VariantData::Tuple(ref fields, id) => {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(id);
|
|
|
|
|
|
|
|
hir::VariantData::Tuple(
|
|
|
|
fields
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|f| self.lower_struct_field(f))
|
|
|
|
.collect(),
|
|
|
|
node_id,
|
|
|
|
hir_id,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
VariantData::Unit(id) => {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(id);
|
|
|
|
|
|
|
|
hir::VariantData::Unit(node_id, hir_id)
|
|
|
|
},
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 22:02:42 -07:00
|
|
|
fn lower_trait_ref(&mut self, p: &TraitRef, itctx: ImplTraitContext<'_>) -> hir::TraitRef {
|
2017-11-10 12:23:59 -05:00
|
|
|
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),
|
2018-03-21 04:24:27 -04:00
|
|
|
qpath => bug!("lower_trait_ref: unexpected QPath `{:?}`", qpath),
|
2017-01-09 17:46:11 +02:00
|
|
|
};
|
2018-07-31 10:43:51 -06:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(p.ref_id);
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::TraitRef {
|
2017-07-03 11:19:51 -07:00
|
|
|
path,
|
2018-07-31 10:43:51 -06:00
|
|
|
ref_id: node_id,
|
|
|
|
hir_ref_id: hir_id,
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_poly_trait_ref(
|
|
|
|
&mut self,
|
|
|
|
p: &PolyTraitRef,
|
2018-08-29 22:02:42 -07:00
|
|
|
mut itctx: ImplTraitContext<'_>,
|
2018-03-21 04:24:27 -04:00
|
|
|
) -> hir::PolyTraitRef {
|
2018-07-21 22:15:11 +03:00
|
|
|
let bound_generic_params = self.lower_generic_params(
|
|
|
|
&p.bound_generic_params,
|
|
|
|
&NodeMap::default(),
|
|
|
|
itctx.reborrow(),
|
|
|
|
);
|
2017-10-16 21:07:26 +02:00
|
|
|
let trait_ref = self.with_parent_impl_lifetime_defs(
|
2018-05-27 16:56:01 +01:00
|
|
|
&bound_generic_params,
|
2017-10-16 21:07:26 +02:00
|
|
|
|this| this.lower_trait_ref(&p.trait_ref, itctx),
|
|
|
|
);
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::PolyTraitRef {
|
2017-10-16 21:07:26 +02:00
|
|
|
bound_generic_params,
|
2017-11-16 22:59:45 -08:00
|
|
|
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 {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(f.id);
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::StructField {
|
|
|
|
span: f.span,
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-05-26 02:50:15 +03:00
|
|
|
ident: match f.ident {
|
2017-03-24 23:03:15 +00:00
|
|
|
Some(ident) => ident,
|
2018-11-27 02:59:49 +00:00
|
|
|
// FIXME(jseyfried): positional field hygiene
|
2018-03-18 02:57:23 +03:00
|
|
|
None => Ident::new(Symbol::intern(&index.to_string()), f.span),
|
2018-05-26 02:50:15 +03:00
|
|
|
},
|
2017-03-14 15:50:40 +01:00
|
|
|
vis: self.lower_visibility(&f.vis, None),
|
2018-09-27 22:34:19 +01: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 {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::Field {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-05-26 02:50:15 +03:00
|
|
|
ident: f.ident,
|
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
|
|
|
|
2018-08-29 22:02:42 -07: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
|
|
|
|
2018-08-29 22:02:42 -07:00
|
|
|
fn lower_param_bounds(&mut self, bounds: &[GenericBound], mut itctx: ImplTraitContext<'_>)
|
2018-06-14 12:08:58 +01:00
|
|
|
-> hir::GenericBounds {
|
2018-06-19 13:47:53 +02:00
|
|
|
bounds.iter().map(|bound| self.lower_param_bound(bound, itctx.reborrow())).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
|
|
|
|
2018-06-27 17:32:48 +02:00
|
|
|
fn lower_async_body(
|
|
|
|
&mut self,
|
|
|
|
decl: &FnDecl,
|
|
|
|
asyncness: IsAsync,
|
|
|
|
body: &Block,
|
|
|
|
) -> hir::BodyId {
|
|
|
|
self.lower_body(Some(decl), |this| {
|
2018-06-28 14:31:46 +02:00
|
|
|
if let IsAsync::Async { closure_id, .. } = asyncness {
|
2018-06-27 17:32:48 +02:00
|
|
|
let async_expr = this.make_async_expr(
|
2018-06-28 14:31:46 +02:00
|
|
|
CaptureBy::Value, closure_id, None,
|
2018-06-27 17:32:48 +02:00
|
|
|
|this| {
|
|
|
|
let body = this.lower_block(body, false);
|
|
|
|
this.expr_block(body, ThinVec::new())
|
|
|
|
});
|
|
|
|
this.expr(body.span, async_expr, ThinVec::new())
|
|
|
|
} else {
|
|
|
|
let body = this.lower_block(body, false);
|
|
|
|
this.expr_block(body, ThinVec::new())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_item_kind(
|
|
|
|
&mut self,
|
|
|
|
id: NodeId,
|
2018-12-01 02:47:08 +00:00
|
|
|
ident: &mut Ident,
|
2018-03-21 04:24:27 -04:00
|
|
|
attrs: &hir::HirVec<Attribute>,
|
2018-11-22 06:41:26 -05:00
|
|
|
vis: &mut hir::Visibility,
|
2018-03-21 04:24:27 -04:00
|
|
|
i: &ItemKind,
|
2018-07-11 23:36:06 +08:00
|
|
|
) -> hir::ItemKind {
|
2016-05-10 01:11:59 +00:00
|
|
|
match *i {
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::ExternCrate(orig_name) => hir::ItemKind::ExternCrate(orig_name),
|
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
|
|
|
|
2018-12-01 02:47:08 +00:00
|
|
|
self.lower_use_tree(use_tree, &prefix, id, vis, ident, 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));
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Static(
|
2018-09-08 07:52:03 +01:00
|
|
|
self.lower_ty(
|
|
|
|
t,
|
|
|
|
if self.sess.features_untracked().impl_trait_in_bindings {
|
|
|
|
ImplTraitContext::Existential(None)
|
|
|
|
} else {
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
|
2018-09-08 07:52:03 +01:00
|
|
|
}
|
|
|
|
),
|
2018-03-21 04:24:27 -04:00
|
|
|
self.lower_mutability(m),
|
|
|
|
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));
|
2018-09-08 07:52:03 +01:00
|
|
|
hir::ItemKind::Const(
|
|
|
|
self.lower_ty(
|
|
|
|
t,
|
|
|
|
if self.sess.features_untracked().impl_trait_in_bindings {
|
|
|
|
ImplTraitContext::Existential(None)
|
|
|
|
} else {
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
|
2018-09-08 07:52:03 +01:00
|
|
|
}
|
|
|
|
),
|
|
|
|
value
|
|
|
|
)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2018-05-16 22:55:18 -07:00
|
|
|
ItemKind::Fn(ref decl, header, ref generics, ref body) => {
|
2018-03-21 04:45:21 -04:00
|
|
|
let fn_def_id = self.resolver.definitions().local_def_id(id);
|
2018-09-24 04:25:23 +01:00
|
|
|
self.with_new_scopes(|this| {
|
|
|
|
// Note: we don't need to change the return type from `T` to
|
|
|
|
// `impl Future<Output = T>` here because lower_body
|
|
|
|
// only cares about the input argument patterns in the function
|
|
|
|
// declaration (decl), not the return types.
|
|
|
|
let body_id = this.lower_async_body(decl, header.asyncness, body);
|
|
|
|
|
|
|
|
let (generics, fn_decl) = this.add_in_band_defs(
|
|
|
|
generics,
|
|
|
|
fn_def_id,
|
|
|
|
AnonymousLifetimeMode::PassThrough,
|
|
|
|
|this, idty| this.lower_fn_decl(
|
|
|
|
decl,
|
|
|
|
Some((fn_def_id, idty)),
|
|
|
|
true,
|
|
|
|
header.asyncness.opt_return_id()
|
|
|
|
),
|
|
|
|
);
|
2018-06-06 15:50:59 -07:00
|
|
|
|
2018-09-24 04:25:23 +01:00
|
|
|
hir::ItemKind::Fn(
|
|
|
|
fn_decl,
|
|
|
|
this.lower_fn_header(header),
|
|
|
|
generics,
|
|
|
|
body_id,
|
|
|
|
)
|
2017-02-15 14:52:27 -08:00
|
|
|
})
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
|
|
|
|
ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)),
|
|
|
|
ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)),
|
|
|
|
ItemKind::Ty(ref t, ref generics) => hir::ItemKind::Ty(
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_ty(t, ImplTraitContext::disallowed()),
|
|
|
|
self.lower_generics(generics, ImplTraitContext::disallowed()),
|
2018-03-21 04:24:27 -04:00
|
|
|
),
|
2018-07-03 19:38:14 +02:00
|
|
|
ItemKind::Existential(ref b, ref generics) => hir::ItemKind::Existential(hir::ExistTy {
|
2018-09-27 22:34:19 +01:00
|
|
|
generics: self.lower_generics(generics, ImplTraitContext::disallowed()),
|
|
|
|
bounds: self.lower_param_bounds(b, ImplTraitContext::disallowed()),
|
2018-07-03 19:38:14 +02:00
|
|
|
impl_trait_fn: None,
|
|
|
|
}),
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::Enum(ref enum_definition, ref generics) => hir::ItemKind::Enum(
|
2018-03-21 04:24:27 -04:00
|
|
|
hir::EnumDef {
|
|
|
|
variants: enum_definition
|
|
|
|
.variants
|
|
|
|
.iter()
|
|
|
|
.map(|x| self.lower_variant(x))
|
|
|
|
.collect(),
|
|
|
|
},
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_generics(generics, ImplTraitContext::disallowed()),
|
2018-03-21 04:24:27 -04:00
|
|
|
),
|
2016-05-10 01:11:59 +00:00
|
|
|
ItemKind::Struct(ref struct_def, ref generics) => {
|
|
|
|
let struct_def = self.lower_variant_data(struct_def);
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Struct(
|
2018-04-10 11:04:32 +09:00
|
|
|
struct_def,
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_generics(generics, ImplTraitContext::disallowed()),
|
2018-04-10 11:04:32 +09:00
|
|
|
)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2016-08-09 01:18:47 +03:00
|
|
|
ItemKind::Union(ref vdata, ref generics) => {
|
|
|
|
let vdata = self.lower_variant_data(vdata);
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Union(
|
2018-04-10 11:04:32 +09:00
|
|
|
vdata,
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_generics(generics, ImplTraitContext::disallowed()),
|
2018-04-10 11:04:32 +09:00
|
|
|
)
|
2016-08-09 01:18:47 +03:00
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
ItemKind::Impl(
|
|
|
|
unsafety,
|
|
|
|
polarity,
|
|
|
|
defaultness,
|
|
|
|
ref ast_generics,
|
2018-03-21 04:57:57 -04:00
|
|
|
ref trait_ref,
|
2018-03-21 04:24:27 -04:00
|
|
|
ref ty,
|
|
|
|
ref impl_items,
|
|
|
|
) => {
|
2018-03-21 04:45:21 -04:00
|
|
|
let def_id = self.resolver.definitions().local_def_id(id);
|
2018-03-21 04:57:57 -04:00
|
|
|
|
|
|
|
// Lower the "impl header" first. This ordering is important
|
|
|
|
// for in-band lifetimes! Consider `'a` here:
|
|
|
|
//
|
|
|
|
// impl Foo<'a> for u32 {
|
|
|
|
// fn method(&'a self) { .. }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Because we start by lowering the `Foo<'a> for u32`
|
|
|
|
// part, we will add `'a` to the list of generics on
|
|
|
|
// the impl. When we then encounter it later in the
|
|
|
|
// method, it will not be considered an in-band
|
|
|
|
// lifetime to be added, but rather a reference to a
|
|
|
|
// parent lifetime.
|
2018-03-21 17:12:39 -04:00
|
|
|
let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs(
|
|
|
|
ast_generics,
|
|
|
|
def_id,
|
|
|
|
AnonymousLifetimeMode::CreateParameter,
|
2018-06-19 13:47:53 +02:00
|
|
|
|this, _| {
|
2018-03-21 04:57:57 -04:00
|
|
|
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
|
2018-09-27 22:34:19 +01:00
|
|
|
this.lower_trait_ref(trait_ref, ImplTraitContext::disallowed())
|
2017-11-16 22:59:45 -08:00
|
|
|
});
|
|
|
|
|
2018-03-21 04:57:57 -04:00
|
|
|
if let Some(ref trait_ref) = trait_ref {
|
2017-11-16 22:59:45 -08:00
|
|
|
if let Def::Trait(def_id) = trait_ref.path.def {
|
2018-07-21 22:43:31 +03:00
|
|
|
this.trait_impls.entry(def_id).or_default().push(id);
|
2017-11-16 22:59:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 22:34:19 +01:00
|
|
|
let lowered_ty = this.lower_ty(ty, ImplTraitContext::disallowed());
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2018-03-21 04:57:57 -04:00
|
|
|
(trait_ref, lowered_ty)
|
2018-03-21 17:12:39 -04:00
|
|
|
},
|
|
|
|
);
|
2017-11-16 22:59:45 -08:00
|
|
|
|
|
|
|
let new_impl_items = self.with_in_scope_lifetime_defs(
|
2018-05-27 16:56:01 +01:00
|
|
|
&ast_generics.params,
|
2017-10-16 21:07:26 +02:00
|
|
|
|this| {
|
2018-03-21 04:24:27 -04:00
|
|
|
impl_items
|
|
|
|
.iter()
|
2017-10-16 21:07:26 +02:00
|
|
|
.map(|item| this.lower_impl_item_ref(item))
|
|
|
|
.collect()
|
2018-03-21 04:24:27 -04:00
|
|
|
},
|
2017-10-16 21:07:26 +02:00
|
|
|
);
|
2017-02-19 14:46:29 +02:00
|
|
|
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Impl(
|
2018-03-21 04:24:27 -04:00
|
|
|
self.lower_unsafety(unsafety),
|
|
|
|
self.lower_impl_polarity(polarity),
|
|
|
|
self.lower_defaultness(defaultness, true /* [1] */),
|
|
|
|
generics,
|
2018-03-21 04:57:57 -04:00
|
|
|
trait_ref,
|
2018-03-21 04:24:27 -04:00
|
|
|
lowered_ty,
|
|
|
|
new_impl_items,
|
|
|
|
)
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2017-10-12 09:51:31 -03:00
|
|
|
ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref items) => {
|
2018-09-27 22:34:19 +01:00
|
|
|
let bounds = self.lower_param_bounds(bounds, ImplTraitContext::disallowed());
|
2018-03-21 04:24:27 -04:00
|
|
|
let items = items
|
|
|
|
.iter()
|
|
|
|
.map(|item| self.lower_trait_item_ref(item))
|
|
|
|
.collect();
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Trait(
|
2018-03-21 04:24:27 -04:00
|
|
|
self.lower_is_auto(is_auto),
|
|
|
|
self.lower_unsafety(unsafety),
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_generics(generics, ImplTraitContext::disallowed()),
|
2018-03-21 04:24:27 -04:00
|
|
|
bounds,
|
|
|
|
items,
|
|
|
|
)
|
2017-11-21 06:49:15 -08:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
ItemKind::TraitAlias(ref generics, ref bounds) => hir::ItemKind::TraitAlias(
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_generics(generics, ImplTraitContext::disallowed()),
|
|
|
|
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
|
2018-03-21 04:24:27 -04:00
|
|
|
),
|
2017-10-02 12:28:16 +00:00
|
|
|
ItemKind::MacroDef(..) | ItemKind::Mac(..) => 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
|
2018-11-27 02:59:49 +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
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_use_tree(
|
|
|
|
&mut self,
|
|
|
|
tree: &UseTree,
|
|
|
|
prefix: &Path,
|
|
|
|
id: NodeId,
|
2018-11-22 06:41:26 -05:00
|
|
|
vis: &mut hir::Visibility,
|
2018-12-01 02:47:08 +00:00
|
|
|
ident: &mut Ident,
|
2018-03-21 04:24:27 -04:00
|
|
|
attrs: &hir::HirVec<Attribute>,
|
2018-07-11 23:36:06 +08:00
|
|
|
) -> hir::ItemKind {
|
2018-11-21 13:35:54 -05:00
|
|
|
debug!("lower_use_tree(tree={:?})", tree);
|
|
|
|
debug!("lower_use_tree: vis = {:?}", vis);
|
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
let path = &tree.prefix;
|
2018-10-11 21:15:18 +13:00
|
|
|
let segments = prefix
|
|
|
|
.segments
|
|
|
|
.iter()
|
|
|
|
.chain(path.segments.iter())
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
2017-09-26 23:04:00 +02:00
|
|
|
|
|
|
|
match tree.kind {
|
2018-06-13 11:44:06 -05:00
|
|
|
UseTreeKind::Simple(rename, id1, id2) => {
|
2018-12-01 02:47:08 +00:00
|
|
|
*ident = tree.ident();
|
2017-09-26 23:04:00 +02:00
|
|
|
|
2018-12-01 02:47:08 +00:00
|
|
|
// First, apply the prefix to the path.
|
2017-09-26 23:04:00 +02:00
|
|
|
let mut path = Path {
|
2018-10-11 21:15:18 +13:00
|
|
|
segments,
|
2018-03-21 04:24:27 -04:00
|
|
|
span: path.span,
|
2017-09-26 23:04:00 +02:00
|
|
|
};
|
|
|
|
|
2018-12-01 02:47:08 +00:00
|
|
|
// Correctly resolve `self` imports.
|
2018-03-21 04:24:27 -04:00
|
|
|
if path.segments.len() > 1
|
2018-12-02 03:35:55 +03:00
|
|
|
&& path.segments.last().unwrap().ident.name == keywords::SelfLower.name()
|
2018-03-21 04:24:27 -04:00
|
|
|
{
|
2017-09-26 23:04:00 +02:00
|
|
|
let _ = path.segments.pop();
|
2018-03-09 18:58:44 +03:00
|
|
|
if rename.is_none() {
|
2018-12-01 02:47:08 +00:00
|
|
|
*ident = path.segments.last().unwrap().ident;
|
2017-09-26 23:04:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 11:44:06 -05:00
|
|
|
let parent_def_index = self.current_hir_id_owner.last().unwrap().0;
|
2018-06-15 14:47:07 -05:00
|
|
|
let mut defs = self.expect_full_def_from_use(id);
|
2018-12-01 02:47:08 +00:00
|
|
|
// We want to return *something* from this function, so hold onto the first item
|
|
|
|
// for later.
|
2018-07-29 17:40:36 +01:00
|
|
|
let ret_def = defs.next().unwrap_or(Def::Err);
|
2018-06-13 11:44:06 -05:00
|
|
|
|
2018-10-11 21:15:18 +13:00
|
|
|
// Here, we are looping over namespaces, if they exist for the definition
|
|
|
|
// being imported. We only handle type and value namespaces because we
|
|
|
|
// won't be dealing with macros in the rest of the compiler.
|
|
|
|
// Essentially a single `use` which imports two names is desugared into
|
|
|
|
// two imports.
|
2018-06-13 11:44:06 -05:00
|
|
|
for (def, &new_node_id) in defs.zip([id1, id2].iter()) {
|
|
|
|
let vis = vis.clone();
|
2018-12-01 02:47:08 +00:00
|
|
|
let ident = ident.clone();
|
2018-10-11 21:15:18 +13:00
|
|
|
let mut path = path.clone();
|
|
|
|
for seg in &mut path.segments {
|
|
|
|
seg.id = self.sess.next_node_id();
|
|
|
|
}
|
2018-06-13 11:44:06 -05:00
|
|
|
let span = path.span;
|
|
|
|
self.resolver.definitions().create_def_with_parent(
|
|
|
|
parent_def_index,
|
|
|
|
new_node_id,
|
|
|
|
DefPathData::Misc,
|
|
|
|
DefIndexAddressSpace::High,
|
|
|
|
Mark::root(),
|
|
|
|
span);
|
|
|
|
self.allocate_hir_id_counter(new_node_id, &path);
|
|
|
|
|
|
|
|
self.with_hir_id_owner(new_node_id, |this| {
|
|
|
|
let new_id = this.lower_node_id(new_node_id);
|
2018-10-11 21:15:18 +13:00
|
|
|
let path =
|
2018-10-31 11:05:58 +13:00
|
|
|
this.lower_path_extra(def, &path, ParamMode::Explicit, None);
|
2018-07-11 23:36:06 +08:00
|
|
|
let item = hir::ItemKind::Use(P(path), hir::UseKind::Single);
|
2018-06-30 20:34:18 -07:00
|
|
|
let vis_kind = match vis.node {
|
2018-07-01 11:05:10 -07:00
|
|
|
hir::VisibilityKind::Public => hir::VisibilityKind::Public,
|
|
|
|
hir::VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
|
|
|
|
hir::VisibilityKind::Inherited => hir::VisibilityKind::Inherited,
|
2018-07-31 10:43:51 -06:00
|
|
|
hir::VisibilityKind::Restricted { ref path, id: _, hir_id: _ } => {
|
|
|
|
let id = this.next_id();
|
2018-11-21 13:36:11 -05:00
|
|
|
let path = this.renumber_segment_ids(path);
|
2018-07-01 11:05:10 -07:00
|
|
|
hir::VisibilityKind::Restricted {
|
2018-10-29 21:06:27 +13:00
|
|
|
path,
|
2018-07-31 10:43:51 -06:00
|
|
|
id: id.node_id,
|
|
|
|
hir_id: id.hir_id,
|
2018-06-13 11:44:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-30 20:34:18 -07:00
|
|
|
let vis = respan(vis.span, vis_kind);
|
2018-06-13 11:44:06 -05:00
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
this.insert_item(
|
2018-06-13 11:44:06 -05:00
|
|
|
new_id.node_id,
|
|
|
|
hir::Item {
|
|
|
|
id: new_id.node_id,
|
|
|
|
hir_id: new_id.hir_id,
|
2018-12-01 02:47:08 +00:00
|
|
|
ident,
|
2018-06-13 11:44:06 -05:00
|
|
|
attrs: attrs.clone(),
|
|
|
|
node: item,
|
|
|
|
vis,
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-11 21:15:18 +13:00
|
|
|
let path =
|
2018-10-31 11:05:58 +13:00
|
|
|
P(self.lower_path_extra(ret_def, &path, ParamMode::Explicit, None));
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Use(path, hir::UseKind::Single)
|
2017-09-26 23:04:00 +02:00
|
|
|
}
|
|
|
|
UseTreeKind::Glob => {
|
2018-03-21 04:24:27 -04:00
|
|
|
let path = P(self.lower_path(
|
|
|
|
id,
|
|
|
|
&Path {
|
2018-10-11 21:15:18 +13:00
|
|
|
segments,
|
2018-03-21 04:24:27 -04:00
|
|
|
span: path.span,
|
|
|
|
},
|
|
|
|
ParamMode::Explicit,
|
|
|
|
));
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Use(path, hir::UseKind::Glob)
|
2017-09-26 23:04:00 +02:00
|
|
|
}
|
|
|
|
UseTreeKind::Nested(ref trees) => {
|
2018-12-01 02:47:08 +00:00
|
|
|
// Nested imports are desugared into simple imports.
|
|
|
|
// So, if we start with
|
2018-11-22 06:48:13 -05:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// pub(x) use foo::{a, b};
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// we will create three items:
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// pub(x) use foo::a;
|
|
|
|
// pub(x) use foo::b;
|
|
|
|
// pub(x) use foo::{}; // <-- this is called the `ListStem`
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// The first two are produced by recursively invoking
|
|
|
|
// `lower_use_tree` (and indeed there may be things
|
|
|
|
// like `use foo::{a::{b, c}}` and so forth). They
|
|
|
|
// wind up being directly added to
|
|
|
|
// `self.items`. However, the structure of this
|
|
|
|
// function also requires us to return one item, and
|
|
|
|
// for that we return the `{}` import (called the
|
2018-12-01 02:47:08 +00:00
|
|
|
// `ListStem`).
|
2018-10-11 21:15:18 +13:00
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
let prefix = Path {
|
2018-10-11 21:15:18 +13:00
|
|
|
segments,
|
2017-09-26 23:04:00 +02:00
|
|
|
span: prefix.span.to(path.span),
|
|
|
|
};
|
|
|
|
|
2018-12-01 02:47:08 +00:00
|
|
|
// Add all the nested `PathListItem`s to the HIR.
|
2017-09-26 23:04:00 +02:00
|
|
|
for &(ref use_tree, id) in trees {
|
|
|
|
self.allocate_hir_id_counter(id, &use_tree);
|
2018-10-11 21:15:18 +13:00
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
let LoweredNodeId {
|
|
|
|
node_id: new_id,
|
|
|
|
hir_id: new_hir_id,
|
|
|
|
} = self.lower_node_id(id);
|
|
|
|
|
2018-11-22 06:41:26 -05:00
|
|
|
let mut vis = vis.clone();
|
2018-12-01 02:47:08 +00:00
|
|
|
let mut ident = ident.clone();
|
2018-10-11 21:15:18 +13:00
|
|
|
let mut prefix = prefix.clone();
|
2017-09-26 23:04:00 +02:00
|
|
|
|
2018-12-01 02:47:08 +00:00
|
|
|
// Give the segments new node-ids since they are being cloned.
|
2018-10-11 21:15:18 +13:00
|
|
|
for seg in &mut prefix.segments {
|
|
|
|
seg.id = self.sess.next_node_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each `use` import is an item and thus are owners of the
|
|
|
|
// names in the path. Up to this point the nested import is
|
|
|
|
// the current owner, since we want each desugared import to
|
|
|
|
// own its own names, we have to adjust the owner before
|
|
|
|
// lowering the rest of the import.
|
2017-09-26 23:04:00 +02:00
|
|
|
self.with_hir_id_owner(new_id, |this| {
|
2018-10-11 21:15:18 +13:00
|
|
|
let item = this.lower_use_tree(use_tree,
|
|
|
|
&prefix,
|
|
|
|
new_id,
|
2018-11-22 06:41:26 -05:00
|
|
|
&mut vis,
|
2018-12-01 02:47:08 +00:00
|
|
|
&mut ident,
|
2018-10-11 21:15:18 +13:00
|
|
|
attrs);
|
|
|
|
|
2018-06-30 20:34:18 -07:00
|
|
|
let vis_kind = match vis.node {
|
2018-07-01 11:05:10 -07:00
|
|
|
hir::VisibilityKind::Public => hir::VisibilityKind::Public,
|
|
|
|
hir::VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
|
|
|
|
hir::VisibilityKind::Inherited => hir::VisibilityKind::Inherited,
|
2018-07-31 10:43:51 -06:00
|
|
|
hir::VisibilityKind::Restricted { ref path, id: _, hir_id: _ } => {
|
|
|
|
let id = this.next_id();
|
2018-11-21 13:36:11 -05:00
|
|
|
let path = this.renumber_segment_ids(path);
|
2018-07-01 11:05:10 -07:00
|
|
|
hir::VisibilityKind::Restricted {
|
2018-11-21 13:36:11 -05:00
|
|
|
path: path,
|
2018-07-31 10:43:51 -06:00
|
|
|
id: id.node_id,
|
|
|
|
hir_id: id.hir_id,
|
2017-09-26 23:04:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-30 20:34:18 -07:00
|
|
|
let vis = respan(vis.span, vis_kind);
|
2017-09-26 23:04:00 +02:00
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
this.insert_item(
|
2018-03-21 04:24:27 -04:00
|
|
|
new_id,
|
|
|
|
hir::Item {
|
|
|
|
id: new_id,
|
|
|
|
hir_id: new_hir_id,
|
2018-12-01 02:47:08 +00:00
|
|
|
ident,
|
2018-03-21 04:24:27 -04:00
|
|
|
attrs: attrs.clone(),
|
|
|
|
node: item,
|
|
|
|
vis,
|
|
|
|
span: use_tree.span,
|
|
|
|
},
|
|
|
|
);
|
2017-09-26 23:04:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-22 06:41:26 -05:00
|
|
|
// Subtle and a bit hacky: we lower the privacy level
|
|
|
|
// of the list stem to "private" most of the time, but
|
|
|
|
// not for "restricted" paths. The key thing is that
|
|
|
|
// we don't want it to stay as `pub` (with no caveats)
|
|
|
|
// because that affects rustdoc and also the lints
|
|
|
|
// about `pub` items. But we can't *always* make it
|
|
|
|
// private -- particularly not for restricted paths --
|
|
|
|
// because it contains node-ids that would then be
|
|
|
|
// unused, failing the check that HirIds are "densely
|
|
|
|
// assigned".
|
|
|
|
match vis.node {
|
|
|
|
hir::VisibilityKind::Public |
|
|
|
|
hir::VisibilityKind::Crate(_) |
|
|
|
|
hir::VisibilityKind::Inherited => {
|
|
|
|
*vis = respan(prefix.span.shrink_to_lo(), hir::VisibilityKind::Inherited);
|
|
|
|
}
|
|
|
|
hir::VisibilityKind::Restricted { .. } => {
|
2018-12-01 02:47:08 +00:00
|
|
|
// Do nothing here, as described in the comment on the match.
|
2018-11-22 06:41:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-27 23:38:09 +03:00
|
|
|
let def = self.expect_full_def_from_use(id).next().unwrap_or(Def::Err);
|
2018-10-31 11:05:58 +13:00
|
|
|
let path = P(self.lower_path_extra(def, &prefix, ParamMode::Explicit, None));
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Use(path, hir::UseKind::ListStem)
|
2017-09-26 23:04:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:36:11 -05:00
|
|
|
/// Paths like the visibility path in `pub(super) use foo::{bar, baz}` are repeated
|
|
|
|
/// many times in the HIR tree; for each occurrence, we need to assign distinct
|
2019-02-08 14:53:55 +01:00
|
|
|
/// `NodeId`s. (See, e.g., #56128.)
|
2018-11-21 13:36:11 -05:00
|
|
|
fn renumber_segment_ids(&mut self, path: &P<hir::Path>) -> P<hir::Path> {
|
|
|
|
debug!("renumber_segment_ids(path = {:?})", path);
|
|
|
|
let mut path = path.clone();
|
|
|
|
for seg in path.segments.iter_mut() {
|
|
|
|
if seg.id.is_some() {
|
|
|
|
seg.id = Some(self.next_id().node_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem {
|
2018-05-24 14:51:33 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(i.id);
|
|
|
|
let trait_item_def_id = self.resolver.definitions().local_def_id(node_id);
|
|
|
|
|
|
|
|
let (generics, node) = match i.node {
|
|
|
|
TraitItemKind::Const(ref ty, ref default) => (
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_generics(&i.generics, ImplTraitContext::disallowed()),
|
2018-05-24 14:51:33 +02:00
|
|
|
hir::TraitItemKind::Const(
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_ty(ty, ImplTraitContext::disallowed()),
|
2018-05-24 14:51:33 +02:00
|
|
|
default
|
|
|
|
.as_ref()
|
|
|
|
.map(|x| self.lower_body(None, |this| this.lower_expr(x))),
|
2018-03-21 04:24:27 -04:00
|
|
|
),
|
2018-05-24 14:51:33 +02:00
|
|
|
),
|
|
|
|
TraitItemKind::Method(ref sig, None) => {
|
2018-09-24 04:25:23 +01:00
|
|
|
let names = self.lower_fn_args_to_names(&sig.decl);
|
|
|
|
let (generics, sig) = self.lower_method_sig(
|
|
|
|
&i.generics,
|
|
|
|
sig,
|
|
|
|
trait_item_def_id,
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
(generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Required(names)))
|
2018-05-24 14:51:33 +02:00
|
|
|
}
|
|
|
|
TraitItemKind::Method(ref sig, Some(ref body)) => {
|
2018-09-24 04:25:23 +01:00
|
|
|
let body_id = self.lower_body(Some(&sig.decl), |this| {
|
|
|
|
let body = this.lower_block(body, false);
|
|
|
|
this.expr_block(body, ThinVec::new())
|
|
|
|
});
|
|
|
|
let (generics, sig) = self.lower_method_sig(
|
|
|
|
&i.generics,
|
|
|
|
sig,
|
|
|
|
trait_item_def_id,
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
(generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Provided(body_id)))
|
2018-05-24 14:51:33 +02:00
|
|
|
}
|
|
|
|
TraitItemKind::Type(ref bounds, ref default) => (
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_generics(&i.generics, ImplTraitContext::disallowed()),
|
2018-05-24 14:51:33 +02:00
|
|
|
hir::TraitItemKind::Type(
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
|
2018-05-24 14:51:33 +02:00
|
|
|
default
|
|
|
|
.as_ref()
|
2018-09-27 22:34:19 +01:00
|
|
|
.map(|x| self.lower_ty(x, ImplTraitContext::disallowed())),
|
2018-03-21 04:24:27 -04:00
|
|
|
),
|
2018-05-24 14:51:33 +02:00
|
|
|
),
|
|
|
|
TraitItemKind::Macro(..) => panic!("Shouldn't exist any more"),
|
|
|
|
};
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2018-05-24 14:51:33 +02:00
|
|
|
hir::TraitItem {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-06-10 22:24:24 +03:00
|
|
|
ident: i.ident,
|
2018-05-24 14:51:33 +02:00
|
|
|
attrs: self.lower_attrs(&i.attrs),
|
|
|
|
generics,
|
|
|
|
node,
|
|
|
|
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())
|
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
TraitItemKind::Method(ref sig, ref default) => (
|
|
|
|
hir::AssociatedItemKind::Method {
|
2016-12-04 04:21:06 +02:00
|
|
|
has_self: sig.decl.has_self(),
|
2018-03-21 04:24:27 -04:00
|
|
|
},
|
|
|
|
default.is_some(),
|
|
|
|
),
|
2016-12-04 04:21:06 +02:00
|
|
|
TraitItemKind::Macro(..) => unimplemented!(),
|
|
|
|
};
|
|
|
|
hir::TraitItemRef {
|
|
|
|
id: hir::TraitItemId { node_id: i.id },
|
2018-06-10 22:24:24 +03:00
|
|
|
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 {
|
2018-05-24 14:51:33 +02:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(i.id);
|
|
|
|
let impl_item_def_id = self.resolver.definitions().local_def_id(node_id);
|
|
|
|
|
|
|
|
let (generics, node) = match i.node {
|
|
|
|
ImplItemKind::Const(ref ty, ref expr) => {
|
|
|
|
let body_id = self.lower_body(None, |this| this.lower_expr(expr));
|
|
|
|
(
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_generics(&i.generics, ImplTraitContext::disallowed()),
|
2018-05-24 14:51:33 +02:00
|
|
|
hir::ImplItemKind::Const(
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_ty(ty, ImplTraitContext::disallowed()),
|
2018-05-24 14:51:33 +02:00
|
|
|
body_id,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ImplItemKind::Method(ref sig, ref body) => {
|
2018-09-24 04:25:23 +01:00
|
|
|
let body_id = self.lower_async_body(&sig.decl, sig.header.asyncness, body);
|
|
|
|
let impl_trait_return_allow = !self.is_in_trait_impl;
|
|
|
|
let (generics, sig) = self.lower_method_sig(
|
|
|
|
&i.generics,
|
|
|
|
sig,
|
|
|
|
impl_item_def_id,
|
|
|
|
impl_trait_return_allow,
|
|
|
|
sig.header.asyncness.opt_return_id(),
|
|
|
|
);
|
|
|
|
(generics, hir::ImplItemKind::Method(sig, body_id))
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2018-05-24 14:51:33 +02:00
|
|
|
ImplItemKind::Type(ref ty) => (
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_generics(&i.generics, ImplTraitContext::disallowed()),
|
|
|
|
hir::ImplItemKind::Type(self.lower_ty(ty, ImplTraitContext::disallowed())),
|
2018-05-24 14:51:33 +02:00
|
|
|
),
|
2018-07-03 19:38:14 +02:00
|
|
|
ImplItemKind::Existential(ref bounds) => (
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_generics(&i.generics, ImplTraitContext::disallowed()),
|
2018-07-03 19:38:14 +02:00
|
|
|
hir::ImplItemKind::Existential(
|
2018-09-27 22:34:19 +01:00
|
|
|
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
|
2018-07-03 19:38:14 +02:00
|
|
|
),
|
|
|
|
),
|
2018-05-24 14:51:33 +02:00
|
|
|
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
|
|
|
|
};
|
|
|
|
|
|
|
|
hir::ImplItem {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-06-10 22:24:24 +03:00
|
|
|
ident: i.ident,
|
2018-05-24 14:51:33 +02:00
|
|
|
attrs: self.lower_attrs(&i.attrs),
|
|
|
|
generics,
|
|
|
|
vis: self.lower_visibility(&i.vis, None),
|
|
|
|
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
|
|
|
|
node,
|
|
|
|
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 },
|
2018-06-10 22:24:24 +03:00
|
|
|
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,
|
2018-07-03 19:38:14 +02:00
|
|
|
ImplItemKind::Existential(..) => hir::AssociatedItemKind::Existential,
|
2018-03-21 04:24:27 -04: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
|
|
|
|
2018-08-30 11:42:16 +02:00
|
|
|
fn lower_item_id(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> {
|
2017-03-05 05:15:58 +00:00
|
|
|
match i.node {
|
2017-09-26 23:04:00 +02:00
|
|
|
ItemKind::Use(ref use_tree) => {
|
2018-08-13 22:15:16 +03:00
|
|
|
let mut vec = smallvec![hir::ItemId { id: i.id }];
|
2018-06-13 11:44:06 -05:00
|
|
|
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
|
2018-06-18 16:23:13 +02:00
|
|
|
vec
|
2016-11-24 06:11:31 +02:00
|
|
|
}
|
2018-08-30 11:42:16 +02:00
|
|
|
ItemKind::MacroDef(..) => SmallVec::new(),
|
2018-10-02 10:54:34 +02:00
|
|
|
ItemKind::Fn(..) |
|
|
|
|
ItemKind::Impl(.., None, _, _) => smallvec![hir::ItemId { id: i.id }],
|
2018-09-08 07:52:03 +01:00
|
|
|
ItemKind::Static(ref ty, ..) => {
|
|
|
|
let mut ids = smallvec![hir::ItemId { id: i.id }];
|
|
|
|
if self.sess.features_untracked().impl_trait_in_bindings {
|
|
|
|
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
|
|
|
|
visitor.visit_ty(ty);
|
|
|
|
}
|
|
|
|
ids
|
|
|
|
},
|
|
|
|
ItemKind::Const(ref ty, ..) => {
|
|
|
|
let mut ids = smallvec![hir::ItemId { id: i.id }];
|
|
|
|
if self.sess.features_untracked().impl_trait_in_bindings {
|
|
|
|
let mut visitor = ImplTraitTypeIdVisitor { ids: &mut ids };
|
|
|
|
visitor.visit_ty(ty);
|
|
|
|
}
|
|
|
|
ids
|
|
|
|
},
|
2018-08-13 22:15:16 +03:00
|
|
|
_ => smallvec![hir::ItemId { id: i.id }],
|
2016-11-24 06:11:31 +02:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-06-13 11:44:06 -05:00
|
|
|
fn lower_item_id_use_tree(&mut self,
|
|
|
|
tree: &UseTree,
|
|
|
|
base_id: NodeId,
|
2018-08-30 11:42:16 +02:00
|
|
|
vec: &mut SmallVec<[hir::ItemId; 1]>)
|
2018-06-13 11:44:06 -05:00
|
|
|
{
|
2017-09-26 23:04:00 +02:00
|
|
|
match tree.kind {
|
2018-03-21 04:24:27 -04:00
|
|
|
UseTreeKind::Nested(ref nested_vec) => for &(ref nested, id) in nested_vec {
|
|
|
|
vec.push(hir::ItemId { id });
|
2018-06-13 11:44:06 -05:00
|
|
|
self.lower_item_id_use_tree(nested, id, vec);
|
2018-03-21 04:24:27 -04:00
|
|
|
},
|
2017-09-26 23:04:00 +02:00
|
|
|
UseTreeKind::Glob => {}
|
2018-06-13 11:44:06 -05:00
|
|
|
UseTreeKind::Simple(_, id1, id2) => {
|
|
|
|
for (_, &id) in self.expect_full_def_from_use(base_id)
|
|
|
|
.skip(1)
|
|
|
|
.zip([id1, id2].iter())
|
|
|
|
{
|
|
|
|
vec.push(hir::ItemId { id });
|
|
|
|
}
|
|
|
|
},
|
2017-09-26 23:04:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 05:15:58 +00:00
|
|
|
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item> {
|
2018-12-01 02:47:08 +00:00
|
|
|
let mut ident = i.ident;
|
2018-11-22 06:41:26 -05: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 {
|
2018-07-21 02:49:34 +03:00
|
|
|
if !def.legacy || attr::contains_name(&i.attrs, "macro_export") ||
|
|
|
|
attr::contains_name(&i.attrs, "rustc_doc_only_macro") {
|
2017-09-15 08:28:34 -07:00
|
|
|
let body = self.lower_token_stream(def.stream());
|
2019-02-02 15:40:08 +01:00
|
|
|
let hir_id = self.lower_node_id(i.id).hir_id;
|
2017-03-05 05:15:58 +00:00
|
|
|
self.exported_macros.push(hir::MacroDef {
|
2018-12-01 02:47:08 +00:00
|
|
|
name: ident.name,
|
2017-07-03 11:19:51 -07:00
|
|
|
vis,
|
|
|
|
attrs,
|
2017-03-25 01:46:38 +00:00
|
|
|
id: i.id,
|
2019-02-02 15:40:08 +01:00
|
|
|
hir_id,
|
2017-03-25 01:46:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-12-01 02:47:08 +00:00
|
|
|
let node = self.lower_item_kind(i.id, &mut ident, &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,
|
2018-12-01 02:47:08 +00:00
|
|
|
ident,
|
2017-07-03 11:19:51 -07:00
|
|
|
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 {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(i.id);
|
2018-05-24 14:51:33 +02:00
|
|
|
let def_id = self.resolver.definitions().local_def_id(node_id);
|
|
|
|
hir::ForeignItem {
|
|
|
|
id: node_id,
|
2019-02-02 15:40:08 +01:00
|
|
|
hir_id,
|
2018-12-01 02:47:08 +00:00
|
|
|
ident: i.ident,
|
2018-05-24 14:51:33 +02:00
|
|
|
attrs: self.lower_attrs(&i.attrs),
|
|
|
|
node: match i.node {
|
|
|
|
ForeignItemKind::Fn(ref fdec, ref generics) => {
|
|
|
|
let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
|
|
|
|
generics,
|
|
|
|
def_id,
|
|
|
|
AnonymousLifetimeMode::PassThrough,
|
2018-06-19 13:47:53 +02:00
|
|
|
|this, _| {
|
2018-05-24 14:51:33 +02:00
|
|
|
(
|
|
|
|
// Disallow impl Trait in foreign items
|
2018-06-26 11:56:24 +02:00
|
|
|
this.lower_fn_decl(fdec, None, false, None),
|
2018-05-24 14:51:33 +02:00
|
|
|
this.lower_fn_args_to_names(fdec),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
);
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2018-07-11 22:56:44 +08:00
|
|
|
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
|
2018-05-24 14:51:33 +02:00
|
|
|
}
|
|
|
|
ForeignItemKind::Static(ref t, m) => {
|
2018-09-27 22:34:19 +01:00
|
|
|
hir::ForeignItemKind::Static(
|
|
|
|
self.lower_ty(t, ImplTraitContext::disallowed()), m)
|
2018-05-24 14:51:33 +02:00
|
|
|
}
|
2018-07-11 22:56:44 +08:00
|
|
|
ForeignItemKind::Ty => hir::ForeignItemKind::Type,
|
2018-05-24 14:51:33 +02:00
|
|
|
ForeignItemKind::Macro(_) => panic!("shouldn't exist here"),
|
|
|
|
},
|
|
|
|
vis: self.lower_visibility(&i.vis, None),
|
|
|
|
span: i.span,
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_method_sig(
|
|
|
|
&mut self,
|
2018-06-19 10:09:00 +02:00
|
|
|
generics: &Generics,
|
2018-03-21 04:24:27 -04:00
|
|
|
sig: &MethodSig,
|
2018-03-21 04:45:21 -04:00
|
|
|
fn_def_id: DefId,
|
2018-03-21 04:24:27 -04:00
|
|
|
impl_trait_return_allow: bool,
|
2018-06-26 11:56:24 +02:00
|
|
|
is_async: Option<NodeId>,
|
2018-06-19 10:09:00 +02:00
|
|
|
) -> (hir::Generics, hir::MethodSig) {
|
|
|
|
let header = self.lower_fn_header(sig.header);
|
|
|
|
let (generics, decl) = self.add_in_band_defs(
|
|
|
|
generics,
|
|
|
|
fn_def_id,
|
|
|
|
AnonymousLifetimeMode::PassThrough,
|
2018-06-19 13:47:53 +02:00
|
|
|
|this, idty| this.lower_fn_decl(
|
|
|
|
&sig.decl,
|
|
|
|
Some((fn_def_id, idty)),
|
|
|
|
impl_trait_return_allow,
|
|
|
|
is_async,
|
|
|
|
),
|
2018-06-19 10:09:00 +02:00
|
|
|
);
|
|
|
|
(generics, hir::MethodSig { header, decl })
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 22:55:18 -07:00
|
|
|
fn lower_fn_header(&mut self, h: FnHeader) -> hir::FnHeader {
|
|
|
|
hir::FnHeader {
|
|
|
|
unsafety: self.lower_unsafety(h.unsafety),
|
|
|
|
asyncness: self.lower_asyncness(h.asyncness),
|
|
|
|
constness: self.lower_constness(h.constness),
|
|
|
|
abi: h.abi,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-16 22:55:18 -07:00
|
|
|
fn lower_asyncness(&mut self, a: IsAsync) -> hir::IsAsync {
|
|
|
|
match a {
|
2018-06-26 11:56:24 +02:00
|
|
|
IsAsync::Async { .. } => hir::IsAsync::Async,
|
2018-05-16 22:55:18 -07:00
|
|
|
IsAsync::NotAsync => hir::IsAsync::NotAsync,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-07-11 18:44:53 +08:00
|
|
|
BinOpKind::Add => hir::BinOpKind::Add,
|
|
|
|
BinOpKind::Sub => hir::BinOpKind::Sub,
|
|
|
|
BinOpKind::Mul => hir::BinOpKind::Mul,
|
|
|
|
BinOpKind::Div => hir::BinOpKind::Div,
|
|
|
|
BinOpKind::Rem => hir::BinOpKind::Rem,
|
|
|
|
BinOpKind::And => hir::BinOpKind::And,
|
|
|
|
BinOpKind::Or => hir::BinOpKind::Or,
|
|
|
|
BinOpKind::BitXor => hir::BinOpKind::BitXor,
|
|
|
|
BinOpKind::BitAnd => hir::BinOpKind::BitAnd,
|
|
|
|
BinOpKind::BitOr => hir::BinOpKind::BitOr,
|
|
|
|
BinOpKind::Shl => hir::BinOpKind::Shl,
|
|
|
|
BinOpKind::Shr => hir::BinOpKind::Shr,
|
|
|
|
BinOpKind::Eq => hir::BinOpKind::Eq,
|
|
|
|
BinOpKind::Lt => hir::BinOpKind::Lt,
|
|
|
|
BinOpKind::Le => hir::BinOpKind::Le,
|
|
|
|
BinOpKind::Ne => hir::BinOpKind::Ne,
|
|
|
|
BinOpKind::Ge => hir::BinOpKind::Ge,
|
|
|
|
BinOpKind::Gt => hir::BinOpKind::Gt,
|
2016-05-10 01:11:59 +00:00
|
|
|
},
|
|
|
|
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> {
|
2018-02-24 15:27:06 +03:00
|
|
|
let node = match p.node {
|
|
|
|
PatKind::Wild => hir::PatKind::Wild,
|
2018-03-18 16:47:09 +03:00
|
|
|
PatKind::Ident(ref binding_mode, ident, ref sub) => {
|
2018-02-24 15:27:06 +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,
|
2018-03-21 04:24:27 -04:00
|
|
|
_ => p.id,
|
2018-02-24 15:27:06 +03:00
|
|
|
};
|
2019-02-02 15:40:08 +01:00
|
|
|
let hir_id = self.lower_node_id(canonical_id).hir_id;
|
2018-03-21 04:24:27 -04:00
|
|
|
hir::PatKind::Binding(
|
|
|
|
self.lower_binding_mode(binding_mode),
|
|
|
|
canonical_id,
|
2019-02-02 15:40:08 +01:00
|
|
|
hir_id,
|
2018-06-10 19:33:30 +03:00
|
|
|
ident,
|
2018-03-21 04:24:27 -04:00
|
|
|
sub.as_ref().map(|x| self.lower_pat(x)),
|
|
|
|
)
|
2018-02-24 15:27:06 +03:00
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
Some(def) => hir::PatKind::Path(hir::QPath::Resolved(
|
|
|
|
None,
|
|
|
|
P(hir::Path {
|
2018-03-18 16:47:09 +03:00
|
|
|
span: ident.span,
|
2018-02-24 15:27:06 +03:00
|
|
|
def,
|
2018-06-10 17:40:45 +03:00
|
|
|
segments: hir_vec![hir::PathSegment::from_ident(ident)],
|
2018-03-21 04:24:27 -04:00
|
|
|
}),
|
|
|
|
)),
|
2018-02-24 15:27:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PatKind::Lit(ref e) => hir::PatKind::Lit(P(self.lower_expr(e))),
|
|
|
|
PatKind::TupleStruct(ref path, ref pats, ddpos) => {
|
2018-03-21 04:24:27 -04:00
|
|
|
let qpath = self.lower_qpath(
|
|
|
|
p.id,
|
|
|
|
&None,
|
|
|
|
path,
|
|
|
|
ParamMode::Optional,
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::disallowed(),
|
2018-03-21 04:24:27 -04:00
|
|
|
);
|
|
|
|
hir::PatKind::TupleStruct(
|
|
|
|
qpath,
|
|
|
|
pats.iter().map(|x| self.lower_pat(x)).collect(),
|
|
|
|
ddpos,
|
|
|
|
)
|
2018-02-24 15:27:06 +03:00
|
|
|
}
|
2018-09-06 10:46:55 +08:00
|
|
|
PatKind::Path(ref qself, ref path) => {
|
|
|
|
let qpath = self.lower_qpath(
|
|
|
|
p.id,
|
|
|
|
qself,
|
|
|
|
path,
|
|
|
|
ParamMode::Optional,
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::disallowed(),
|
2018-09-06 10:46:55 +08:00
|
|
|
);
|
|
|
|
hir::PatKind::Path(qpath)
|
|
|
|
}
|
2018-02-24 15:27:06 +03:00
|
|
|
PatKind::Struct(ref path, ref fields, etc) => {
|
2018-03-21 04:24:27 -04:00
|
|
|
let qpath = self.lower_qpath(
|
|
|
|
p.id,
|
|
|
|
&None,
|
|
|
|
path,
|
|
|
|
ParamMode::Optional,
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::disallowed(),
|
2018-03-21 04:24:27 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
let fs = fields
|
|
|
|
.iter()
|
2019-02-02 15:40:08 +01:00
|
|
|
.map(|f| {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
|
|
|
|
|
|
|
Spanned {
|
|
|
|
span: f.span,
|
|
|
|
node: hir::FieldPat {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
|
|
|
ident: f.node.ident,
|
|
|
|
pat: self.lower_pat(&f.node.pat),
|
|
|
|
is_shorthand: f.node.is_shorthand,
|
|
|
|
},
|
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
})
|
|
|
|
.collect();
|
2018-02-24 15:27:06 +03:00
|
|
|
hir::PatKind::Struct(qpath, fs, etc)
|
|
|
|
}
|
|
|
|
PatKind::Tuple(ref elts, ddpos) => {
|
|
|
|
hir::PatKind::Tuple(elts.iter().map(|x| self.lower_pat(x)).collect(), ddpos)
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
2018-05-28 19:32:03 -07:00
|
|
|
PatKind::Range(ref e1, ref e2, Spanned { node: ref end, .. }) => hir::PatKind::Range(
|
2018-03-21 04:24:27 -04:00
|
|
|
P(self.lower_expr(e1)),
|
|
|
|
P(self.lower_expr(e2)),
|
|
|
|
self.lower_range_end(end),
|
|
|
|
),
|
|
|
|
PatKind::Slice(ref before, ref slice, ref after) => hir::PatKind::Slice(
|
|
|
|
before.iter().map(|x| self.lower_pat(x)).collect(),
|
|
|
|
slice.as_ref().map(|x| self.lower_pat(x)),
|
|
|
|
after.iter().map(|x| self.lower_pat(x)).collect(),
|
|
|
|
),
|
2018-02-24 15:27:06 +03:00
|
|
|
PatKind::Paren(ref inner) => return self.lower_pat(inner),
|
|
|
|
PatKind::Mac(_) => panic!("Shouldn't exist here"),
|
|
|
|
};
|
2017-08-04 09:49:40 +02:00
|
|
|
|
2018-02-24 15:27:06 +03: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,
|
2018-02-24 15:27:06 +03:00
|
|
|
node,
|
2016-05-10 01:11:59 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 21:28:50 +03:00
|
|
|
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
|
2018-07-20 15:59:00 +02:00
|
|
|
self.with_new_scopes(|this| {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(c.id);
|
|
|
|
hir::AnonConst {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
|
|
|
body: this.lower_body(None, |this| this.lower_expr(&c.value)),
|
|
|
|
}
|
|
|
|
})
|
2018-05-17 21:28:50 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-07-11 20:05:29 +08:00
|
|
|
ExprKind::Box(ref inner) => hir::ExprKind::Box(P(self.lower_expr(inner))),
|
2018-05-24 17:34:09 -04:00
|
|
|
ExprKind::ObsoleteInPlace(..) => {
|
|
|
|
self.sess.abort_if_errors();
|
|
|
|
span_bug!(e.span, "encountered ObsoleteInPlace expr during lowering");
|
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
ExprKind::Array(ref exprs) => {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Array(exprs.iter().map(|x| self.lower_expr(x)).collect())
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
ExprKind::Repeat(ref expr, ref count) => {
|
|
|
|
let expr = P(self.lower_expr(expr));
|
2018-05-17 21:28:50 +03:00
|
|
|
let count = self.lower_anon_const(count);
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Repeat(expr, count)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
ExprKind::Tup(ref elts) => {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Tup(elts.iter().map(|x| self.lower_expr(x)).collect())
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
ExprKind::Call(ref f, ref args) => {
|
|
|
|
let f = P(self.lower_expr(f));
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Call(f, args.iter().map(|x| self.lower_expr(x)).collect())
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2017-07-07 02:39:55 +03:00
|
|
|
ExprKind::MethodCall(ref seg, ref args) => {
|
2019-02-07 16:03:57 +11:00
|
|
|
let hir_seg = P(self.lower_path_segment(
|
2018-03-21 04:24:27 -04:00
|
|
|
e.span,
|
|
|
|
seg,
|
|
|
|
ParamMode::Optional,
|
|
|
|
0,
|
|
|
|
ParenthesizedGenericArgs::Err,
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::disallowed(),
|
2018-10-11 21:15:18 +13:00
|
|
|
None,
|
2019-02-07 16:03:57 +11:00
|
|
|
));
|
2017-03-14 15:50:40 +01:00
|
|
|
let args = args.iter().map(|x| self.lower_expr(x)).collect();
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::MethodCall(hir_seg, seg.ident.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));
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Binary(binop, lhs, rhs)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
ExprKind::Unary(op, ref ohs) => {
|
|
|
|
let op = self.lower_unop(op);
|
|
|
|
let ohs = P(self.lower_expr(ohs));
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Unary(op, ohs)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2019-01-16 13:51:24 +11:00
|
|
|
ExprKind::Lit(ref l) => hir::ExprKind::Lit((*l).clone()),
|
2017-03-14 15:50:40 +01:00
|
|
|
ExprKind::Cast(ref expr, ref ty) => {
|
|
|
|
let expr = P(self.lower_expr(expr));
|
2018-09-27 22:34:19 +01:00
|
|
|
hir::ExprKind::Cast(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));
|
2018-09-27 22:34:19 +01:00
|
|
|
hir::ExprKind::Type(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));
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::AddrOf(m, ohs)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
// 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(..) => {
|
2018-11-27 02:59:49 +00:00
|
|
|
// Wrap the `if let` expr in a block.
|
2017-03-14 15:50:40 +01:00
|
|
|
let span = els.span;
|
|
|
|
let els = P(self.lower_expr(els));
|
2018-03-21 04:24:27 -04: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());
|
|
|
|
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::If(P(self.lower_expr(cond)), P(then_expr), else_opt)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
ExprKind::While(ref cond, ref body, opt_label) => self.with_loop_scope(e.id, |this| {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::While(
|
2018-03-21 04:24:27 -04:00
|
|
|
this.with_loop_condition_scope(|this| P(this.lower_expr(cond))),
|
|
|
|
this.lower_block(body, false),
|
|
|
|
this.lower_label(opt_label),
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Loop(
|
2018-03-21 04:24:27 -04:00
|
|
|
this.lower_block(body, false),
|
|
|
|
this.lower_label(opt_label),
|
|
|
|
hir::LoopSource::Loop,
|
|
|
|
)
|
|
|
|
}),
|
2018-07-21 18:47:02 -07:00
|
|
|
ExprKind::TryBlock(ref body) => {
|
2018-03-25 00:16:16 -07:00
|
|
|
self.with_catch_scope(body.id, |this| {
|
2019-02-04 11:24:55 +01:00
|
|
|
let unstable_span = this.mark_span_with_reason(
|
2019-02-03 12:55:00 +01:00
|
|
|
CompilerDesugaringKind::TryBlock,
|
|
|
|
body.span,
|
2019-02-07 14:19:06 +01:00
|
|
|
Some(vec![
|
2019-02-03 12:55:00 +01:00
|
|
|
Symbol::intern("try_trait"),
|
2019-02-07 14:19:06 +01:00
|
|
|
].into()),
|
2019-02-03 12:55:00 +01:00
|
|
|
);
|
2018-03-25 00:16:16 -07:00
|
|
|
let mut block = this.lower_block(body, true).into_inner();
|
|
|
|
let tail = block.expr.take().map_or_else(
|
|
|
|
|| {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = this.next_id();
|
2018-08-18 12:14:09 +02:00
|
|
|
let span = this.sess.source_map().end_point(unstable_span);
|
2018-03-25 00:16:16 -07:00
|
|
|
hir::Expr {
|
|
|
|
id: node_id,
|
2018-04-10 00:45:57 -07:00
|
|
|
span,
|
2018-07-11 20:05:29 +08:00
|
|
|
node: hir::ExprKind::Tup(hir_vec![]),
|
2018-03-25 00:16:16 -07:00
|
|
|
attrs: ThinVec::new(),
|
|
|
|
hir_id,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|x: P<hir::Expr>| x.into_inner(),
|
|
|
|
);
|
|
|
|
block.expr = Some(this.wrap_in_try_constructor(
|
|
|
|
"from_ok", tail, unstable_span));
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Block(P(block), None)
|
2018-03-25 00:16:16 -07:00
|
|
|
})
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
ExprKind::Match(ref expr, ref arms) => hir::ExprKind::Match(
|
2018-03-21 04:24:27 -04:00
|
|
|
P(self.lower_expr(expr)),
|
|
|
|
arms.iter().map(|x| self.lower_arm(x)).collect(),
|
|
|
|
hir::MatchSource::Normal,
|
|
|
|
),
|
2018-06-06 15:50:59 -07:00
|
|
|
ExprKind::Async(capture_clause, closure_node_id, ref block) => {
|
|
|
|
self.make_async_expr(capture_clause, closure_node_id, None, |this| {
|
|
|
|
this.with_new_scopes(|this| {
|
|
|
|
let block = this.lower_block(block, false);
|
|
|
|
this.expr_block(block, ThinVec::new())
|
|
|
|
})
|
|
|
|
})
|
2018-06-23 13:12:15 +01:00
|
|
|
}
|
2018-06-06 15:50:59 -07:00
|
|
|
ExprKind::Closure(
|
2018-06-23 13:12:15 +01:00
|
|
|
capture_clause, asyncness, movability, ref decl, ref body, fn_decl_span
|
|
|
|
) => {
|
2018-06-26 16:35:52 +02:00
|
|
|
if let IsAsync::Async { closure_id, .. } = asyncness {
|
2018-06-23 13:12:15 +01:00
|
|
|
let outer_decl = FnDecl {
|
|
|
|
inputs: decl.inputs.clone(),
|
|
|
|
output: FunctionRetTy::Default(fn_decl_span),
|
|
|
|
variadic: false,
|
|
|
|
};
|
|
|
|
// We need to lower the declaration outside the new scope, because we
|
|
|
|
// have to conserve the state of being inside a loop condition for the
|
|
|
|
// closure argument types.
|
2018-06-26 11:56:24 +02:00
|
|
|
let fn_decl = self.lower_fn_decl(&outer_decl, None, false, None);
|
2018-06-23 13:12:15 +01:00
|
|
|
|
|
|
|
self.with_new_scopes(|this| {
|
2018-11-27 02:59:49 +00:00
|
|
|
// FIXME(cramertj): allow `async` non-`move` closures with arguments.
|
2018-06-06 15:50:59 -07:00
|
|
|
if capture_clause == CaptureBy::Ref &&
|
|
|
|
!decl.inputs.is_empty()
|
|
|
|
{
|
|
|
|
struct_span_err!(
|
2018-05-24 14:51:33 +02:00
|
|
|
this.sess,
|
|
|
|
fn_decl_span,
|
2018-06-23 20:14:04 +02:00
|
|
|
E0708,
|
2018-06-06 15:50:59 -07:00
|
|
|
"`async` non-`move` closures with arguments \
|
|
|
|
are not currently supported",
|
|
|
|
)
|
|
|
|
.help("consider using `let` statements to manually capture \
|
2018-11-27 02:59:49 +00:00
|
|
|
variables by reference before entering an \
|
|
|
|
`async move` closure")
|
2018-06-06 15:50:59 -07:00
|
|
|
.emit();
|
2018-05-24 14:51:33 +02:00
|
|
|
}
|
2018-06-06 15:50:59 -07:00
|
|
|
|
|
|
|
// Transform `async |x: u8| -> X { ... }` into
|
2018-11-27 02:59:49 +00:00
|
|
|
// `|x: u8| future_from_generator(|| -> X { ... })`.
|
2018-06-06 15:50:59 -07:00
|
|
|
let body_id = this.lower_body(Some(&outer_decl), |this| {
|
|
|
|
let async_ret_ty = if let FunctionRetTy::Ty(ty) = &decl.output {
|
|
|
|
Some(&**ty)
|
|
|
|
} else { None };
|
|
|
|
let async_body = this.make_async_expr(
|
2018-06-26 11:56:24 +02:00
|
|
|
capture_clause, closure_id, async_ret_ty,
|
2018-06-06 15:50:59 -07:00
|
|
|
|this| {
|
|
|
|
this.with_new_scopes(|this| this.lower_expr(body))
|
|
|
|
});
|
|
|
|
this.expr(fn_decl_span, async_body, ThinVec::new())
|
|
|
|
});
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Closure(
|
2018-06-06 15:50:59 -07:00
|
|
|
this.lower_capture_clause(capture_clause),
|
2018-06-23 13:12:15 +01:00
|
|
|
fn_decl,
|
2018-06-06 15:50:59 -07:00
|
|
|
body_id,
|
|
|
|
fn_decl_span,
|
|
|
|
None,
|
|
|
|
)
|
2018-06-23 13:12:15 +01:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Lower outside new scope to preserve `is_in_loop_condition`.
|
2018-06-26 11:56:24 +02:00
|
|
|
let fn_decl = self.lower_fn_decl(decl, None, false, None);
|
2018-06-23 13:12:15 +01:00
|
|
|
|
|
|
|
self.with_new_scopes(|this| {
|
2018-06-06 15:50:59 -07:00
|
|
|
let mut is_generator = false;
|
|
|
|
let body_id = this.lower_body(Some(decl), |this| {
|
|
|
|
let e = this.lower_expr(body);
|
|
|
|
is_generator = this.is_generator;
|
|
|
|
e
|
|
|
|
});
|
|
|
|
let generator_option = if is_generator {
|
|
|
|
if !decl.inputs.is_empty() {
|
|
|
|
span_err!(
|
|
|
|
this.sess,
|
|
|
|
fn_decl_span,
|
|
|
|
E0628,
|
|
|
|
"generators cannot have explicit arguments"
|
|
|
|
);
|
|
|
|
this.sess.abort_if_errors();
|
|
|
|
}
|
|
|
|
Some(match movability {
|
|
|
|
Movability::Movable => hir::GeneratorMovability::Movable,
|
|
|
|
Movability::Static => hir::GeneratorMovability::Static,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if movability == Movability::Static {
|
|
|
|
span_err!(
|
|
|
|
this.sess,
|
|
|
|
fn_decl_span,
|
2018-06-23 20:14:04 +02:00
|
|
|
E0697,
|
2018-06-06 15:50:59 -07:00
|
|
|
"closures cannot be static"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
};
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Closure(
|
2018-06-06 15:50:59 -07:00
|
|
|
this.lower_capture_clause(capture_clause),
|
2018-06-23 13:12:15 +01:00
|
|
|
fn_decl,
|
2018-06-06 15:50:59 -07:00
|
|
|
body_id,
|
|
|
|
fn_decl_span,
|
|
|
|
generator_option,
|
|
|
|
)
|
2018-06-23 13:12:15 +01:00
|
|
|
})
|
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-04-16 05:44:39 +02:00
|
|
|
ExprKind::Block(ref blk, opt_label) => {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Block(self.lower_block(blk,
|
2018-09-08 07:52:03 +01:00
|
|
|
opt_label.is_some()),
|
|
|
|
self.lower_label(opt_label))
|
2018-04-16 05:44:39 +02:00
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
ExprKind::Assign(ref el, ref er) => {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Assign(P(self.lower_expr(el)), P(self.lower_expr(er)))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
ExprKind::AssignOp(op, ref el, ref er) => hir::ExprKind::AssignOp(
|
2018-03-21 04:24:27 -04:00
|
|
|
self.lower_binop(op),
|
|
|
|
P(self.lower_expr(el)),
|
|
|
|
P(self.lower_expr(er)),
|
|
|
|
),
|
2018-07-11 20:05:29 +08:00
|
|
|
ExprKind::Field(ref el, ident) => hir::ExprKind::Field(P(self.lower_expr(el)), ident),
|
2017-03-14 15:50:40 +01:00
|
|
|
ExprKind::Index(ref el, ref er) => {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Index(P(self.lower_expr(el)), P(self.lower_expr(er)))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Desugar `<start>..=<end>` into `std::ops::RangeInclusive::new(<start>, <end>)`.
|
2018-04-06 02:03:22 +08:00
|
|
|
ExprKind::Range(Some(ref e1), Some(ref e2), RangeLimits::Closed) => {
|
2018-05-03 22:36:44 +08:00
|
|
|
let id = self.next_id();
|
2018-04-06 02:03:22 +08:00
|
|
|
let e1 = self.lower_expr(e1);
|
|
|
|
let e2 = self.lower_expr(e2);
|
2018-08-01 07:37:38 -06:00
|
|
|
let ty_path = P(self.std_path(e.span, &["ops", "RangeInclusive"], None, false));
|
|
|
|
let ty = P(self.ty_path(id, e.span, hir::QPath::Resolved(None, ty_path)));
|
2018-06-28 00:12:17 +03:00
|
|
|
let new_seg = P(hir::PathSegment::from_ident(Ident::from_str("new")));
|
2018-04-06 02:03:22 +08:00
|
|
|
let new_path = hir::QPath::TypeRelative(ty, new_seg);
|
2018-08-01 07:37:38 -06:00
|
|
|
let new = P(self.expr(e.span, hir::ExprKind::Path(new_path), ThinVec::new()));
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Call(new, hir_vec![e1, e2])
|
2018-04-06 02:03:22 +08:00
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
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",
|
2018-04-06 02:03:22 +08:00
|
|
|
(&Some(..), &Some(..), Closed) => unreachable!(),
|
2018-03-21 04:24:27 -04:00
|
|
|
(_, &None, Closed) => self.diagnostic()
|
|
|
|
.span_fatal(e.span, "inclusive range with no end")
|
|
|
|
.raise(),
|
2017-03-23 19:26:38 +02:00
|
|
|
};
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
let fields = e1.iter()
|
|
|
|
.map(|e| ("start", e))
|
|
|
|
.chain(e2.iter().map(|e| ("end", e)))
|
2017-03-23 19:26:38 +02:00
|
|
|
.map(|(s, e)| {
|
|
|
|
let expr = P(self.lower_expr(&e));
|
2018-08-01 07:37:38 -06:00
|
|
|
let ident = Ident::new(Symbol::intern(s), e.span);
|
|
|
|
self.field(ident, expr, e.span)
|
2018-03-21 04:24:27 -04:00
|
|
|
})
|
|
|
|
.collect::<P<[hir::Field]>>();
|
2017-03-23 19:26:38 +02:00
|
|
|
|
|
|
|
let is_unit = fields.is_empty();
|
2018-10-13 20:51:25 +02:00
|
|
|
let struct_path = ["ops", path];
|
2018-08-01 07:37:38 -06:00
|
|
|
let struct_path = self.std_path(e.span, &struct_path, None, is_unit);
|
2017-03-23 19:26:38 +02:00
|
|
|
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 {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Path(struct_path)
|
2017-03-23 19:26:38 +02:00
|
|
|
} else {
|
2019-02-07 16:03:57 +11:00
|
|
|
hir::ExprKind::Struct(P(struct_path), fields, None)
|
2017-03-23 19:26:38 +02:00
|
|
|
},
|
2018-08-01 07:37:38 -06:00
|
|
|
span: e.span,
|
2017-03-23 19:26:38 +02:00
|
|
|
attrs: e.attrs.clone(),
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
|
|
|
}
|
2018-09-06 10:46:55 +08:00
|
|
|
ExprKind::Path(ref qself, ref path) => {
|
|
|
|
let qpath = self.lower_qpath(
|
|
|
|
e.id,
|
|
|
|
qself,
|
|
|
|
path,
|
|
|
|
ParamMode::Optional,
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::disallowed(),
|
2018-09-06 10:46:55 +08:00
|
|
|
);
|
|
|
|
hir::ExprKind::Path(qpath)
|
|
|
|
}
|
2018-01-16 01:44:32 +03:00
|
|
|
ExprKind::Break(opt_label, ref opt_expr) => {
|
|
|
|
let destination = if self.is_in_loop_condition && opt_label.is_none() {
|
2017-03-14 15:50:40 +01:00
|
|
|
hir::Destination {
|
2018-01-16 01:44:32 +03:00
|
|
|
label: None,
|
2018-05-11 15:00:09 +02:00
|
|
|
target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-16 01:44:32 +03:00
|
|
|
self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Break(
|
2018-03-21 04:24:27 -04:00
|
|
|
destination,
|
|
|
|
opt_expr.as_ref().map(|x| P(self.lower_expr(x))),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ExprKind::Continue(opt_label) => {
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Continue(if self.is_in_loop_condition && opt_label.is_none() {
|
2018-03-21 04:24:27 -04:00
|
|
|
hir::Destination {
|
|
|
|
label: None,
|
2018-05-11 15:00:09 +02:00
|
|
|
target_id: Err(hir::LoopIdError::UnlabeledCfInWhileCondition).into(),
|
2018-03-21 04:24:27 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.lower_loop_destination(opt_label.map(|label| (e.id, label)))
|
|
|
|
})
|
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
ExprKind::Ret(ref e) => hir::ExprKind::Ret(e.as_ref().map(|x| P(self.lower_expr(x)))),
|
2017-03-14 15:50:40 +01:00
|
|
|
ExprKind::InlineAsm(ref asm) => {
|
|
|
|
let hir_asm = hir::InlineAsm {
|
|
|
|
inputs: asm.inputs.iter().map(|&(ref c, _)| c.clone()).collect(),
|
2018-03-21 04:24:27 -04:00
|
|
|
outputs: asm.outputs
|
|
|
|
.iter()
|
|
|
|
.map(|out| hir::InlineAsmOutput {
|
2017-03-14 15:50:40 +01:00
|
|
|
constraint: out.constraint.clone(),
|
|
|
|
is_rw: out.is_rw,
|
|
|
|
is_indirect: out.is_indirect,
|
2018-10-09 15:53:37 -07:00
|
|
|
span: out.expr.span,
|
2018-03-21 04:24:27 -04:00
|
|
|
})
|
|
|
|
.collect(),
|
2017-03-14 15:50:40 +01:00
|
|
|
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
|
|
|
};
|
2018-03-21 04:24:27 -04: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();
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::InlineAsm(P(hir_asm), outputs, inputs)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-07-11 20:05:29 +08:00
|
|
|
ExprKind::Struct(ref path, ref fields, ref maybe_expr) => hir::ExprKind::Struct(
|
2019-02-07 16:03:57 +11:00
|
|
|
P(self.lower_qpath(
|
2018-03-21 04:24:27 -04:00
|
|
|
e.id,
|
|
|
|
&None,
|
|
|
|
path,
|
|
|
|
ParamMode::Optional,
|
2018-09-27 22:34:19 +01:00
|
|
|
ImplTraitContext::disallowed(),
|
2019-02-07 16:03:57 +11:00
|
|
|
)),
|
2018-03-21 04:24:27 -04:00
|
|
|
fields.iter().map(|x| self.lower_field(x)).collect(),
|
|
|
|
maybe_expr.as_ref().map(|x| P(self.lower_expr(x))),
|
|
|
|
),
|
2017-03-14 15:50:40 +01:00
|
|
|
ExprKind::Paren(ref ex) => {
|
|
|
|
let mut ex = self.lower_expr(ex);
|
2018-11-27 02:59:49 +00:00
|
|
|
// Include parens in span, but only if it is a super-span.
|
2017-03-14 15:50:40 +01:00
|
|
|
if e.span.contains(ex.span) {
|
|
|
|
ex.span = e.span;
|
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
// Merge attributes into the inner expression.
|
2017-03-14 15:50:40 +01:00
|
|
|
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;
|
2018-03-21 04:24:27 -04:00
|
|
|
let expr = opt_expr
|
|
|
|
.as_ref()
|
|
|
|
.map(|x| self.lower_expr(x))
|
2018-07-12 08:20:31 +08:00
|
|
|
.unwrap_or_else(||
|
|
|
|
self.expr(e.span, hir::ExprKind::Tup(hir_vec![]), ThinVec::new())
|
|
|
|
);
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Yield(P(expr))
|
2016-12-26 14:34:03 +01:00
|
|
|
}
|
|
|
|
|
2018-12-17 04:57:32 +03:00
|
|
|
ExprKind::Err => hir::ExprKind::Err,
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Desugar `ExprIfLet`
|
|
|
|
// from: `if let <pat> = <sub_expr> <body> [<else_opt>]`
|
2018-02-24 03:12:35 +03:00
|
|
|
ExprKind::IfLet(ref pats, ref sub_expr, ref body, ref else_opt) => {
|
2017-03-14 15:50:40 +01:00
|
|
|
// 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()));
|
2018-02-24 03:12:35 +03:00
|
|
|
let pats = pats.iter().map(|pat| self.lower_pat(pat)).collect();
|
|
|
|
arms.push(self.arm(pats, body_expr));
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Match(
|
2017-03-14 15:50:40 +01:00
|
|
|
sub_expr,
|
|
|
|
arms.into(),
|
|
|
|
hir::MatchSource::IfLetDesugar {
|
2017-07-03 11:19:51 -07:00
|
|
|
contains_else_clause,
|
2018-03-21 04:24:27 -04:00
|
|
|
},
|
|
|
|
)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Desugar `ExprWhileLet`
|
|
|
|
// from: `[opt_ident]: while let <pat> = <sub_expr> <body>`
|
2018-02-24 03:12:35 +03:00
|
|
|
ExprKind::WhileLet(ref pats, ref sub_expr, ref body, opt_label) => {
|
2017-03-14 15:50:40 +01:00
|
|
|
// 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.
|
2018-03-21 04:24:27 -04:00
|
|
|
let (body, break_expr, sub_expr) = self.with_loop_scope(e.id, |this| {
|
|
|
|
(
|
|
|
|
this.lower_block(body, false),
|
|
|
|
this.expr_break(e.span, ThinVec::new()),
|
|
|
|
this.with_loop_condition_scope(|this| P(this.lower_expr(sub_expr))),
|
|
|
|
)
|
|
|
|
});
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
// `<pat> => <body>`
|
|
|
|
let pat_arm = {
|
|
|
|
let body_expr = P(self.expr_block(body, ThinVec::new()));
|
2018-02-24 03:12:35 +03:00
|
|
|
let pats = pats.iter().map(|pat| self.lower_pat(pat)).collect();
|
|
|
|
self.arm(pats, body_expr)
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
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];
|
2018-03-21 04:24:27 -04:00
|
|
|
let match_expr = self.expr(
|
|
|
|
sub_expr.span,
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Match(sub_expr, arms, hir::MatchSource::WhileLetDesugar),
|
2018-03-21 04:24:27 -04:00
|
|
|
ThinVec::new(),
|
|
|
|
);
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
// `[opt_ident]: loop { ... }`
|
|
|
|
let loop_block = P(self.block_expr(P(match_expr)));
|
2018-07-11 20:05:29 +08:00
|
|
|
let loop_expr = hir::ExprKind::Loop(
|
2018-03-21 04:24:27 -04:00
|
|
|
loop_block,
|
|
|
|
self.lower_label(opt_label),
|
|
|
|
hir::LoopSource::WhileLet,
|
|
|
|
);
|
2018-11-27 02:59:49 +00:00
|
|
|
// Add attributes to the outer returned expr node.
|
2017-03-14 15:50:40 +01:00
|
|
|
loop_expr
|
|
|
|
}
|
2016-05-02 23:26:18 +00:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Desugar `ExprForLoop`
|
|
|
|
// from: `[opt_ident]: for <pat> in <head> <body>`
|
2018-01-16 01:44:32 +03:00
|
|
|
ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => {
|
2017-03-14 15:50:40 +01:00
|
|
|
// 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;
|
2018-07-11 18:54:37 +08:00
|
|
|
// StmtKind::Expr(<body>);
|
2017-03-14 15:50:40 +01:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// result
|
|
|
|
// }
|
|
|
|
|
|
|
|
// expand <head>
|
|
|
|
let head = self.lower_expr(head);
|
2018-01-25 14:07:45 -08:00
|
|
|
let head_sp = head.span;
|
2019-02-04 11:24:55 +01:00
|
|
|
let desugared_span = self.mark_span_with_reason(
|
2018-10-03 14:14:11 +01:00
|
|
|
CompilerDesugaringKind::ForLoop,
|
2018-10-04 14:48:32 +01:00
|
|
|
head_sp,
|
2019-02-07 14:19:06 +01:00
|
|
|
None,
|
2018-10-03 14:14:11 +01:00
|
|
|
);
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
let iter = self.str_to_ident("iter");
|
|
|
|
|
2017-06-23 18:23:23 +02:00
|
|
|
let next_ident = self.str_to_ident("__next");
|
2018-03-21 04:24:27 -04:00
|
|
|
let next_pat = self.pat_ident_binding_mode(
|
2018-10-03 14:14:11 +01:00
|
|
|
desugared_span,
|
2018-03-21 04:24:27 -04:00
|
|
|
next_ident,
|
|
|
|
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");
|
2018-01-25 14:07:45 -08:00
|
|
|
let val_pat = self.pat_ident(pat.span, val_ident);
|
|
|
|
let val_expr = P(self.expr_ident(pat.span, val_ident, val_pat.id));
|
|
|
|
let next_expr = P(self.expr_ident(pat.span, next_ident, next_pat.id));
|
2018-03-21 04:24:27 -04:00
|
|
|
let assign = P(self.expr(
|
|
|
|
pat.span,
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Assign(next_expr, val_expr),
|
2018-03-21 04:24:27 -04:00
|
|
|
ThinVec::new(),
|
|
|
|
));
|
2018-01-25 14:07:45 -08:00
|
|
|
let some_pat = self.pat_some(pat.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 = {
|
2018-03-21 04:24:27 -04:00
|
|
|
let break_expr =
|
|
|
|
self.with_loop_scope(e.id, |this| this.expr_break(e.span, ThinVec::new()));
|
2017-03-14 15:50:40 +01:00
|
|
|
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`
|
2018-10-03 14:14:11 +01:00
|
|
|
let iter_pat = self.pat_ident_binding_mode(
|
|
|
|
desugared_span,
|
|
|
|
iter,
|
|
|
|
hir::BindingAnnotation::Mutable
|
|
|
|
);
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
// `match ::std::iter::Iterator::next(&mut iter) { ... }`
|
|
|
|
let match_expr = {
|
2018-01-25 14:07:45 -08:00
|
|
|
let iter = P(self.expr_ident(head_sp, iter, iter_pat.id));
|
|
|
|
let ref_mut_iter = self.expr_mut_addr_of(head_sp, iter);
|
2017-03-14 15:50:40 +01:00
|
|
|
let next_path = &["iter", "Iterator", "next"];
|
2018-06-21 22:24:24 -07:00
|
|
|
let next_path = P(self.expr_std_path(head_sp, next_path, None, ThinVec::new()));
|
2018-03-21 04:24:27 -04:00
|
|
|
let next_expr = P(self.expr_call(head_sp, next_path, hir_vec![ref_mut_iter]));
|
2017-03-14 15:50:40 +01:00
|
|
|
let arms = hir_vec![pat_arm, break_arm];
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
P(self.expr(
|
|
|
|
head_sp,
|
2018-07-12 08:20:31 +08:00
|
|
|
hir::ExprKind::Match(
|
|
|
|
next_expr,
|
|
|
|
arms,
|
|
|
|
hir::MatchSource::ForLoopDesugar
|
|
|
|
),
|
2018-03-21 04:24:27 -04:00
|
|
|
ThinVec::new(),
|
|
|
|
))
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
2019-01-17 09:45:02 +11:00
|
|
|
let match_stmt = hir::Stmt {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2019-01-17 09:45:02 +11:00
|
|
|
node: hir::StmtKind::Expr(match_expr),
|
|
|
|
span: head_sp,
|
|
|
|
};
|
2017-06-13 18:36:01 +02:00
|
|
|
|
2018-01-25 14:07:45 -08:00
|
|
|
let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat.id));
|
2017-06-14 13:36:30 +02:00
|
|
|
|
2017-06-23 18:23:23 +02:00
|
|
|
// `let mut __next`
|
2018-10-03 14:14:11 +01:00
|
|
|
let next_let = self.stmt_let_pat(
|
|
|
|
desugared_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);
|
2018-03-21 04:24:27 -04:00
|
|
|
let pat_let = self.stmt_let_pat(
|
|
|
|
head_sp,
|
2017-06-13 18:36:01 +02:00
|
|
|
Some(next_expr),
|
2017-05-27 20:20:17 +02:00
|
|
|
pat,
|
2018-03-21 04:24:27 -04:00
|
|
|
hir::LocalSource::ForLoopDesugar,
|
|
|
|
);
|
2017-05-27 20:20:17 +02:00
|
|
|
|
2018-01-25 14:07:45 -08:00
|
|
|
let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
|
2017-05-27 20:20:17 +02:00
|
|
|
let body_expr = P(self.expr_block(body_block, ThinVec::new()));
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
2019-01-17 09:45:02 +11:00
|
|
|
let body_stmt = hir::Stmt {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2019-01-17 09:45:02 +11:00
|
|
|
node: hir::StmtKind::Expr(body_expr),
|
|
|
|
span: body.span,
|
|
|
|
};
|
2017-05-27 20:20:17 +02:00
|
|
|
|
2018-03-21 04:24:27 -04: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 { ... }`
|
2018-07-11 20:05:29 +08:00
|
|
|
let loop_expr = hir::ExprKind::Loop(
|
2018-03-21 04:24:27 -04:00
|
|
|
loop_block,
|
|
|
|
self.lower_label(opt_label),
|
|
|
|
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"];
|
2018-06-21 22:24:24 -07:00
|
|
|
let into_iter = P(self.expr_std_path(
|
|
|
|
head_sp, into_iter_path, None, ThinVec::new()));
|
2018-01-25 14:07:45 -08:00
|
|
|
P(self.expr_call(head_sp, into_iter, hir_vec![head]))
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
2016-05-10 01:11:59 +00:00
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
let match_expr = P(self.expr_match(
|
|
|
|
head_sp,
|
|
|
|
into_iter_expr,
|
|
|
|
hir_vec![iter_arm],
|
|
|
|
hir::MatchSource::ForLoopDesugar,
|
|
|
|
));
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
// `{ let _result = ...; _result }`
|
2018-11-27 02:59:49 +00:00
|
|
|
// Underscore prevents an `unused_variables` lint if the head diverges.
|
2017-03-14 15:50:40 +01:00
|
|
|
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)));
|
2018-11-27 02:59:49 +00:00
|
|
|
// Add the attributes to the outer returned expr node.
|
2017-03-14 15:50:40 +01:00
|
|
|
return self.expr_block(block, e.attrs.clone());
|
|
|
|
}
|
2017-02-17 15:12:47 -08:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Desugar `ExprKind::Try`
|
|
|
|
// from: `<expr>?`
|
2017-03-14 15:50:40 +01:00
|
|
|
ExprKind::Try(ref sub_expr) => {
|
2018-11-27 02:59:49 +00:00
|
|
|
// into:
|
2017-03-14 15:50:40 +01:00
|
|
|
//
|
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
|
|
|
|
2019-02-04 11:24:55 +01:00
|
|
|
let unstable_span = self.mark_span_with_reason(
|
2019-02-03 12:55:00 +01:00
|
|
|
CompilerDesugaringKind::QuestionMark,
|
|
|
|
e.span,
|
2019-02-07 14:19:06 +01:00
|
|
|
Some(vec![
|
2019-02-03 12:55:00 +01:00
|
|
|
Symbol::intern("try_trait")
|
2019-02-07 14:19:06 +01:00
|
|
|
].into()),
|
2019-02-03 12:55:00 +01:00
|
|
|
);
|
2016-05-03 13:01:54 +12:00
|
|
|
|
2018-11-27 02:59:49 +00: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"];
|
2018-06-21 22:24:24 -07:00
|
|
|
let path = P(self.expr_std_path(
|
|
|
|
unstable_span, path, None, ThinVec::new()));
|
2017-03-14 15:50:40 +01:00
|
|
|
P(self.expr_call(e.span, path, hir_vec![sub_expr]))
|
|
|
|
};
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// `#[allow(unreachable_code)]`
|
2017-03-14 15:50:40 +01:00
|
|
|
let attr = {
|
2018-11-27 02:59:49 +00:00
|
|
|
// `allow(unreachable_code)`
|
2017-03-14 15:50:40 +01:00
|
|
|
let allow = {
|
2018-03-24 21:17:27 +03:00
|
|
|
let allow_ident = Ident::from_str("allow").with_span_pos(e.span);
|
|
|
|
let uc_ident = Ident::from_str("unreachable_code").with_span_pos(e.span);
|
|
|
|
let uc_nested = attr::mk_nested_word_item(uc_ident);
|
|
|
|
attr::mk_list_item(e.span, allow_ident, vec![uc_nested])
|
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];
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// `Ok(val) => #[allow(unreachable_code)] val,`
|
2017-03-14 15:50:40 +01:00
|
|
|
let ok_arm = {
|
|
|
|
let val_ident = self.str_to_ident("val");
|
|
|
|
let val_pat = self.pat_ident(e.span, val_ident);
|
2018-03-21 04:24:27 -04:00
|
|
|
let val_expr = P(self.expr_ident_with_attrs(
|
|
|
|
e.span,
|
|
|
|
val_ident,
|
|
|
|
val_pat.id,
|
|
|
|
ThinVec::from(attrs.clone()),
|
|
|
|
));
|
2017-03-14 15:50:40 +01:00
|
|
|
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
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// `Err(err) => #[allow(unreachable_code)]
|
|
|
|
// 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"];
|
2018-06-21 22:24:24 -07:00
|
|
|
let from = P(self.expr_std_path(
|
|
|
|
e.span, path, None, ThinVec::new()));
|
2017-03-14 15:50:40 +01:00
|
|
|
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
|
|
|
};
|
2018-03-25 00:16:16 -07:00
|
|
|
let from_err_expr =
|
|
|
|
self.wrap_in_try_constructor("from_error", from_expr, unstable_span);
|
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,
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Break(
|
2017-03-14 15:50:40 +01:00
|
|
|
hir::Destination {
|
2018-01-16 01:44:32 +03:00
|
|
|
label: None,
|
2018-05-14 19:07:05 +02:00
|
|
|
target_id: Ok(catch_node),
|
2017-03-14 15:50:40 +01:00
|
|
|
},
|
2018-03-21 04:24:27 -04:00
|
|
|
Some(from_err_expr),
|
2017-03-14 15:50:40 +01:00
|
|
|
),
|
2018-03-21 04:24:27 -04:00
|
|
|
thin_attrs,
|
|
|
|
))
|
2017-03-14 15:50:40 +01:00
|
|
|
} else {
|
2018-07-11 20:05:29 +08:00
|
|
|
P(self.expr(e.span, hir::ExprKind::Ret(Some(from_err_expr)), thin_attrs))
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
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
|
|
|
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Match(
|
2018-03-21 04:24:27 -04:00
|
|
|
discr,
|
|
|
|
hir_vec![err_arm, ok_arm],
|
|
|
|
hir::MatchSource::TryDesugar,
|
|
|
|
)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
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
|
|
|
|
2018-08-30 11:42:16 +02:00
|
|
|
fn lower_stmt(&mut self, s: &Stmt) -> SmallVec<[hir::Stmt; 1]> {
|
2018-08-13 22:15:16 +03:00
|
|
|
smallvec![match s.node {
|
2018-08-19 03:40:50 +01:00
|
|
|
StmtKind::Local(ref l) => {
|
|
|
|
let (l, item_ids) = self.lower_local(l);
|
2018-08-30 11:42:16 +02:00
|
|
|
let mut ids: SmallVec<[hir::Stmt; 1]> = item_ids
|
2018-08-19 03:40:50 +01:00
|
|
|
.into_iter()
|
2019-02-02 15:40:08 +01:00
|
|
|
.map(|item_id| {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
|
|
|
|
|
|
|
hir::Stmt {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
|
|
|
node: hir::StmtKind::Item(P(item_id)),
|
|
|
|
span: s.span,
|
|
|
|
}
|
2018-08-19 03:40:50 +01:00
|
|
|
})
|
|
|
|
.collect();
|
2019-02-02 15:40:08 +01:00
|
|
|
ids.push({
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(s.id);
|
|
|
|
|
|
|
|
hir::Stmt {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
|
|
|
node: hir::StmtKind::Local(P(l)),
|
|
|
|
span: s.span,
|
|
|
|
}
|
2018-08-19 03:40:50 +01:00
|
|
|
});
|
|
|
|
return ids;
|
2016-06-17 02:30:01 +00:00
|
|
|
},
|
2016-11-24 06:11:31 +02:00
|
|
|
StmtKind::Item(ref it) => {
|
|
|
|
// Can only use the ID once.
|
|
|
|
let mut id = Some(s.id);
|
2018-03-21 04:24:27 -04:00
|
|
|
return self.lower_item_id(it)
|
|
|
|
.into_iter()
|
2019-02-02 15:40:08 +01:00
|
|
|
.map(|item_id| {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = id.take()
|
|
|
|
.map(|id| self.lower_node_id(id))
|
|
|
|
.unwrap_or_else(|| self.next_id());
|
|
|
|
|
|
|
|
hir::Stmt {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
|
|
|
node: hir::StmtKind::Item(P(item_id)),
|
|
|
|
span: s.span,
|
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
})
|
|
|
|
.collect();
|
2015-12-07 17:17:41 +03:00
|
|
|
}
|
2019-02-02 15:40:08 +01:00
|
|
|
StmtKind::Expr(ref e) => {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(s.id);
|
|
|
|
|
|
|
|
hir::Stmt {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
|
|
|
node: hir::StmtKind::Expr(P(self.lower_expr(e))),
|
|
|
|
span: s.span,
|
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
},
|
2019-02-02 15:40:08 +01:00
|
|
|
StmtKind::Semi(ref e) => {
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(s.id);
|
|
|
|
|
|
|
|
hir::Stmt {
|
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
|
|
|
node: hir::StmtKind::Semi(P(self.lower_expr(e))),
|
|
|
|
span: s.span,
|
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
},
|
2016-05-10 01:11:59 +00:00
|
|
|
StmtKind::Mac(..) => panic!("Shouldn't exist here"),
|
2018-08-13 22:15:16 +03: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.
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_visibility(
|
|
|
|
&mut self,
|
|
|
|
v: &Visibility,
|
|
|
|
explicit_owner: Option<NodeId>,
|
|
|
|
) -> hir::Visibility {
|
2018-06-30 20:34:18 -07:00
|
|
|
let node = match v.node {
|
2018-07-01 11:05:10 -07:00
|
|
|
VisibilityKind::Public => hir::VisibilityKind::Public,
|
|
|
|
VisibilityKind::Crate(sugar) => hir::VisibilityKind::Crate(sugar),
|
2018-07-31 10:43:51 -06:00
|
|
|
VisibilityKind::Restricted { ref path, id } => {
|
2018-11-21 13:35:54 -05:00
|
|
|
debug!("lower_visibility: restricted path id = {:?}", id);
|
2018-07-31 10:43:51 -06:00
|
|
|
let lowered_id = if let Some(owner) = explicit_owner {
|
|
|
|
self.lower_node_id_with_owner(id, owner)
|
2018-03-21 04:24:27 -04:00
|
|
|
} else {
|
2018-07-31 10:43:51 -06:00
|
|
|
self.lower_node_id(id)
|
|
|
|
};
|
2018-10-11 21:15:18 +13:00
|
|
|
let def = self.expect_full_def(id);
|
2018-07-31 10:43:51 -06:00
|
|
|
hir::VisibilityKind::Restricted {
|
2018-10-11 21:15:18 +13:00
|
|
|
path: P(self.lower_path_extra(
|
|
|
|
def,
|
|
|
|
path,
|
|
|
|
ParamMode::Explicit,
|
|
|
|
explicit_owner,
|
|
|
|
)),
|
2018-07-31 10:43:51 -06:00
|
|
|
id: lowered_id.node_id,
|
|
|
|
hir_id: lowered_id.hir_id,
|
|
|
|
}
|
2018-03-21 04:24:27 -04:00
|
|
|
},
|
2018-07-01 11:05:10 -07:00
|
|
|
VisibilityKind::Inherited => hir::VisibilityKind::Inherited,
|
2018-06-30 20:34:18 -07:00
|
|
|
};
|
|
|
|
respan(v.span, node)
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-07-03 19:38:14 +02:00
|
|
|
fn lower_defaultness(&self, d: Defaultness, has_value: bool) -> hir::Defaultness {
|
2016-05-10 01:11:59 +00:00
|
|
|
match d {
|
2018-03-21 04:24:27 -04:00
|
|
|
Defaultness::Default => hir::Defaultness::Default {
|
|
|
|
has_value: has_value,
|
|
|
|
},
|
2016-11-14 11:00:02 -05:00
|
|
|
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 {
|
2018-03-21 04:24:27 -04:00
|
|
|
BindingMode::ByValue(Mutability::Immutable) => hir::BindingAnnotation::Unannotated,
|
2017-07-21 19:29:43 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-26 02:50:15 +03:00
|
|
|
fn field(&mut self, ident: Ident, expr: P<hir::Expr>, span: Span) -> hir::Field {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
hir::Field {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-05-26 02:50:15 +03:00
|
|
|
ident,
|
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> {
|
2018-07-11 20:05:29 +08:00
|
|
|
let expr_break = hir::ExprKind::Break(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
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn expr_call(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
e: P<hir::Expr>,
|
|
|
|
args: hir::HirVec<hir::Expr>,
|
|
|
|
) -> hir::Expr {
|
2018-07-11 20:05:29 +08:00
|
|
|
self.expr(span, hir::ExprKind::Call(e, args), ThinVec::new())
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2018-06-10 17:40:45 +03:00
|
|
|
fn expr_ident(&mut self, span: Span, ident: Ident, binding: NodeId) -> hir::Expr {
|
|
|
|
self.expr_ident_with_attrs(span, ident, binding, ThinVec::new())
|
2017-01-17 21:32:27 +08:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn expr_ident_with_attrs(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
2018-06-10 17:40:45 +03:00
|
|
|
ident: Ident,
|
2018-03-21 04:24:27 -04:00
|
|
|
binding: NodeId,
|
|
|
|
attrs: ThinVec<Attribute>,
|
|
|
|
) -> hir::Expr {
|
2018-07-11 20:05:29 +08:00
|
|
|
let expr_path = hir::ExprKind::Path(hir::QPath::Resolved(
|
2018-03-21 04:24:27 -04:00
|
|
|
None,
|
|
|
|
P(hir::Path {
|
|
|
|
span,
|
|
|
|
def: Def::Local(binding),
|
2018-06-10 17:40:45 +03:00
|
|
|
segments: hir_vec![hir::PathSegment::from_ident(ident)],
|
2018-03-21 04:24:27 -04:00
|
|
|
}),
|
|
|
|
));
|
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 {
|
2018-07-11 20:05:29 +08:00
|
|
|
self.expr(span, hir::ExprKind::AddrOf(hir::MutMutable, e), ThinVec::new())
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn expr_std_path(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
components: &[&str],
|
2018-06-21 23:24:51 -07:00
|
|
|
params: Option<P<hir::GenericArgs>>,
|
2018-03-21 04:24:27 -04:00
|
|
|
attrs: ThinVec<Attribute>,
|
|
|
|
) -> hir::Expr {
|
2018-06-21 22:24:24 -07:00
|
|
|
let path = self.std_path(span, components, params, true);
|
2018-03-21 04:24:27 -04:00
|
|
|
self.expr(
|
|
|
|
span,
|
2018-07-11 20:05:29 +08:00
|
|
|
hir::ExprKind::Path(hir::QPath::Resolved(None, P(path))),
|
2018-03-21 04:24:27 -04:00
|
|
|
attrs,
|
|
|
|
)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn expr_match(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
arg: P<hir::Expr>,
|
|
|
|
arms: hir::HirVec<hir::Arm>,
|
|
|
|
source: hir::MatchSource,
|
|
|
|
) -> hir::Expr {
|
2018-07-11 20:05:29 +08:00
|
|
|
self.expr(span, hir::ExprKind::Match(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 {
|
2018-07-11 20:05:29 +08:00
|
|
|
self.expr(b.span, hir::ExprKind::Block(b, None), 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_tuple(&mut self, sp: Span, exprs: hir::HirVec<hir::Expr>) -> P<hir::Expr> {
|
2018-07-11 20:05:29 +08:00
|
|
|
P(self.expr(sp, hir::ExprKind::Tup(exprs), ThinVec::new()))
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 17:24:42 +13:00
|
|
|
|
2018-07-11 20:05:29 +08:00
|
|
|
fn expr(&mut self, span: Span, node: hir::ExprKind, 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
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn stmt_let_pat(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
|
|
|
ex: Option<P<hir::Expr>>,
|
|
|
|
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();
|
|
|
|
|
2019-01-17 10:39:24 +11:00
|
|
|
let local = 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,
|
2019-01-17 10:39:24 +11:00
|
|
|
};
|
2019-02-02 15:40:08 +01:00
|
|
|
|
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
2019-01-17 09:45:02 +11:00
|
|
|
hir::Stmt {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2019-01-17 10:39:24 +11:00
|
|
|
node: hir::StmtKind::Local(P(local)),
|
2019-01-17 09:45:02 +11:00
|
|
|
span: sp
|
|
|
|
}
|
2017-05-27 20:20:17 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn stmt_let(
|
|
|
|
&mut self,
|
|
|
|
sp: Span,
|
|
|
|
mutbl: bool,
|
2018-06-10 17:40:45 +03:00
|
|
|
ident: Ident,
|
2018-03-21 04:24:27 -04:00
|
|
|
ex: P<hir::Expr>,
|
|
|
|
) -> (hir::Stmt, NodeId) {
|
2017-05-27 20:20:17 +02:00
|
|
|
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;
|
2018-03-21 04:24:27 -04: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
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn block_all(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
stmts: hir::HirVec<hir::Stmt>,
|
|
|
|
expr: Option<P<hir::Expr>>,
|
|
|
|
) -> 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
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn pat_std_enum(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
components: &[&str],
|
|
|
|
subpats: hir::HirVec<P<hir::Pat>>,
|
|
|
|
) -> P<hir::Pat> {
|
2018-06-21 22:24:24 -07:00
|
|
|
let path = self.std_path(span, components, None, 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
|
|
|
|
2018-06-10 17:40:45 +03:00
|
|
|
fn pat_ident(&mut self, span: Span, ident: Ident) -> P<hir::Pat> {
|
|
|
|
self.pat_ident_binding_mode(span, ident, hir::BindingAnnotation::Unannotated)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn pat_ident_binding_mode(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
2018-06-10 17:40:45 +03:00
|
|
|
ident: Ident,
|
2018-03-21 04:24:27 -04:00
|
|
|
bm: hir::BindingAnnotation,
|
|
|
|
) -> 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,
|
2019-02-02 15:40:08 +01:00
|
|
|
node: hir::PatKind::Binding(bm, node_id, hir_id, ident.with_span_pos(span), 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`.
|
2018-06-21 22:24:24 -07:00
|
|
|
fn std_path(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
components: &[&str],
|
2018-06-21 23:24:51 -07:00
|
|
|
params: Option<P<hir::GenericArgs>>,
|
2018-06-21 22:24:24 -07:00
|
|
|
is_value: bool
|
|
|
|
) -> hir::Path {
|
2018-10-11 21:15:18 +13:00
|
|
|
let mut path = self.resolver
|
2018-10-25 15:23:45 +13:00
|
|
|
.resolve_str_path(span, self.crate_root, components, is_value);
|
|
|
|
path.segments.last_mut().unwrap().args = params;
|
|
|
|
|
2018-10-11 21:15:18 +13:00
|
|
|
|
|
|
|
for seg in path.segments.iter_mut() {
|
|
|
|
if let Some(id) = seg.id {
|
|
|
|
seg.id = Some(self.lower_node_id(id).node_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 19:05:27 +02:00
|
|
|
fn ty_path(&mut self, id: LoweredNodeId, span: Span, qpath: hir::QPath) -> hir::Ty {
|
2017-01-17 16:46:23 +02:00
|
|
|
let mut id = id;
|
|
|
|
let node = match qpath {
|
|
|
|
hir::QPath::Resolved(None, path) => {
|
2018-07-11 22:41:03 +08:00
|
|
|
// Turn trait object paths into `TyKind::TraitObject` instead.
|
2018-10-22 00:45:24 +01:00
|
|
|
match path.def {
|
|
|
|
Def::Trait(_) | Def::TraitAlias(_) => {
|
|
|
|
let principal = hir::PolyTraitRef {
|
|
|
|
bound_generic_params: hir::HirVec::new(),
|
|
|
|
trait_ref: hir::TraitRef {
|
|
|
|
path: path.and_then(|path| path),
|
|
|
|
ref_id: id.node_id,
|
|
|
|
hir_ref_id: id.hir_id,
|
|
|
|
},
|
|
|
|
span,
|
|
|
|
};
|
2017-01-17 16:46:23 +02:00
|
|
|
|
2018-10-22 00:45:24 +01:00
|
|
|
// The original ID is taken by the `PolyTraitRef`,
|
|
|
|
// so the `Ty` itself needs a different one.
|
|
|
|
id = self.next_id();
|
|
|
|
hir::TyKind::TraitObject(hir_vec![principal], self.elided_dyn_bound(span))
|
|
|
|
}
|
|
|
|
_ => hir::TyKind::Path(hir::QPath::Resolved(None, path)),
|
2017-01-17 16:46:23 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
_ => hir::TyKind::Path(qpath),
|
2017-01-17 16:46:23 +02:00
|
|
|
};
|
2018-06-20 19:05:27 +02:00
|
|
|
hir::Ty {
|
2018-03-21 04:24:27 -04:00
|
|
|
id: id.node_id,
|
|
|
|
hir_id: id.hir_id,
|
|
|
|
node,
|
|
|
|
span,
|
2018-06-20 19:05:27 +02:00
|
|
|
}
|
2016-10-27 05:17:42 +03:00
|
|
|
}
|
2017-01-09 17:46:11 +02:00
|
|
|
|
2018-03-21 16:59:28 -04:00
|
|
|
/// Invoked to create the lifetime argument for a type `&T`
|
|
|
|
/// with no explicit lifetime.
|
|
|
|
fn elided_ref_lifetime(&mut self, span: Span) -> hir::Lifetime {
|
2018-03-21 17:12:39 -04:00
|
|
|
match self.anonymous_lifetime_mode {
|
|
|
|
// Intercept when we are in an impl header and introduce an in-band lifetime.
|
|
|
|
// Hence `impl Foo for &u32` becomes `impl<'f> Foo for &'f u32` for some fresh
|
|
|
|
// `'f`.
|
|
|
|
AnonymousLifetimeMode::CreateParameter => {
|
|
|
|
let fresh_name = self.collect_fresh_in_band_lifetime(span);
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
2018-03-21 17:12:39 -04:00
|
|
|
hir::Lifetime {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_id,
|
2018-03-21 17:12:39 -04:00
|
|
|
span,
|
2018-06-01 23:23:48 +01:00
|
|
|
name: hir::LifetimeName::Param(fresh_name),
|
2018-03-21 17:12:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:51:44 -04:00
|
|
|
AnonymousLifetimeMode::ReportError => self.new_error_lifetime(None, span),
|
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
AnonymousLifetimeMode::PassThrough => self.new_implicit_lifetime(span),
|
|
|
|
}
|
2018-03-21 16:59:28 -04:00
|
|
|
}
|
|
|
|
|
2018-10-11 15:51:44 -04:00
|
|
|
/// Report an error on illegal use of `'_` or a `&T` with no explicit lifetime;
|
|
|
|
/// return a "error lifetime".
|
|
|
|
fn new_error_lifetime(&mut self, id: Option<NodeId>, span: Span) -> hir::Lifetime {
|
|
|
|
let (id, msg, label) = match id {
|
|
|
|
Some(id) => (id, "`'_` cannot be used here", "`'_` is a reserved lifetime name"),
|
|
|
|
|
|
|
|
None => (
|
|
|
|
self.next_id().node_id,
|
|
|
|
"`&` without an explicit lifetime name cannot be used here",
|
|
|
|
"explicit lifetime name needed here",
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut err = struct_span_err!(
|
|
|
|
self.sess,
|
|
|
|
span,
|
|
|
|
E0637,
|
|
|
|
"{}",
|
|
|
|
msg,
|
|
|
|
);
|
|
|
|
err.span_label(span, label);
|
|
|
|
err.emit();
|
|
|
|
|
|
|
|
self.new_named_lifetime(id, span, hir::LifetimeName::Error)
|
|
|
|
}
|
|
|
|
|
2018-03-21 16:59:28 -04:00
|
|
|
/// Invoked to create the lifetime argument(s) for a path like
|
|
|
|
/// `std::cell::Ref<T>`; note that implicit lifetimes in these
|
|
|
|
/// sorts of cases are deprecated. This may therefore report a warning or an
|
|
|
|
/// error, depending on the mode.
|
|
|
|
fn elided_path_lifetimes(&mut self, span: Span, count: usize) -> P<[hir::Lifetime]> {
|
2018-03-21 17:12:39 -04:00
|
|
|
match self.anonymous_lifetime_mode {
|
2018-11-27 02:59:49 +00:00
|
|
|
// N.B., We intentionally ignore the create-parameter mode here
|
2018-03-21 17:12:39 -04:00
|
|
|
// and instead "pass through" to resolve-lifetimes, which will then
|
|
|
|
// report an error. This is because we don't want to support
|
|
|
|
// impl elision for deprecated forms like
|
|
|
|
//
|
|
|
|
// impl Foo for std::cell::Ref<u32> // note lack of '_
|
|
|
|
AnonymousLifetimeMode::CreateParameter => {}
|
|
|
|
|
2018-10-11 15:51:44 -04:00
|
|
|
AnonymousLifetimeMode::ReportError => {
|
|
|
|
return (0..count)
|
|
|
|
.map(|_| self.new_error_lifetime(None, span))
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
// This is the normal case.
|
|
|
|
AnonymousLifetimeMode::PassThrough => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
(0..count)
|
|
|
|
.map(|_| self.new_implicit_lifetime(span))
|
|
|
|
.collect()
|
2018-03-21 16:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Invoked to create the lifetime argument(s) for an elided trait object
|
|
|
|
/// bound, like the bound in `Box<dyn Debug>`. This method is not invoked
|
|
|
|
/// when the bound is written, even if it is written with `'_` like in
|
|
|
|
/// `Box<dyn Debug + '_>`. In those cases, `lower_lifetime` is invoked.
|
|
|
|
fn elided_dyn_bound(&mut self, span: Span) -> hir::Lifetime {
|
2018-03-21 17:12:39 -04:00
|
|
|
match self.anonymous_lifetime_mode {
|
|
|
|
// NB. We intentionally ignore the create-parameter mode here.
|
|
|
|
// and instead "pass through" to resolve-lifetimes, which will apply
|
|
|
|
// the object-lifetime-defaulting rules. Elided object lifetime defaults
|
|
|
|
// do not act like other elided lifetimes. In other words, given this:
|
|
|
|
//
|
|
|
|
// impl Foo for Box<dyn Debug>
|
|
|
|
//
|
|
|
|
// we do not introduce a fresh `'_` to serve as the bound, but instead
|
|
|
|
// ultimately translate to the equivalent of:
|
|
|
|
//
|
|
|
|
// impl Foo for Box<dyn Debug + 'static>
|
|
|
|
//
|
|
|
|
// `resolve_lifetime` has the code to make that happen.
|
|
|
|
AnonymousLifetimeMode::CreateParameter => {}
|
|
|
|
|
2018-10-11 15:51:44 -04:00
|
|
|
AnonymousLifetimeMode::ReportError => {
|
|
|
|
// ReportError applies to explicit use of `'_`.
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:12:39 -04:00
|
|
|
// This is the normal case.
|
|
|
|
AnonymousLifetimeMode::PassThrough => {}
|
|
|
|
}
|
|
|
|
|
2018-03-21 16:59:28 -04:00
|
|
|
self.new_implicit_lifetime(span)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_implicit_lifetime(&mut self, span: Span) -> hir::Lifetime {
|
2019-02-02 15:40:08 +01:00
|
|
|
let LoweredNodeId { node_id, hir_id } = self.next_id();
|
|
|
|
|
2017-01-09 17:46:11 +02:00
|
|
|
hir::Lifetime {
|
2019-02-02 15:40:08 +01:00
|
|
|
id: node_id,
|
|
|
|
hir_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
|
|
|
}
|
|
|
|
}
|
2018-02-22 22:44:44 -08:00
|
|
|
|
2018-02-23 13:52:28 -08:00
|
|
|
fn maybe_lint_bare_trait(&self, span: Span, id: NodeId, is_global: bool) {
|
2018-04-13 17:07:33 -05:00
|
|
|
self.sess.buffer_lint_with_diagnostic(
|
2018-05-19 01:13:53 +03:00
|
|
|
builtin::BARE_TRAIT_OBJECTS,
|
2018-04-13 17:07:33 -05:00
|
|
|
id,
|
|
|
|
span,
|
|
|
|
"trait objects without an explicit `dyn` are deprecated",
|
|
|
|
builtin::BuiltinLintDiagnostics::BareTraitObject(span, is_global),
|
|
|
|
)
|
2018-02-22 22:44:44 -08:00
|
|
|
}
|
2018-03-25 00:16:16 -07:00
|
|
|
|
|
|
|
fn wrap_in_try_constructor(
|
|
|
|
&mut self,
|
|
|
|
method: &'static str,
|
|
|
|
e: hir::Expr,
|
|
|
|
unstable_span: Span,
|
|
|
|
) -> P<hir::Expr> {
|
|
|
|
let path = &["ops", "Try", method];
|
2018-06-21 22:24:24 -07:00
|
|
|
let from_err = P(self.expr_std_path(unstable_span, path, None,
|
2018-03-25 00:16:16 -07:00
|
|
|
ThinVec::new()));
|
|
|
|
P(self.expr_call(e.span, from_err, hir_vec![e]))
|
|
|
|
}
|
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
|
|
|
|
}
|