Skip to content

Commit ee05f6e

Browse files
committed
resolve: Do not block derive helper resolutions on single import resolutions
Derive helpers conflict currently conflict with anything else, so if some resolution from a single import appears later, it will result in error anyway
1 parent 31789a6 commit ee05f6e

File tree

7 files changed

+90
-21
lines changed

7 files changed

+90
-21
lines changed

src/librustc_resolve/macros.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
575575
// 5. Standard library prelude (de-facto closed, controlled).
576576
// 6. Language prelude (closed, controlled).
577577
// (Macro NS)
578+
// 0. Derive helpers (open, not controlled). All ambiguities with other names
579+
// are currently reported as errors. They should be higher in priority than preludes
580+
// and probably even names in modules according to the "general principles" above. They
581+
// also should be subject to restricted shadowing because are effectively produced by
582+
// derives (you need to resolve the derive first to add helpers into scope), but they
583+
// should be available before the derive is expanded for compatibility.
584+
// It's mess in general, so we are being conservative for now.
578585
// 1. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
579586
// (open, not controlled).
580587
// 2. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
@@ -583,13 +590,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
583590
// 2b. Standard library prelude is currently implemented as `macro-use` (closed, controlled)
584591
// 3. Language prelude: builtin macros (closed, controlled, except for legacy plugins).
585592
// 4. Language prelude: builtin attributes (closed, controlled).
586-
// N (unordered). Derive helpers (open, not controlled). All ambiguities with other names
587-
// are currently reported as errors. They should be higher in priority than preludes
588-
// and maybe even names in modules according to the "general principles" above. They
589-
// also should be subject to restricted shadowing because are effectively produced by
590-
// derives (you need to resolve the derive first to add helpers into scope), but they
591-
// should be available before the derive is expanded for compatibility.
592-
// It's mess in general, so we are being conservative for now.
593593

594594
assert!(ns == TypeNS || ns == MacroNS);
595595
assert!(force || !record_used); // `record_used` implies `force`
@@ -621,7 +621,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
621621
}
622622

623623
// Go through all the scopes and try to resolve the name.
624-
let mut where_to_resolve = WhereToResolve::Module(parent_scope.module);
624+
let mut where_to_resolve = WhereToResolve::DeriveHelpers;
625625
let mut use_prelude = !parent_scope.module.no_implicit_prelude;
626626
loop {
627627
let result = match where_to_resolve {
@@ -751,8 +751,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
751751
}
752752
WhereToResolve::MacroUsePrelude => WhereToResolve::BuiltinMacros,
753753
WhereToResolve::BuiltinMacros => WhereToResolve::BuiltinAttrs,
754-
WhereToResolve::BuiltinAttrs => WhereToResolve::DeriveHelpers,
755-
WhereToResolve::DeriveHelpers => break, // nowhere else to search
754+
WhereToResolve::BuiltinAttrs => break, // nowhere else to search
755+
WhereToResolve::DeriveHelpers => WhereToResolve::Module(parent_scope.module),
756756
WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
757757
WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,
758758
WhereToResolve::StdLibPrelude => WhereToResolve::BuiltinTypes,

src/test/compile-fail-fulldeps/proc-macro/proc-macro-attributes.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
// aux-build:derive-b.rs
1212
// ignore-stage1
1313

14-
#![allow(warnings)]
15-
1614
#[macro_use]
1715
extern crate derive_b;
1816

19-
#[B] //~ ERROR `B` is a derive mode
20-
#[C]
17+
#[B]
18+
#[C] //~ ERROR attribute `C` is currently unknown to the compiler
2119
#[B(D)]
2220
#[B(E = "foo")]
2321
#[B(arbitrary tokens)]

src/test/ui-fulldeps/custom-derive/auxiliary/plugin.rs

+10
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ pub fn derive_foo(input: TokenStream) -> TokenStream {
2525
pub fn derive_bar(input: TokenStream) -> TokenStream {
2626
panic!("lolnope");
2727
}
28+
29+
#[proc_macro_derive(WithHelper, attributes(helper))]
30+
pub fn with_helper(input: TokenStream) -> TokenStream {
31+
TokenStream::new()
32+
}
33+
34+
#[proc_macro_attribute]
35+
pub fn helper(_: TokenStream, input: TokenStream) -> TokenStream {
36+
input
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// aux-build:plugin.rs
2+
// ignore-stage1
3+
4+
#[macro_use(WithHelper)]
5+
extern crate plugin;
6+
7+
use plugin::helper;
8+
9+
#[derive(WithHelper)]
10+
#[helper] //~ ERROR `helper` is ambiguous
11+
struct S;
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0659]: `helper` is ambiguous
2+
--> $DIR/helper-attr-blocked-by-import-ambig.rs:10:3
3+
|
4+
LL | #[helper] //~ ERROR `helper` is ambiguous
5+
| ^^^^^^ ambiguous name
6+
|
7+
note: `helper` could refer to the name defined here
8+
--> $DIR/helper-attr-blocked-by-import-ambig.rs:9:10
9+
|
10+
LL | #[derive(WithHelper)]
11+
| ^^^^^^^^^^
12+
note: `helper` could also refer to the name imported here
13+
--> $DIR/helper-attr-blocked-by-import-ambig.rs:7:5
14+
|
15+
LL | use plugin::helper;
16+
| ^^^^^^^^^^^^^^
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0659`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// compile-pass
2+
// aux-build:plugin.rs
3+
// ignore-stage1
4+
5+
#[macro_use(WithHelper)]
6+
extern crate plugin;
7+
8+
use self::one::*;
9+
use self::two::*;
10+
11+
mod helper {}
12+
13+
mod one {
14+
use helper;
15+
16+
#[derive(WithHelper)]
17+
#[helper]
18+
struct One;
19+
}
20+
21+
mod two {
22+
use helper;
23+
24+
#[derive(WithHelper)]
25+
#[helper]
26+
struct Two;
27+
}
28+
29+
fn main() {}

src/test/ui-fulldeps/proc-macro/derive-helper-shadowing.stderr

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ error[E0659]: `my_attr` is ambiguous
44
LL | #[my_attr] //~ ERROR `my_attr` is ambiguous
55
| ^^^^^^^ ambiguous name
66
|
7-
note: `my_attr` could refer to the name imported here
8-
--> $DIR/derive-helper-shadowing.rs:4:5
9-
|
10-
LL | use derive_helper_shadowing::*;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: `my_attr` could also refer to the name defined here
7+
note: `my_attr` could refer to the name defined here
138
--> $DIR/derive-helper-shadowing.rs:7:10
149
|
1510
LL | #[derive(MyTrait)]
1611
| ^^^^^^^
17-
= note: consider adding an explicit import of `my_attr` to disambiguate
12+
note: `my_attr` could also refer to the name imported here
13+
--> $DIR/derive-helper-shadowing.rs:4:5
14+
|
15+
LL | use derive_helper_shadowing::*;
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1817

1918
error: aborting due to previous error
2019

0 commit comments

Comments
 (0)