Skip to content

Commit

Permalink
resolve: Attempt to resolve unresolved paths in macro namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jul 18, 2019
1 parent 3845a08 commit a7726ce
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ impl<'a> Resolver<'a> {
};

match (res, source) {
(Res::Def(DefKind::Macro(..), _), _) => {
(Res::Def(DefKind::Macro(MacroKind::Bang), _), _) => {
err.span_suggestion(
span,
"use `!` to invoke the macro",
Expand Down Expand Up @@ -574,7 +574,7 @@ impl<'a> Resolver<'a> {
for derive in &parent_scope.derives {
let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
if let Ok((Some(ext), _)) = this.resolve_macro_path(
derive, MacroKind::Derive, &parent_scope, true, true
derive, MacroKind::Derive, &parent_scope, false, false
) {
suggestions.extend(ext.helper_attrs.iter().map(|name| {
TypoSuggestion::from_res(*name, res)
Expand Down
32 changes: 17 additions & 15 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3664,9 +3664,7 @@ impl<'a> Resolver<'a> {
crate_lint: CrateLint,
) -> Option<PartialRes> {
let mut fin_res = None;
// FIXME: can't resolve paths in macro namespace yet, macros are
// processed by the little special hack below.
for (i, ns) in [primary_ns, TypeNS, ValueNS, /*MacroNS*/].iter().cloned().enumerate() {
for (i, ns) in [primary_ns, TypeNS, ValueNS].iter().cloned().enumerate() {
if i == 0 || ns != primary_ns {
match self.resolve_qpath(id, qself, path, ns, span, global_by_default, crate_lint) {
// If defer_to_typeck, then resolution > no resolution,
Expand All @@ -3675,21 +3673,25 @@ impl<'a> Resolver<'a> {
defer_to_typeck =>
return Some(partial_res),
partial_res => if fin_res.is_none() { fin_res = partial_res },
};
}
}
}
if primary_ns != MacroNS &&
(self.macro_names.contains(&path[0].ident.modern()) ||
self.builtin_macros.get(&path[0].ident.name).cloned()
.and_then(NameBinding::macro_kind) == Some(MacroKind::Bang) ||
self.macro_use_prelude.get(&path[0].ident.name).cloned()
.and_then(NameBinding::macro_kind) == Some(MacroKind::Bang)) {
// Return some dummy definition, it's enough for error reporting.
return Some(PartialRes::new(Res::Def(
DefKind::Macro(MacroKind::Bang),
DefId::local(CRATE_DEF_INDEX),
)));

// `MacroNS`
assert!(primary_ns != MacroNS);
if qself.is_none() {
let path_seg = |seg: &Segment| ast::PathSegment::from_ident(seg.ident);
let path = Path { segments: path.iter().map(path_seg).collect(), span };
let parent_scope =
ParentScope { module: self.current_module, ..self.dummy_parent_scope() };
for macro_kind in &[MacroKind::Bang, MacroKind::Attr, MacroKind::Derive] {
if let Ok((_, res)) = self.resolve_macro_path(&path, *macro_kind,
&parent_scope, false, false) {
return Some(PartialRes::new(res));
}
}
}

fin_res
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/hygiene/rustc-macro-transparency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ fn main() {
Opaque; //~ ERROR cannot find value `Opaque` in this scope

transparent; // OK
semitransparent; //~ ERROR cannot find value `semitransparent` in this scope
opaque; //~ ERROR cannot find value `opaque` in this scope
semitransparent; //~ ERROR expected value, found macro `semitransparent`
opaque; //~ ERROR expected value, found macro `opaque`
}
11 changes: 6 additions & 5 deletions src/test/ui/hygiene/rustc-macro-transparency.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ error[E0425]: cannot find value `Opaque` in this scope
LL | Opaque;
| ^^^^^^ help: a local variable with a similar name exists: `opaque`

error[E0425]: cannot find value `semitransparent` in this scope
error[E0423]: expected value, found macro `semitransparent`
--> $DIR/rustc-macro-transparency.rs:29:5
|
LL | semitransparent;
| ^^^^^^^^^^^^^^^ not found in this scope
| ^^^^^^^^^^^^^^^ help: use `!` to invoke the macro: `semitransparent!`

error[E0425]: cannot find value `opaque` in this scope
error[E0423]: expected value, found macro `opaque`
--> $DIR/rustc-macro-transparency.rs:30:5
|
LL | opaque;
| ^^^^^^ not found in this scope
| ^^^^^^ help: use `!` to invoke the macro: `opaque!`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0425`.
Some errors have detailed explanations: E0423, E0425.
For more information about an error, try `rustc --explain E0423`.
7 changes: 3 additions & 4 deletions src/test/ui/impl-trait/universal_wrong_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ fn foo(f: impl Display + Clone) -> String {
wants_clone(f);
}

fn wants_debug(g: impl Debug) { } //~ ERROR cannot find
fn wants_display(g: impl Debug) { } //~ ERROR cannot find
fn wants_debug(g: impl Debug) { } //~ ERROR expected trait, found derive macro `Debug`
fn wants_display(g: impl Debug) { } //~ ERROR expected trait, found derive macro `Debug`
fn wants_clone(g: impl Clone) { }

fn main() {
}
fn main() {}
14 changes: 7 additions & 7 deletions src/test/ui/impl-trait/universal_wrong_bounds.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error[E0405]: cannot find trait `Debug` in this scope
error[E0404]: expected trait, found derive macro `Debug`
--> $DIR/universal_wrong_bounds.rs:9:24
|
LL | fn wants_debug(g: impl Debug) { }
| ^^^^^ not found in this scope
help: possible candidate is found in another module, you can import it into scope
| ^^^^^ not a trait
help: possible better candidate is found in another module, you can import it into scope
|
LL | use std::fmt::Debug;
|

error[E0405]: cannot find trait `Debug` in this scope
error[E0404]: expected trait, found derive macro `Debug`
--> $DIR/universal_wrong_bounds.rs:10:26
|
LL | fn wants_display(g: impl Debug) { }
| ^^^^^ not found in this scope
help: possible candidate is found in another module, you can import it into scope
| ^^^^^ not a trait
help: possible better candidate is found in another module, you can import it into scope
|
LL | use std::fmt::Debug;
|

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0405`.
For more information about this error, try `rustc --explain E0404`.
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-37534.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
struct Foo<T: ?Hash> { }
//~^ ERROR cannot find trait `Hash` in this scope
//~^ ERROR expected trait, found derive macro `Hash`
//~^^ ERROR parameter `T` is never used
//~^^^ WARN default bound relaxed for a type parameter, but this does nothing

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/issues/issue-37534.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
error[E0405]: cannot find trait `Hash` in this scope
error[E0404]: expected trait, found derive macro `Hash`
--> $DIR/issue-37534.rs:1:16
|
LL | struct Foo<T: ?Hash> { }
| ^^^^ not found in this scope
help: possible candidate is found in another module, you can import it into scope
| ^^^^ not a trait
help: possible better candidate is found in another module, you can import it into scope
|
LL | use std::hash::Hash;
|
Expand All @@ -24,5 +24,5 @@ LL | struct Foo<T: ?Hash> { }

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0392, E0405.
Some errors have detailed explanations: E0392, E0404.
For more information about an error, try `rustc --explain E0392`.
6 changes: 3 additions & 3 deletions src/test/ui/no-implicit-prelude-nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod foo {
mod baz {
struct Test;
impl Add for Test {} //~ ERROR cannot find trait `Add` in this scope
impl Clone for Test {} //~ ERROR cannot find trait `Clone` in this scope
impl Clone for Test {} //~ ERROR expected trait, found derive macro `Clone`
impl Iterator for Test {} //~ ERROR cannot find trait `Iterator` in this scope
impl ToString for Test {} //~ ERROR cannot find trait `ToString` in this scope
impl Writer for Test {} //~ ERROR cannot find trait `Writer` in this scope
Expand All @@ -21,7 +21,7 @@ mod foo {

struct Test;
impl Add for Test {} //~ ERROR cannot find trait `Add` in this scope
impl Clone for Test {} //~ ERROR cannot find trait `Clone` in this scope
impl Clone for Test {} //~ ERROR expected trait, found derive macro `Clone`
impl Iterator for Test {} //~ ERROR cannot find trait `Iterator` in this scope
impl ToString for Test {} //~ ERROR cannot find trait `ToString` in this scope
impl Writer for Test {} //~ ERROR cannot find trait `Writer` in this scope
Expand All @@ -36,7 +36,7 @@ fn qux() {
mod qux_inner {
struct Test;
impl Add for Test {} //~ ERROR cannot find trait `Add` in this scope
impl Clone for Test {} //~ ERROR cannot find trait `Clone` in this scope
impl Clone for Test {} //~ ERROR expected trait, found derive macro `Clone`
impl Iterator for Test {} //~ ERROR cannot find trait `Iterator` in this scope
impl ToString for Test {} //~ ERROR cannot find trait `ToString` in this scope
impl Writer for Test {} //~ ERROR cannot find trait `Writer` in this scope
Expand Down
22 changes: 11 additions & 11 deletions src/test/ui/no-implicit-prelude-nested.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ help: possible candidate is found in another module, you can import it into scop
LL | use std::ops::Add;
|

error[E0405]: cannot find trait `Clone` in this scope
error[E0404]: expected trait, found derive macro `Clone`
--> $DIR/no-implicit-prelude-nested.rs:12:14
|
LL | impl Clone for Test {}
| ^^^^^ not found in this scope
help: possible candidates are found in other modules, you can import them into scope
| ^^^^^ not a trait
help: possible better candidates are found in other modules, you can import them into scope
|
LL | use std::clone::Clone;
|
Expand Down Expand Up @@ -72,12 +72,12 @@ help: possible candidate is found in another module, you can import it into scop
LL | use std::ops::Add;
|

error[E0405]: cannot find trait `Clone` in this scope
error[E0404]: expected trait, found derive macro `Clone`
--> $DIR/no-implicit-prelude-nested.rs:24:10
|
LL | impl Clone for Test {}
| ^^^^^ not found in this scope
help: possible candidates are found in other modules, you can import them into scope
| ^^^^^ not a trait
help: possible better candidates are found in other modules, you can import them into scope
|
LL | use std::clone::Clone;
|
Expand Down Expand Up @@ -136,12 +136,12 @@ help: possible candidate is found in another module, you can import it into scop
LL | use std::ops::Add;
|

error[E0405]: cannot find trait `Clone` in this scope
error[E0404]: expected trait, found derive macro `Clone`
--> $DIR/no-implicit-prelude-nested.rs:39:14
|
LL | impl Clone for Test {}
| ^^^^^ not found in this scope
help: possible candidates are found in other modules, you can import them into scope
| ^^^^^ not a trait
help: possible better candidates are found in other modules, you can import them into scope
|
LL | use std::clone::Clone;
|
Expand Down Expand Up @@ -192,5 +192,5 @@ LL | use std::prelude::v1::drop;

error: aborting due to 18 previous errors

Some errors have detailed explanations: E0405, E0425.
For more information about an error, try `rustc --explain E0405`.
Some errors have detailed explanations: E0404, E0405, E0425.
For more information about an error, try `rustc --explain E0404`.
2 changes: 1 addition & 1 deletion src/test/ui/no-implicit-prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

struct Test;
impl Add for Test {} //~ ERROR cannot find trait `Add` in this scope
impl Clone for Test {} //~ ERROR cannot find trait `Clone` in this scope
impl Clone for Test {} //~ ERROR expected trait, found derive macro `Clone`
impl Iterator for Test {} //~ ERROR cannot find trait `Iterator` in this scope
impl ToString for Test {} //~ ERROR cannot find trait `ToString` in this scope
impl Writer for Test {} //~ ERROR cannot find trait `Writer` in this scope
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/no-implicit-prelude.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ help: possible candidate is found in another module, you can import it into scop
LL | use std::ops::Add;
|

error[E0405]: cannot find trait `Clone` in this scope
error[E0404]: expected trait, found derive macro `Clone`
--> $DIR/no-implicit-prelude.rs:11:6
|
LL | impl Clone for Test {}
| ^^^^^ not found in this scope
help: possible candidates are found in other modules, you can import them into scope
| ^^^^^ not a trait
help: possible better candidates are found in other modules, you can import them into scope
|
LL | use std::clone::Clone;
|
Expand Down Expand Up @@ -64,5 +64,5 @@ LL | use std::prelude::v1::drop;

error: aborting due to 6 previous errors

Some errors have detailed explanations: E0405, E0425.
For more information about an error, try `rustc --explain E0405`.
Some errors have detailed explanations: E0404, E0405, E0425.
For more information about an error, try `rustc --explain E0404`.

0 comments on commit a7726ce

Please sign in to comment.