Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: Do not block derive helper resolutions on single import resolutions #54518

Merged
merged 1 commit into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,13 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
// 5. Standard library prelude (de-facto closed, controlled).
// 6. Language prelude (closed, controlled).
// (Macro NS)
// 0. Derive helpers (open, not controlled). All ambiguities with other names
// are currently reported as errors. They should be higher in priority than preludes
// and probably even names in modules according to the "general principles" above. They
// also should be subject to restricted shadowing because are effectively produced by
// derives (you need to resolve the derive first to add helpers into scope), but they
// should be available before the derive is expanded for compatibility.
// It's mess in general, so we are being conservative for now.
// 1. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
// (open, not controlled).
// 2. `macro_use` prelude (open, the open part is from macro expansions, not controlled).
Expand All @@ -583,13 +590,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
// 2b. Standard library prelude is currently implemented as `macro-use` (closed, controlled)
// 3. Language prelude: builtin macros (closed, controlled, except for legacy plugins).
// 4. Language prelude: builtin attributes (closed, controlled).
// N (unordered). Derive helpers (open, not controlled). All ambiguities with other names
// are currently reported as errors. They should be higher in priority than preludes
// and maybe even names in modules according to the "general principles" above. They
// also should be subject to restricted shadowing because are effectively produced by
// derives (you need to resolve the derive first to add helpers into scope), but they
// should be available before the derive is expanded for compatibility.
// It's mess in general, so we are being conservative for now.

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

// Go through all the scopes and try to resolve the name.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment may need an update?

I think the large comment above this one may also need a slight tweak?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we are still going through scopes and trying to resolve, just in slightly different order.
I've updated the larger comment above.

let mut where_to_resolve = WhereToResolve::Module(parent_scope.module);
let mut where_to_resolve = WhereToResolve::DeriveHelpers;
let mut use_prelude = !parent_scope.module.no_implicit_prelude;
loop {
let result = match where_to_resolve {
Expand Down Expand Up @@ -751,8 +751,8 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
}
WhereToResolve::MacroUsePrelude => WhereToResolve::BuiltinMacros,
WhereToResolve::BuiltinMacros => WhereToResolve::BuiltinAttrs,
WhereToResolve::BuiltinAttrs => WhereToResolve::DeriveHelpers,
WhereToResolve::DeriveHelpers => break, // nowhere else to search
WhereToResolve::BuiltinAttrs => break, // nowhere else to search
WhereToResolve::DeriveHelpers => WhereToResolve::Module(parent_scope.module),
WhereToResolve::ExternPrelude => WhereToResolve::ToolPrelude,
WhereToResolve::ToolPrelude => WhereToResolve::StdLibPrelude,
WhereToResolve::StdLibPrelude => WhereToResolve::BuiltinTypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
// aux-build:derive-b.rs
// ignore-stage1

#![allow(warnings)]

#[macro_use]
extern crate derive_b;

#[B] //~ ERROR `B` is a derive mode
#[C]
#[B]
#[C] //~ ERROR attribute `C` is currently unknown to the compiler
#[B(D)]
#[B(E = "foo")]
#[B(arbitrary tokens)]
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui-fulldeps/custom-derive/auxiliary/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ pub fn derive_foo(input: TokenStream) -> TokenStream {
pub fn derive_bar(input: TokenStream) -> TokenStream {
panic!("lolnope");
}

#[proc_macro_derive(WithHelper, attributes(helper))]
pub fn with_helper(input: TokenStream) -> TokenStream {
TokenStream::new()
}

#[proc_macro_attribute]
pub fn helper(_: TokenStream, input: TokenStream) -> TokenStream {
input
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// aux-build:plugin.rs
// ignore-stage1

#[macro_use(WithHelper)]
extern crate plugin;

use plugin::helper;

#[derive(WithHelper)]
#[helper] //~ ERROR `helper` is ambiguous
struct S;

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0659]: `helper` is ambiguous
--> $DIR/helper-attr-blocked-by-import-ambig.rs:10:3
|
LL | #[helper] //~ ERROR `helper` is ambiguous
| ^^^^^^ ambiguous name
|
note: `helper` could refer to the name defined here
--> $DIR/helper-attr-blocked-by-import-ambig.rs:9:10
|
LL | #[derive(WithHelper)]
| ^^^^^^^^^^
note: `helper` could also refer to the name imported here
--> $DIR/helper-attr-blocked-by-import-ambig.rs:7:5
|
LL | use plugin::helper;
| ^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0659`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// compile-pass
// aux-build:plugin.rs
// ignore-stage1

#[macro_use(WithHelper)]
extern crate plugin;

use self::one::*;
use self::two::*;

mod helper {}

mod one {
use helper;

#[derive(WithHelper)]
#[helper]
struct One;
}

mod two {
use helper;

#[derive(WithHelper)]
#[helper]
struct Two;
}

fn main() {}
13 changes: 6 additions & 7 deletions src/test/ui-fulldeps/proc-macro/derive-helper-shadowing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ error[E0659]: `my_attr` is ambiguous
LL | #[my_attr] //~ ERROR `my_attr` is ambiguous
| ^^^^^^^ ambiguous name
|
note: `my_attr` could refer to the name imported here
--> $DIR/derive-helper-shadowing.rs:4:5
|
LL | use derive_helper_shadowing::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `my_attr` could also refer to the name defined here
note: `my_attr` could refer to the name defined here
--> $DIR/derive-helper-shadowing.rs:7:10
|
LL | #[derive(MyTrait)]
| ^^^^^^^
= note: consider adding an explicit import of `my_attr` to disambiguate
note: `my_attr` could also refer to the name imported here
--> $DIR/derive-helper-shadowing.rs:4:5
|
LL | use derive_helper_shadowing::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down