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-08-12 15:39:49 -05:00
|
|
|
use crate::macros::{LegacyBinding, LegacyScope};
|
2019-02-06 11:15:23 -06:00
|
|
|
use crate::resolve_imports::ImportDirective;
|
|
|
|
use crate::resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
|
|
|
|
use crate::{Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, Segment, ToNameBinding};
|
2019-07-11 13:45:43 -05:00
|
|
|
use crate::{ModuleOrUniformRoot, ParentScope, PerNS, Resolver, ResolverArenas, ExternPreludeEntry};
|
2019-02-06 11:15:23 -06:00
|
|
|
use crate::Namespace::{self, TypeNS, ValueNS, MacroNS};
|
2019-08-08 15:57:35 -05:00
|
|
|
use crate::{ResolutionError, Determinacy, PathResult, CrateLint};
|
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, *};
|
2019-06-20 03:52:31 -05:00
|
|
|
use rustc::hir::def_id::{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};
|
2019-08-08 15:57:35 -05:00
|
|
|
use syntax::ext::base::{MacroKind, SyntaxExtension};
|
2019-07-15 17:04:05 -05:00
|
|
|
use syntax::ext::hygiene::ExpnId;
|
2019-01-07 09:09:17 -06:00
|
|
|
use syntax::feature_gate::is_builtin_attr;
|
2019-06-05 05:54:54 -05:00
|
|
|
use syntax::parse::token::{self, Token};
|
2019-08-08 15:57:35 -05:00
|
|
|
use syntax::{span_err, struct_span_err};
|
2019-05-11 09:41:37 -05:00
|
|
|
use syntax::symbol::{kw, sym};
|
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-20 11:36:05 -05:00
|
|
|
type Res = def::Res<NodeId>;
|
2019-04-03 02:07:45 -05:00
|
|
|
|
2019-07-15 17:04:05 -05:00
|
|
|
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, ExpnId) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 17:04:05 -05:00
|
|
|
impl<'a> ToNameBinding<'a> for (Res, ty::Visibility, Span, ExpnId) {
|
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 {
|
2019-04-20 11:36:05 -05:00
|
|
|
kind: NameBindingKind::Res(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;
|
|
|
|
|
2019-07-15 17:04:05 -05:00
|
|
|
impl<'a> ToNameBinding<'a> for (Res, ty::Visibility, Span, ExpnId, IsMacroExport) {
|
2018-07-29 06:51:17 -05:00
|
|
|
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
|
|
|
|
arenas.alloc_name_binding(NameBinding {
|
2019-04-20 11:36:05 -05:00
|
|
|
kind: NameBindingKind::Res(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:57:35 -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]
|
|
|
|
}
|
|
|
|
|
|
|
|
let macros_only = self.cstore.dep_kind_untracked(def_id.krate).macros_only();
|
|
|
|
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 {
|
|
|
|
(self.cstore.crate_name_untracked(def_id.krate).as_interned_str(), None)
|
|
|
|
} else {
|
|
|
|
let def_key = self.cstore.def_key(def_id);
|
|
|
|
(def_key.disambiguated_data.data.get_opt_name().unwrap(),
|
|
|
|
Some(self.get_module(DefId { index: def_key.parent.unwrap(), ..def_id })))
|
|
|
|
};
|
|
|
|
|
|
|
|
let kind = ModuleKind::Def(DefKind::Mod, def_id, name.as_symbol());
|
|
|
|
let module = self.arenas.alloc_module(ModuleData::new(
|
|
|
|
parent, kind, def_id, ExpnId::root(), DUMMY_SP
|
|
|
|
));
|
|
|
|
self.extern_module_map.insert((def_id, macros_only), module);
|
|
|
|
module
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn macro_def_scope(&mut self, expn_id: ExpnId) -> Module<'a> {
|
|
|
|
let def_id = match self.macro_defs.get(&expn_id) {
|
|
|
|
Some(def_id) => *def_id,
|
|
|
|
None => return self.graph_root,
|
|
|
|
};
|
|
|
|
if let Some(id) = self.definitions.as_local_node_id(def_id) {
|
|
|
|
self.local_macro_def_scopes[&id]
|
|
|
|
} else {
|
|
|
|
let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
|
|
|
|
self.get_module(module_def_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn get_macro(&mut self, res: Res) -> Option<Lrc<SyntaxExtension>> {
|
|
|
|
match res {
|
|
|
|
Res::Def(DefKind::Macro(..), def_id) => self.get_macro_by_def_id(def_id),
|
|
|
|
Res::NonMacroAttr(attr_kind) =>
|
|
|
|
Some(self.non_macro_attr(attr_kind == NonMacroAttrKind::Tool)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn get_macro_by_def_id(&mut self, def_id: DefId) -> Option<Lrc<SyntaxExtension>> {
|
|
|
|
if let Some(ext) = self.macro_map.get(&def_id) {
|
|
|
|
return Some(ext.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
let macro_def = match self.cstore.load_macro_untracked(def_id, &self.session) {
|
|
|
|
LoadedMacro::MacroDef(macro_def) => macro_def,
|
|
|
|
LoadedMacro::ProcMacro(ext) => return Some(ext),
|
|
|
|
};
|
|
|
|
|
|
|
|
let ext = self.compile_macro(¯o_def, self.cstore.crate_edition_untracked(def_id.krate));
|
|
|
|
self.macro_map.insert(def_id, ext.clone());
|
|
|
|
Some(ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensures that the reduced graph rooted at the given external module
|
|
|
|
/// is built, building it if it is not.
|
|
|
|
pub fn populate_module_if_necessary(&mut self, module: Module<'a>) {
|
|
|
|
if module.populated.get() { return }
|
|
|
|
let def_id = module.def_id().unwrap();
|
|
|
|
for child in self.cstore.item_children_untracked(def_id, self.session) {
|
|
|
|
let child = child.map_id(|_| panic!("unexpected id"));
|
2019-08-12 15:19:36 -05:00
|
|
|
BuildReducedGraphVisitor { parent_scope: ParentScope::default(module), r: self }
|
|
|
|
.build_reduced_graph_for_external_crate_res(child);
|
2019-08-08 15:57:35 -05:00
|
|
|
}
|
|
|
|
module.populated.set(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BuildReducedGraphVisitor<'a, 'b> {
|
|
|
|
pub r: &'b mut Resolver<'a>,
|
|
|
|
pub parent_scope: ParentScope<'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
|
|
|
|
fn resolve_visibility(&mut self, vis: &ast::Visibility) -> ty::Visibility {
|
|
|
|
let parent_scope = &self.parent_scope;
|
|
|
|
match vis.node {
|
|
|
|
ast::VisibilityKind::Public => ty::Visibility::Public,
|
|
|
|
ast::VisibilityKind::Crate(..) => {
|
|
|
|
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))
|
|
|
|
}
|
|
|
|
ast::VisibilityKind::Inherited => {
|
|
|
|
ty::Visibility::Restricted(parent_scope.module.normal_ancestor_id)
|
|
|
|
}
|
|
|
|
ast::VisibilityKind::Restricted { ref path, id, .. } => {
|
|
|
|
// For visibilities we are not ready to provide correct implementation of "uniform
|
|
|
|
// paths" right now, so on 2018 edition we only allow module-relative paths for now.
|
|
|
|
// On 2015 edition visibilities are resolved as crate-relative by default,
|
|
|
|
// so we are prepending a root segment if necessary.
|
|
|
|
let ident = path.segments.get(0).expect("empty path in visibility").ident;
|
|
|
|
let crate_root = if ident.is_path_segment_keyword() {
|
|
|
|
None
|
|
|
|
} else if ident.span.rust_2018() {
|
|
|
|
let msg = "relative paths are not supported in visibilities on 2018 edition";
|
|
|
|
self.r.session.struct_span_err(ident.span, msg)
|
|
|
|
.span_suggestion(
|
|
|
|
path.span,
|
|
|
|
"try",
|
|
|
|
format!("crate::{}", path),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
return ty::Visibility::Public;
|
|
|
|
} else {
|
|
|
|
let ctxt = ident.span.ctxt();
|
|
|
|
Some(Segment::from_ident(Ident::new(
|
|
|
|
kw::PathRoot, path.span.shrink_to_lo().with_ctxt(ctxt)
|
|
|
|
)))
|
|
|
|
};
|
|
|
|
|
|
|
|
let segments = crate_root.into_iter()
|
|
|
|
.chain(path.segments.iter().map(|seg| seg.into())).collect::<Vec<_>>();
|
|
|
|
let expected_found_error = |this: &Self, res: Res| {
|
|
|
|
let path_str = Segment::names_to_string(&segments);
|
|
|
|
struct_span_err!(this.r.session, path.span, E0577,
|
|
|
|
"expected module, found {} `{}`", res.descr(), path_str)
|
|
|
|
.span_label(path.span, "not a module").emit();
|
|
|
|
};
|
|
|
|
match self.r.resolve_path(
|
|
|
|
&segments,
|
|
|
|
Some(TypeNS),
|
|
|
|
parent_scope,
|
|
|
|
true,
|
|
|
|
path.span,
|
|
|
|
CrateLint::SimplePath(id),
|
|
|
|
) {
|
|
|
|
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
|
|
|
|
let res = module.res().expect("visibility resolved to unnamed block");
|
|
|
|
self.r.record_partial_res(id, PartialRes::new(res));
|
|
|
|
if module.is_normal() {
|
|
|
|
if res == Res::Err {
|
|
|
|
ty::Visibility::Public
|
|
|
|
} else {
|
|
|
|
let vis = ty::Visibility::Restricted(res.def_id());
|
|
|
|
if self.r.is_accessible_from(vis, parent_scope.module) {
|
|
|
|
vis
|
|
|
|
} else {
|
|
|
|
let msg =
|
|
|
|
"visibilities can only be restricted to ancestor modules";
|
|
|
|
self.r.session.span_err(path.span, msg);
|
|
|
|
ty::Visibility::Public
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
expected_found_error(self, res);
|
|
|
|
ty::Visibility::Public
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PathResult::Module(..) => {
|
|
|
|
self.r.session.span_err(path.span, "visibility must resolve to a module");
|
|
|
|
ty::Visibility::Public
|
|
|
|
}
|
|
|
|
PathResult::NonModule(partial_res) => {
|
|
|
|
expected_found_error(self, partial_res.base_res());
|
|
|
|
ty::Visibility::Public
|
|
|
|
}
|
|
|
|
PathResult::Failed { span, label, suggestion, .. } => {
|
|
|
|
self.r.report_error(
|
|
|
|
span, ResolutionError::FailedToResolve { label, suggestion }
|
|
|
|
);
|
|
|
|
ty::Visibility::Public
|
|
|
|
}
|
|
|
|
PathResult::Indeterminate => {
|
|
|
|
span_err!(self.r.session, path.span, E0578,
|
|
|
|
"cannot determine resolution for the visibility");
|
|
|
|
ty::Visibility::Public
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 06:06:42 -05:00
|
|
|
fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Name>) {
|
|
|
|
if !field_names.is_empty() {
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.field_names.insert(def_id, field_names);
|
2019-08-08 06:06:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
// Add an import directive to the current module.
|
|
|
|
fn add_import_directive(
|
|
|
|
&mut self,
|
|
|
|
module_path: Vec<Segment>,
|
|
|
|
subclass: ImportDirectiveSubclass<'a>,
|
|
|
|
span: Span,
|
|
|
|
id: NodeId,
|
|
|
|
item: &ast::Item,
|
|
|
|
root_span: Span,
|
|
|
|
root_id: NodeId,
|
|
|
|
vis: ty::Visibility,
|
|
|
|
) {
|
2019-08-12 17:39:10 -05:00
|
|
|
let current_module = self.parent_scope.module;
|
2019-08-08 15:57:35 -05:00
|
|
|
let directive = self.r.arenas.alloc_import_directive(ImportDirective {
|
2019-08-12 17:39:10 -05:00
|
|
|
parent_scope: self.parent_scope,
|
2019-08-08 15:57:35 -05:00
|
|
|
module_path,
|
|
|
|
imported_module: Cell::new(None),
|
|
|
|
subclass,
|
|
|
|
span,
|
|
|
|
id,
|
|
|
|
use_span: item.span,
|
|
|
|
use_span_with_attributes: item.span_with_attributes(),
|
|
|
|
has_attributes: !item.attrs.is_empty(),
|
|
|
|
root_span,
|
|
|
|
root_id,
|
|
|
|
vis: Cell::new(vis),
|
|
|
|
used: Cell::new(false),
|
|
|
|
});
|
|
|
|
|
|
|
|
debug!("add_import_directive({:?})", directive);
|
|
|
|
|
|
|
|
self.r.indeterminate_imports.push(directive);
|
|
|
|
match directive.subclass {
|
|
|
|
SingleImport { target, type_ns_only, .. } => {
|
|
|
|
self.r.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
|
|
|
|
let mut resolution = this.resolution(current_module, target, ns).borrow_mut();
|
|
|
|
resolution.add_single_import(directive);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// We don't add prelude imports to the globs since they only affect lexical scopes,
|
|
|
|
// which are not relevant to import resolution.
|
|
|
|
GlobImport { is_prelude: true, .. } => {}
|
|
|
|
GlobImport { .. } => current_module.globs.borrow_mut().push(directive),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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(
|
2019-05-11 09:41:37 -05:00
|
|
|
kw::PathRoot, 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() ||
|
2019-05-11 09:41:37 -05:00
|
|
|
prefix.len() == 1 && prefix[0].ident.name == kw::PathRoot
|
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`
|
2019-05-11 09:41:37 -05:00
|
|
|
if source.ident.name == kw::SelfLower {
|
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) {
|
2019-08-08 15:32:58 -05:00
|
|
|
self.r.report_error(
|
2017-09-26 16:04:00 -05:00
|
|
|
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`
|
2019-05-11 09:41:37 -05:00
|
|
|
if source.ident.name == kw::SelfLower {
|
2019-08-08 15:32:58 -05:00
|
|
|
self.r.report_error(
|
|
|
|
use_tree.span, ResolutionError::SelfImportsOnlyAllowedWithin
|
|
|
|
);
|
2017-09-26 16:04:00 -05:00
|
|
|
}
|
2014-12-23 13:34:36 -06:00
|
|
|
|
2017-09-26 16:04:00 -05:00
|
|
|
// Disallow `use $crate;`
|
2019-05-11 09:41:37 -05:00
|
|
|
if source.ident.name == kw::DollarCrate && module_path.is_empty() {
|
2019-08-08 06:06:42 -05:00
|
|
|
let crate_root = self.r.resolve_crate_root(source.ident);
|
2017-09-26 16:04:00 -05:00
|
|
|
let crate_name = match crate_root.kind {
|
2019-04-20 11:46:19 -05:00
|
|
|
ModuleKind::Def(.., name) => name,
|
2017-09-26 16:04:00 -05:00
|
|
|
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`.
|
2019-05-11 09:41:37 -05:00
|
|
|
if crate_name != kw::Invalid {
|
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 {
|
2019-05-11 09:41:37 -05:00
|
|
|
name: kw::PathRoot,
|
2018-09-11 22:21:50 -05:00
|
|
|
span: source.ident.span,
|
|
|
|
},
|
2019-08-08 06:06:42 -05:00
|
|
|
id: Some(self.r.session.next_node_id()),
|
2018-09-11 22:21:50 -05:00
|
|
|
});
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.session.struct_span_warn(item.span, "`$crate` may not be imported")
|
2017-09-26 16:04:00 -05:00
|
|
|
.note("`use $crate;` was erroneously allowed and \
|
|
|
|
will become a hard error in a future release")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 09:41:37 -05:00
|
|
|
if ident.name == kw::Crate {
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.session.span_err(ident.span,
|
2018-08-09 08:29:22 -05:00
|
|
|
"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 {
|
2019-07-03 03:44:57 -05:00
|
|
|
type_ns: Cell::new(Err(Determinacy::Undetermined)),
|
|
|
|
value_ns: Cell::new(Err(Determinacy::Undetermined)),
|
|
|
|
macro_ns: Cell::new(Err(Determinacy::Undetermined)),
|
2018-04-29 18:20:14 -05:00
|
|
|
},
|
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,
|
2017-09-26 16:04:00 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
ast::UseTreeKind::Glob => {
|
|
|
|
let subclass = GlobImport {
|
2019-05-07 22:21:18 -05:00
|
|
|
is_prelude: attr::contains_name(&item.attrs, sym::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,
|
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 {
|
2019-05-11 09:41:37 -05:00
|
|
|
if use_tree.ident().name == kw::SelfLower {
|
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 {
|
2019-08-08 15:32:58 -05:00
|
|
|
let mut e = self.r.into_struct_error(
|
2017-09-26 16:04:00 -05:00
|
|
|
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
|
2019-08-08 15:57:35 -05:00
|
|
|
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-06-11 13:47:52 -05:00
|
|
|
// `a::b::c::{self as _}`, 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(
|
2019-05-11 09:41:37 -05:00
|
|
|
Ident::new(kw::SelfLower, new_span)
|
2018-10-27 15:38:09 -05:00
|
|
|
),
|
|
|
|
kind: ast::UseTreeKind::Simple(
|
2019-06-11 13:47:52 -05:00
|
|
|
Some(Ident::new(kw::Underscore, 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
|
2019-08-08 15:57:35 -05:00
|
|
|
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.
|
2019-08-07 19:44:16 -05:00
|
|
|
fn build_reduced_graph_for_item(&mut self, item: &Item) {
|
2019-08-08 15:57:35 -05:00
|
|
|
let parent_scope = &self.parent_scope;
|
2018-10-21 17:28:59 -05:00
|
|
|
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;
|
2019-08-08 15:57:35 -05:00
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
2017-09-26 16:04:00 -05:00
|
|
|
|
|
|
|
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
|
2019-08-08 15:57:35 -05:00
|
|
|
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) => {
|
2019-05-11 09:41:37 -05:00
|
|
|
let module = if orig_name.is_none() && ident.name == kw::SelfLower {
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.session
|
2018-10-25 17:39:47 -05:00
|
|
|
.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;
|
2019-05-11 09:41:37 -05:00
|
|
|
} else if orig_name == Some(kw::SelfLower) {
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.graph_root
|
2018-10-25 17:39:47 -05:00
|
|
|
} else {
|
2019-08-08 06:06:42 -05:00
|
|
|
let crate_id = self.r.crate_loader.process_extern_crate(
|
|
|
|
item, &self.r.definitions
|
2019-08-05 13:18:50 -05:00
|
|
|
);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX })
|
2018-10-25 17:39:47 -05:00
|
|
|
};
|
|
|
|
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.populate_module_if_necessary(module);
|
2017-12-06 12:50:55 -06:00
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
let used = self.process_legacy_macro_imports(item, module);
|
2016-11-28 20:53:00 -06:00
|
|
|
let binding =
|
2019-08-08 06:06:42 -05:00
|
|
|
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.r.arenas);
|
|
|
|
let directive = self.r.arenas.alloc_import_directive(ImportDirective {
|
2018-11-04 16:11:59 -06:00
|
|
|
root_id: item.id,
|
|
|
|
id: item.id,
|
2019-08-12 17:39:10 -05:00
|
|
|
parent_scope: self.parent_scope,
|
2018-11-04 16:11:59 -06:00
|
|
|
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),
|
|
|
|
});
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.potentially_unused_imports.push(directive);
|
|
|
|
let imported_binding = self.r.import(binding, directive);
|
|
|
|
if ptr::eq(parent, self.r.graph_root) {
|
|
|
|
if let Some(entry) = self.r.extern_prelude.get(&ident.modern()) {
|
2019-07-15 17:04:05 -05:00
|
|
|
if expansion != ExpnId::root() && orig_name.is_some() &&
|
2018-10-23 17:03:47 -05:00
|
|
|
entry.extern_crate_item.is_none() {
|
2019-08-08 06:06:42 -05:00
|
|
|
let msg = "macro-expanded `extern crate` items cannot \
|
|
|
|
shadow names passed with `--extern`";
|
|
|
|
self.r.session.span_err(item.span, msg);
|
2018-10-23 17:03:47 -05:00
|
|
|
}
|
|
|
|
}
|
2019-08-08 06:06:42 -05:00
|
|
|
let entry = self.r.extern_prelude.entry(ident.modern())
|
2018-10-23 17:03:47 -05:00
|
|
|
.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
|
|
|
}
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.define(parent, ident, TypeNS, imported_binding);
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
|
2017-03-15 21:27:40 -05:00
|
|
|
ItemKind::GlobalAsm(..) => {}
|
|
|
|
|
2019-05-11 11:08:09 -05:00
|
|
|
ItemKind::Mod(..) if ident.name == kw::Invalid => {} // Crate root
|
2016-09-14 16:03:09 -05:00
|
|
|
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Mod(..) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
let def_id = self.r.definitions.local_def_id(item.id);
|
2019-04-20 11:46:19 -05:00
|
|
|
let module_kind = ModuleKind::Def(DefKind::Mod, def_id, ident.name);
|
2019-08-08 06:06:42 -05:00
|
|
|
let module = self.r.arenas.alloc_module(ModuleData {
|
2016-09-19 00:25:17 -05:00
|
|
|
no_implicit_prelude: parent.no_implicit_prelude || {
|
2019-05-07 22:21:18 -05:00
|
|
|
attr::contains_name(&item.attrs, sym::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
|
|
|
});
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
|
|
|
self.r.module_map.insert(def_id, module);
|
2016-08-14 18:42:05 -05:00
|
|
|
|
|
|
|
// Descend into the module.
|
2019-08-07 19:44:16 -05:00
|
|
|
self.parent_scope.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(..) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
let res = Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id));
|
|
|
|
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-08-26 11:23:42 -05:00
|
|
|
ItemKind::Const(..) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
let res = Res::Def(DefKind::Const, self.r.definitions.local_def_id(item.id));
|
|
|
|
self.r.define(parent, ident, ValueNS, (res, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-08-26 11:23:42 -05:00
|
|
|
ItemKind::Fn(..) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
let res = Res::Def(DefKind::Fn, self.r.definitions.local_def_id(item.id));
|
|
|
|
self.r.define(parent, ident, ValueNS, (res, 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).
|
2019-08-08 15:57:35 -05:00
|
|
|
self.define_macro(item);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// These items live in the type namespace.
|
2019-08-02 05:02:08 -05:00
|
|
|
ItemKind::TyAlias(..) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
let res = Res::Def(DefKind::TyAlias, self.r.definitions.local_def_id(item.id));
|
|
|
|
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2019-07-31 18:41:54 -05:00
|
|
|
ItemKind::OpaqueTy(_, _) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
let res = Res::Def(DefKind::OpaqueTy, self.r.definitions.local_def_id(item.id));
|
|
|
|
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
2018-07-03 12:38:14 -05:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Enum(ref enum_definition, _) => {
|
2019-04-20 11:46:19 -05:00
|
|
|
let module_kind = ModuleKind::Def(
|
|
|
|
DefKind::Enum,
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.definitions.local_def_id(item.id),
|
2019-04-20 11:46:19 -05:00
|
|
|
ident.name,
|
|
|
|
);
|
2019-08-08 06:06:42 -05:00
|
|
|
let module = self.r.new_module(parent,
|
2017-05-10 06:19:29 -05:00
|
|
|
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);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.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 {
|
2019-08-12 15:19:36 -05:00
|
|
|
self.build_reduced_graph_for_variant(variant, module, vis);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 08:48:57 -05:00
|
|
|
ItemKind::TraitAlias(..) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
let res = Res::Def(DefKind::TraitAlias, self.r.definitions.local_def_id(item.id));
|
|
|
|
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
|
2017-10-02 08:48:57 -05:00
|
|
|
}
|
|
|
|
|
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.
|
2019-08-08 06:06:42 -05:00
|
|
|
let def_id = self.r.definitions.local_def_id(item.id);
|
2019-04-20 11:36:05 -05:00
|
|
|
let res = Res::Def(DefKind::Struct, def_id);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.define(parent, ident, TypeNS, (res, 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
|
|
|
|
2019-05-07 22:21:18 -05:00
|
|
|
let has_non_exhaustive = attr::contains_name(&item.attrs, sym::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| {
|
2019-08-08 15:57:35 -05:00
|
|
|
let field_vis = self.resolve_visibility(&field.vis);
|
2019-08-08 06:06:42 -05:00
|
|
|
if ctor_vis.is_at_least(field_vis, &*self.r) {
|
2017-01-08 15:21:35 -06:00
|
|
|
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();
|
2019-08-08 06:06:42 -05:00
|
|
|
let item_def_id = self.r.definitions.local_def_id(item.id);
|
2019-08-08 15:57:35 -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-04-20 11:36:05 -05:00
|
|
|
let ctor_res = Res::Def(
|
2019-04-20 10:26:26 -05:00
|
|
|
DefKind::Ctor(CtorOf::Struct, CtorKind::from_ast(struct_def)),
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.definitions.local_def_id(ctor_node_id),
|
2019-04-20 10:26:26 -05:00
|
|
|
);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
|
|
|
|
self.r.struct_constructors.insert(res.def_id(), (ctor_res, 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, _) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
let res = Res::Def(DefKind::Union, self.r.definitions.local_def_id(item.id));
|
|
|
|
self.r.define(parent, ident, TypeNS, (res, 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| {
|
2019-08-08 15:57:35 -05:00
|
|
|
self.resolve_visibility(&field.vis);
|
2016-08-06 13:56:02 -05:00
|
|
|
field.ident.map(|ident| ident.name)
|
|
|
|
}).collect();
|
2019-08-08 06:06:42 -05:00
|
|
|
let item_def_id = self.r.definitions.local_def_id(item.id);
|
2019-08-08 15:57:35 -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
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
ItemKind::Impl(.., ref impl_items) => {
|
|
|
|
for impl_item in impl_items {
|
|
|
|
self.resolve_visibility(&impl_item.vis);
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-09-14 18:39:13 -05:00
|
|
|
ItemKind::Trait(..) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
let def_id = self.r.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.
|
2019-04-20 11:46:19 -05:00
|
|
|
let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name);
|
2019-08-08 06:06:42 -05:00
|
|
|
let module = self.r.new_module(parent,
|
2017-05-10 06:19:29 -05:00
|
|
|
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);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
2019-08-07 19:44:16 -05:00
|
|
|
self.parent_scope.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>,
|
2019-08-12 15:19:36 -05:00
|
|
|
vis: ty::Visibility) {
|
|
|
|
let expn_id = self.parent_scope.expansion;
|
2019-08-13 19:40:21 -05:00
|
|
|
let ident = variant.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-08-13 19:40:21 -05:00
|
|
|
let def_id = self.r.definitions.local_def_id(variant.id);
|
2019-04-20 11:36:05 -05:00
|
|
|
let res = Res::Def(DefKind::Variant, def_id);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.define(parent, ident, TypeNS, (res, vis, variant.span, expn_id));
|
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;
|
2019-08-13 19:40:21 -05:00
|
|
|
let has_non_exhaustive = attr::contains_name(&variant.attrs, sym::non_exhaustive);
|
2019-03-22 11:19:12 -05:00
|
|
|
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.
|
2019-08-13 19:40:21 -05:00
|
|
|
let ctor_node_id = variant.data.ctor_id().unwrap_or(variant.id);
|
2019-08-08 06:06:42 -05:00
|
|
|
let ctor_def_id = self.r.definitions.local_def_id(ctor_node_id);
|
2019-08-13 19:40:21 -05:00
|
|
|
let ctor_kind = CtorKind::from_ast(&variant.data);
|
2019-04-20 11:36:05 -05:00
|
|
|
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, variant.span, expn_id));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs the reduced graph for one foreign item.
|
2019-08-07 19:44:16 -05:00
|
|
|
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
|
2019-04-20 11:36:05 -05:00
|
|
|
let (res, ns) = match item.node {
|
2016-04-23 22:26:10 -05:00
|
|
|
ForeignItemKind::Fn(..) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
(Res::Def(DefKind::Fn, self.r.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(..) => {
|
2019-08-08 06:06:42 -05:00
|
|
|
(Res::Def(DefKind::Static, self.r.definitions.local_def_id(item.id)), ValueNS)
|
2017-09-03 13:53:58 -05:00
|
|
|
}
|
|
|
|
ForeignItemKind::Ty => {
|
2019-08-08 06:06:42 -05:00
|
|
|
(Res::Def(DefKind::ForeignTy, self.r.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
|
|
|
};
|
2019-08-07 19:44:16 -05:00
|
|
|
let parent = self.parent_scope.module;
|
|
|
|
let expansion = self.parent_scope.expansion;
|
2019-08-08 15:57:35 -05:00
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2019-08-07 19:44:16 -05:00
|
|
|
fn build_reduced_graph_for_block(&mut self, block: &Block) {
|
|
|
|
let parent = self.parent_scope.module;
|
|
|
|
let expansion = self.parent_scope.expansion;
|
2014-12-30 12:16:42 -06:00
|
|
|
if self.block_needs_anonymous_module(block) {
|
2019-08-08 06:06:42 -05:00
|
|
|
let module = self.r.new_module(parent,
|
2017-05-10 06:58:41 -05:00
|
|
|
ModuleKind::Block(block.id),
|
|
|
|
parent.normal_ancestor_id,
|
2019-08-07 19:44:16 -05:00
|
|
|
expansion,
|
2017-05-10 06:58:41 -05:00
|
|
|
block.span);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.block_map.insert(block.id, module);
|
2019-08-07 19:44:16 -05:00
|
|
|
self.parent_scope.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-08-12 15:19:36 -05:00
|
|
|
fn build_reduced_graph_for_external_crate_res(&mut self, child: Export<ast::NodeId>) {
|
|
|
|
let parent = self.parent_scope.module;
|
2019-04-20 11:36:05 -05:00
|
|
|
let Export { ident, res, vis, span } = child;
|
2018-11-30 13:34:24 -06:00
|
|
|
// 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();
|
2019-07-15 17:04:05 -05:00
|
|
|
let expansion = ExpnId::root(); // FIXME(jseyfried) intercrate hygiene
|
2019-04-20 11:36:05 -05:00
|
|
|
match res {
|
|
|
|
Res::Def(kind @ DefKind::Mod, def_id)
|
|
|
|
| Res::Def(kind @ DefKind::Enum, def_id) => {
|
2019-08-08 15:57:35 -05:00
|
|
|
let module = self.r.new_module(parent,
|
2019-04-20 11:46:19 -05:00
|
|
|
ModuleKind::Def(kind, def_id, ident.name),
|
2017-05-10 06:19:29 -05:00
|
|
|
def_id,
|
2017-03-22 03:39:51 -05:00
|
|
|
expansion,
|
2017-05-10 06:19:29 -05:00
|
|
|
span);
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2019-04-20 11:36:05 -05:00
|
|
|
Res::Def(DefKind::Variant, _)
|
|
|
|
| Res::Def(DefKind::TyAlias, _)
|
|
|
|
| Res::Def(DefKind::ForeignTy, _)
|
2019-07-31 18:41:54 -05:00
|
|
|
| Res::Def(DefKind::OpaqueTy, _)
|
2019-04-20 11:36:05 -05:00
|
|
|
| Res::Def(DefKind::TraitAlias, _)
|
|
|
|
| Res::PrimTy(..)
|
|
|
|
| Res::ToolMod => {
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.define(parent, ident, TypeNS, (res, vis, DUMMY_SP, expansion));
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2019-04-20 11:36:05 -05:00
|
|
|
Res::Def(DefKind::Fn, _)
|
|
|
|
| Res::Def(DefKind::Static, _)
|
|
|
|
| Res::Def(DefKind::Const, _)
|
|
|
|
| Res::Def(DefKind::Ctor(CtorOf::Variant, ..), _) => {
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.define(parent, ident, ValueNS, (res, vis, DUMMY_SP, expansion));
|
2017-01-28 17:56:52 -06:00
|
|
|
}
|
2019-04-20 11:36:05 -05:00
|
|
|
Res::Def(DefKind::Ctor(CtorOf::Struct, ..), def_id) => {
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.define(parent, ident, ValueNS, (res, vis, DUMMY_SP, expansion));
|
2017-01-28 17:56:52 -06:00
|
|
|
|
|
|
|
if let Some(struct_def_id) =
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.cstore.def_key(def_id).parent
|
2017-01-28 17:56:52 -06:00
|
|
|
.map(|index| DefId { krate: def_id.krate, index: index }) {
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.struct_constructors.insert(struct_def_id, (res, vis));
|
2017-01-28 17:56:52 -06:00
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2019-04-20 11:36:05 -05:00
|
|
|
Res::Def(DefKind::Trait, def_id) => {
|
2019-04-20 11:46:19 -05:00
|
|
|
let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name);
|
2019-08-08 15:57:35 -05:00
|
|
|
let module = self.r.new_module(parent,
|
2017-05-10 06:19:29 -05:00
|
|
|
module_kind,
|
|
|
|
parent.normal_ancestor_id,
|
2017-03-22 03:39:51 -05:00
|
|
|
expansion,
|
2017-05-10 06:19:29 -05:00
|
|
|
span);
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
|
2015-10-26 14:31:11 -05:00
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
for child in self.r.cstore.item_children_untracked(def_id, self.r.session) {
|
2019-04-20 11:36:05 -05:00
|
|
|
let res = child.res.map_id(|_| panic!("unexpected id"));
|
2019-05-19 03:26:08 -05:00
|
|
|
let ns = if let Res::Def(DefKind::AssocTy, _) = res {
|
2019-04-20 10:26:26 -05:00
|
|
|
TypeNS
|
|
|
|
} else { ValueNS };
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.define(module, child.ident, ns,
|
2019-04-20 11:36:05 -05:00
|
|
|
(res, ty::Visibility::Public, DUMMY_SP, expansion));
|
2015-10-26 14:31:11 -05:00
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
if self.r.cstore.associated_item_cloned_untracked(child.res.def_id())
|
2017-03-17 21:10:13 -05:00
|
|
|
.method_has_self_argument {
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.has_self.insert(res.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-04-20 11:36:05 -05:00
|
|
|
Res::Def(DefKind::Struct, def_id) | Res::Def(DefKind::Union, def_id) => {
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.define(parent, ident, TypeNS, (res, 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.
|
2019-08-08 15:57:35 -05:00
|
|
|
let field_names = self.r.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-04-20 11:36:05 -05:00
|
|
|
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
|
2019-08-08 15:57:35 -05:00
|
|
|
self.r.define(parent, ident, MacroNS, (res, vis, DUMMY_SP, expansion));
|
2016-10-25 17:05:02 -05:00
|
|
|
}
|
2019-04-20 11:36:05 -05:00
|
|
|
_ => bug!("unexpected resolution: {:?}", res)
|
2014-12-30 12:16:42 -06: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) {
|
2019-08-08 06:06:42 -05:00
|
|
|
if self.r.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)";
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.session.struct_span_err(span, &msg).note(note).emit();
|
2016-10-28 01:52:45 -05:00
|
|
|
}
|
|
|
|
}
|
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.
|
2019-08-08 15:57:35 -05:00
|
|
|
fn process_legacy_macro_imports(&mut self, item: &Item, module: Module<'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 {
|
2019-05-07 22:21:18 -05:00
|
|
|
if attr.check_name(sym::macro_use) {
|
2019-08-07 19:44:16 -05:00
|
|
|
if self.parent_scope.module.parent.is_some() {
|
2019-08-08 06:06:42 -05:00
|
|
|
span_err!(self.r.session, item.span, E0468,
|
2018-10-25 17:15:51 -05:00
|
|
|
"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 {
|
2019-05-11 09:41:37 -05:00
|
|
|
if orig_name == kw::SelfLower {
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.session.span_err(attr.span,
|
2018-10-25 17:39:47 -05:00
|
|
|
"`macro_use` is not supported on `extern crate self`");
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 06:06:42 -05:00
|
|
|
let ill_formed = |span| span_err!(self.r.session, span, E0466, "bad macro import");
|
2018-10-25 17:15:51 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
let macro_use_directive =
|
|
|
|
|this: &Self, span| this.r.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,
|
2019-08-12 17:39:10 -05:00
|
|
|
parent_scope: this.parent_scope,
|
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),
|
|
|
|
});
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
let allow_shadowing = self.parent_scope.expansion == ExpnId::root();
|
2018-10-25 17:15:51 -05:00
|
|
|
if let Some(span) = import_all {
|
2019-08-08 15:57:35 -05:00
|
|
|
let directive = macro_use_directive(self, span);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.potentially_unused_imports.push(directive);
|
2016-11-28 20:07:12 -06:00
|
|
|
module.for_each_child(|ident, ns, binding| if ns == MacroNS {
|
2019-08-08 06:06:42 -05:00
|
|
|
let imported_binding = self.r.import(binding, directive);
|
2017-01-14 02:58:50 -06:00
|
|
|
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() {
|
2019-08-08 06:06:42 -05:00
|
|
|
let result = self.r.resolve_ident_in_module(
|
2018-08-09 08:29:22 -05:00
|
|
|
ModuleOrUniformRoot::Module(module),
|
|
|
|
ident,
|
|
|
|
MacroNS,
|
2019-08-08 15:57:35 -05:00
|
|
|
&self.parent_scope,
|
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-08-08 15:57:35 -05:00
|
|
|
let directive = macro_use_directive(self, ident.span);
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.potentially_unused_imports.push(directive);
|
|
|
|
let imported_binding = self.r.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-08-08 06:06:42 -05:00
|
|
|
span_err!(self.r.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 {
|
2019-05-07 22:21:18 -05:00
|
|
|
if attr.check_name(sym::macro_escape) {
|
2016-09-16 01:45:03 -05:00
|
|
|
let msg = "macro_escape is a deprecated synonym for macro_use";
|
2019-08-08 06:06:42 -05:00
|
|
|
let mut err = self.r.session.struct_span_warn(attr.span, msg);
|
2016-11-14 06:00:25 -06:00
|
|
|
if let ast::AttrStyle::Inner = attr.style {
|
2019-07-23 14:26:01 -05:00
|
|
|
err.help("consider an outer attribute, `#[macro_use]` mod ...").emit();
|
2016-09-16 01:45:03 -05:00
|
|
|
} else {
|
|
|
|
err.emit();
|
|
|
|
}
|
2019-05-07 22:21:18 -05:00
|
|
|
} else if !attr.check_name(sym::macro_use) {
|
2016-09-16 01:45:03 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !attr.is_word() {
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.session.span_err(attr.span, "arguments to macro_use are not allowed here");
|
2016-09-16 01:45:03 -05:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
2016-03-02 03:18:47 -06:00
|
|
|
|
2019-08-12 15:39:49 -05:00
|
|
|
fn visit_invoc(&mut self, id: ast::NodeId) -> LegacyScope<'a> {
|
2019-07-15 17:42:58 -05:00
|
|
|
let invoc_id = id.placeholder_to_expn_id();
|
2019-07-02 05:47:28 -05:00
|
|
|
|
2019-08-12 17:39:10 -05:00
|
|
|
self.parent_scope.module.unresolved_invocations.borrow_mut().insert(invoc_id);
|
2019-07-02 05:47:28 -05:00
|
|
|
|
2019-08-12 17:39:10 -05:00
|
|
|
let old_parent_scope = self.r.invocation_parent_scopes.insert(invoc_id, self.parent_scope);
|
2019-08-12 15:39:49 -05:00
|
|
|
assert!(old_parent_scope.is_none(), "invocation data is reset for an invocation");
|
2019-07-02 05:47:28 -05:00
|
|
|
|
2019-08-12 15:39:49 -05:00
|
|
|
LegacyScope::Invocation(invoc_id)
|
2016-09-14 16:03:09 -05:00
|
|
|
}
|
2019-08-08 15:57:35 -05:00
|
|
|
|
|
|
|
fn proc_macro_stub(item: &ast::Item) -> Option<(MacroKind, Ident, Span)> {
|
|
|
|
if attr::contains_name(&item.attrs, sym::proc_macro) {
|
|
|
|
return Some((MacroKind::Bang, item.ident, item.span));
|
|
|
|
} else if attr::contains_name(&item.attrs, sym::proc_macro_attribute) {
|
|
|
|
return Some((MacroKind::Attr, item.ident, item.span));
|
|
|
|
} else if let Some(attr) = attr::find_by_name(&item.attrs, sym::proc_macro_derive) {
|
|
|
|
if let Some(nested_meta) = attr.meta_item_list().and_then(|list| list.get(0).cloned()) {
|
|
|
|
if let Some(ident) = nested_meta.ident() {
|
|
|
|
return Some((MacroKind::Derive, ident, ident.span));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn define_macro(&mut self, item: &ast::Item) -> LegacyScope<'a> {
|
|
|
|
let parent_scope = &self.parent_scope;
|
|
|
|
let expansion = parent_scope.expansion;
|
|
|
|
let (ext, ident, span, is_legacy) = match &item.node {
|
|
|
|
ItemKind::MacroDef(def) => {
|
|
|
|
let ext = self.r.compile_macro(item, self.r.session.edition());
|
|
|
|
(ext, item.ident, item.span, def.legacy)
|
|
|
|
}
|
|
|
|
ItemKind::Fn(..) => match Self::proc_macro_stub(item) {
|
|
|
|
Some((macro_kind, ident, span)) => {
|
|
|
|
self.r.proc_macro_stubs.insert(item.id);
|
|
|
|
(self.r.dummy_ext(macro_kind), ident, span, false)
|
|
|
|
}
|
|
|
|
None => return parent_scope.legacy,
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let def_id = self.r.definitions.local_def_id(item.id);
|
|
|
|
let res = Res::Def(DefKind::Macro(ext.macro_kind()), def_id);
|
|
|
|
self.r.macro_map.insert(def_id, ext);
|
|
|
|
self.r.local_macro_def_scopes.insert(item.id, parent_scope.module);
|
|
|
|
|
|
|
|
if is_legacy {
|
|
|
|
let ident = ident.modern();
|
|
|
|
self.r.macro_names.insert(ident);
|
|
|
|
let is_macro_export = attr::contains_name(&item.attrs, sym::macro_export);
|
|
|
|
let vis = if is_macro_export {
|
|
|
|
ty::Visibility::Public
|
|
|
|
} else {
|
|
|
|
ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))
|
|
|
|
};
|
|
|
|
let binding = (res, vis, span, expansion).to_name_binding(self.r.arenas);
|
|
|
|
self.r.set_binding_parent_module(binding, parent_scope.module);
|
|
|
|
self.r.all_macros.insert(ident.name, res);
|
|
|
|
if is_macro_export {
|
|
|
|
let module = self.r.graph_root;
|
|
|
|
self.r.define(module, ident, MacroNS,
|
|
|
|
(res, vis, span, expansion, IsMacroExport));
|
|
|
|
} else {
|
|
|
|
self.r.check_reserved_macro_name(ident, res);
|
|
|
|
self.r.unused_macros.insert(item.id, span);
|
|
|
|
}
|
|
|
|
LegacyScope::Binding(self.r.arenas.alloc_legacy_binding(LegacyBinding {
|
|
|
|
parent_legacy_scope: parent_scope.legacy, binding, ident
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
let module = parent_scope.module;
|
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
|
|
|
if vis != ty::Visibility::Public {
|
|
|
|
self.r.unused_macros.insert(item.id, span);
|
|
|
|
}
|
|
|
|
self.r.define(module, ident, MacroNS, (res, vis, span, expansion));
|
|
|
|
self.parent_scope.legacy
|
|
|
|
}
|
|
|
|
}
|
2016-09-14 16:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! method {
|
|
|
|
($visit:ident: $ty:ty, $invoc:path, $walk:ident) => {
|
2019-08-08 15:57:35 -05:00
|
|
|
fn $visit(&mut self, node: &'b $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
|
|
|
}
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
impl<'a, 'b> Visitor<'b> 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);
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
fn visit_item(&mut self, item: &'b 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(..) => {
|
2019-08-08 15:57:35 -05:00
|
|
|
self.parent_scope.legacy = self.define_macro(item);
|
2017-03-04 23:15:58 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ItemKind::Mac(..) => {
|
2019-08-12 15:39:49 -05:00
|
|
|
self.parent_scope.legacy = self.visit_invoc(item.id);
|
2016-12-01 05:20:04 -06:00
|
|
|
return
|
2016-10-06 03:04:30 -05:00
|
|
|
}
|
2019-08-05 13:18:50 -05:00
|
|
|
ItemKind::Mod(..) => self.contains_macro_use(&item.attrs),
|
2016-10-06 03:04:30 -05:00
|
|
|
_ => false,
|
|
|
|
};
|
2016-09-14 16:03:09 -05:00
|
|
|
|
2019-08-07 19:44:16 -05:00
|
|
|
let orig_current_module = self.parent_scope.module;
|
|
|
|
let orig_current_legacy_scope = self.parent_scope.legacy;
|
|
|
|
self.build_reduced_graph_for_item(item);
|
2016-09-16 17:21:46 -05:00
|
|
|
visit::walk_item(self, item);
|
2019-08-07 19:44:16 -05:00
|
|
|
self.parent_scope.module = orig_current_module;
|
2016-10-06 03:04:30 -05:00
|
|
|
if !macro_use {
|
2019-08-07 19:44:16 -05:00
|
|
|
self.parent_scope.legacy = orig_current_legacy_scope;
|
2016-10-06 03:04:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
fn visit_stmt(&mut self, stmt: &'b ast::Stmt) {
|
2016-10-06 03:04:30 -05:00
|
|
|
if let ast::StmtKind::Mac(..) = stmt.node {
|
2019-08-12 15:39:49 -05:00
|
|
|
self.parent_scope.legacy = 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
|
|
|
}
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &'b ForeignItem) {
|
2018-03-10 20:16:26 -06:00
|
|
|
if let ForeignItemKind::Macro(_) = foreign_item.node {
|
|
|
|
self.visit_invoc(foreign_item.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-07 19:44:16 -05:00
|
|
|
self.build_reduced_graph_for_foreign_item(foreign_item);
|
2016-09-14 16:03:09 -05:00
|
|
|
visit::walk_foreign_item(self, foreign_item);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
fn visit_block(&mut self, block: &'b Block) {
|
2019-08-07 19:44:16 -05:00
|
|
|
let orig_current_module = self.parent_scope.module;
|
|
|
|
let orig_current_legacy_scope = self.parent_scope.legacy;
|
|
|
|
self.build_reduced_graph_for_block(block);
|
2016-09-16 17:21:46 -05:00
|
|
|
visit::walk_block(self, block);
|
2019-08-07 19:44:16 -05:00
|
|
|
self.parent_scope.module = orig_current_module;
|
|
|
|
self.parent_scope.legacy = orig_current_legacy_scope;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-09-14 18:39:13 -05:00
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
fn visit_trait_item(&mut self, item: &'b TraitItem) {
|
2019-08-07 19:44:16 -05:00
|
|
|
let parent = self.parent_scope.module;
|
2016-09-14 18:39:13 -05:00
|
|
|
|
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.
|
2019-08-08 06:06:42 -05:00
|
|
|
let item_def_id = self.r.definitions.local_def_id(item.id);
|
2019-04-20 11:36:05 -05:00
|
|
|
let (res, ns) = match item.node {
|
2019-05-19 03:26:08 -05:00
|
|
|
TraitItemKind::Const(..) => (Res::Def(DefKind::AssocConst, item_def_id), ValueNS),
|
2017-03-17 21:10:13 -05:00
|
|
|
TraitItemKind::Method(ref sig, _) => {
|
|
|
|
if sig.decl.has_self() {
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.has_self.insert(item_def_id);
|
2017-03-17 21:10:13 -05:00
|
|
|
}
|
2019-04-20 11:36:05 -05:00
|
|
|
(Res::Def(DefKind::Method, item_def_id), ValueNS)
|
2017-03-17 21:10:13 -05:00
|
|
|
}
|
2019-05-19 03:26:08 -05:00
|
|
|
TraitItemKind::Type(..) => (Res::Def(DefKind::AssocTy, 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;
|
2019-08-07 19:44:16 -05:00
|
|
|
let expansion = self.parent_scope.expansion;
|
2019-08-08 06:06:42 -05:00
|
|
|
self.r.define(parent, item.ident, ns, (res, vis, item.span, expansion));
|
2016-09-14 18:39:13 -05:00
|
|
|
|
2019-08-07 19:44:16 -05:00
|
|
|
self.parent_scope.module = parent.parent.unwrap(); // nearest normal ancestor
|
2016-09-14 18:39:13 -05:00
|
|
|
visit::walk_trait_item(self, item);
|
2019-08-07 19:44:16 -05:00
|
|
|
self.parent_scope.module = parent;
|
2016-09-14 18:39:13 -05:00
|
|
|
}
|
2017-10-23 03:22:28 -05:00
|
|
|
|
2019-06-05 05:54:54 -05:00
|
|
|
fn visit_token(&mut self, t: Token) {
|
|
|
|
if let token::Interpolated(nt) = t.kind {
|
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
|
|
|
|
2019-08-08 15:57:35 -05:00
|
|
|
fn visit_attribute(&mut self, attr: &'b ast::Attribute) {
|
2018-09-02 16:04:54 -05:00
|
|
|
if !attr.is_sugared_doc && is_builtin_attr(attr) {
|
2019-08-12 17:39:10 -05:00
|
|
|
self.r.builtin_attrs.push((attr.path.segments[0].ident, self.parent_scope));
|
2018-09-02 16:04:54 -05:00
|
|
|
}
|
|
|
|
visit::walk_attribute(self, attr);
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|