Skip to content

Commit a7726ce

Browse files
committed
resolve: Attempt to resolve unresolved paths in macro namespace
1 parent 3845a08 commit a7726ce

12 files changed

+62
-60
lines changed

src/librustc_resolve/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ impl<'a> Resolver<'a> {
414414
};
415415

416416
match (res, source) {
417-
(Res::Def(DefKind::Macro(..), _), _) => {
417+
(Res::Def(DefKind::Macro(MacroKind::Bang), _), _) => {
418418
err.span_suggestion(
419419
span,
420420
"use `!` to invoke the macro",
@@ -574,7 +574,7 @@ impl<'a> Resolver<'a> {
574574
for derive in &parent_scope.derives {
575575
let parent_scope = ParentScope { derives: Vec::new(), ..*parent_scope };
576576
if let Ok((Some(ext), _)) = this.resolve_macro_path(
577-
derive, MacroKind::Derive, &parent_scope, true, true
577+
derive, MacroKind::Derive, &parent_scope, false, false
578578
) {
579579
suggestions.extend(ext.helper_attrs.iter().map(|name| {
580580
TypoSuggestion::from_res(*name, res)

src/librustc_resolve/lib.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -3664,9 +3664,7 @@ impl<'a> Resolver<'a> {
36643664
crate_lint: CrateLint,
36653665
) -> Option<PartialRes> {
36663666
let mut fin_res = None;
3667-
// FIXME: can't resolve paths in macro namespace yet, macros are
3668-
// processed by the little special hack below.
3669-
for (i, ns) in [primary_ns, TypeNS, ValueNS, /*MacroNS*/].iter().cloned().enumerate() {
3667+
for (i, ns) in [primary_ns, TypeNS, ValueNS].iter().cloned().enumerate() {
36703668
if i == 0 || ns != primary_ns {
36713669
match self.resolve_qpath(id, qself, path, ns, span, global_by_default, crate_lint) {
36723670
// If defer_to_typeck, then resolution > no resolution,
@@ -3675,21 +3673,25 @@ impl<'a> Resolver<'a> {
36753673
defer_to_typeck =>
36763674
return Some(partial_res),
36773675
partial_res => if fin_res.is_none() { fin_res = partial_res },
3678-
};
3676+
}
36793677
}
36803678
}
3681-
if primary_ns != MacroNS &&
3682-
(self.macro_names.contains(&path[0].ident.modern()) ||
3683-
self.builtin_macros.get(&path[0].ident.name).cloned()
3684-
.and_then(NameBinding::macro_kind) == Some(MacroKind::Bang) ||
3685-
self.macro_use_prelude.get(&path[0].ident.name).cloned()
3686-
.and_then(NameBinding::macro_kind) == Some(MacroKind::Bang)) {
3687-
// Return some dummy definition, it's enough for error reporting.
3688-
return Some(PartialRes::new(Res::Def(
3689-
DefKind::Macro(MacroKind::Bang),
3690-
DefId::local(CRATE_DEF_INDEX),
3691-
)));
3679+
3680+
// `MacroNS`
3681+
assert!(primary_ns != MacroNS);
3682+
if qself.is_none() {
3683+
let path_seg = |seg: &Segment| ast::PathSegment::from_ident(seg.ident);
3684+
let path = Path { segments: path.iter().map(path_seg).collect(), span };
3685+
let parent_scope =
3686+
ParentScope { module: self.current_module, ..self.dummy_parent_scope() };
3687+
for macro_kind in &[MacroKind::Bang, MacroKind::Attr, MacroKind::Derive] {
3688+
if let Ok((_, res)) = self.resolve_macro_path(&path, *macro_kind,
3689+
&parent_scope, false, false) {
3690+
return Some(PartialRes::new(res));
3691+
}
3692+
}
36923693
}
3694+
36933695
fin_res
36943696
}
36953697

src/test/ui/hygiene/rustc-macro-transparency.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ fn main() {
2626
Opaque; //~ ERROR cannot find value `Opaque` in this scope
2727

2828
transparent; // OK
29-
semitransparent; //~ ERROR cannot find value `semitransparent` in this scope
30-
opaque; //~ ERROR cannot find value `opaque` in this scope
29+
semitransparent; //~ ERROR expected value, found macro `semitransparent`
30+
opaque; //~ ERROR expected value, found macro `opaque`
3131
}

src/test/ui/hygiene/rustc-macro-transparency.stderr

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ error[E0425]: cannot find value `Opaque` in this scope
44
LL | Opaque;
55
| ^^^^^^ help: a local variable with a similar name exists: `opaque`
66

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

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

1919
error: aborting due to 3 previous errors
2020

21-
For more information about this error, try `rustc --explain E0425`.
21+
Some errors have detailed explanations: E0423, E0425.
22+
For more information about an error, try `rustc --explain E0423`.

src/test/ui/impl-trait/universal_wrong_bounds.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ fn foo(f: impl Display + Clone) -> String {
66
wants_clone(f);
77
}
88

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

13-
fn main() {
14-
}
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
error[E0405]: cannot find trait `Debug` in this scope
1+
error[E0404]: expected trait, found derive macro `Debug`
22
--> $DIR/universal_wrong_bounds.rs:9:24
33
|
44
LL | fn wants_debug(g: impl Debug) { }
5-
| ^^^^^ not found in this scope
6-
help: possible candidate is found in another module, you can import it into scope
5+
| ^^^^^ not a trait
6+
help: possible better candidate is found in another module, you can import it into scope
77
|
88
LL | use std::fmt::Debug;
99
|
1010

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

2121
error: aborting due to 2 previous errors
2222

23-
For more information about this error, try `rustc --explain E0405`.
23+
For more information about this error, try `rustc --explain E0404`.

src/test/ui/issues/issue-37534.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
struct Foo<T: ?Hash> { }
2-
//~^ ERROR cannot find trait `Hash` in this scope
2+
//~^ ERROR expected trait, found derive macro `Hash`
33
//~^^ ERROR parameter `T` is never used
44
//~^^^ WARN default bound relaxed for a type parameter, but this does nothing
55

src/test/ui/issues/issue-37534.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0405]: cannot find trait `Hash` in this scope
1+
error[E0404]: expected trait, found derive macro `Hash`
22
--> $DIR/issue-37534.rs:1:16
33
|
44
LL | struct Foo<T: ?Hash> { }
5-
| ^^^^ not found in this scope
6-
help: possible candidate is found in another module, you can import it into scope
5+
| ^^^^ not a trait
6+
help: possible better candidate is found in another module, you can import it into scope
77
|
88
LL | use std::hash::Hash;
99
|
@@ -24,5 +24,5 @@ LL | struct Foo<T: ?Hash> { }
2424

2525
error: aborting due to 2 previous errors
2626

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

src/test/ui/no-implicit-prelude-nested.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod foo {
99
mod baz {
1010
struct Test;
1111
impl Add for Test {} //~ ERROR cannot find trait `Add` in this scope
12-
impl Clone for Test {} //~ ERROR cannot find trait `Clone` in this scope
12+
impl Clone for Test {} //~ ERROR expected trait, found derive macro `Clone`
1313
impl Iterator for Test {} //~ ERROR cannot find trait `Iterator` in this scope
1414
impl ToString for Test {} //~ ERROR cannot find trait `ToString` in this scope
1515
impl Writer for Test {} //~ ERROR cannot find trait `Writer` in this scope
@@ -21,7 +21,7 @@ mod foo {
2121

2222
struct Test;
2323
impl Add for Test {} //~ ERROR cannot find trait `Add` in this scope
24-
impl Clone for Test {} //~ ERROR cannot find trait `Clone` in this scope
24+
impl Clone for Test {} //~ ERROR expected trait, found derive macro `Clone`
2525
impl Iterator for Test {} //~ ERROR cannot find trait `Iterator` in this scope
2626
impl ToString for Test {} //~ ERROR cannot find trait `ToString` in this scope
2727
impl Writer for Test {} //~ ERROR cannot find trait `Writer` in this scope
@@ -36,7 +36,7 @@ fn qux() {
3636
mod qux_inner {
3737
struct Test;
3838
impl Add for Test {} //~ ERROR cannot find trait `Add` in this scope
39-
impl Clone for Test {} //~ ERROR cannot find trait `Clone` in this scope
39+
impl Clone for Test {} //~ ERROR expected trait, found derive macro `Clone`
4040
impl Iterator for Test {} //~ ERROR cannot find trait `Iterator` in this scope
4141
impl ToString for Test {} //~ ERROR cannot find trait `ToString` in this scope
4242
impl Writer for Test {} //~ ERROR cannot find trait `Writer` in this scope

src/test/ui/no-implicit-prelude-nested.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ help: possible candidate is found in another module, you can import it into scop
88
LL | use std::ops::Add;
99
|
1010

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

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

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

193193
error: aborting due to 18 previous errors
194194

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

src/test/ui/no-implicit-prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
struct Test;
1010
impl Add for Test {} //~ ERROR cannot find trait `Add` in this scope
11-
impl Clone for Test {} //~ ERROR cannot find trait `Clone` in this scope
11+
impl Clone for Test {} //~ ERROR expected trait, found derive macro `Clone`
1212
impl Iterator for Test {} //~ ERROR cannot find trait `Iterator` in this scope
1313
impl ToString for Test {} //~ ERROR cannot find trait `ToString` in this scope
1414
impl Writer for Test {} //~ ERROR cannot find trait `Writer` in this scope

src/test/ui/no-implicit-prelude.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ help: possible candidate is found in another module, you can import it into scop
88
LL | use std::ops::Add;
99
|
1010

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

6565
error: aborting due to 6 previous errors
6666

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

0 commit comments

Comments
 (0)