resolve: Attempt to resolve unresolved paths in macro namespace

This commit is contained in:
Vadim Petrochenkov 2019-07-15 03:28:17 +03:00
parent 3845a08a55
commit a7726ce086
12 changed files with 62 additions and 60 deletions

View file

@ -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",
@ -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)

View file

@ -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,
@ -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
}

View file

@ -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`
}

View file

@ -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`.

View file

@ -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() {}

View file

@ -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`.

View file

@ -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

View file

@ -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;
|
@ -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`.

View file

@ -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
@ -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
@ -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

View file

@ -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;
|
@ -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;
|
@ -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;
|
@ -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`.

View file

@ -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

View file

@ -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;
|
@ -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`.