Skip to content

Commit cfc9b51

Browse files
authored
Auto merge of #37213 - jseyfried:refactor_crate_var, r=nrc
macros: improve `$crate` This PR refactors the implementation of `$crate` so that - `$crate` is only allowed at the start of a path (like `super`), - we can make `$crate` work with inter-crate re-exports (groundwork for macro modularization), and - we can support importing macros from an extern crate that is not declared at the crate root (also groundwork for macro modularization). This is a [breaking-change]. For example, the following would break: ```rust fn foo() {} macro_rules! m { () => { $crate foo $crate () $crate $crate; //^ Today, `$crate` is allowed just about anywhere in unexported macros. } } fn main() { m!(); } ``` r? @nrc
2 parents a41505f + 8b0c292 commit cfc9b51

File tree

14 files changed

+97
-133
lines changed

14 files changed

+97
-133
lines changed

src/librustc_resolve/build_reduced_graph.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind};
3737
use syntax::ast::{Mutability, StmtKind, TraitItem, TraitItemKind};
3838
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
3939
use syntax::ext::base::{SyntaxExtension, Resolver as SyntaxResolver};
40+
use syntax::ext::expand::mark_tts;
4041
use syntax::ext::hygiene::Mark;
4142
use syntax::feature_gate::{self, emit_feature_err};
4243
use syntax::ext::tt::macro_rules;
@@ -95,22 +96,22 @@ impl<'b> Resolver<'b> {
9596
// Extract and intern the module part of the path. For
9697
// globs and lists, the path is found directly in the AST;
9798
// for simple paths we have to munge the path a little.
98-
let module_path: Vec<Name> = match view_path.node {
99+
let module_path: Vec<_> = match view_path.node {
99100
ViewPathSimple(_, ref full_path) => {
100101
full_path.segments
101102
.split_last()
102103
.unwrap()
103104
.1
104105
.iter()
105-
.map(|seg| seg.identifier.name)
106+
.map(|seg| seg.identifier)
106107
.collect()
107108
}
108109

109110
ViewPathGlob(ref module_ident_path) |
110111
ViewPathList(ref module_ident_path, _) => {
111112
module_ident_path.segments
112113
.iter()
113-
.map(|seg| seg.identifier.name)
114+
.map(|seg| seg.identifier)
114115
.collect()
115116
}
116117
};
@@ -159,7 +160,7 @@ impl<'b> Resolver<'b> {
159160
(module_path.clone(), node.name.name, rename)
160161
} else {
161162
let name = match module_path.last() {
162-
Some(name) => *name,
163+
Some(ident) => ident.name,
163164
None => {
164165
resolve_error(
165166
self,
@@ -207,11 +208,16 @@ impl<'b> Resolver<'b> {
207208
};
208209

209210
let mut custom_derive_crate = false;
211+
// The mark of the expansion that generates the loaded macros.
212+
let mut opt_mark = None;
210213
for loaded_macro in self.crate_loader.load_macros(item, is_crate_root) {
214+
let mark = opt_mark.unwrap_or_else(Mark::fresh);
215+
opt_mark = Some(mark);
211216
match loaded_macro.kind {
212217
LoadedMacroKind::Def(mut def) => {
213218
if def.use_locally {
214219
self.macro_names.insert(def.ident.name);
220+
def.body = mark_tts(&def.body, mark);
215221
let ext = macro_rules::compile(&self.session.parse_sess, &def);
216222
import_macro(self, def.ident.name, ext, loaded_macro.import_site);
217223
}
@@ -249,6 +255,17 @@ impl<'b> Resolver<'b> {
249255
});
250256
self.define(parent, name, TypeNS, (module, sp, vis));
251257

258+
if let Some(mark) = opt_mark {
259+
let invocation = self.arenas.alloc_invocation_data(InvocationData {
260+
module: Cell::new(module),
261+
def_index: CRATE_DEF_INDEX,
262+
const_integer: false,
263+
legacy_scope: Cell::new(LegacyScope::Empty),
264+
expansion: Cell::new(LegacyScope::Empty),
265+
});
266+
self.invocations.insert(mark, invocation);
267+
}
268+
252269
self.populate_module_if_necessary(module);
253270
} else if custom_derive_crate {
254271
// Define an empty module

0 commit comments

Comments
 (0)