Skip to content

Commit 08ea5b7

Browse files
committed
Fix #[test] shadowing in macro_prelude
1 parent 9b27de4 commit 08ea5b7

File tree

7 files changed

+70
-6
lines changed

7 files changed

+70
-6
lines changed

src/librustc_resolve/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,7 @@ pub struct Resolver<'a, 'b: 'a> {
14121412
crate_loader: &'a mut CrateLoader<'b>,
14131413
macro_names: FxHashSet<Ident>,
14141414
macro_prelude: FxHashMap<Name, &'a NameBinding<'a>>,
1415+
unshadowable_attrs: FxHashMap<Name, &'a NameBinding<'a>>,
14151416
pub all_macros: FxHashMap<Name, Def>,
14161417
macro_map: FxHashMap<DefId, Lrc<SyntaxExtension>>,
14171418
macro_defs: FxHashMap<Mark, DefId>,
@@ -1729,6 +1730,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
17291730
crate_loader,
17301731
macro_names: FxHashSet(),
17311732
macro_prelude: FxHashMap(),
1733+
unshadowable_attrs: FxHashMap(),
17321734
all_macros: FxHashMap(),
17331735
macro_map: FxHashMap(),
17341736
invocations,

src/librustc_resolve/macros.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,23 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
207207
self.macro_prelude.insert(ident.name, binding);
208208
}
209209

210+
fn add_unshadowable_attr(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>) {
211+
let def_id = DefId {
212+
krate: BUILTIN_MACROS_CRATE,
213+
index: DefIndex::from_array_index(self.macro_map.len(),
214+
DefIndexAddressSpace::Low),
215+
};
216+
let kind = ext.kind();
217+
self.macro_map.insert(def_id, ext);
218+
let binding = self.arenas.alloc_name_binding(NameBinding {
219+
kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
220+
span: DUMMY_SP,
221+
vis: ty::Visibility::Invisible,
222+
expansion: Mark::root(),
223+
});
224+
self.unshadowable_attrs.insert(ident.name, binding);
225+
}
226+
210227
fn resolve_imports(&mut self) {
211228
ImportResolver { resolver: self }.resolve_imports()
212229
}
@@ -462,8 +479,10 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
462479
return def;
463480
}
464481

465-
if kind == MacroKind::Attr && *&path[0].as_str() == "test" {
466-
return Ok(self.macro_prelude.get(&path[0].name).unwrap().def())
482+
if kind == MacroKind::Attr && path.len() == 1 {
483+
if let Some(ext) = self.unshadowable_attrs.get(&path[0].name) {
484+
return Ok(ext.def());
485+
}
467486
}
468487

469488
let legacy_resolution = self.resolve_legacy_scope(&invocation.legacy_scope, path[0], false);

src/libsyntax/ext/base.rs

+3
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ pub trait Resolver {
721721
fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment,
722722
derives: &[Mark]);
723723
fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>);
724+
fn add_unshadowable_attr(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>);
724725

725726
fn resolve_imports(&mut self);
726727
// Resolves attribute and derive legacy macros from `#![plugin(..)]`.
@@ -729,6 +730,7 @@ pub trait Resolver {
729730

730731
fn resolve_macro_invocation(&mut self, invoc: &Invocation, scope: Mark, force: bool)
731732
-> Result<Option<Lrc<SyntaxExtension>>, Determinacy>;
733+
732734
fn resolve_macro_path(&mut self, path: &ast::Path, kind: MacroKind, scope: Mark,
733735
derives_in_scope: &[ast::Path], force: bool)
734736
-> Result<Lrc<SyntaxExtension>, Determinacy>;
@@ -759,6 +761,7 @@ impl Resolver for DummyResolver {
759761
fn visit_ast_fragment_with_placeholders(&mut self, _invoc: Mark, _fragment: &AstFragment,
760762
_derives: &[Mark]) {}
761763
fn add_builtin(&mut self, _ident: ast::Ident, _ext: Lrc<SyntaxExtension>) {}
764+
fn add_unshadowable_attr(&mut self, _ident: ast::Ident, _ext: Lrc<SyntaxExtension>) {}
762765

763766
fn resolve_imports(&mut self) {}
764767
fn find_legacy_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>, _allow_derive: bool)

src/libsyntax/feature_gate.rs

-2
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,6 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG
769769
("cfg_attr", Normal, Ungated),
770770
("main", Normal, Ungated),
771771
("start", Normal, Ungated),
772-
("test", Normal, Ungated),
773-
("bench", Normal, Ungated),
774772
("repr", Normal, Ungated),
775773
("path", Normal, Ungated),
776774
("abi", Normal, Ungated),

src/libsyntax_ext/lib.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
7171
enable_quotes: bool) {
7272
deriving::register_builtin_derives(resolver);
7373

74+
{
75+
let mut register_unshadowable = |name, ext| {
76+
resolver.add_unshadowable_attr(ast::Ident::with_empty_ctxt(name), Lrc::new(ext));
77+
};
78+
79+
register_unshadowable(Symbol::intern("test"),
80+
MultiModifier(Box::new(test::expand_test)));
81+
82+
register_unshadowable(Symbol::intern("bench"),
83+
MultiModifier(Box::new(test::expand_bench)));
84+
}
85+
7486
let mut register = |name, ext| {
7587
resolver.add_builtin(ast::Ident::with_empty_ctxt(name), Lrc::new(ext));
7688
};
@@ -133,8 +145,6 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
133145
assert: assert::expand_assert,
134146
}
135147

136-
register(Symbol::intern("test"), MultiModifier(Box::new(test::expand_test)));
137-
register(Symbol::intern("bench"), MultiModifier(Box::new(test::expand_bench)));
138148

139149
// format_args uses `unstable` things internally.
140150
register(Symbol::intern("format_args"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 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+
#[macro_export]
12+
macro_rules! test {
13+
() => {};
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 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+
// compile-pass
12+
// aux-build:test_macro.rs
13+
// compile-flags:--test
14+
15+
#[macro_use] extern crate test_macro;
16+
17+
#[test]
18+
fn foo(){}

0 commit comments

Comments
 (0)