2014-12-30 12:16:42 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Reduced graph building
|
|
|
|
//!
|
|
|
|
//! Here we build the "reduced graph": the graph of the module tree without
|
|
|
|
//! any imports resolved.
|
|
|
|
|
2016-10-11 03:42:06 +00:00
|
|
|
use macros::{InvocationData, LegacyScope};
|
2016-10-28 03:40:58 +00:00
|
|
|
use resolve_imports::ImportDirective;
|
2016-11-10 06:19:54 +00:00
|
|
|
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
|
2016-11-26 12:47:52 +00:00
|
|
|
use {Module, ModuleData, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
|
2018-08-09 16:29:22 +03:00
|
|
|
use {ModuleOrUniformRoot, PerNS, Resolver, ResolverArenas};
|
2016-10-25 22:05:02 +00:00
|
|
|
use Namespace::{self, TypeNS, ValueNS, MacroNS};
|
2015-12-21 10:00:43 +13:00
|
|
|
use {resolve_error, resolve_struct_error, ResolutionError};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def::*;
|
2017-03-22 08:39:51 +00:00
|
|
|
use rustc::hir::def_id::{BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
|
2016-09-19 23:49:01 +03:00
|
|
|
use rustc::ty;
|
2018-07-31 15:23:31 -06:00
|
|
|
use rustc::middle::cstore::CrateStore;
|
2018-07-31 17:23:29 -06:00
|
|
|
use rustc_metadata::cstore::LoadedMacro;
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-08-20 00:23:32 +00:00
|
|
|
use std::cell::Cell;
|
2018-02-27 17:11:14 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2016-08-20 00:23:32 +00:00
|
|
|
|
2016-11-29 02:07:12 +00:00
|
|
|
use syntax::ast::{Name, Ident};
|
2016-06-05 09:56:05 +00:00
|
|
|
use syntax::attr;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind, NodeId};
|
|
|
|
use syntax::ast::{Mutability, StmtKind, TraitItem, TraitItemKind, Variant};
|
2018-07-12 13:24:59 +03:00
|
|
|
use syntax::ext::base::{MacroKind, SyntaxExtension};
|
2016-11-10 06:19:54 +00:00
|
|
|
use syntax::ext::base::Determinacy::Undetermined;
|
2016-09-16 06:45:03 +00:00
|
|
|
use syntax::ext::hygiene::Mark;
|
2016-10-02 01:41:19 +00:00
|
|
|
use syntax::ext::tt::macro_rules;
|
2017-10-23 17:22:28 +09:00
|
|
|
use syntax::parse::token::{self, Token};
|
2017-12-12 11:57:58 -08:00
|
|
|
use syntax::std_inject::injected_crate_name;
|
2016-11-16 08:21:52 +00:00
|
|
|
use syntax::symbol::keywords;
|
2016-04-24 03:26:10 +00:00
|
|
|
use syntax::visit::{self, Visitor};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
|
|
|
|
2016-11-07 22:23:26 +00:00
|
|
|
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
|
2016-11-29 02:53:00 +00:00
|
|
|
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
|
|
|
|
arenas.alloc_name_binding(NameBinding {
|
2016-11-07 22:23:26 +00:00
|
|
|
kind: NameBindingKind::Module(self.0),
|
|
|
|
vis: self.1,
|
|
|
|
span: self.2,
|
|
|
|
expansion: self.3,
|
2016-11-29 02:53:00 +00:00
|
|
|
})
|
2016-01-14 01:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 22:23:26 +00:00
|
|
|
impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
|
2016-11-29 02:53:00 +00:00
|
|
|
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
|
|
|
|
arenas.alloc_name_binding(NameBinding {
|
2018-07-29 14:51:17 +03:00
|
|
|
kind: NameBindingKind::Def(self.0, false),
|
|
|
|
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),
|
2016-11-07 22:23:26 +00:00
|
|
|
vis: self.1,
|
|
|
|
span: self.2,
|
|
|
|
expansion: self.3,
|
2016-11-29 02:53:00 +00:00
|
|
|
})
|
2016-01-14 01:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 07:46:25 +00:00
|
|
|
#[derive(Default, PartialEq, Eq)]
|
|
|
|
struct LegacyMacroImports {
|
|
|
|
import_all: Option<Span>,
|
|
|
|
imports: Vec<(Name, Span)>,
|
|
|
|
}
|
|
|
|
|
2018-07-31 15:23:31 -06:00
|
|
|
impl<'a, 'cl> Resolver<'a, 'cl> {
|
2016-01-14 01:42:45 +00:00
|
|
|
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
|
|
|
|
/// otherwise, reports an error.
|
2017-03-18 01:55:51 +00:00
|
|
|
pub fn define<T>(&mut self, parent: Module<'a>, ident: Ident, ns: Namespace, def: T)
|
2016-11-26 12:48:46 +00:00
|
|
|
where T: ToNameBinding<'a>,
|
2016-07-28 02:34:01 +00:00
|
|
|
{
|
2016-11-29 02:53:00 +00:00
|
|
|
let binding = def.to_name_binding(self.arenas);
|
|
|
|
if let Err(old_binding) = self.try_define(parent, ident, ns, binding) {
|
2016-11-29 02:07:12 +00: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 10:51:22 +00:00
|
|
|
// If any statements are items, we need to create an anonymous module
|
2016-06-17 02:30:01 +00:00
|
|
|
block.stmts.iter().any(|statement| match statement.node {
|
2016-09-14 21:03:09 +00:00
|
|
|
StmtKind::Item(_) | StmtKind::Mac(_) => true,
|
2016-06-17 02:30:01 +00:00
|
|
|
_ => false,
|
|
|
|
})
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-09-15 00:51:46 +03: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 11:13:57 +03:00
|
|
|
fn build_reduced_graph_for_use_tree(
|
|
|
|
&mut self,
|
|
|
|
root_use_tree: &ast::UseTree,
|
|
|
|
root_id: NodeId,
|
|
|
|
use_tree: &ast::UseTree,
|
|
|
|
id: NodeId,
|
|
|
|
vis: ty::Visibility,
|
|
|
|
prefix: &ast::Path,
|
|
|
|
mut uniform_paths_canary_emitted: bool,
|
|
|
|
nested: bool,
|
|
|
|
item: &Item,
|
|
|
|
expansion: Mark,
|
|
|
|
) {
|
|
|
|
debug!("build_reduced_graph_for_use_tree(prefix={:?}, \
|
|
|
|
uniform_paths_canary_emitted={}, \
|
|
|
|
use_tree={:?}, nested={})",
|
|
|
|
prefix, uniform_paths_canary_emitted, use_tree, nested);
|
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
let is_prelude = attr::contains_name(&item.attrs, "prelude_import");
|
|
|
|
let path = &use_tree.prefix;
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
let mut module_path: Vec<_> = prefix.segments.iter()
|
|
|
|
.chain(path.segments.iter())
|
2018-03-18 16:47:09 +03:00
|
|
|
.map(|seg| seg.ident)
|
2017-09-26 23:04:00 +02:00
|
|
|
.collect();
|
2014-12-23 21:34:36 +02:00
|
|
|
|
2018-08-11 11:13:57 +03:00
|
|
|
debug!("build_reduced_graph_for_use_tree: module_path={:?}", module_path);
|
|
|
|
|
|
|
|
// `#[feature(uniform_paths)]` allows an unqualified import path,
|
|
|
|
// e.g. `use x::...;` to resolve not just globally (`use ::x::...;`)
|
|
|
|
// but also relatively (`use self::x::...;`). To catch ambiguities
|
|
|
|
// that might arise from both of these being available and resolution
|
|
|
|
// silently picking one of them, an artificial `use self::x as _;`
|
|
|
|
// import is injected as a "canary", and an error is emitted if it
|
|
|
|
// successfully resolves while an `x` external crate exists.
|
|
|
|
//
|
2018-08-13 18:24:08 +03:00
|
|
|
// For each block scope around the `use` item, one special canary
|
|
|
|
// import of the form `use x as _;` is also injected, having its
|
|
|
|
// parent set to that scope; `resolve_imports` will only resolve
|
|
|
|
// it within its appropriate scope; if any of them successfully
|
|
|
|
// resolve, an ambiguity error is emitted, since the original
|
|
|
|
// import can't see the item in the block scope (`self::x` only
|
|
|
|
// looks in the enclosing module), but a non-`use` path could.
|
|
|
|
//
|
2018-08-11 11:13:57 +03:00
|
|
|
// Additionally, the canary might be able to catch limitations of the
|
|
|
|
// current implementation, where `::x` may be chosen due to `self::x`
|
|
|
|
// not existing, but `self::x` could appear later, from macro expansion.
|
|
|
|
//
|
|
|
|
// NB. The canary currently only errors if the `x::...` path *could*
|
|
|
|
// resolve as a relative path through the extern crate, i.e. `x` is
|
|
|
|
// in `extern_prelude`, *even though* `::x` might still forcefully
|
|
|
|
// load a non-`extern_prelude` crate.
|
|
|
|
// While always producing an ambiguity errors if `self::x` exists and
|
|
|
|
// a crate *could* be loaded, would be more conservative, imports for
|
|
|
|
// local modules named `test` (or less commonly, `syntax` or `log`),
|
|
|
|
// would need to be qualified (e.g. `self::test`), which is considered
|
|
|
|
// ergonomically unacceptable.
|
|
|
|
let emit_uniform_paths_canary =
|
|
|
|
!uniform_paths_canary_emitted &&
|
|
|
|
module_path.get(0).map_or(false, |ident| {
|
|
|
|
!ident.is_path_segment_keyword()
|
|
|
|
});
|
|
|
|
if emit_uniform_paths_canary {
|
|
|
|
// Relative paths should only get here if the feature-gate is on.
|
|
|
|
assert!(self.session.rust_2018() &&
|
|
|
|
self.session.features_untracked().uniform_paths);
|
|
|
|
|
|
|
|
let source = module_path[0];
|
2018-08-13 18:24:08 +03:00
|
|
|
// Helper closure to emit a canary with the given base path.
|
|
|
|
let emit = |this: &mut Self, base: Option<Ident>| {
|
|
|
|
let subclass = SingleImport {
|
|
|
|
target: Ident {
|
|
|
|
name: keywords::Underscore.name().gensymed(),
|
|
|
|
span: source.span,
|
|
|
|
},
|
|
|
|
source,
|
|
|
|
result: PerNS {
|
|
|
|
type_ns: Cell::new(Err(Undetermined)),
|
|
|
|
value_ns: Cell::new(Err(Undetermined)),
|
|
|
|
macro_ns: Cell::new(Err(Undetermined)),
|
|
|
|
},
|
|
|
|
type_ns_only: false,
|
|
|
|
};
|
|
|
|
this.add_import_directive(
|
|
|
|
base.into_iter().collect(),
|
|
|
|
subclass.clone(),
|
|
|
|
source.span,
|
|
|
|
id,
|
|
|
|
root_use_tree.span,
|
|
|
|
root_id,
|
|
|
|
ty::Visibility::Invisible,
|
|
|
|
expansion,
|
|
|
|
true, // is_uniform_paths_canary
|
|
|
|
);
|
2018-08-11 11:13:57 +03:00
|
|
|
};
|
2018-08-13 18:24:08 +03:00
|
|
|
|
|
|
|
// A single simple `self::x` canary.
|
|
|
|
emit(self, Some(Ident {
|
|
|
|
name: keywords::SelfValue.name(),
|
|
|
|
span: source.span,
|
|
|
|
}));
|
|
|
|
|
|
|
|
// One special unprefixed canary per block scope around
|
|
|
|
// the import, to detect items unreachable by `self::x`.
|
|
|
|
let orig_current_module = self.current_module;
|
|
|
|
let mut span = source.span.modern();
|
|
|
|
loop {
|
|
|
|
match self.current_module.kind {
|
|
|
|
ModuleKind::Block(..) => emit(self, None),
|
|
|
|
ModuleKind::Def(..) => break,
|
|
|
|
}
|
|
|
|
match self.hygienic_lexical_parent(self.current_module, &mut span) {
|
|
|
|
Some(module) => {
|
|
|
|
self.current_module = module;
|
|
|
|
}
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.current_module = orig_current_module;
|
2018-08-11 11:13:57 +03:00
|
|
|
|
|
|
|
uniform_paths_canary_emitted = true;
|
|
|
|
}
|
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
match use_tree.kind {
|
2018-06-13 11:44:06 -05:00
|
|
|
ast::UseTreeKind::Simple(rename, ..) => {
|
2018-03-09 18:58:44 +03:00
|
|
|
let mut ident = use_tree.ident();
|
2018-03-18 16:47:09 +03:00
|
|
|
let mut source = module_path.pop().unwrap();
|
2017-09-26 23:04:00 +02:00
|
|
|
let mut type_ns_only = false;
|
|
|
|
|
|
|
|
if nested {
|
|
|
|
// Correctly handle `self`
|
|
|
|
if source.name == keywords::SelfValue.name() {
|
|
|
|
type_ns_only = true;
|
|
|
|
|
2018-07-13 12:38:49 -07:00
|
|
|
let empty_prefix = module_path.last().map_or(true, |ident| {
|
|
|
|
ident.name == keywords::CrateRoot.name()
|
|
|
|
});
|
|
|
|
if empty_prefix {
|
2017-09-26 23:04:00 +02:00
|
|
|
resolve_error(
|
|
|
|
self,
|
|
|
|
use_tree.span,
|
|
|
|
ResolutionError::
|
|
|
|
SelfImportOnlyInImportListWithNonEmptyPrefix
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-23 21:34:36 +02:00
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
// Replace `use foo::self;` with `use foo;`
|
2018-07-13 12:38:49 -07:00
|
|
|
source = module_path.pop().unwrap();
|
2018-03-09 18:58:44 +03:00
|
|
|
if rename.is_none() {
|
2018-07-13 12:38:49 -07:00
|
|
|
ident = source;
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
2017-09-26 23:04:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Disallow `self`
|
|
|
|
if source.name == keywords::SelfValue.name() {
|
|
|
|
resolve_error(self,
|
|
|
|
use_tree.span,
|
|
|
|
ResolutionError::SelfImportsOnlyAllowedWithin);
|
|
|
|
}
|
2014-12-23 21:34:36 +02:00
|
|
|
|
2017-09-26 23:04:00 +02:00
|
|
|
// Disallow `use $crate;`
|
2018-07-13 12:38:49 -07:00
|
|
|
if source.name == keywords::DollarCrate.name() && module_path.is_empty() {
|
2018-06-24 19:12:00 +03:00
|
|
|
let crate_root = self.resolve_crate_root(source);
|
2017-09-26 23:04:00 +02:00
|
|
|
let crate_name = match crate_root.kind {
|
|
|
|
ModuleKind::Def(_, name) => name,
|
|
|
|
ModuleKind::Block(..) => unreachable!(),
|
2016-11-10 06:19:54 +00:00
|
|
|
};
|
2018-08-09 16:29:22 +03: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 12:38:49 -07:00
|
|
|
// `crate_name` should not be interpreted as relative.
|
|
|
|
module_path.push(Ident {
|
|
|
|
name: keywords::CrateRoot.name(),
|
|
|
|
span: source.span,
|
|
|
|
});
|
2018-08-09 16:29:22 +03:00
|
|
|
source.name = crate_name;
|
|
|
|
}
|
2018-03-09 18:58:44 +03:00
|
|
|
if rename.is_none() {
|
2017-09-26 23:04:00 +02:00
|
|
|
ident.name = crate_name;
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
2017-09-26 23:04:00 +02: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 16:29:22 +03: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 23:04:00 +02:00
|
|
|
let subclass = SingleImport {
|
|
|
|
target: ident,
|
|
|
|
source,
|
2018-04-30 02:20:14 +03:00
|
|
|
result: PerNS {
|
|
|
|
type_ns: Cell::new(Err(Undetermined)),
|
|
|
|
value_ns: Cell::new(Err(Undetermined)),
|
|
|
|
macro_ns: Cell::new(Err(Undetermined)),
|
|
|
|
},
|
2017-09-26 23:04:00 +02:00
|
|
|
type_ns_only,
|
|
|
|
};
|
|
|
|
self.add_import_directive(
|
2018-05-18 17:18:04 -04:00
|
|
|
module_path,
|
|
|
|
subclass,
|
|
|
|
use_tree.span,
|
|
|
|
id,
|
|
|
|
root_use_tree.span,
|
|
|
|
root_id,
|
|
|
|
vis,
|
|
|
|
expansion,
|
2018-08-11 11:13:57 +03:00
|
|
|
false, // is_uniform_paths_canary
|
2017-09-26 23:04:00 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
ast::UseTreeKind::Glob => {
|
|
|
|
let subclass = GlobImport {
|
|
|
|
is_prelude,
|
|
|
|
max_vis: Cell::new(ty::Visibility::Invisible),
|
|
|
|
};
|
|
|
|
self.add_import_directive(
|
2018-05-18 17:18:04 -04:00
|
|
|
module_path,
|
|
|
|
subclass,
|
|
|
|
use_tree.span,
|
|
|
|
id,
|
|
|
|
root_use_tree.span,
|
|
|
|
root_id,
|
|
|
|
vis,
|
|
|
|
expansion,
|
2018-08-11 11:13:57 +03:00
|
|
|
false, // is_uniform_paths_canary
|
2017-09-26 23:04:00 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
ast::UseTreeKind::Nested(ref items) => {
|
|
|
|
let prefix = ast::Path {
|
2018-03-18 16:47:09 +03:00
|
|
|
segments: module_path.into_iter()
|
2018-03-19 03:54:56 +03:00
|
|
|
.map(|ident| ast::PathSegment::from_ident(ident))
|
2017-09-26 23:04:00 +02:00
|
|
|
.collect(),
|
|
|
|
span: path.span,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Ensure there is at most one `self` in the list
|
|
|
|
let self_spans = items.iter().filter_map(|&(ref use_tree, _)| {
|
2018-03-09 18:58:44 +03:00
|
|
|
if let ast::UseTreeKind::Simple(..) = use_tree.kind {
|
|
|
|
if use_tree.ident().name == keywords::SelfValue.name() {
|
2017-09-26 23:04:00 +02:00
|
|
|
return Some(use_tree.span);
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 23:04:00 +02: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 19:53:39 -08:00
|
|
|
e.span_label(*other_span, "another `self` import appears here");
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
2017-09-26 23:04:00 +02:00
|
|
|
|
|
|
|
e.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
for &(ref tree, id) in items {
|
|
|
|
self.build_reduced_graph_for_use_tree(
|
2018-08-11 11:13:57 +03:00
|
|
|
root_use_tree,
|
|
|
|
root_id,
|
|
|
|
tree,
|
|
|
|
id,
|
|
|
|
vis,
|
|
|
|
&prefix,
|
|
|
|
uniform_paths_canary_emitted,
|
|
|
|
true,
|
|
|
|
item,
|
|
|
|
expansion,
|
2017-09-26 23:04:00 +02:00
|
|
|
);
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-26 23:04:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs the reduced graph for one item.
|
|
|
|
fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
|
|
|
|
let parent = self.current_module;
|
|
|
|
let ident = item.ident;
|
|
|
|
let sp = item.span;
|
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
ItemKind::Use(ref use_tree) => {
|
2018-07-13 12:38:49 -07:00
|
|
|
let uniform_paths =
|
|
|
|
self.session.rust_2018() &&
|
|
|
|
self.session.features_untracked().uniform_paths;
|
2018-03-10 02:02:39 +03:00
|
|
|
// Imports are resolved as global by default, add starting root segment.
|
2018-07-13 12:38:49 -07:00
|
|
|
let root = if !uniform_paths {
|
|
|
|
use_tree.prefix.make_root()
|
|
|
|
} else {
|
|
|
|
// Except when `#![feature(uniform_paths)]` is on.
|
|
|
|
None
|
|
|
|
};
|
2017-09-26 23:04:00 +02:00
|
|
|
let prefix = ast::Path {
|
2018-07-13 12:38:49 -07:00
|
|
|
segments: root.into_iter().collect(),
|
2017-09-26 23:04:00 +02:00
|
|
|
span: use_tree.span,
|
|
|
|
};
|
|
|
|
|
|
|
|
self.build_reduced_graph_for_use_tree(
|
2018-08-11 11:13:57 +03:00
|
|
|
use_tree,
|
|
|
|
item.id,
|
|
|
|
use_tree,
|
|
|
|
item.id,
|
|
|
|
vis,
|
|
|
|
&prefix,
|
|
|
|
false, // uniform_paths_canary_emitted
|
|
|
|
false,
|
|
|
|
item,
|
|
|
|
expansion,
|
2017-09-26 23:04:00 +02:00
|
|
|
);
|
|
|
|
}
|
2014-12-23 21:34:36 +02:00
|
|
|
|
2018-03-09 18:51:48 +03:00
|
|
|
ItemKind::ExternCrate(orig_name) => {
|
2018-04-10 23:01:24 +09:00
|
|
|
let crate_id = self.crate_loader.process_extern_crate(item, &self.definitions);
|
2017-03-22 08:39:51 +00:00
|
|
|
let module =
|
|
|
|
self.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX });
|
2017-01-14 07:35:54 +00:00
|
|
|
self.populate_module_if_necessary(module);
|
2017-12-12 11:57:58 -08:00
|
|
|
if injected_crate_name().map_or(false, |name| item.ident.name == name) {
|
2017-12-06 10:50:55 -08:00
|
|
|
self.injected_crate = Some(module);
|
|
|
|
}
|
|
|
|
|
2017-01-14 07:35:54 +00:00
|
|
|
let used = self.process_legacy_macro_imports(item, module, expansion);
|
2016-11-29 02:53:00 +00:00
|
|
|
let binding =
|
|
|
|
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
|
2016-11-05 20:30:40 +00:00
|
|
|
let directive = self.arenas.alloc_import_directive(ImportDirective {
|
2018-05-18 17:18:04 -04:00
|
|
|
root_id: item.id,
|
2016-11-05 20:30:40 +00:00
|
|
|
id: item.id,
|
2017-08-06 22:54:09 -07:00
|
|
|
parent,
|
2018-08-09 16:29:22 +03:00
|
|
|
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
|
2018-03-09 18:51:48 +03:00
|
|
|
subclass: ImportDirectiveSubclass::ExternCrate(orig_name),
|
2018-05-18 17:18:04 -04:00
|
|
|
root_span: item.span,
|
2016-11-05 20:30:40 +00:00
|
|
|
span: item.span,
|
|
|
|
module_path: Vec::new(),
|
|
|
|
vis: Cell::new(vis),
|
2017-08-06 22:54:09 -07:00
|
|
|
expansion,
|
2017-01-14 07:35:54 +00:00
|
|
|
used: Cell::new(used),
|
2018-08-11 11:13:57 +03:00
|
|
|
is_uniform_paths_canary: false,
|
2016-11-05 20:30:40 +00:00
|
|
|
});
|
2017-01-14 07:35:54 +00:00
|
|
|
self.potentially_unused_imports.push(directive);
|
2016-11-05 20:30:40 +00:00
|
|
|
let imported_binding = self.import(binding, directive);
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, TypeNS, imported_binding);
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
2017-03-15 21:27:40 -05:00
|
|
|
ItemKind::GlobalAsm(..) => {}
|
|
|
|
|
2016-09-14 21:03:09 +00:00
|
|
|
ItemKind::Mod(..) if item.ident == keywords::Invalid.ident() => {} // Crate root
|
|
|
|
|
2016-04-24 03:26:10 +00:00
|
|
|
ItemKind::Mod(..) => {
|
2016-12-20 08:32:15 +00: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 12:47:52 +00:00
|
|
|
let module = self.arenas.alloc_module(ModuleData {
|
2016-09-19 05:25:17 +00:00
|
|
|
no_implicit_prelude: parent.no_implicit_prelude || {
|
2016-06-05 09:56:05 +00:00
|
|
|
attr::contains_name(&item.attrs, "no_implicit_prelude")
|
2016-09-19 05:25:17 +00:00
|
|
|
},
|
2017-03-22 08:39:51 +00:00
|
|
|
..ModuleData::new(Some(parent), module_kind, def_id, expansion, item.span)
|
2016-06-05 09:56:05 +00:00
|
|
|
});
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
2016-12-20 08:32:15 +00:00
|
|
|
self.module_map.insert(def_id, module);
|
2016-08-14 23:42:05 +00:00
|
|
|
|
|
|
|
// Descend into the module.
|
|
|
|
self.current_module = module;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2018-04-10 23:01:24 +09: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.
|
2016-04-24 03:26:10 +00:00
|
|
|
ItemKind::Static(_, m, _) => {
|
|
|
|
let mutbl = m == Mutability::Mutable;
|
|
|
|
let def = Def::Static(self.definitions.local_def_id(item.id), mutbl);
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-08-26 19:23:42 +03:00
|
|
|
ItemKind::Const(..) => {
|
2016-04-24 03:26:10 +00:00
|
|
|
let def = Def::Const(self.definitions.local_def_id(item.id));
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-08-26 19:23:42 +03:00
|
|
|
ItemKind::Fn(..) => {
|
2016-04-24 03:26:10 +00:00
|
|
|
let def = Def::Fn(self.definitions.local_def_id(item.id));
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, ValueNS, (def, vis, sp, expansion));
|
2018-07-12 13:24:59 +03: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()) {
|
|
|
|
if let Some(ident) = trait_attr.name().map(Ident::with_empty_ctxt) {
|
|
|
|
let sp = trait_attr.span;
|
|
|
|
let def = Def::Macro(def.def_id(), MacroKind::ProcMacroStub);
|
|
|
|
self.define(parent, ident, MacroNS, (def, vis, sp, expansion));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// These items live in the type namespace.
|
2016-04-24 03:26:10 +00:00
|
|
|
ItemKind::Ty(..) => {
|
|
|
|
let def = Def::TyAlias(self.definitions.local_def_id(item.id));
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2018-07-03 19:38:14 +02: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-24 03:26:10 +00:00
|
|
|
ItemKind::Enum(ref enum_definition, _) => {
|
2016-09-19 05:25:17 +00:00
|
|
|
let def = Def::Enum(self.definitions.local_def_id(item.id));
|
2016-12-20 08:32:15 +00:00
|
|
|
let module_kind = ModuleKind::Def(def, ident.name);
|
2017-05-10 13:19:29 +02:00
|
|
|
let module = self.new_module(parent,
|
|
|
|
module_kind,
|
|
|
|
parent.normal_ancestor_id,
|
2017-03-22 08:39:51 +00:00
|
|
|
expansion,
|
2017-05-10 13:19:29 +02:00
|
|
|
item.span);
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
2014-12-30 12:52:51 -06:00
|
|
|
|
2015-01-31 12:20:46 -05:00
|
|
|
for variant in &(*enum_definition).variants {
|
2016-11-07 22:23:26 +00:00
|
|
|
self.build_reduced_graph_for_variant(variant, module, vis, expansion);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 13:48:57 +00: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-24 03:26:10 +00:00
|
|
|
ItemKind::Struct(ref struct_def, _) => {
|
2014-12-30 12:16:42 -06:00
|
|
|
// Define a name in the type namespace.
|
2017-11-03 19:17:54 +00:00
|
|
|
let def_id = self.definitions.local_def_id(item.id);
|
|
|
|
let def = Def::Struct(def_id);
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2017-01-09 00:21:35 +03:00
|
|
|
let mut ctor_vis = vis;
|
2017-11-03 19:17:54 +00:00
|
|
|
|
2017-12-26 16:52:27 +09:00
|
|
|
let has_non_exhaustive = attr::contains_name(&item.attrs, "non_exhaustive");
|
2017-11-03 19:17:54 +00: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-15 00:51:46 +03:00
|
|
|
let field_names = struct_def.fields().iter().filter_map(|field| {
|
2017-01-09 00:21:35 +03:00
|
|
|
let field_vis = self.resolve_visibility(&field.vis);
|
|
|
|
if ctor_vis.is_at_least(field_vis, &*self) {
|
|
|
|
ctor_vis = field_vis;
|
|
|
|
}
|
2016-04-24 03:26:10 +00:00
|
|
|
field.ident.map(|ident| ident.name)
|
2016-04-09 23:19:53 +00:00
|
|
|
}).collect();
|
2016-04-24 03:26:10 +00:00
|
|
|
let item_def_id = self.definitions.local_def_id(item.id);
|
2016-09-15 00:51:46 +03:00
|
|
|
self.insert_field_names(item_def_id, field_names);
|
2017-01-09 00:21:35 +03:00
|
|
|
|
|
|
|
// If this is a tuple or unit struct, define a name
|
|
|
|
// in the value namespace as well.
|
|
|
|
if !struct_def.is_struct() {
|
|
|
|
let ctor_def = Def::StructCtor(self.definitions.local_def_id(struct_def.id()),
|
|
|
|
CtorKind::from_ast(struct_def));
|
|
|
|
self.define(parent, ident, ValueNS, (ctor_def, ctor_vis, sp, expansion));
|
2017-01-29 02:56:52 +03:00
|
|
|
self.struct_constructors.insert(def.def_id(), (ctor_def, ctor_vis));
|
2017-01-09 00:21:35 +03:00
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-08-06 21:56:02 +03:00
|
|
|
ItemKind::Union(ref vdata, _) => {
|
|
|
|
let def = Def::Union(self.definitions.local_def_id(item.id));
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
|
2016-08-06 21:56:02 +03:00
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
// Record field names for error reporting.
|
|
|
|
let field_names = vdata.fields().iter().filter_map(|field| {
|
2016-08-06 21:56:02 +03: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-15 00:51:46 +03:00
|
|
|
self.insert_field_names(item_def_id, field_names);
|
2016-08-06 21:56:02 +03:00
|
|
|
}
|
2016-08-29 05:04:31 +00:00
|
|
|
|
2017-12-01 10:01:23 -02:00
|
|
|
ItemKind::Impl(..) => {}
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-09-14 23:39:13 +00:00
|
|
|
ItemKind::Trait(..) => {
|
2016-04-24 03:26:10 +00:00
|
|
|
let def_id = self.definitions.local_def_id(item.id);
|
2015-11-16 07:59:50 +00:00
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
// Add all the items within to a new module.
|
2016-12-20 08:32:15 +00:00
|
|
|
let module_kind = ModuleKind::Def(Def::Trait(def_id), ident.name);
|
2017-05-10 13:19:29 +02:00
|
|
|
let module = self.new_module(parent,
|
|
|
|
module_kind,
|
|
|
|
parent.normal_ancestor_id,
|
2017-03-22 08:39:51 +00:00
|
|
|
expansion,
|
2017-05-10 13:19:29 +02:00
|
|
|
item.span);
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, TypeNS, (module, vis, sp, expansion));
|
2016-09-14 23:39:13 +00:00
|
|
|
self.current_module = module;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2017-10-02 13:48:57 +00:00
|
|
|
|
2017-03-05 05:15:58 +00: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 08:16:32 +00:00
|
|
|
fn build_reduced_graph_for_variant(&mut self,
|
|
|
|
variant: &Variant,
|
2016-11-26 12:48:46 +00:00
|
|
|
parent: Module<'a>,
|
2016-11-17 08:16:32 +00:00
|
|
|
vis: ty::Visibility,
|
|
|
|
expansion: Mark) {
|
2018-03-19 01:21:30 +03:00
|
|
|
let ident = variant.node.ident;
|
2016-09-15 00:51:46 +03:00
|
|
|
let def_id = self.definitions.local_def_id(variant.node.data.id());
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
// Define a name in the type namespace.
|
|
|
|
let def = Def::Variant(def_id);
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, variant.span, expansion));
|
2016-09-15 00:51:46 +03: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.
|
2016-09-15 00:51:46 +03:00
|
|
|
let ctor_kind = CtorKind::from_ast(&variant.node.data);
|
2016-09-15 00:51:46 +03:00
|
|
|
let ctor_def = Def::VariantCtor(def_id, ctor_kind);
|
2017-11-03 19:17:54 +00:00
|
|
|
|
2016-11-29 02:07:12 +00:00
|
|
|
self.define(parent, ident, ValueNS, (ctor_def, vis, variant.span, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs the reduced graph for one foreign item.
|
2016-11-17 08:16:32 +00:00
|
|
|
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem, expansion: Mark) {
|
2017-09-03 19:53:58 +01:00
|
|
|
let (def, ns) = match item.node {
|
2016-04-24 03:26:10 +00:00
|
|
|
ForeignItemKind::Fn(..) => {
|
2017-09-03 19:53:58 +01:00
|
|
|
(Def::Fn(self.definitions.local_def_id(item.id)), ValueNS)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-04-24 03:26:10 +00:00
|
|
|
ForeignItemKind::Static(_, m) => {
|
2017-09-03 19:53:58 +01:00
|
|
|
(Def::Static(self.definitions.local_def_id(item.id), m), ValueNS)
|
|
|
|
}
|
|
|
|
ForeignItemKind::Ty => {
|
2018-08-22 02:13:31 +01:00
|
|
|
(Def::TyForeign(self.definitions.local_def_id(item.id)), TypeNS)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2018-03-10 18:16:26 -08:00
|
|
|
ForeignItemKind::Macro(_) => unreachable!(),
|
2015-02-05 09:19:07 +02:00
|
|
|
};
|
2016-11-29 02:07:12 +00:00
|
|
|
let parent = self.current_module;
|
2016-11-17 08:16:32 +00:00
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
2017-09-03 19:53:58 +01:00
|
|
|
self.define(parent, item.ident, ns, (def, vis, item.span, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
fn build_reduced_graph_for_block(&mut self, block: &Block, expansion: Mark) {
|
2016-08-15 01:08:31 +00:00
|
|
|
let parent = self.current_module;
|
2014-12-30 12:16:42 -06:00
|
|
|
if self.block_needs_anonymous_module(block) {
|
2017-05-10 13:58:41 +02:00
|
|
|
let module = self.new_module(parent,
|
|
|
|
ModuleKind::Block(block.id),
|
|
|
|
parent.normal_ancestor_id,
|
2017-03-22 08:39:51 +00:00
|
|
|
expansion,
|
2017-05-10 13:58:41 +02:00
|
|
|
block.span);
|
2016-12-20 08:32:15 +00: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-03-01 02:29:06 +00:00
|
|
|
/// Builds the reduced graph for a single item in an external crate.
|
2016-11-26 12:48:46 +00:00
|
|
|
fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'a>, child: Export) {
|
2017-12-04 21:17:42 -08:00
|
|
|
let Export { ident, def, vis, span, .. } = child;
|
2016-09-15 00:51:46 +03:00
|
|
|
let def_id = def.def_id();
|
2017-03-22 08:39:51 +00:00
|
|
|
let expansion = Mark::root(); // FIXME(jseyfried) intercrate hygiene
|
2014-12-30 12:16:42 -06:00
|
|
|
match def {
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::Mod(..) | Def::Enum(..) => {
|
2017-05-10 13:19:29 +02:00
|
|
|
let module = self.new_module(parent,
|
|
|
|
ModuleKind::Def(def, ident.name),
|
|
|
|
def_id,
|
2017-03-22 08:39:51 +00:00
|
|
|
expansion,
|
2017-05-10 13:19:29 +02:00
|
|
|
span);
|
2017-03-22 08:39:51 +00:00
|
|
|
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2018-08-22 02:13:31 +01:00
|
|
|
Def::Variant(..) | Def::TyAlias(..) | Def::TyForeign(..) => {
|
2017-03-22 08:39:51 +00:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
|
2015-10-26 20:31:11 +01:00
|
|
|
}
|
2017-01-29 02:56:52 +03:00
|
|
|
Def::Fn(..) | Def::Static(..) | Def::Const(..) | Def::VariantCtor(..) => {
|
2017-03-22 08:39:51 +00:00
|
|
|
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
|
2017-01-29 02:56:52 +03:00
|
|
|
}
|
|
|
|
Def::StructCtor(..) => {
|
2017-03-22 08:39:51 +00:00
|
|
|
self.define(parent, ident, ValueNS, (def, vis, DUMMY_SP, expansion));
|
2017-01-29 02:56:52 +03:00
|
|
|
|
|
|
|
if let Some(struct_def_id) =
|
2017-09-05 16:48:24 +02:00
|
|
|
self.cstore.def_key(def_id).parent
|
2017-01-29 02:56:52 +03: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
|
|
|
}
|
2016-09-15 00:51:46 +03:00
|
|
|
Def::Trait(..) => {
|
2016-12-20 08:32:15 +00:00
|
|
|
let module_kind = ModuleKind::Def(def, ident.name);
|
2017-05-10 13:19:29 +02:00
|
|
|
let module = self.new_module(parent,
|
|
|
|
module_kind,
|
|
|
|
parent.normal_ancestor_id,
|
2017-03-22 08:39:51 +00:00
|
|
|
expansion,
|
2017-05-10 13:19:29 +02:00
|
|
|
span);
|
2017-03-22 08:39:51 +00:00
|
|
|
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
|
2015-10-26 20:31:11 +01:00
|
|
|
|
2017-09-05 16:48:24 +02:00
|
|
|
for child in self.cstore.item_children_untracked(def_id, self.session) {
|
2016-12-01 01:35:25 +03:00
|
|
|
let ns = if let Def::AssociatedTy(..) = child.def { TypeNS } else { ValueNS };
|
2017-03-27 00:46:00 +00:00
|
|
|
self.define(module, child.ident, ns,
|
|
|
|
(child.def, ty::Visibility::Public, DUMMY_SP, expansion));
|
2015-10-26 20:31:11 +01:00
|
|
|
|
2017-09-05 16:48:24 +02:00
|
|
|
if self.cstore.associated_item_cloned_untracked(child.def.def_id())
|
2017-03-18 02:10:13 +00:00
|
|
|
.method_has_self_argument {
|
|
|
|
self.has_self.insert(child.def.def_id());
|
|
|
|
}
|
2016-12-01 01:35:25 +03:00
|
|
|
}
|
|
|
|
module.populated.set(true);
|
2016-09-15 00:51:46 +03:00
|
|
|
}
|
2016-12-01 01:35:25 +03:00
|
|
|
Def::Struct(..) | Def::Union(..) => {
|
2017-03-22 08:39:51 +00:00
|
|
|
self.define(parent, ident, TypeNS, (def, vis, DUMMY_SP, expansion));
|
2016-08-06 21:56:02 +03:00
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
// Record field names for error reporting.
|
2017-09-05 16:48:24 +02:00
|
|
|
let field_names = self.cstore.struct_field_names_untracked(def_id);
|
2016-09-15 00:51:46 +03:00
|
|
|
self.insert_field_names(def_id, field_names);
|
2016-08-06 21:56:02 +03:00
|
|
|
}
|
2016-10-25 22:05:02 +00:00
|
|
|
Def::Macro(..) => {
|
2017-03-22 08:39:51 +00:00
|
|
|
self.define(parent, ident, MacroNS, (def, vis, DUMMY_SP, expansion));
|
2016-10-25 22:05:02 +00:00
|
|
|
}
|
2016-12-01 01:35:25 +03:00
|
|
|
_ => bug!("unexpected definition: {:?}", def)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 08:39:51 +00: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 16:48:24 +02:00
|
|
|
let macros_only = self.cstore.dep_kind_untracked(def_id.krate).macros_only();
|
2017-03-22 08:39:51 +00: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 23:02:41 +02:00
|
|
|
(self.cstore.crate_name_untracked(def_id.krate).as_interned_str(), None)
|
2017-03-22 08:39:51 +00:00
|
|
|
} else {
|
2017-09-05 16:48:24 +02:00
|
|
|
let def_key = self.cstore.def_key(def_id);
|
2017-03-22 08:39:51 +00: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 23:02:41 +02:00
|
|
|
let kind = ModuleKind::Def(Def::Mod(def_id), name.as_symbol());
|
2017-06-17 10:28:31 +00: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 05:17:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
pub fn macro_def_scope(&mut self, expansion: Mark) -> Module<'a> {
|
2017-03-01 23:48:16 +00: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]
|
2017-03-16 20:39:41 +00:00
|
|
|
} else if def_id.krate == BUILTIN_MACROS_CRATE {
|
2017-12-06 10:50:55 -08:00
|
|
|
self.injected_crate.unwrap_or(self.graph_root)
|
2017-03-01 23:48:16 +00:00
|
|
|
} else {
|
|
|
|
let module_def_id = ty::DefIdTree::parent(&*self, def_id).unwrap();
|
2017-03-22 08:39:51 +00:00
|
|
|
self.get_module(module_def_id)
|
2017-03-01 23:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 17:11:14 +01:00
|
|
|
pub fn get_macro(&mut self, def: Def) -> Lrc<SyntaxExtension> {
|
2016-11-27 10:27:41 +00:00
|
|
|
let def_id = match def {
|
2017-02-23 20:12:33 +10:30
|
|
|
Def::Macro(def_id, ..) => def_id,
|
2018-08-04 00:25:45 +03:00
|
|
|
Def::NonMacroAttr(attr_kind) => return Lrc::new(SyntaxExtension::NonMacroAttr {
|
|
|
|
mark_used: attr_kind == NonMacroAttrKind::Tool,
|
|
|
|
}),
|
2018-08-03 02:05:00 +03:00
|
|
|
_ => panic!("expected `Def::Macro` or `Def::NonMacroAttr`"),
|
2016-10-28 06:52:45 +00:00
|
|
|
};
|
|
|
|
if let Some(ext) = self.macro_map.get(&def_id) {
|
|
|
|
return ext.clone();
|
|
|
|
}
|
|
|
|
|
2017-09-05 16:48:24 +02:00
|
|
|
let macro_def = match self.cstore.load_macro_untracked(def_id, &self.session) {
|
2017-03-05 05:15:58 +00:00
|
|
|
LoadedMacro::MacroDef(macro_def) => macro_def,
|
2016-11-05 20:30:40 +00:00
|
|
|
LoadedMacro::ProcMacro(ext) => return ext,
|
|
|
|
};
|
|
|
|
|
2018-02-27 17:11:14 +01:00
|
|
|
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
|
2018-02-14 16:11:02 +01:00
|
|
|
&self.session.features_untracked(),
|
2018-05-13 03:51:46 +03:00
|
|
|
¯o_def,
|
|
|
|
self.cstore.crate_edition_untracked(def_id.krate)));
|
2016-10-28 06:52:45 +00:00
|
|
|
self.macro_map.insert(def_id, ext.clone());
|
|
|
|
ext
|
|
|
|
}
|
|
|
|
|
2016-03-02 09:18:47 +00:00
|
|
|
/// Ensures that the reduced graph rooted at the given external module
|
|
|
|
/// is built, building it if it is not.
|
2016-11-26 12:48:46 +00:00
|
|
|
pub fn populate_module_if_necessary(&mut self, module: Module<'a>) {
|
2016-03-02 09:18:47 +00:00
|
|
|
if module.populated.get() { return }
|
2017-08-31 08:07:39 -07:00
|
|
|
let def_id = module.def_id().unwrap();
|
2017-09-05 16:48:24 +02:00
|
|
|
for child in self.cstore.item_children_untracked(def_id, self.session) {
|
2016-03-02 10:22:24 +00:00
|
|
|
self.build_reduced_graph_for_external_crate_def(module, child);
|
2016-03-02 09:18:47 +00:00
|
|
|
}
|
|
|
|
module.populated.set(true)
|
|
|
|
}
|
2016-09-16 06:45:03 +00:00
|
|
|
|
2016-11-17 08:16:32 +00:00
|
|
|
fn legacy_import_macro(&mut self,
|
|
|
|
name: Name,
|
2016-11-26 12:48:46 +00:00
|
|
|
binding: &'a NameBinding<'a>,
|
2016-11-17 08:16:32 +00:00
|
|
|
span: Span,
|
|
|
|
allow_shadowing: bool) {
|
2018-07-16 21:36:13 +03:00
|
|
|
if self.macro_prelude.insert(name, binding).is_some() && !allow_shadowing {
|
2016-10-28 06:52:45 +00: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 07:46:25 +00:00
|
|
|
|
2017-01-14 07:35:54 +00:00
|
|
|
// This returns true if we should consider the underlying `extern crate` to be used.
|
|
|
|
fn process_legacy_macro_imports(&mut self, item: &Item, module: Module<'a>, expansion: Mark)
|
|
|
|
-> bool {
|
2016-11-05 20:30:40 +00:00
|
|
|
let allow_shadowing = expansion == Mark::root();
|
|
|
|
let legacy_imports = self.legacy_macro_imports(&item.attrs);
|
2018-05-14 03:22:52 +03:00
|
|
|
let used = legacy_imports != LegacyMacroImports::default();
|
2016-11-05 20:30:40 +00:00
|
|
|
|
2018-04-15 16:59:00 +03:00
|
|
|
// `#[macro_use]` is only allowed at the crate root.
|
2017-01-14 07:35:54 +00:00
|
|
|
if self.current_module.parent.is_some() && used {
|
2016-11-05 20:30:40 +00:00
|
|
|
span_err!(self.session, item.span, E0468,
|
|
|
|
"an `extern crate` loading macros must be at the crate root");
|
|
|
|
}
|
|
|
|
|
2017-01-14 08:58:50 +00:00
|
|
|
let (graph_root, arenas) = (self.graph_root, self.arenas);
|
|
|
|
let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
|
2018-05-18 17:18:04 -04:00
|
|
|
root_id: item.id,
|
2017-01-14 08:58:50 +00:00
|
|
|
id: item.id,
|
|
|
|
parent: graph_root,
|
2018-08-09 16:29:22 +03:00
|
|
|
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
|
2017-01-14 08:58:50 +00:00
|
|
|
subclass: ImportDirectiveSubclass::MacroUse,
|
2018-05-18 17:18:04 -04:00
|
|
|
root_span: span,
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2017-01-14 08:58:50 +00:00
|
|
|
module_path: Vec::new(),
|
|
|
|
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
|
2017-08-06 22:54:09 -07:00
|
|
|
expansion,
|
2017-01-14 08:58:50 +00:00
|
|
|
used: Cell::new(false),
|
2018-08-11 11:13:57 +03:00
|
|
|
is_uniform_paths_canary: false,
|
2017-01-14 08:58:50 +00:00
|
|
|
});
|
|
|
|
|
2016-10-28 06:52:45 +00:00
|
|
|
if let Some(span) = legacy_imports.import_all {
|
2017-01-14 08:58:50 +00:00
|
|
|
let directive = macro_use_directive(span);
|
|
|
|
self.potentially_unused_imports.push(directive);
|
2016-11-29 02:07:12 +00:00
|
|
|
module.for_each_child(|ident, ns, binding| if ns == MacroNS {
|
2017-01-14 08:58:50 +00:00
|
|
|
let imported_binding = self.import(binding, directive);
|
|
|
|
self.legacy_import_macro(ident.name, imported_binding, span, allow_shadowing);
|
2016-10-28 06:52:45 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
for (name, span) in legacy_imports.imports {
|
2016-11-29 02:07:12 +00:00
|
|
|
let ident = Ident::with_empty_ctxt(name);
|
2018-08-09 16:29:22 +03:00
|
|
|
let result = self.resolve_ident_in_module(
|
|
|
|
ModuleOrUniformRoot::Module(module),
|
|
|
|
ident,
|
|
|
|
MacroNS,
|
|
|
|
false,
|
|
|
|
span,
|
|
|
|
);
|
2016-11-26 12:21:47 +00:00
|
|
|
if let Ok(binding) = result {
|
2017-01-14 08:58:50 +00:00
|
|
|
let directive = macro_use_directive(span);
|
|
|
|
self.potentially_unused_imports.push(directive);
|
|
|
|
let imported_binding = self.import(binding, directive);
|
|
|
|
self.legacy_import_macro(name, imported_binding, span, allow_shadowing);
|
2016-10-17 07:46:25 +00:00
|
|
|
} else {
|
2016-10-28 06:52:45 +00:00
|
|
|
span_err!(self.session, span, E0469, "imported macro not found");
|
2016-10-17 07:46:25 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 06:52:45 +00:00
|
|
|
}
|
2017-01-14 07:35:54 +00:00
|
|
|
used
|
2016-10-17 07:46:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 06:45:03 +00:00
|
|
|
// does this attribute list contain "macro_use"?
|
|
|
|
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 12:00:25 +00:00
|
|
|
if let ast::AttrStyle::Inner = attr.style {
|
2016-09-16 06:45:03 +00: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-10-17 07:46:25 +00:00
|
|
|
|
|
|
|
fn legacy_macro_imports(&mut self, attrs: &[ast::Attribute]) -> LegacyMacroImports {
|
|
|
|
let mut imports = LegacyMacroImports::default();
|
|
|
|
for attr in attrs {
|
|
|
|
if attr.check_name("macro_use") {
|
|
|
|
match attr.meta_item_list() {
|
|
|
|
Some(names) => for attr in names {
|
|
|
|
if let Some(word) = attr.word() {
|
2018-04-17 15:33:39 +02:00
|
|
|
imports.imports.push((word.name(), attr.span()));
|
2016-10-17 07:46:25 +00:00
|
|
|
} else {
|
|
|
|
span_err!(self.session, attr.span(), E0466, "bad macro import");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => imports.import_all = Some(attr.span),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
imports
|
|
|
|
}
|
2016-03-02 09:18:47 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 15:23:31 -06:00
|
|
|
pub struct BuildReducedGraphVisitor<'a, 'b: 'a, 'c: 'b> {
|
|
|
|
pub resolver: &'a mut Resolver<'b, 'c>,
|
2016-10-06 08:04:30 +00:00
|
|
|
pub legacy_scope: LegacyScope<'b>,
|
2016-10-11 03:42:06 +00:00
|
|
|
pub expansion: Mark,
|
2016-09-14 21:03:09 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 15:23:31 -06:00
|
|
|
impl<'a, 'b, 'cl> BuildReducedGraphVisitor<'a, 'b, 'cl> {
|
2016-10-06 08:04:30 +00:00
|
|
|
fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> {
|
2017-03-16 10:23:33 +00:00
|
|
|
let mark = id.placeholder_to_mark();
|
2016-11-11 10:51:15 +00:00
|
|
|
self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
|
|
|
|
let invocation = self.resolver.invocations[&mark];
|
2016-10-06 08:04:30 +00:00
|
|
|
invocation.module.set(self.resolver.current_module);
|
|
|
|
invocation.legacy_scope.set(self.legacy_scope);
|
|
|
|
invocation
|
2016-09-14 21:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! method {
|
|
|
|
($visit:ident: $ty:ty, $invoc:path, $walk:ident) => {
|
2016-12-06 11:26:52 +01:00
|
|
|
fn $visit(&mut self, node: &'a $ty) {
|
2016-10-06 08:04:30 +00:00
|
|
|
if let $invoc(..) = node.node {
|
|
|
|
self.visit_invoc(node.id);
|
|
|
|
} else {
|
|
|
|
visit::$walk(self, node);
|
2016-09-14 21:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2018-07-31 15:23:31 -06:00
|
|
|
impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
|
2016-09-14 21:03:09 +00: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 11:26:52 +01:00
|
|
|
fn visit_item(&mut self, item: &'a Item) {
|
2016-10-06 08:04:30 +00:00
|
|
|
let macro_use = match item.node {
|
2017-03-05 05:15:58 +00:00
|
|
|
ItemKind::MacroDef(..) => {
|
2017-03-18 01:55:51 +00:00
|
|
|
self.resolver.define_macro(item, self.expansion, &mut self.legacy_scope);
|
2017-03-05 05:15:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ItemKind::Mac(..) => {
|
|
|
|
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
|
2016-12-01 11:20:04 +00:00
|
|
|
return
|
2016-10-06 08:04:30 +00:00
|
|
|
}
|
|
|
|
ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),
|
|
|
|
_ => false,
|
|
|
|
};
|
2016-09-14 21:03:09 +00:00
|
|
|
|
2016-10-06 08:04:30 +00:00
|
|
|
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
|
2016-10-11 03:42:06 +00:00
|
|
|
self.resolver.build_reduced_graph_for_item(item, self.expansion);
|
2016-09-16 22:21:46 +00:00
|
|
|
visit::walk_item(self, item);
|
|
|
|
self.resolver.current_module = parent;
|
2016-10-06 08:04:30 +00:00
|
|
|
if !macro_use {
|
|
|
|
self.legacy_scope = legacy_scope;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
|
2016-10-06 08:04:30 +00:00
|
|
|
if let ast::StmtKind::Mac(..) = stmt.node {
|
|
|
|
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
|
|
|
|
} else {
|
|
|
|
visit::walk_stmt(self, stmt);
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) {
|
2018-03-10 18:16:26 -08:00
|
|
|
if let ForeignItemKind::Macro(_) = foreign_item.node {
|
|
|
|
self.visit_invoc(foreign_item.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-07 22:23:26 +00:00
|
|
|
self.resolver.build_reduced_graph_for_foreign_item(foreign_item, self.expansion);
|
2016-09-14 21:03:09 +00:00
|
|
|
visit::walk_foreign_item(self, foreign_item);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_block(&mut self, block: &'a Block) {
|
2016-10-06 08:04:30 +00:00
|
|
|
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
|
2017-03-22 08:39:51 +00:00
|
|
|
self.resolver.build_reduced_graph_for_block(block, self.expansion);
|
2016-09-16 22:21:46 +00:00
|
|
|
visit::walk_block(self, block);
|
|
|
|
self.resolver.current_module = parent;
|
2016-10-06 08:04:30 +00:00
|
|
|
self.legacy_scope = legacy_scope;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-09-14 23:39:13 +00:00
|
|
|
|
2016-12-06 11:26:52 +01:00
|
|
|
fn visit_trait_item(&mut self, item: &'a TraitItem) {
|
2016-09-14 23:39:13 +00:00
|
|
|
let parent = self.resolver.current_module;
|
|
|
|
|
2016-10-04 16:36:14 +11:00
|
|
|
if let TraitItemKind::Macro(_) = item.node {
|
2016-10-06 08:04:30 +00:00
|
|
|
self.visit_invoc(item.id);
|
|
|
|
return
|
2016-10-04 16:36:14 +11:00
|
|
|
}
|
|
|
|
|
2016-09-14 23:39:13 +00:00
|
|
|
// Add the item to the trait info.
|
|
|
|
let item_def_id = self.resolver.definitions.local_def_id(item.id);
|
2017-03-18 02:10:13 +00: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 16:36:14 +11:00
|
|
|
TraitItemKind::Macro(_) => bug!(), // handled above
|
2016-09-14 23:39:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let vis = ty::Visibility::Public;
|
2016-11-29 02:07:12 +00:00
|
|
|
self.resolver.define(parent, item.ident, ns, (def, vis, item.span, self.expansion));
|
2016-09-14 23:39:13 +00: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 17:22:28 +09:00
|
|
|
|
|
|
|
fn visit_token(&mut self, t: Token) {
|
|
|
|
if let Token::Interpolated(nt) = t {
|
|
|
|
match nt.0 {
|
|
|
|
token::NtExpr(ref expr) => {
|
|
|
|
if let ast::ExprKind::Mac(..) = expr.node {
|
|
|
|
self.visit_invoc(expr.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|