Skip to content

Commit d052d28

Browse files
committed
Improve interaction between macros 2.0 and macro_rules!.
1 parent 3dfbc88 commit d052d28

File tree

8 files changed

+126
-9
lines changed

8 files changed

+126
-9
lines changed

src/libproc_macro/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl FromStr for TokenStream {
9595
// notify the expansion info that it is unhygienic
9696
let mark = Mark::fresh(mark);
9797
mark.set_expn_info(expn_info);
98-
let span = call_site.with_ctxt(call_site.ctxt().apply_mark(mark));
98+
let span = call_site.with_ctxt(SyntaxContext::empty().apply_mark(mark));
9999
let stream = parse::parse_stream_from_source_str(name, src, sess, Some(span));
100100
Ok(__internal::token_stream_wrap(stream))
101101
})

src/librustc_resolve/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'a> Resolver<'a> {
156156

157157
// Disallow `use $crate;`
158158
if source.name == keywords::DollarCrate.name() && path.segments.len() == 1 {
159-
let crate_root = self.resolve_crate_root(source.ctxt);
159+
let crate_root = self.resolve_crate_root(source.ctxt, true);
160160
let crate_name = match crate_root.kind {
161161
ModuleKind::Def(_, name) => name,
162162
ModuleKind::Block(..) => unreachable!(),

src/librustc_resolve/lib.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use rustc::hir::{Freevar, FreevarMap, TraitCandidate, TraitMap, GlobMap};
4242
use rustc::util::nodemap::{NodeMap, NodeSet, FxHashMap, FxHashSet, DefIdMap};
4343

4444
use syntax::codemap::{dummy_spanned, respan};
45-
use syntax::ext::hygiene::{Mark, SyntaxContext};
45+
use syntax::ext::hygiene::{Mark, MarkKind, SyntaxContext};
4646
use syntax::ast::{self, Name, NodeId, Ident, SpannedIdent, FloatTy, IntTy, UintTy};
4747
use syntax::ext::base::SyntaxExtension;
4848
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
@@ -1775,8 +1775,17 @@ impl<'a> Resolver<'a> {
17751775
result
17761776
}
17771777

1778-
fn resolve_crate_root(&mut self, mut ctxt: SyntaxContext) -> Module<'a> {
1779-
let module = match ctxt.adjust(Mark::root()) {
1778+
fn resolve_crate_root(&mut self, mut ctxt: SyntaxContext, legacy: bool) -> Module<'a> {
1779+
let mark = if legacy {
1780+
// When resolving `$crate` from a `macro_rules!` invoked in a `macro`,
1781+
// we don't want to pretend that the `macro_rules!` definition is in the `macro`
1782+
// as described in `SyntaxContext::apply_mark`, so we ignore prepended modern marks.
1783+
ctxt.marks().into_iter().find(|&mark| mark.kind() != MarkKind::Modern)
1784+
} else {
1785+
ctxt = ctxt.modern();
1786+
ctxt.adjust(Mark::root())
1787+
};
1788+
let module = match mark {
17801789
Some(def) => self.macro_def_scope(def),
17811790
None => return self.graph_root,
17821791
};
@@ -2961,11 +2970,11 @@ impl<'a> Resolver<'a> {
29612970
(i == 1 && name == keywords::Crate.name() &&
29622971
path[0].node.name == keywords::CrateRoot.name()) {
29632972
// `::a::b` or `::crate::a::b`
2964-
module = Some(self.resolve_crate_root(ident.node.ctxt.modern()));
2973+
module = Some(self.resolve_crate_root(ident.node.ctxt, false));
29652974
continue
29662975
} else if i == 0 && name == keywords::DollarCrate.name() {
29672976
// `$crate::a::b`
2968-
module = Some(self.resolve_crate_root(ident.node.ctxt));
2977+
module = Some(self.resolve_crate_root(ident.node.ctxt, true));
29692978
continue
29702979
} else if i == 1 && self.session.features.borrow().extern_absolute_paths &&
29712980
path[0].node.name == keywords::CrateRoot.name() &&

src/librustc_resolve/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'a> base::Resolver for Resolver<'a> {
140140
let ident = path.segments[0].identifier;
141141
if ident.name == keywords::DollarCrate.name() {
142142
path.segments[0].identifier.name = keywords::CrateRoot.name();
143-
let module = self.0.resolve_crate_root(ident.ctxt);
143+
let module = self.0.resolve_crate_root(ident.ctxt, true);
144144
if !module.is_local() {
145145
let span = path.segments[0].span;
146146
path.segments.insert(1, match module.kind {

src/librustc_resolve/resolve_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
620620
"crate root imports need to be explicitly named: \
621621
`use crate as name;`".to_string()));
622622
} else {
623-
Some(self.resolve_crate_root(source.ctxt.modern()))
623+
Some(self.resolve_crate_root(source.ctxt.modern(), false))
624624
}
625625
} else if extern_absolute_paths &&
626626
!token::Ident(source).is_path_segment_keyword() {

src/libsyntax_pos/hygiene.rs

+39
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,33 @@ impl SyntaxContext {
181181

182182
/// Extend a syntax context with a given mark
183183
pub fn apply_mark(self, mark: Mark) -> SyntaxContext {
184+
if mark.kind() == MarkKind::Modern {
185+
return self.apply_mark_internal(mark);
186+
}
187+
188+
let call_site_ctxt =
189+
mark.expn_info().map_or(SyntaxContext::empty(), |info| info.call_site.ctxt()).modern();
190+
if call_site_ctxt == SyntaxContext::empty() {
191+
return self.apply_mark_internal(mark);
192+
}
193+
194+
// Otherwise, `mark` is a macros 1.0 definition and the call site is in a
195+
// macros 2.0 expansion, i.e. a macros 1.0 invocation is in a macros 2.0 definition.
196+
//
197+
// In this case, the tokens from the macros 1.0 definition inherit the hygiene
198+
// at their invocation. That is, we pretend that the macros 1.0 definition
199+
// was defined at its invocation (i.e. inside the macros 2.0 definition)
200+
// so that the macros 2.0 definition remains hygienic.
201+
//
202+
// See the example at `test/run-pass/hygiene/legacy_interaction.rs`.
203+
let mut ctxt = call_site_ctxt;
204+
for mark in self.marks() {
205+
ctxt = ctxt.apply_mark_internal(mark);
206+
}
207+
ctxt.apply_mark_internal(mark)
208+
}
209+
210+
fn apply_mark_internal(self, mark: Mark) -> SyntaxContext {
184211
HygieneData::with(|data| {
185212
let syntax_contexts = &mut data.syntax_contexts;
186213
let mut modern = syntax_contexts[self.0 as usize].modern;
@@ -215,6 +242,18 @@ impl SyntaxContext {
215242
})
216243
}
217244

245+
pub fn marks(mut self) -> Vec<Mark> {
246+
HygieneData::with(|data| {
247+
let mut marks = Vec::new();
248+
while self != SyntaxContext::empty() {
249+
marks.push(data.syntax_contexts[self.0 as usize].outer_mark);
250+
self = data.syntax_contexts[self.0 as usize].prev_ctxt;
251+
}
252+
marks.reverse();
253+
marks
254+
})
255+
}
256+
218257
/// Adjust this context for resolution in a scope created by the given expansion.
219258
/// For example, consider the following three resolutions of `f`:
220259
/// ```rust
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-pretty pretty-printing is unhygienic
12+
13+
#[macro_export]
14+
macro_rules! m {
15+
() => {
16+
fn f() {} // (2)
17+
g(); // (1)
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-pretty pretty-printing is unhygienic
12+
13+
// aux-build:legacy_interaction.rs
14+
15+
#![feature(decl_macro)]
16+
#[allow(unused)]
17+
18+
extern crate legacy_interaction;
19+
// ^ defines
20+
// ```rust
21+
// macro_rules! m {
22+
// () => {
23+
// fn f() // (1)
24+
// g() // (2)
25+
// }
26+
// }
27+
// ```rust
28+
29+
mod def_site {
30+
// Unless this macro opts out of hygiene, it should resolve the same wherever it is invoked.
31+
pub macro m2() {
32+
::legacy_interaction::m!();
33+
f(); // This should resolve to (1)
34+
fn g() {} // We want (2) resolve to this, not to (4)
35+
}
36+
}
37+
38+
mod use_site {
39+
fn test() {
40+
fn f() -> bool { true } // (3)
41+
fn g() -> bool { true } // (4)
42+
43+
::def_site::m2!();
44+
45+
let _: bool = f(); // This should resolve to (3)
46+
let _: bool = g(); // This should resolve to (4)
47+
}
48+
}
49+
50+
fn main() {}

0 commit comments

Comments
 (0)