2019-02-08 07:53:55 -06:00
|
|
|
//! Reduced graph building.
|
2014-12-30 12:16:42 -06:00
|
|
|
//!
|
|
|
|
//! Here we build the "reduced graph": the graph of the module tree without
|
|
|
|
//! any imports resolved.
|
|
|
|
|
2019-02-06 11:15:23 -06:00
|
|
|
use crate::macros::{InvocationData, ParentScope, LegacyScope};
|
|
|
|
use crate::resolve_imports::ImportDirective;
|
|
|
|
use crate::resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
|
|
|
|
use crate::{Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, Segment, ToNameBinding};
|
|
|
|
use crate::{ModuleOrUniformRoot, PerNS, Resolver, ResolverArenas, ExternPreludeEntry};
|
|
|
|
use crate::Namespace::{self, TypeNS, ValueNS, MacroNS};
|
|
|
|
use crate::{resolve_error, resolve_struct_error, ResolutionError};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2019-02-06 11:15:23 -06:00
|
|
|
use rustc::bug;
|
2019-04-03 02:07:45 -05:00
|
|
|
use rustc::hir::def::{self, *};
|
2018-09-11 04:18:58 -05:00
|
|
|
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
|
2016-09-19 15:49:01 -05:00
|
|
|
use rustc::ty;
|
2018-07-31 16:23:31 -05:00
|
|
|
use rustc::middle::cstore::CrateStore;
|
2018-07-31 18:23:29 -05:00
|
|
|
use rustc_metadata::cstore::LoadedMacro;
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-08-19 19:23:32 -05:00
|
|
|
use std::cell::Cell;
|
2018-09-28 17:31:54 -05:00
|
|
|
use std::ptr;
|
2018-02-27 10:11:14 -06:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2016-08-19 19:23:32 -05:00
|
|
|
|
2019-02-07 09:56:05 -06:00
|
|
|
use errors::Applicability;
|
2019-01-17 09:18:56 -06:00
|
|
|
|
2016-11-28 20:07:12 -06:00
|
|
|
use syntax::ast::{Name, Ident};
|
2016-06-05 04:56:05 -05:00
|
|
|
use syntax::attr;
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2017-09-26 16:04:00 -05:00
|
|
|
use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId};
|
2019-04-19 15:32:26 -05:00
|
|
|
use syntax::ast::{MetaItemKind, StmtKind, TraitItem, TraitItemKind, Variant};
|
2018-07-12 05:24:59 -05:00
|
|
|
use syntax::ext::base::{MacroKind, SyntaxExtension};
|
2016-11-10 00:19:54 -06:00
|
|
|
use syntax::ext::base::Determinacy::Undetermined;
|
2016-09-16 01:45:03 -05:00
|
|
|
use syntax::ext::hygiene::Mark;
|
2016-10-01 20:41:19 -05:00
|
|
|
use syntax::ext::tt::macro_rules;
|
2019-01-07 09:09:17 -06:00
|
|
|
use syntax::feature_gate::is_builtin_attr;
|
2017-10-23 03:22:28 -05:00
|
|
|
use syntax::parse::token::{self, Token};
|
2019-02-06 11:15:23 -06:00
|
|
|
use syntax::span_err;
|
2017-12-12 13:57:58 -06:00
|
|
|
use syntax::std_inject::injected_crate_name;
|
2016-11-16 02:21:52 -06:00
|
|
|
use syntax::symbol::keywords;
|
2016-04-23 22:26:10 -05:00
|
|
|
use syntax::visit::{self, Visitor};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
|
|
|
|
2019-02-06 11:15:23 -06:00
|
|
|
use log::debug;
|
|
|
|
|
2019-04-03 02:07:45 -05:00
|
|
|
type Def = def::Def<NodeId>;
|
|
|
|
|
2016-11-07 16:23:26 -06:00
|
|
|
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
|
2016-11-28 20:53:00 -06:00
|
|
|
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
|
|
|
|
arenas.alloc_name_binding(NameBinding {
|
2016-11-07 16:23:26 -06:00
|
|
|
kind: NameBindingKind::Module(self.0),
|
2018-12-29 09:15:29 -06:00
|
|
|
ambiguity: None,
|
2016-11-07 16:23:26 -06:00
|
|
|
vis: self.1,
|
|
|
|
span: self.2,
|
|
|
|
expansion: self.3,
|
2016-11-28 20:53:00 -06:00
|
|
|
})
|
2016-01-13 19:42:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 16:23:26 -06:00
|
|
|
impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
|
2016-11-28 20:53:00 -06:00
|
|
|
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
|
|
|
|
arenas.alloc_name_binding(NameBinding {
|
2018-07-29 06:51:17 -05:00
|
|
|
kind: NameBindingKind::Def(self.0, false),
|
2018-12-29 09:15:29 -06:00
|
|
|
ambiguity: None,
|
2018-07-29 06:51:17 -05:00
|
|
|
vis: self.1,
|
|
|
|
span: self.2,
|
|
|
|
expansion: self.3,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct IsMacroExport;
|
|
|
|
|
|
|
|
impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark, IsMacroExport) {
|
|
|
|
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
|
|
|
|
arenas.alloc_name_binding(NameBinding {
|
|
|
|
kind: NameBindingKind::Def(self.0, true),
|
2018-12-29 09:15:29 -06:00
|
|
|
ambiguity: None,
|
2016-11-07 16:23:26 -06:00
|
|
|
vis: self.1,
|
|
|
|
span: self.2,
|
|
|
|
expansion: self.3,
|
2016-11-28 20:53:00 -06:00
|
|
|
})
|
2016-01-13 19:42:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-09 22:37:10 -06:00
|
|
|
impl<'a> Resolver<'a> {
|
2016-01-13 19:42:45 -06:00
|
|
|
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
|
|
|
|
/// otherwise, reports an error.
|
2017-03-17 20:55:51 -05:00
|
|
|
pub fn define<T>(&mut self, parent: Module<'a>, ident: Ident, ns: Namespace, def: T)
|
2016-11-26 06:48:46 -06:00
|
|
|
where T: ToNameBinding<'a>,
|
2016-07-27 21:34:01 -05:00
|
|
|
{
|
2016-11-28 20:53:00 -06:00
|
|
|
let binding = def.to_name_binding(self.arenas);
|
|
|
|
if let Err(old_binding) = self.try_define(parent, ident, ns, binding) {
|
2016-11-28 20:07:12 -06:00
|
|
|
self.report_conflict(parent, ident, ns, old_binding, &binding);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
|
2016-01-27 04:51:22 -06:00
|
|
|
// If any statements are items, we need to create an anonymous module
|
2016-06-16 21:30:01 -05:00
|
|
|
block.stmts.iter().any(|statement| match statement.node {
|
2016-09-14 16:03:09 -05:00
|
|
|
StmtKind::Item(_) | StmtKind::Mac(_) => true,
|
2016-06-16 21:30:01 -05:00
|
|
|
_ => false,
|
|
|
|
})
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Name>) {
|
|
|
|
if !field_names.is_empty() {
|
|
|
|
self.field_names.insert(def_id, field_names);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 03:13:57 -05:00
|
|
|
fn build_reduced_graph_for_use_tree(
|
|
|
|
&mut self,
|
2018-10-27 12:23:54 -05:00
|
|
|
// This particular use tree
|
2018-08-11 03:13:57 -05:00
|
|
|
use_tree: &ast::UseTree,
|
|
|
|
id: NodeId,
|
2018-09-11 22:21:50 -05:00
|
|
|
parent_prefix: &[Segment],
|
2018-08-11 03:13:57 -05:00
|
|
|
nested: bool,
|
2018-10-27 12:23:54 -05:00
|
|
|
// The whole `use` item
|
2018-10-21 17:28:59 -05:00
|
|
|
parent_scope: ParentScope<'a>,
|
2018-10-27 12:23:54 -05:00
|
|
|
item: &Item,
|
|
|
|
vis: ty::Visibility,
|
|
|
|
root_span: Span,
|
2018-08-11 03:13:57 -05:00
|
|
|
) {
|
2018-11-03 11:41:44 -05:00
|
|
|
debug!("build_reduced_graph_for_use_tree(parent_prefix={:?}, use_tree={:?}, nested={})",
|
|
|
|
parent_prefix, use_tree, nested);
|
2018-08-11 03:13:57 -05:00
|
|
|
|
2018-11-03 14:02:36 -05:00
|
|
|
let mut prefix_iter = parent_prefix.iter().cloned()
|
2018-11-18 05:41:06 -06:00
|
|
|
.chain(use_tree.prefix.segments.iter().map(|seg| seg.into())).peekable();
|
2018-11-03 14:02:36 -05:00
|
|
|
|
|
|
|
// On 2015 edition imports are resolved as crate-relative by default,
|
|
|
|
// so prefixes are prepended with crate root segment if necessary.
|
|
|
|
// The root is prepended lazily, when the first non-empty prefix or terminating glob
|
|
|
|
// appears, so imports in braced groups can have roots prepended independently.
|
|
|
|
let is_glob = if let ast::UseTreeKind::Glob = use_tree.kind { true } else { false };
|
2018-11-17 18:25:59 -06:00
|
|
|
let crate_root = match prefix_iter.peek() {
|
2019-01-16 17:17:22 -06:00
|
|
|
Some(seg) if !seg.ident.is_path_segment_keyword() && seg.ident.span.rust_2015() => {
|
2018-11-17 18:25:59 -06:00
|
|
|
Some(seg.ident.span.ctxt())
|
|
|
|
}
|
|
|
|
None if is_glob && use_tree.span.rust_2015() => {
|
|
|
|
Some(use_tree.span.ctxt())
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}.map(|ctxt| Segment::from_ident(Ident::new(
|
2018-12-01 18:35:55 -06:00
|
|
|
keywords::PathRoot.name(), use_tree.prefix.span.shrink_to_lo().with_ctxt(ctxt)
|
2018-11-17 18:25:59 -06:00
|
|
|
)));
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2018-11-03 14:02:36 -05:00
|
|
|
let prefix = crate_root.into_iter().chain(prefix_iter).collect::<Vec<_>>();
|
2018-09-06 05:29:45 -05:00
|
|
|
debug!("build_reduced_graph_for_use_tree: prefix={:?}", prefix);
|
2018-11-03 14:02:36 -05:00
|
|
|
|
2018-10-27 15:38:09 -05:00
|
|
|
let empty_for_self = |prefix: &[Segment]| {
|
|
|
|
prefix.is_empty() ||
|
2018-12-01 18:35:55 -06:00
|
|
|
prefix.len() == 1 && prefix[0].ident.name == keywords::PathRoot.name()
|
2018-10-27 15:38:09 -05:00
|
|
|
};
|
2017-09-26 16:04:00 -05:00
|
|
|
match use_tree.kind {
|
2018-06-13 11:44:06 -05:00
|
|
|
ast::UseTreeKind::Simple(rename, ..) => {
|
2018-11-30 13:34:24 -06:00
|
|
|
let mut ident = use_tree.ident().gensym_if_underscore();
|
2018-09-06 05:29:45 -05:00
|
|
|
let mut module_path = prefix;
|
2018-03-18 08:47:09 -05:00
|
|
|
let mut source = module_path.pop().unwrap();
|
2017-09-26 16:04:00 -05:00
|
|
|
let mut type_ns_only = false;
|
|
|
|
|
|
|
|
if nested {
|
|
|
|
// Correctly handle `self`
|
2018-12-01 18:35:55 -06:00
|
|
|
if source.ident.name == keywords::SelfLower.name() {
|
2017-09-26 16:04:00 -05:00
|
|
|
type_ns_only = true;
|
|
|
|
|
2018-10-27 15:38:09 -05:00
|
|
|
if empty_for_self(&module_path) {
|
2017-09-26 16:04:00 -05:00
|
|
|
resolve_error(
|
|
|
|
self,
|
|
|
|
use_tree.span,
|
|
|
|
ResolutionError::
|
|
|
|
SelfImportOnlyInImportListWithNonEmptyPrefix
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-23 13:34:36 -06:00
|
|
|
|
2017-09-26 16:04:00 -05:00
|
|
|
// Replace `use foo::self;` with `use foo;`
|
2018-07-13 14:38:49 -05:00
|
|
|
source = module_path.pop().unwrap();
|
2018-03-09 09:58:44 -06:00
|
|
|
if rename.is_none() {
|
2018-09-11 22:21:50 -05:00
|
|
|
ident = source.ident;
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
2017-09-26 16:04:00 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Disallow `self`
|
2018-12-01 18:35:55 -06:00
|
|
|
if source.ident.name == keywords::SelfLower.name() {
|
2017-09-26 16:04:00 -05:00
|
|
|
resolve_error(self,
|
|
|
|
use_tree.span,
|
|
|
|
ResolutionError::SelfImportsOnlyAllowedWithin);
|
|
|
|
}
|
2014-12-23 13:34:36 -06:00
|
|
|
|
2017-09-26 16:04:00 -05:00
|
|
|
// Disallow `use $crate;`
|
2018-09-11 22:21:50 -05:00
|
|
|
if source.ident.name == keywords::DollarCrate.name() && module_path.is_empty() {
|
|
|
|
let crate_root = self.resolve_crate_root(source.ident);
|
2017-09-26 16:04:00 -05:00
|
|
|
let crate_name = match crate_root.kind {
|
|
|
|
ModuleKind::Def(_, name) => name,
|
|
|
|
ModuleKind::Block(..) => unreachable!(),
|
2016-11-10 00:19:54 -06:00
|
|
|
};
|
2018-08-09 08:29:22 -05:00
|
|
|
// HACK(eddyb) unclear how good this is, but keeping `$crate`
|
|
|
|
// in `source` breaks `src/test/compile-fail/import-crate-var.rs`,
|
|
|
|
// while the current crate doesn't have a valid `crate_name`.
|
|
|
|
if crate_name != keywords::Invalid.name() {
|
2018-07-13 14:38:49 -05:00
|
|
|
// `crate_name` should not be interpreted as relative.
|
2018-09-11 22:21:50 -05:00
|
|
|
module_path.push(Segment {
|
|
|
|
ident: Ident {
|
2018-12-01 18:35:55 -06:00
|
|
|
name: keywords::PathRoot.name(),
|
2018-09-11 22:21:50 -05:00
|
|
|
span: source.ident.span,
|
|
|
|
},
|
|
|
|
id: Some(self.session.next_node_id()),
|
|
|
|
});
|
|
|
|
source.ident.name = crate_name;
|
2018-08-09 08:29:22 -05:00
|
|
|
}
|
2018-03-09 09:58:44 -06:00
|
|
|
if rename.is_none() {
|
2017-09-26 16:04:00 -05:00
|
|
|
ident.name = crate_name;
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:04:00 -05:00
|
|
|
self.session.struct_span_warn(item.span, "`$crate` may not be imported")
|
|
|
|
.note("`use $crate;` was erroneously allowed and \
|
|
|
|
will become a hard error in a future release")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 08:29:22 -05:00
|
|
|
if ident.name == keywords::Crate.name() {
|
|
|
|
self.session.span_err(ident.span,
|
|
|
|
"crate root imports need to be explicitly named: \
|
|
|
|
`use crate as name;`");
|
|
|
|
}
|
|
|
|
|
2017-09-26 16:04:00 -05:00
|
|
|
let subclass = SingleImport {
|
2018-09-11 22:21:50 -05:00
|
|
|
source: source.ident,
|
2018-11-30 12:16:21 -06:00
|
|
|
target: ident,
|
|
|
|
source_bindings: PerNS {
|
2018-04-29 18:20:14 -05:00
|
|
|
type_ns: Cell::new(Err(Undetermined)),
|
|
|
|
value_ns: Cell::new(Err(Undetermined)),
|
|
|
|
macro_ns: Cell::new(Err(Undetermined)),
|
|
|
|
},
|
2018-11-30 12:16:21 -06:00
|
|
|
target_bindings: PerNS {
|
|
|
|
type_ns: Cell::new(None),
|
|
|
|
value_ns: Cell::new(None),
|
|
|
|
macro_ns: Cell::new(None),
|
|
|
|
},
|
2017-09-26 16:04:00 -05:00
|
|
|
type_ns_only,
|
2019-01-29 06:34:40 -06:00
|
|
|
nested,
|
2017-09-26 16:04:00 -05:00
|
|
|
};
|
|
|
|
self.add_import_directive(
|
2018-05-18 16:18:04 -05:00
|
|
|
module_path,
|
|
|
|
subclass,
|
|
|
|
use_tree.span,
|
|
|
|
id,
|
2019-01-29 06:34:40 -06:00
|
|
|
item,
|
2018-10-27 12:23:54 -05:00
|
|
|
root_span,
|
|
|
|
item.id,
|
2018-05-18 16:18:04 -05:00
|
|
|
vis,
|
2018-10-21 17:28:59 -05:00
|
|
|
parent_scope,
|
2017-09-26 16:04:00 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
ast::UseTreeKind::Glob => {
|
|
|
|
let subclass = GlobImport {
|
2018-10-27 12:23:54 -05:00
|
|
|
is_prelude: attr::contains_name(&item.attrs, "prelude_import"),
|
2017-09-26 16:04:00 -05:00
|
|
|
max_vis: Cell::new(ty::Visibility::Invisible),
|
|
|
|
};
|
|
|
|
self.add_import_directive(
|
2018-09-06 05:29:45 -05:00
|
|
|
prefix,
|
2018-05-18 16:18:04 -05:00
|
|
|
subclass,
|
|
|
|
use_tree.span,
|
|
|
|
id,
|
2019-01-29 06:34:40 -06:00
|
|
|
item,
|
2018-10-27 12:23:54 -05:00
|
|
|
root_span,
|
|
|
|
item.id,
|
2018-05-18 16:18:04 -05:00
|
|
|
vis,
|
2018-10-21 17:28:59 -05:00
|
|
|
parent_scope,
|
2017-09-26 16:04:00 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
ast::UseTreeKind::Nested(ref items) => {
|
|
|
|
// Ensure there is at most one `self` in the list
|
|
|
|
let self_spans = items.iter().filter_map(|&(ref use_tree, _)| {
|
2018-03-09 09:58:44 -06:00
|
|
|
if let ast::UseTreeKind::Simple(..) = use_tree.kind {
|
2018-12-01 18:35:55 -06:00
|
|
|
if use_tree.ident().name == keywords::SelfLower.name() {
|
2017-09-26 16:04:00 -05:00
|
|
|
return Some(use_tree.span);
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 16:04:00 -05:00
|
|
|
|
|
|
|
None
|
|
|
|
}).collect::<Vec<_>>();
|
|
|
|
if self_spans.len() > 1 {
|
|
|
|
let mut e = resolve_struct_error(self,
|
|
|
|
self_spans[0],
|
|
|
|
ResolutionError::SelfImportCanOnlyAppearOnceInTheList);
|
|
|
|
|
|
|
|
for other_span in self_spans.iter().skip(1) {
|
2017-12-19 21:53:39 -06:00
|
|
|
e.span_label(*other_span, "another `self` import appears here");
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
2017-09-26 16:04:00 -05:00
|
|
|
|
|
|
|
e.emit();
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:08:47 -05:00
|
|
|
for &(ref tree, id) in items {
|
2017-09-26 16:04:00 -05:00
|
|
|
self.build_reduced_graph_for_use_tree(
|
2018-10-27 12:23:54 -05:00
|
|
|
// This particular use tree
|
2018-11-03 11:41:44 -05:00
|
|
|
tree, id, &prefix, true,
|
2018-10-27 12:23:54 -05:00
|
|
|
// The whole `use` item
|
|
|
|
parent_scope.clone(), item, vis, root_span,
|
2017-09-26 16:04:00 -05:00
|
|
|
);
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
2018-10-27 15:38:09 -05:00
|
|
|
|
|
|
|
// Empty groups `a::b::{}` are turned into synthetic `self` imports
|
2019-03-19 13:32:17 -05:00
|
|
|
// `a::b::c::{self as __dummy}`, so that their prefixes are correctly
|
2018-10-27 15:38:09 -05:00
|
|
|
// resolved and checked for privacy/stability/etc.
|
|
|
|
if items.is_empty() && !empty_for_self(&prefix) {
|
|
|
|
let new_span = prefix[prefix.len() - 1].ident.span;
|
|
|
|
let tree = ast::UseTree {
|
|
|
|
prefix: ast::Path::from_ident(
|
2018-12-01 18:35:55 -06:00
|
|
|
Ident::new(keywords::SelfLower.name(), new_span)
|
2018-10-27 15:38:09 -05:00
|
|
|
),
|
|
|
|
kind: ast::UseTreeKind::Simple(
|
2019-03-19 13:32:17 -05:00
|
|
|
Some(Ident::new(Name::gensym("__dummy"), new_span)),
|
2018-10-27 15:38:09 -05:00
|
|
|
ast::DUMMY_NODE_ID,
|
|
|
|
ast::DUMMY_NODE_ID,
|
|
|
|
),
|
|
|
|
span: use_tree.span,
|
|
|
|
};
|
|
|
|
self.build_reduced_graph_for_use_tree(
|
|
|
|
// This particular use tree
|
2018-11-03 11:41:44 -05:00
|
|
|
&tree, id, &prefix, true,
|
2018-10-27 15:38:09 -05:00
|
|
|
// The whole `use` item
|
2018-11-27 21:19:22 -06:00
|
|
|
parent_scope, item, ty::Visibility::Invisible, root_span,
|
2018-10-27 15:38:09 -05:00
|
|
|
);
|
|
|
|
}
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
2017-09-26 16:04:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs the reduced graph for one item.
|
2018-10-21 17:28:59 -05:00
|
|
|
fn build_reduced_graph_for_item(&mut self, item: &Item, parent_scope: ParentScope<'a>) {
|
|
|
|
let parent = parent_scope.module;
|
|
|
|
let expansion = parent_scope.expansion;
|
2018-11-30 13:34:24 -06:00
|
|
|
let ident = item.ident.gensym_if_underscore();
|
2017-09-26 16:04:00 -05:00
|
|
|
let sp = item.span;
|
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
ItemKind::Use(ref use_tree) => {
|
|
|
|
self.build_reduced_graph_for_use_tree(
|
2018-10-27 12:23:54 -05:00
|
|
|
// This particular use tree
|
2018-11-03 11:41:44 -05:00
|
|
|
use_tree, item.id, &[], false,
|
2018-10-27 12:23:54 -05:00
|
|
|
// The whole `use` item
|
|
|
|
parent_scope, item, vis, use_tree.span,
|
2017-09-26 16:04:00 -05:00
|
|
|
);
|
|
|
|
}
|
2014-12-23 13:34:36 -06:00
|
|
|
|
2018-03-09 09:51:48 -06:00
|
|
|
ItemKind::ExternCrate(orig_name) => {
|
2018-12-01 18:35:55 -06:00
|
|
|
let module = if orig_name.is_none() && ident.name == keywords::SelfLower.name() {
|
2018-10-25 17:39:47 -05:00
|
|
|
self.session
|
|
|
|
.struct_span_err(item.span, "`extern crate self;` requires renaming")
|
2019-01-25 15:03:27 -06:00
|
|
|
.span_suggestion(
|
2019-01-17 09:18:56 -06:00
|
|
|
item.span,
|
|
|
|
"try",
|
|
|
|
"extern crate self as name;".into(),
|
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
)
|
2018-10-25 17:39:47 -05:00
|
|
|
.emit();
|
|
|
|
return;
|
2018-12-01 18:35:55 -06:00
|
|
|
} else if orig_name == Some(keywords::SelfLower.name()) {
|
2018-10-25 17:39:47 -05:00
|
|
|
self.graph_root
|
|
|
|
} else {
|
|
|
|
let crate_id = self.crate_loader.process_extern_crate(item, &self.definitions);
|
|
|
|
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX })
|
|
|
|
};
|
|
|
|
|
2017-01-14 01:35:54 -06:00
|
|
|
self.populate_module_if_necessary(module);
|
2018-09-28 17:31:54 -05:00
|
|
|
if injected_crate_name().map_or(false, |name| ident.name == name) {
|
2017-12-06 12:50:55 -06:00
|
|
|
self.injected_crate = Some(module);
|
|
|
|
}
|
|
|
|
|
2018-10-21 17:28:59 -05:00
|
|
|
let used = self.process_legacy_macro_imports(item, module, &parent_scope);
|
2016-11-28 20:53:00 -06:00
|
|
|
let binding =
|
|
|
|
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
|
2018-11-04 16:11:59 -06:00
|
|
|
let directive = self.arenas.alloc_import_directive(ImportDirective {
|
|
|
|
root_id: item.id,
|
|
|
|
id: item.id,
|
|
|
|
parent_scope,
|
|
|
|
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
|
|
|
|
subclass: ImportDirectiveSubclass::ExternCrate {
|
|
|
|
source: orig_name,
|
|
|
|
target: ident,
|
|
|
|
},
|
2019-01-29 06:34:40 -06:00
|
|
|
has_attributes: !item.attrs.is_empty(),
|
|
|
|
use_span_with_attributes: item.span_with_attributes(),
|
|
|
|
use_span: item.span,
|
2018-11-04 16:11:59 -06:00
|
|
|
root_span: item.span,
|
|
|
|
span: item.span,
|
|
|
|
module_path: Vec::new(),
|
|
|
|
vis: Cell::new(vis),
|
|
|
|
used: Cell::new(used),
|
|
|
|
});
|
|
|
|
self.potentially_unused_imports.push(directive);
|
|
|
|
let imported_binding = self.import(binding, directive);
|
2018-09-28 17:31:54 -05:00
|
|
|
if ptr::eq(self.current_module, self.graph_root) {
|
2018-10-23 17:03:47 -05:00
|
|
|
if let Some(entry) = self.extern_prelude.get(&ident.modern()) {
|
|
|
|
if expansion != Mark::root() && orig_name.is_some() &&
|
|
|
|
entry.extern_crate_item.is_none() {
|
|
|
|
self.session.span_err(item.span, "macro-expanded `extern crate` items \
|
|
|
|
cannot shadow names passed with \
|
|
|
|
`--extern`");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let entry = self.extern_prelude.entry(ident.modern())
|
|
|
|
.or_insert(ExternPreludeEntry {
|
2018-09-28 17:31:54 -05:00
|
|
|
extern_crate_item: None,
|
|
|
|
introduced_by_item: true,
|
2018-10-23 17:03:47 -05:00
|
|
|
});
|
2018-11-04 16:11:59 -06:00
|
|
|
entry.extern_crate_item = Some(imported_binding);
|
2018-10-23 17:03:47 -05:00
|
|
|
if orig_name.is_some() {
|
|
|
|
entry.introduced_by_item = true;
|
|
|
|
}
|
2018-09-28 17:31:54 -05:00
|
|
|
}
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, TypeNS, imported_binding);
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
|
2017-03-15 21:27:40 -05:00
|
|
|
ItemKind::GlobalAsm(..) => {}
|
|
|
|
|
2018-09-28 17:31:54 -05:00
|
|
|
ItemKind::Mod(..) if ident == keywords::Invalid.ident() => {} // Crate root
|
2016-09-14 16:03:09 -05:00
|
|
|
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Mod(..) => {
|
2016-12-20 02:32:15 -06:00
|
|
|
let def_id = self.definitions.local_def_id(item.id);
|
|
|
|
let module_kind = ModuleKind::Def(Def::Mod(def_id), ident.name);
|
2016-11-26 06:47:52 -06:00
|
|
|
let module = self.arenas.alloc_module(ModuleData {
|
2016-09-19 00:25:17 -05:00
|
|
|
no_implicit_prelude: parent.no_implicit_prelude || {
|
2016-06-05 04:56:05 -05:00
|
|
|
attr::contains_name(&item.attrs, "no_implicit_prelude")
|
2016-09-19 00:25:17 -05:00
|
|
|
},
|
2017-03-22 03:39:51 -05:00
|
|
|
..ModuleData::new(Some(parent), module_kind, def_id, expansion, item.span)
|
2016-06-05 04:56:05 -05:00
|
|
|
});
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
2016-12-20 02:32:15 -06:00
|
|
|
self.module_map.insert(def_id, module);
|
2016-08-14 18:42:05 -05:00
|
|
|
|
|
|
|
// Descend into the module.
|
|
|
|
self.current_module = module;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2018-04-10 09:01:24 -05:00
|
|
|
// Handled in `rustc_metadata::{native_libs,link_args}`
|
|
|
|
ItemKind::ForeignMod(..) => {}
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
// These items live in the value namespace.
|
2019-04-19 15:32:26 -05:00
|
|
|
ItemKind::Static(..) => {
|
|
|
|
let def = Def::Static(self.definitions.local_def_id(item.id));
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-08-26 11:23:42 -05:00
|
|
|
ItemKind::Const(..) => {
|
2016-04-23 22:26:10 -05:00
|
|
|
let def = Def::Const(self.definitions.local_def_id(item.id));
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-08-26 11:23:42 -05:00
|
|
|
ItemKind::Fn(..) => {
|
2016-04-23 22:26:10 -05:00
|
|
|
let def = Def::Fn(self.definitions.local_def_id(item.id));
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
|
2018-07-12 05:24:59 -05:00
|
|
|
|
|
|
|
// Functions introducing procedural macros reserve a slot
|
|
|
|
// in the macro namespace as well (see #52225).
|
|
|
|
if attr::contains_name(&item.attrs, "proc_macro") ||
|
|
|
|
attr::contains_name(&item.attrs, "proc_macro_attribute") {
|
|
|
|
let def = Def::Macro(def.def_id(), MacroKind::ProcMacroStub);
|
|
|
|
self.define(parent, ident, MacroNS, (def, vis, sp, expansion));
|
|
|
|
}
|
|
|
|
if let Some(attr) = attr::find_by_name(&item.attrs, "proc_macro_derive") {
|
|
|
|
if let Some(trait_attr) =
|
|
|
|
attr.meta_item_list().and_then(|list| list.get(0).cloned()) {
|
2019-02-28 00:17:24 -06:00
|
|
|
if let Some(ident) = trait_attr.ident() {
|
2018-07-12 05:24:59 -05:00
|
|
|
let def = Def::Macro(def.def_id(), MacroKind::ProcMacroStub);
|
2019-02-28 00:17:24 -06:00
|
|
|
self.define(parent, ident, MacroNS, (def, vis, ident.span, expansion));
|
2018-07-12 05:24:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// These items live in the type namespace.
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Ty(..) => {
|
|
|
|
let def = Def::TyAlias(self.definitions.local_def_id(item.id));
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2018-07-03 12:38:14 -05:00
|
|
|
ItemKind::Existential(_, _) => {
|
|
|
|
let def = Def::Existential(self.definitions.local_def_id(item.id));
|
|
|
|
self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Enum(ref enum_definition, _) => {
|
2016-09-19 00:25:17 -05:00
|
|
|
let def = Def::Enum(self.definitions.local_def_id(item.id));
|
2016-12-20 02:32:15 -06:00
|
|
|
let module_kind = ModuleKind::Def(def, ident.name);
|
2017-05-10 06:19:29 -05:00
|
|
|
let module = self.new_module(parent,
|
|
|
|
module_kind,
|
|
|
|
parent.normal_ancestor_id,
|
2017-03-22 03:39:51 -05:00
|
|
|
expansion,
|
2017-05-10 06:19:29 -05:00
|
|
|
item.span);
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
2014-12-30 12:52:51 -06:00
|
|
|
|
2015-01-31 11:20:46 -06:00
|
|
|
for variant in &(*enum_definition).variants {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.build_reduced_graph_for_variant(variant, module, vis, expansion);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 08:48:57 -05:00
|
|
|
ItemKind::TraitAlias(..) => {
|
|
|
|
let def = Def::TraitAlias(self.definitions.local_def_id(item.id));
|
|
|
|
self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
|
|
|
|
}
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
// These items live in both the type and value namespaces.
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Struct(ref struct_def, _) => {
|
2014-12-30 12:16:42 -06:00
|
|
|
// Define a name in the type namespace.
|
2017-11-03 14:17:54 -05:00
|
|
|
let def_id = self.definitions.local_def_id(item.id);
|
|
|
|
let def = Def::Struct(def_id);
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2017-01-08 15:21:35 -06:00
|
|
|
let mut ctor_vis = vis;
|
2017-11-03 14:17:54 -05:00
|
|
|
|
2017-12-26 01:52:27 -06:00
|
|
|
let has_non_exhaustive = attr::contains_name(&item.attrs, "non_exhaustive");
|
2017-11-03 14:17:54 -05:00
|
|
|
|
|
|
|
// If the structure is marked as non_exhaustive then lower the visibility
|
|
|
|
// to within the crate.
|
|
|
|
if has_non_exhaustive && vis == ty::Visibility::Public {
|
|
|
|
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record field names for error reporting.
|
2016-09-14 16:51:46 -05:00
|
|
|
let field_names = struct_def.fields().iter().filter_map(|field| {
|
2017-01-08 15:21:35 -06:00
|
|
|
let field_vis = self.resolve_visibility(&field.vis);
|
|
|
|
if ctor_vis.is_at_least(field_vis, &*self) {
|
|
|
|
ctor_vis = field_vis;
|
|
|
|
}
|
2016-04-23 22:26:10 -05:00
|
|
|
field.ident.map(|ident| ident.name)
|
2016-04-09 18:19:53 -05:00
|
|
|
}).collect();
|
2016-04-23 22:26:10 -05:00
|
|
|
let item_def_id = self.definitions.local_def_id(item.id);
|
2016-09-14 16:51:46 -05:00
|
|
|
self.insert_field_names(item_def_id, field_names);
|
2017-01-08 15:21:35 -06:00
|
|
|
|
|
|
|
// If this is a tuple or unit struct, define a name
|
|
|
|
// in the value namespace as well.
|
2019-03-21 17:38:50 -05:00
|
|
|
if let Some(ctor_node_id) = struct_def.ctor_id() {
|
2019-03-24 13:16:44 -05:00
|
|
|
let ctor_def = Def::Ctor(self.definitions.local_def_id(ctor_node_id),
|
|
|
|
CtorOf::Struct,
|
2019-03-21 17:38:50 -05:00
|
|
|
CtorKind::from_ast(struct_def));
|
2017-01-08 15:21:35 -06:00
|
|
|
self.define(parent, ident, ValueNS, (ctor_def, ctor_vis, sp, expansion));
|
2017-01-28 17:56:52 -06:00
|
|
|
self.struct_constructors.insert(def.def_id(), (ctor_def, ctor_vis));
|
2017-01-08 15:21:35 -06:00
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-08-06 13:56:02 -05:00
|
|
|
ItemKind::Union(ref vdata, _) => {
|
|
|
|
let def = Def::Union(self.definitions.local_def_id(item.id));
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
|
2016-08-06 13:56:02 -05:00
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
// Record field names for error reporting.
|
|
|
|
let field_names = vdata.fields().iter().filter_map(|field| {
|
2016-08-06 13:56:02 -05:00
|
|
|
self.resolve_visibility(&field.vis);
|
|
|
|
field.ident.map(|ident| ident.name)
|
|
|
|
}).collect();
|
|
|
|
let item_def_id = self.definitions.local_def_id(item.id);
|
2016-09-14 16:51:46 -05:00
|
|
|
self.insert_field_names(item_def_id, field_names);
|
2016-08-06 13:56:02 -05:00
|
|
|
}
|
2016-08-29 00:04:31 -05:00
|
|
|
|
2017-12-01 06:01:23 -06:00
|
|
|
ItemKind::Impl(..) => {}
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-09-14 18:39:13 -05:00
|
|
|
ItemKind::Trait(..) => {
|
2016-04-23 22:26:10 -05:00
|
|
|
let def_id = self.definitions.local_def_id(item.id);
|
2015-11-16 01:59:50 -06:00
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
// Add all the items within to a new module.
|
2016-12-20 02:32:15 -06:00
|
|
|
let module_kind = ModuleKind::Def(Def::Trait(def_id), ident.name);
|
2017-05-10 06:19:29 -05:00
|
|
|
let module = self.new_module(parent,
|
|
|
|
module_kind,
|
|
|
|
parent.normal_ancestor_id,
|
2017-03-22 03:39:51 -05:00
|
|
|
expansion,
|
2017-05-10 06:19:29 -05:00
|
|
|
item.span);
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
2016-09-14 18:39:13 -05:00
|
|
|
self.current_module = module;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2017-10-02 08:48:57 -05:00
|
|
|
|
2017-03-04 23:15:58 -06:00
|
|
|
ItemKind::MacroDef(..) | ItemKind::Mac(_) => unreachable!(),
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructs the reduced graph for one variant. Variants exist in the
|
|
|
|
// type and value namespaces.
|
2016-11-17 02:16:32 -06:00
|
|
|
fn build_reduced_graph_for_variant(&mut self,
|
|
|
|
variant: &Variant,
|
2016-11-26 06:48:46 -06:00
|
|
|
parent: Module<'a>,
|
2016-11-17 02:16:32 -06:00
|
|
|
vis: ty::Visibility,
|
|
|
|
expansion: Mark) {
|
2018-03-18 17:21:30 -05:00
|
|
|
let ident = variant.node.ident;
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
// Define a name in the type namespace.
|
2019-03-24 10:41:09 -05:00
|
|
|
let def_id = self.definitions.local_def_id(variant.node.id);
|
2016-09-14 16:51:46 -05:00
|
|
|
let def = Def::Variant(def_id);
|
2016-11-28 20:07:12 -06:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, variant.span, expansion));
|
2016-09-14 16:51:46 -05:00
|
|
|
|
2019-03-22 11:19:12 -05:00
|
|
|
// If the variant is marked as non_exhaustive then lower the visibility to within the
|
|
|
|
// crate.
|
|
|
|
let mut ctor_vis = vis;
|
|
|
|
let has_non_exhaustive = attr::contains_name(&variant.node.attrs, "non_exhaustive");
|
|
|
|
if has_non_exhaustive && vis == ty::Visibility::Public {
|
|
|
|
ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
// Define a constructor name in the value namespace.
|
|
|
|
// Braced variants, unlike structs, generate unusable names in
|
|
|
|
// value namespace, they are reserved for possible future use.
|
2019-03-24 10:41:09 -05:00
|
|
|
// It's ok to use the variant's id as a ctor id since an
|
|
|
|
// error will be reported on any use of such resolution anyway.
|
|
|
|
let ctor_node_id = variant.node.data.ctor_id().unwrap_or(variant.node.id);
|
|
|
|
let ctor_def_id = self.definitions.local_def_id(ctor_node_id);
|
2016-09-14 16:51:46 -05:00
|
|
|
let ctor_kind = CtorKind::from_ast(&variant.node.data);
|
2019-03-24 13:16:44 -05:00
|
|
|
let ctor_def = Def::Ctor(ctor_def_id, CtorOf::Variant, ctor_kind);
|
2019-03-22 11:19:12 -05:00
|
|
|
self.define(parent, ident, ValueNS, (ctor_def, ctor_vis, variant.span, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs the reduced graph for one foreign item.
|
2016-11-17 02:16:32 -06:00
|
|
|
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem, expansion: Mark) {
|
2017-09-03 13:53:58 -05:00
|
|
|
let (def, ns) = match item.node {
|
2016-04-23 22:26:10 -05:00
|
|
|
ForeignItemKind::Fn(..) => {
|
2017-09-03 13:53:58 -05:00
|
|
|
(Def::Fn(self.definitions.local_def_id(item.id)), ValueNS)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2019-04-19 15:32:26 -05:00
|
|
|
ForeignItemKind::Static(..) => {
|
|
|
|
(Def::Static(self.definitions.local_def_id(item.id)), ValueNS)
|
2017-09-03 13:53:58 -05:00
|
|
|
}
|
|
|
|
ForeignItemKind::Ty => {
|
2018-08-22 05:47:31 -05:00
|
|
|
(Def::ForeignTy(self.definitions.local_def_id(item.id)), TypeNS)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2018-03-10 20:16:26 -06:00
|
|
|
ForeignItemKind::Macro(_) => unreachable!(),
|
2015-02-05 01:19:07 -06:00
|
|
|
};
|
2016-11-28 20:07:12 -06:00
|
|
|
let parent = self.current_module;
|
2016-11-17 02:16:32 -06:00
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
2017-09-03 13:53:58 -05:00
|
|
|
self.define(parent, item.ident, ns, (def, vis, item.span, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2017-03-22 03:39:51 -05:00
|
|
|
fn build_reduced_graph_for_block(&mut self, block: &Block, expansion: Mark) {
|
2016-08-14 20:08:31 -05:00
|
|
|
let parent = self.current_module;
|
2014-12-30 12:16:42 -06:00
|
|
|
if self.block_needs_anonymous_module(block) {
|
2017-05-10 06:58:41 -05:00
|
|
|
let module = self.new_module(parent,
|
|
|
|
ModuleKind::Block(block.id),
|
|
|
|
parent.normal_ancestor_id,
|
2017-03-22 03:39:51 -05:00
|
|
|
expansion,
|
2017-05-10 06:58:41 -05:00
|
|
|
block.span);
|
2016-12-20 02:32:15 -06:00
|
|
|
self.block_map.insert(block.id, module);
|
|
|
|
self.current_module = module; // Descend into the block.
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 20:29:06 -06:00
|
|
|
/// Builds the reduced graph for a single item in an external crate.
|
2019-04-03 02:07:45 -05:00
|
|
|
fn build_reduced_graph_for_external_crate_def(
|
|
|
|
&mut self,
|
|
|
|
parent: Module<'a>,
|
|
|
|
child: Export<ast::NodeId>,
|
|
|
|
) {
|
2018-11-30 13:34:24 -06:00
|
|
|
let Export { ident, def, vis, span } = child;
|
|
|
|
// FIXME: We shouldn't create the gensym here, it should come from metadata,
|
|
|
|
// but metadata cannot encode gensyms currently, so we create it here.
|
|
|
|
// This is only a guess, two equivalent idents may incorrectly get different gensyms here.
|
|
|
|
let ident = ident.gensym_if_underscore();
|
2017-03-22 03:39:51 -05:00
|
|
|
let expansion = Mark::root(); // FIXME(jseyfried) intercrate hygiene
|
2014-12-30 12:16:42 -06:00
|
|
|
match def {
|
2019-03-09 07:15:40 -06:00
|
|
|
Def::Mod(def_id) | Def::Enum(def_id) => {
|
2017-05-10 06:19:29 -05:00
|
|
|
let module = self.new_module(parent,
|
|
|
|
ModuleKind::Def(def, ident.name),
|
|
|
|
def_id,
|
2017-03-22 03:39:51 -05:00
|
|
|
expansion,
|
2017-05-10 06:19:29 -05:00
|
|
|
span);
|
2017-03-22 03:39:51 -05:00
|
|
|
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2019-03-09 07:15:40 -06:00
|
|
|
Def::Variant(..) | Def::TyAlias(..) | Def::ForeignTy(..) | Def::Existential(..) |
|
|
|
|
Def::TraitAlias(..) | Def::PrimTy(..) | Def::ToolMod => {
|
2017-03-22 03:39:51 -05:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2019-03-24 10:41:09 -05:00
|
|
|
Def::Fn(..) | Def::Static(..) | Def::Const(..) |
|
2019-03-24 13:16:44 -05:00
|
|
|
Def::Ctor(_, CtorOf::Variant, ..) => {
|
2017-03-22 03:39:51 -05:00
|
|
|
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
|
2017-01-28 17:56:52 -06:00
|
|
|
}
|
2019-03-24 13:16:44 -05:00
|
|
|
Def::Ctor(def_id, CtorOf::Struct, ..) => {
|
2017-03-22 03:39:51 -05:00
|
|
|
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
|
2017-01-28 17:56:52 -06:00
|
|
|
|
|
|
|
if let Some(struct_def_id) =
|
2017-09-05 09:48:24 -05:00
|
|
|
self.cstore.def_key(def_id).parent
|
2017-01-28 17:56:52 -06:00
|
|
|
.map(|index| DefId { krate: def_id.krate, index: index }) {
|
|
|
|
self.struct_constructors.insert(struct_def_id, (def, vis));
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2019-03-09 07:15:40 -06:00
|
|
|
Def::Trait(def_id) => {
|
2016-12-20 02:32:15 -06:00
|
|
|
let module_kind = ModuleKind::Def(def, ident.name);
|
2017-05-10 06:19:29 -05:00
|
|
|
let module = self.new_module(parent,
|
|
|
|
module_kind,
|
|
|
|
parent.normal_ancestor_id,
|
2017-03-22 03:39:51 -05:00
|
|
|
expansion,
|
2017-05-10 06:19:29 -05:00
|
|
|
span);
|
2017-03-22 03:39:51 -05:00
|
|
|
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
|
2015-10-26 14:31:11 -05:00
|
|
|
|
2017-09-05 09:48:24 -05:00
|
|
|
for child in self.cstore.item_children_untracked(def_id, self.session) {
|
2019-04-03 02:07:45 -05:00
|
|
|
let def = child.def.map_id(|_| panic!("unexpected id"));
|
|
|
|
let ns = if let Def::AssociatedTy(..) = def { TypeNS } else { ValueNS };
|
2017-03-26 19:46:00 -05:00
|
|
|
self.define(module, child.ident, ns,
|
2019-04-03 02:07:45 -05:00
|
|
|
(def, ty::Visibility::Public, DUMMY_SP, expansion));
|
2015-10-26 14:31:11 -05:00
|
|
|
|
2017-09-05 09:48:24 -05:00
|
|
|
if self.cstore.associated_item_cloned_untracked(child.def.def_id())
|
2017-03-17 21:10:13 -05:00
|
|
|
.method_has_self_argument {
|
2019-04-03 02:07:45 -05:00
|
|
|
self.has_self.insert(def.def_id());
|
2017-03-17 21:10:13 -05:00
|
|
|
}
|
2016-11-30 16:35:25 -06:00
|
|
|
}
|
|
|
|
module.populated.set(true);
|
2016-09-14 16:51:46 -05:00
|
|
|
}
|
2019-03-09 07:15:40 -06:00
|
|
|
Def::Struct(def_id) | Def::Union(def_id) => {
|
2017-03-22 03:39:51 -05:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
|
2016-08-06 13:56:02 -05:00
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
// Record field names for error reporting.
|
2017-09-05 09:48:24 -05:00
|
|
|
let field_names = self.cstore.struct_field_names_untracked(def_id);
|
2016-09-14 16:51:46 -05:00
|
|
|
self.insert_field_names(def_id, field_names);
|
2016-08-06 13:56:02 -05:00
|
|
|
}
|
2019-03-09 07:15:40 -06:00
|
|
|
Def::Macro(..) | Def::NonMacroAttr(..) => {
|
2017-03-22 03:39:51 -05:00
|
|
|
self.define(parent, ident, MacroNS, (def, vis, DUMMY_SP, expansion));
|
2016-10-25 17:05:02 -05:00
|
|
|
}
|
2016-11-30 16:35:25 -06:00
|
|
|
_ => bug!("unexpected definition: {:?}", def)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 03:39:51 -05:00
|
|
|
pub fn get_module(&mut self, def_id: DefId) -> Module<'a> {
|
|
|
|
if def_id.krate == LOCAL_CRATE {
|
|
|
|
return self.module_map[&def_id]
|
|
|
|
}
|
|
|
|
|
2017-09-05 09:48:24 -05:00
|
|
|
let macros_only = self.cstore.dep_kind_untracked(def_id.krate).macros_only();
|
2017-03-22 03:39:51 -05:00
|
|
|
if let Some(&module) = self.extern_module_map.get(&(def_id, macros_only)) {
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (name, parent) = if def_id.index == CRATE_DEF_INDEX {
|
2018-04-11 16:02:41 -05:00
|
|
|
(self.cstore.crate_name_untracked(def_id.krate).as_interned_str(), None)
|
2017-03-22 03:39:51 -05:00
|
|
|
} else {
|
2017-09-05 09:48:24 -05:00
|
|
|
let def_key = self.cstore.def_key(def_id);
|
2017-03-22 03:39:51 -05:00
|
|
|
(def_key.disambiguated_data.data.get_opt_name().unwrap(),
|
|
|
|
Some(self.get_module(DefId { index: def_key.parent.unwrap(), ..def_id })))
|
|
|
|
};
|
|
|
|
|
2018-04-11 16:02:41 -05:00
|
|
|
let kind = ModuleKind::Def(Def::Mod(def_id), name.as_symbol());
|
2017-06-17 05:28:31 -05:00
|
|
|
let module =
|
|
|
|
self.arenas.alloc_module(ModuleData::new(parent, kind, def_id, Mark::root(), DUMMY_SP));
|
|
|
|
self.extern_module_map.insert((def_id, macros_only), module);
|
|
|
|
module
|
2016-10-28 00:17:06 -05:00
|
|
|
}
|
|
|
|
|
2017-03-22 03:39:51 -05:00
|
|
|
pub fn macro_def_scope(&mut self, expansion: Mark) -> Module<'a> {
|
2017-03-01 17:48:16 -06:00
|
|
|
let def_id = self.macro_defs[&expansion];
|
|
|
|
if let Some(id) = self.definitions.as_local_node_id(def_id) {
|
|
|
|
self.local_macro_def_scopes[&id]
|
2018-09-11 04:18:58 -05:00
|
|
|
} else if def_id.krate == CrateNum::BuiltinMacros {
|
2017-12-06 12:50:55 -06:00
|
|
|
self.injected_crate.unwrap_or(self.graph_root)
|
2017-03-01 17:48:16 -06:00
|
|
|
} else {
|
|
|
|
let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
|
2017-03-22 03:39:51 -05:00
|
|
|
self.get_module(module_def_id)
|
2017-03-01 17:48:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 10:11:14 -06:00
|
|
|
pub fn get_macro(&mut self, def: Def) -> Lrc<SyntaxExtension> {
|
2016-11-27 04:27:41 -06:00
|
|
|
let def_id = match def {
|
2017-02-23 03:42:33 -06:00
|
|
|
Def::Macro(def_id, ..) => def_id,
|
2018-08-03 16:25:45 -05:00
|
|
|
Def::NonMacroAttr(attr_kind) => return Lrc::new(SyntaxExtension::NonMacroAttr {
|
|
|
|
mark_used: attr_kind == NonMacroAttrKind::Tool,
|
|
|
|
}),
|
2018-08-02 18:05:00 -05:00
|
|
|
_ => panic!("expected `Def::Macro` or `Def::NonMacroAttr`"),
|
2016-10-28 01:52:45 -05:00
|
|
|
};
|
|
|
|
if let Some(ext) = self.macro_map.get(&def_id) {
|
|
|
|
return ext.clone();
|
|
|
|
}
|
|
|
|
|
2017-09-05 09:48:24 -05:00
|
|
|
let macro_def = match self.cstore.load_macro_untracked(def_id, &self.session) {
|
2017-03-04 23:15:58 -06:00
|
|
|
LoadedMacro::MacroDef(macro_def) => macro_def,
|
2016-11-05 15:30:40 -05:00
|
|
|
LoadedMacro::ProcMacro(ext) => return ext,
|
|
|
|
};
|
|
|
|
|
2018-02-27 10:11:14 -06:00
|
|
|
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
|
2018-02-14 09:11:02 -06:00
|
|
|
&self.session.features_untracked(),
|
2018-05-12 19:51:46 -05:00
|
|
|
¯o_def,
|
|
|
|
self.cstore.crate_edition_untracked(def_id.krate)));
|
2016-10-28 01:52:45 -05:00
|
|
|
self.macro_map.insert(def_id, ext.clone());
|
|
|
|
ext
|
|
|
|
}
|
|
|
|
|
2016-03-02 03:18:47 -06:00
|
|
|
/// Ensures that the reduced graph rooted at the given external module
|
|
|
|
/// is built, building it if it is not.
|
2016-11-26 06:48:46 -06:00
|
|
|
pub fn populate_module_if_necessary(&mut self, module: Module<'a>) {
|
2016-03-02 03:18:47 -06:00
|
|
|
if module.populated.get() { return }
|
2017-08-31 10:07:39 -05:00
|
|
|
let def_id = module.def_id().unwrap();
|
2017-09-05 09:48:24 -05:00
|
|
|
for child in self.cstore.item_children_untracked(def_id, self.session) {
|
2019-04-03 02:07:45 -05:00
|
|
|
let child = child.map_id(|_| panic!("unexpected id"));
|
2016-03-02 04:22:24 -06:00
|
|
|
self.build_reduced_graph_for_external_crate_def(module, child);
|
2016-03-02 03:18:47 -06:00
|
|
|
}
|
|
|
|
module.populated.set(true)
|
|
|
|
}
|
2016-09-16 01:45:03 -05:00
|
|
|
|
2016-11-17 02:16:32 -06:00
|
|
|
fn legacy_import_macro(&mut self,
|
|
|
|
name: Name,
|
2016-11-26 06:48:46 -06:00
|
|
|
binding: &'a NameBinding<'a>,
|
2016-11-17 02:16:32 -06:00
|
|
|
span: Span,
|
|
|
|
allow_shadowing: bool) {
|
2018-09-03 17:14:58 -05:00
|
|
|
if self.macro_use_prelude.insert(name, binding).is_some() && !allow_shadowing {
|
2016-10-28 01:52:45 -05:00
|
|
|
let msg = format!("`{}` is already in scope", name);
|
|
|
|
let note =
|
|
|
|
"macro-expanded `#[macro_use]`s may not shadow existing macros (see RFC 1560)";
|
|
|
|
self.session.struct_span_err(span, &msg).note(note).emit();
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 02:46:25 -05:00
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Returns `true` if we should consider the underlying `extern crate` to be used.
|
2018-10-21 17:28:59 -05:00
|
|
|
fn process_legacy_macro_imports(&mut self, item: &Item, module: Module<'a>,
|
|
|
|
parent_scope: &ParentScope<'a>) -> bool {
|
2018-10-25 17:15:51 -05:00
|
|
|
let mut import_all = None;
|
|
|
|
let mut single_imports = Vec::new();
|
|
|
|
for attr in &item.attrs {
|
|
|
|
if attr.check_name("macro_use") {
|
|
|
|
if self.current_module.parent.is_some() {
|
|
|
|
span_err!(self.session, item.span, E0468,
|
|
|
|
"an `extern crate` loading macros must be at the crate root");
|
|
|
|
}
|
2018-10-25 17:39:47 -05:00
|
|
|
if let ItemKind::ExternCrate(Some(orig_name)) = item.node {
|
2018-12-01 18:35:55 -06:00
|
|
|
if orig_name == keywords::SelfLower.name() {
|
2018-10-25 17:39:47 -05:00
|
|
|
self.session.span_err(attr.span,
|
|
|
|
"`macro_use` is not supported on `extern crate self`");
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 17:15:51 -05:00
|
|
|
let ill_formed = |span| span_err!(self.session, span, E0466, "bad macro import");
|
|
|
|
match attr.meta() {
|
|
|
|
Some(meta) => match meta.node {
|
|
|
|
MetaItemKind::Word => {
|
|
|
|
import_all = Some(meta.span);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MetaItemKind::List(nested_metas) => for nested_meta in nested_metas {
|
2019-02-28 00:17:24 -06:00
|
|
|
match nested_meta.ident() {
|
|
|
|
Some(ident) if nested_meta.is_word() => single_imports.push(ident),
|
2019-03-03 11:56:24 -06:00
|
|
|
_ => ill_formed(nested_meta.span()),
|
2018-10-25 17:15:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MetaItemKind::NameValue(..) => ill_formed(meta.span),
|
|
|
|
}
|
2019-03-03 11:56:24 -06:00
|
|
|
None => ill_formed(attr.span),
|
2018-10-25 17:15:51 -05:00
|
|
|
}
|
|
|
|
}
|
2016-11-05 15:30:40 -05:00
|
|
|
}
|
|
|
|
|
2018-10-21 17:28:59 -05:00
|
|
|
let arenas = self.arenas;
|
2017-01-14 02:58:50 -06:00
|
|
|
let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
|
2018-05-18 16:18:04 -05:00
|
|
|
root_id: item.id,
|
2017-01-14 02:58:50 -06:00
|
|
|
id: item.id,
|
2018-10-21 17:28:59 -05:00
|
|
|
parent_scope: parent_scope.clone(),
|
2018-08-09 08:29:22 -05:00
|
|
|
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
|
2017-01-14 02:58:50 -06:00
|
|
|
subclass: ImportDirectiveSubclass::MacroUse,
|
2019-01-29 06:34:40 -06:00
|
|
|
use_span_with_attributes: item.span_with_attributes(),
|
|
|
|
has_attributes: !item.attrs.is_empty(),
|
|
|
|
use_span: item.span,
|
2018-05-18 16:18:04 -05:00
|
|
|
root_span: span,
|
2017-08-07 00:54:09 -05:00
|
|
|
span,
|
2017-01-14 02:58:50 -06:00
|
|
|
module_path: Vec::new(),
|
|
|
|
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
|
|
|
|
used: Cell::new(false),
|
|
|
|
});
|
|
|
|
|
2018-10-25 17:15:51 -05:00
|
|
|
let allow_shadowing = parent_scope.expansion == Mark::root();
|
|
|
|
if let Some(span) = import_all {
|
2017-01-14 02:58:50 -06:00
|
|
|
let directive = macro_use_directive(span);
|
|
|
|
self.potentially_unused_imports.push(directive);
|
2016-11-28 20:07:12 -06:00
|
|
|
module.for_each_child(|ident, ns, binding| if ns == MacroNS {
|
2017-01-14 02:58:50 -06:00
|
|
|
let imported_binding = self.import(binding, directive);
|
|
|
|
self.legacy_import_macro(ident.name, imported_binding, span, allow_shadowing);
|
2016-10-28 01:52:45 -05:00
|
|
|
});
|
|
|
|
} else {
|
2019-02-28 00:17:24 -06:00
|
|
|
for ident in single_imports.iter().cloned() {
|
2018-08-09 08:29:22 -05:00
|
|
|
let result = self.resolve_ident_in_module(
|
|
|
|
ModuleOrUniformRoot::Module(module),
|
|
|
|
ident,
|
|
|
|
MacroNS,
|
2018-11-08 16:29:07 -06:00
|
|
|
None,
|
2018-08-09 08:29:22 -05:00
|
|
|
false,
|
2019-02-28 00:17:24 -06:00
|
|
|
ident.span,
|
2018-08-09 08:29:22 -05:00
|
|
|
);
|
2016-11-26 06:21:47 -06:00
|
|
|
if let Ok(binding) = result {
|
2019-02-28 00:17:24 -06:00
|
|
|
let directive = macro_use_directive(ident.span);
|
2017-01-14 02:58:50 -06:00
|
|
|
self.potentially_unused_imports.push(directive);
|
|
|
|
let imported_binding = self.import(binding, directive);
|
2019-02-28 00:17:24 -06:00
|
|
|
self.legacy_import_macro(ident.name, imported_binding,
|
|
|
|
ident.span, allow_shadowing);
|
2016-10-17 02:46:25 -05:00
|
|
|
} else {
|
2019-02-28 00:17:24 -06:00
|
|
|
span_err!(self.session, ident.span, E0469, "imported macro not found");
|
2016-10-17 02:46:25 -05:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 01:52:45 -05:00
|
|
|
}
|
2018-10-25 17:15:51 -05:00
|
|
|
import_all.is_some() || !single_imports.is_empty()
|
2016-10-17 02:46:25 -05:00
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Returns `true` if this attribute list contains `macro_use`.
|
2016-09-16 01:45:03 -05:00
|
|
|
fn contains_macro_use(&mut self, attrs: &[ast::Attribute]) -> bool {
|
|
|
|
for attr in attrs {
|
|
|
|
if attr.check_name("macro_escape") {
|
|
|
|
let msg = "macro_escape is a deprecated synonym for macro_use";
|
|
|
|
let mut err = self.session.struct_span_warn(attr.span, msg);
|
2016-11-14 06:00:25 -06:00
|
|
|
if let ast::AttrStyle::Inner = attr.style {
|
2016-09-16 01:45:03 -05:00
|
|
|
err.help("consider an outer attribute, #[macro_use] mod ...").emit();
|
|
|
|
} else {
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
} else if !attr.check_name("macro_use") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !attr.is_word() {
|
|
|
|
self.session.span_err(attr.span, "arguments to macro_use are not allowed here");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
2016-03-02 03:18:47 -06:00
|
|
|
}
|
|
|
|
|
2018-12-09 22:37:10 -06:00
|
|
|
pub struct BuildReducedGraphVisitor<'a, 'b: 'a> {
|
|
|
|
pub resolver: &'a mut Resolver<'b>,
|
2018-08-28 20:48:02 -05:00
|
|
|
pub current_legacy_scope: LegacyScope<'b>,
|
2016-10-10 22:42:06 -05:00
|
|
|
pub expansion: Mark,
|
2016-09-14 16:03:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-09 22:37:10 -06:00
|
|
|
impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
|
2016-10-06 03:04:30 -05:00
|
|
|
fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> {
|
2017-03-16 05:23:33 -05:00
|
|
|
let mark = id.placeholder_to_mark();
|
2016-11-11 04:51:15 -06:00
|
|
|
self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
|
|
|
|
let invocation = self.resolver.invocations[&mark];
|
2016-10-06 03:04:30 -05:00
|
|
|
invocation.module.set(self.resolver.current_module);
|
2018-08-28 20:48:02 -05:00
|
|
|
invocation.parent_legacy_scope.set(self.current_legacy_scope);
|
2016-10-06 03:04:30 -05:00
|
|
|
invocation
|
2016-09-14 16:03:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! method {
|
|
|
|
($visit:ident: $ty:ty, $invoc:path, $walk:ident) => {
|
2016-12-06 04:26:52 -06:00
|
|
|
fn $visit(&mut self, node: &'a $ty) {
|
2016-10-06 03:04:30 -05:00
|
|
|
if let $invoc(..) = node.node {
|
|
|
|
self.visit_invoc(node.id);
|
|
|
|
} else {
|
|
|
|
visit::$walk(self, node);
|
2016-09-14 16:03:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2018-12-09 22:37:10 -06:00
|
|
|
impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
|
2016-09-14 16:03:09 -05:00
|
|
|
method!(visit_impl_item: ast::ImplItem, ast::ImplItemKind::Macro, walk_impl_item);
|
|
|
|
method!(visit_expr: ast::Expr, ast::ExprKind::Mac, walk_expr);
|
|
|
|
method!(visit_pat: ast::Pat, ast::PatKind::Mac, walk_pat);
|
|
|
|
method!(visit_ty: ast::Ty, ast::TyKind::Mac, walk_ty);
|
|
|
|
|
2016-12-06 04:26:52 -06:00
|
|
|
fn visit_item(&mut self, item: &'a Item) {
|
2016-10-06 03:04:30 -05:00
|
|
|
let macro_use = match item.node {
|
2017-03-04 23:15:58 -06:00
|
|
|
ItemKind::MacroDef(..) => {
|
2018-08-28 20:48:02 -05:00
|
|
|
self.resolver.define_macro(item, self.expansion, &mut self.current_legacy_scope);
|
2017-03-04 23:15:58 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ItemKind::Mac(..) => {
|
2018-08-31 14:53:08 -05:00
|
|
|
self.current_legacy_scope = LegacyScope::Invocation(self.visit_invoc(item.id));
|
2016-12-01 05:20:04 -06:00
|
|
|
return
|
2016-10-06 03:04:30 -05:00
|
|
|
}
|
|
|
|
ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),
|
|
|
|
_ => false,
|
|
|
|
};
|
2016-09-14 16:03:09 -05:00
|
|
|
|
2018-08-28 20:48:02 -05:00
|
|
|
let orig_current_module = self.resolver.current_module;
|
|
|
|
let orig_current_legacy_scope = self.current_legacy_scope;
|
2018-10-21 17:28:59 -05:00
|
|
|
let parent_scope = ParentScope {
|
|
|
|
module: self.resolver.current_module,
|
|
|
|
expansion: self.expansion,
|
|
|
|
legacy: self.current_legacy_scope,
|
|
|
|
derives: Vec::new(),
|
|
|
|
};
|
|
|
|
self.resolver.build_reduced_graph_for_item(item, parent_scope);
|
2016-09-16 17:21:46 -05:00
|
|
|
visit::walk_item(self, item);
|
2018-08-28 20:48:02 -05:00
|
|
|
self.resolver.current_module = orig_current_module;
|
2016-10-06 03:04:30 -05:00
|
|
|
if !macro_use {
|
2018-08-28 20:48:02 -05:00
|
|
|
self.current_legacy_scope = orig_current_legacy_scope;
|
2016-10-06 03:04:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 04:26:52 -06:00
|
|
|
fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
|
2016-10-06 03:04:30 -05:00
|
|
|
if let ast::StmtKind::Mac(..) = stmt.node {
|
2018-08-31 14:53:08 -05:00
|
|
|
self.current_legacy_scope = LegacyScope::Invocation(self.visit_invoc(stmt.id));
|
2016-10-06 03:04:30 -05:00
|
|
|
} else {
|
|
|
|
visit::walk_stmt(self, stmt);
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-12-06 04:26:52 -06:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
|
2018-03-10 20:16:26 -06:00
|
|
|
if let ForeignItemKind::Macro(_) = foreign_item.node {
|
|
|
|
self.visit_invoc(foreign_item.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-07 16:23:26 -06:00
|
|
|
self.resolver.build_reduced_graph_for_foreign_item(foreign_item, self.expansion);
|
2016-09-14 16:03:09 -05:00
|
|
|
visit::walk_foreign_item(self, foreign_item);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-12-06 04:26:52 -06:00
|
|
|
fn visit_block(&mut self, block: &'a Block) {
|
2018-08-28 20:48:02 -05:00
|
|
|
let orig_current_module = self.resolver.current_module;
|
|
|
|
let orig_current_legacy_scope = self.current_legacy_scope;
|
2017-03-22 03:39:51 -05:00
|
|
|
self.resolver.build_reduced_graph_for_block(block, self.expansion);
|
2016-09-16 17:21:46 -05:00
|
|
|
visit::walk_block(self, block);
|
2018-08-28 20:48:02 -05:00
|
|
|
self.resolver.current_module = orig_current_module;
|
|
|
|
self.current_legacy_scope = orig_current_legacy_scope;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-09-14 18:39:13 -05:00
|
|
|
|
2016-12-06 04:26:52 -06:00
|
|
|
fn visit_trait_item(&mut self, item: &'a TraitItem) {
|
2016-09-14 18:39:13 -05:00
|
|
|
let parent = self.resolver.current_module;
|
|
|
|
|
2016-10-04 00:36:14 -05:00
|
|
|
if let TraitItemKind::Macro(_) = item.node {
|
2016-10-06 03:04:30 -05:00
|
|
|
self.visit_invoc(item.id);
|
|
|
|
return
|
2016-10-04 00:36:14 -05:00
|
|
|
}
|
|
|
|
|
2016-09-14 18:39:13 -05:00
|
|
|
// Add the item to the trait info.
|
|
|
|
let item_def_id = self.resolver.definitions.local_def_id(item.id);
|
2017-03-17 21:10:13 -05:00
|
|
|
let (def, ns) = match item.node {
|
|
|
|
TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS),
|
|
|
|
TraitItemKind::Method(ref sig, _) => {
|
|
|
|
if sig.decl.has_self() {
|
|
|
|
self.resolver.has_self.insert(item_def_id);
|
|
|
|
}
|
|
|
|
(Def::Method(item_def_id), ValueNS)
|
|
|
|
}
|
|
|
|
TraitItemKind::Type(..) => (Def::AssociatedTy(item_def_id), TypeNS),
|
2016-10-04 00:36:14 -05:00
|
|
|
TraitItemKind::Macro(_) => bug!(), // handled above
|
2016-09-14 18:39:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let vis = ty::Visibility::Public;
|
2016-11-28 20:07:12 -06:00
|
|
|
self.resolver.define(parent, item.ident, ns, (def, vis, item.span, self.expansion));
|
2016-09-14 18:39:13 -05:00
|
|
|
|
|
|
|
self.resolver.current_module = parent.parent.unwrap(); // nearest normal ancestor
|
|
|
|
visit::walk_trait_item(self, item);
|
|
|
|
self.resolver.current_module = parent;
|
|
|
|
}
|
2017-10-23 03:22:28 -05:00
|
|
|
|
|
|
|
fn visit_token(&mut self, t: Token) {
|
|
|
|
if let Token::Interpolated(nt) = t {
|
2019-02-14 16:10:02 -06:00
|
|
|
if let token::NtExpr(ref expr) = *nt {
|
2018-10-17 04:13:44 -05:00
|
|
|
if let ast::ExprKind::Mac(..) = expr.node {
|
|
|
|
self.visit_invoc(expr.id);
|
2017-10-23 03:22:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 16:04:54 -05:00
|
|
|
|
|
|
|
fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
|
|
|
|
if !attr.is_sugared_doc && is_builtin_attr(attr) {
|
2018-09-12 17:41:07 -05:00
|
|
|
let parent_scope = ParentScope {
|
|
|
|
module: self.resolver.current_module.nearest_item_scope(),
|
|
|
|
expansion: self.expansion,
|
|
|
|
legacy: self.current_legacy_scope,
|
|
|
|
// Let's hope discerning built-in attributes from derive helpers is not necessary
|
|
|
|
derives: Vec::new(),
|
|
|
|
};
|
|
|
|
parent_scope.module.builtin_attrs.borrow_mut().push((
|
|
|
|
attr.path.segments[0].ident, parent_scope
|
2018-09-02 16:04:54 -05:00
|
|
|
));
|
|
|
|
}
|
|
|
|
visit::walk_attribute(self, attr);
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|