Skip to content

Commit

Permalink
resolve: Suggest crate:: for resolving ambiguities when appropriate
Browse files Browse the repository at this point in the history
More precise spans for ambiguities from macros
  • Loading branch information
petrochenkov committed Nov 25, 2018
1 parent 3b58880 commit 581b683
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 50 deletions.
22 changes: 16 additions & 6 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,7 @@ impl AmbiguityKind {
/// Miscellaneous bits of metadata for better ambiguity error reporting.
#[derive(Clone, Copy, PartialEq)]
enum AmbiguityErrorMisc {
SuggestCrate,
SuggestSelf,
FromPrelude,
None,
Expand Down Expand Up @@ -4872,12 +4873,21 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
`{ident}` to disambiguate", ident = ident))
}
if b.is_extern_crate() && ident.span.rust_2018() {
help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously",
ident = ident, thing = b.descr()))
}
if misc == AmbiguityErrorMisc::SuggestSelf {
help_msgs.push(format!("use `self::{ident}` to refer to this {thing} unambiguously",
ident = ident, thing = b.descr()))
help_msgs.push(format!(
"use `::{ident}` to refer to this {thing} unambiguously",
ident = ident, thing = b.descr(),
))
}
if misc == AmbiguityErrorMisc::SuggestCrate {
help_msgs.push(format!(
"use `crate::{ident}` to refer to this {thing} unambiguously",
ident = ident, thing = b.descr(),
))
} else if misc == AmbiguityErrorMisc::SuggestSelf {
help_msgs.push(format!(
"use `self::{ident}` to refer to this {thing} unambiguously",
ident = ident, thing = b.descr(),
))
}

if b.span.is_dummy() {
Expand Down
25 changes: 15 additions & 10 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use syntax_pos::{Span, DUMMY_SP};
use errors::Applicability;

use std::cell::Cell;
use std::mem;
use std::{mem, ptr};
use rustc_data_structures::sync::Lrc;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -594,11 +594,12 @@ impl<'a, 'cl> Resolver<'a, 'cl> {

bitflags! {
struct Flags: u8 {
const MACRO_RULES = 1 << 0;
const MODULE = 1 << 1;
const PRELUDE = 1 << 2;
const MISC_SUGGEST_SELF = 1 << 3;
const MISC_FROM_PRELUDE = 1 << 4;
const MACRO_RULES = 1 << 0;
const MODULE = 1 << 1;
const PRELUDE = 1 << 2;
const MISC_SUGGEST_CRATE = 1 << 3;
const MISC_SUGGEST_SELF = 1 << 4;
const MISC_FROM_PRELUDE = 1 << 5;
}
}

Expand Down Expand Up @@ -684,7 +685,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
path_span,
);
match binding {
Ok(binding) => Ok((binding, Flags::MODULE)),
Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
Err((Determinacy::Undetermined, Weak::No)) =>
return Err(Determinacy::determined(force)),
Err((Determinacy::Undetermined, Weak::Yes)) =>
Expand All @@ -706,7 +707,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
self.current_module = orig_current_module;
match binding {
Ok(binding) => {
let misc_flags = if module.is_normal() {
let misc_flags = if ptr::eq(module, self.graph_root) {
Flags::MISC_SUGGEST_CRATE
} else if module.is_normal() {
Flags::MISC_SUGGEST_SELF
} else {
Flags::empty()
Expand Down Expand Up @@ -857,7 +860,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
None
};
if let Some(kind) = ambiguity_error_kind {
let misc = |f: Flags| if f.contains(Flags::MISC_SUGGEST_SELF) {
let misc = |f: Flags| if f.contains(Flags::MISC_SUGGEST_CRATE) {
AmbiguityErrorMisc::SuggestCrate
} else if f.contains(Flags::MISC_SUGGEST_SELF) {
AmbiguityErrorMisc::SuggestSelf
} else if f.contains(Flags::MISC_FROM_PRELUDE) {
AmbiguityErrorMisc::FromPrelude
Expand All @@ -866,7 +871,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
};
self.ambiguity_errors.push(AmbiguityError {
kind,
ident,
ident: orig_ident,
b1: innermost_binding,
b2: binding,
misc1: misc(innermost_flags),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ note: `helper` could also refer to the attribute macro imported here
|
LL | use plugin::helper;
| ^^^^^^^^^^^^^^
= help: use `self::helper` to refer to this attribute macro unambiguously
= help: use `crate::helper` to refer to this attribute macro unambiguously

error: aborting due to previous error

Expand Down
10 changes: 5 additions & 5 deletions src/test/ui-fulldeps/proc-macro/ambiguous-builtin-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ note: `repr` could also refer to the attribute macro imported here
|
LL | use builtin_attrs::*;
| ^^^^^^^^^^^^^^^^
= help: use `self::repr` to refer to this attribute macro unambiguously
= help: use `crate::repr` to refer to this attribute macro unambiguously

error[E0659]: `repr` is ambiguous (built-in attribute vs any other name)
--> $DIR/ambiguous-builtin-attrs.rs:11:19
Expand All @@ -30,7 +30,7 @@ note: `repr` could also refer to the attribute macro imported here
|
LL | use builtin_attrs::*;
| ^^^^^^^^^^^^^^^^
= help: use `self::repr` to refer to this attribute macro unambiguously
= help: use `crate::repr` to refer to this attribute macro unambiguously

error[E0659]: `repr` is ambiguous (built-in attribute vs any other name)
--> $DIR/ambiguous-builtin-attrs.rs:20:34
Expand All @@ -44,7 +44,7 @@ note: `repr` could also refer to the attribute macro imported here
|
LL | use builtin_attrs::*;
| ^^^^^^^^^^^^^^^^
= help: use `self::repr` to refer to this attribute macro unambiguously
= help: use `crate::repr` to refer to this attribute macro unambiguously

error[E0659]: `repr` is ambiguous (built-in attribute vs any other name)
--> $DIR/ambiguous-builtin-attrs.rs:22:11
Expand All @@ -58,7 +58,7 @@ note: `repr` could also refer to the attribute macro imported here
|
LL | use builtin_attrs::*;
| ^^^^^^^^^^^^^^^^
= help: use `self::repr` to refer to this attribute macro unambiguously
= help: use `crate::repr` to refer to this attribute macro unambiguously

error[E0659]: `feature` is ambiguous (built-in attribute vs any other name)
--> $DIR/ambiguous-builtin-attrs.rs:3:4
Expand All @@ -72,7 +72,7 @@ note: `feature` could also refer to the attribute macro imported here
|
LL | use builtin_attrs::*;
| ^^^^^^^^^^^^^^^^
= help: use `self::feature` to refer to this attribute macro unambiguously
= help: use `crate::feature` to refer to this attribute macro unambiguously

error: aborting due to 6 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ note: `my_attr` could also refer to the attribute macro imported here
|
LL | use derive_helper_shadowing::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use `self::my_attr` to refer to this attribute macro unambiguously
= help: use `crate::my_attr` to refer to this attribute macro unambiguously

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// edition:2018
// compile-flags:--extern edition_imports_2015
// aux-build:edition-imports-2015.rs
// error-pattern: `Ambiguous` is ambiguous
// error-pattern: `edition_imports_2015` is ambiguous

mod edition_imports_2015 {
pub struct Path;
Expand All @@ -14,7 +12,8 @@ mod check {
pub struct Ambiguous {}

fn check() {
edition_imports_2015::gen_ambiguous!();
edition_imports_2015::gen_ambiguous!(); //~ ERROR `Ambiguous` is ambiguous
//~| ERROR `edition_imports_2015` is ambiguous
}
}

Expand Down
23 changes: 13 additions & 10 deletions src/test/ui/editions/edition-imports-virtual-2015-ambiguity.stderr
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
error[E0659]: `Ambiguous` is ambiguous (name vs any other name during import resolution)
--> <::edition_imports_2015::gen_ambiguous macros>:1:15
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:15:9
|
LL | ( ) => { use Ambiguous ; type A = :: edition_imports_2015 :: Path ; }
| ^^^^^^^^^ ambiguous name
LL | edition_imports_2015::gen_ambiguous!(); //~ ERROR `Ambiguous` is ambiguous
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ambiguous name
|
note: `Ambiguous` could refer to the struct defined here
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:11:1
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:9:1
|
LL | pub struct Ambiguous {}
| ^^^^^^^^^^^^^^^^^^^^^^^
= help: use `crate::Ambiguous` to refer to this struct unambiguously
note: `Ambiguous` could also refer to the struct defined here
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:14:5
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:12:5
|
LL | pub struct Ambiguous {}
| ^^^^^^^^^^^^^^^^^^^^^^^
= help: use `self::Ambiguous` to refer to this struct unambiguously
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0659]: `edition_imports_2015` is ambiguous (name in the crate root vs extern crate during absolute path resolution)
--> <::edition_imports_2015::gen_ambiguous macros>:1:39
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:15:9
|
LL | ( ) => { use Ambiguous ; type A = :: edition_imports_2015 :: Path ; }
| ^^^^^^^^^^^^^^^^^^^^ ambiguous name
LL | edition_imports_2015::gen_ambiguous!(); //~ ERROR `Ambiguous` is ambiguous
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ambiguous name
|
= note: `edition_imports_2015` could refer to an extern crate passed with `--extern`
= help: use `::edition_imports_2015` to refer to this extern crate unambiguously
note: `edition_imports_2015` could also refer to the module defined here
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:7:1
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:5:1
|
LL | / mod edition_imports_2015 {
LL | | pub struct Path;
LL | | }
| |_^
= help: use `crate::edition_imports_2015` to refer to this module unambiguously
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to 2 previous errors

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/imports/local-modularized-tricky-fail-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod inner2 {

fn main() {
panic!(); //~ ERROR `panic` is ambiguous
//~| ERROR `panic` is ambiguous
}

mod inner3 {
Expand Down
15 changes: 8 additions & 7 deletions src/test/ui/imports/local-modularized-tricky-fail-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LL | use inner1::*;
= help: consider adding an explicit import of `exported` to disambiguate

error[E0659]: `include` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
--> $DIR/local-modularized-tricky-fail-1.rs:56:1
--> $DIR/local-modularized-tricky-fail-1.rs:57:1
|
LL | include!(); //~ ERROR `include` is ambiguous
| ^^^^^^^ ambiguous name
Expand All @@ -38,7 +38,7 @@ LL | | }
...
LL | define_include!();
| ------------------ in this macro invocation
= help: use `self::include` to refer to this macro unambiguously
= help: use `crate::include` to refer to this macro unambiguously

error[E0659]: `panic` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
--> $DIR/local-modularized-tricky-fail-1.rs:45:5
Expand All @@ -57,13 +57,13 @@ LL | | }
...
LL | define_panic!();
| ---------------- in this macro invocation
= help: use `self::panic` to refer to this macro unambiguously
= help: use `crate::panic` to refer to this macro unambiguously

error[E0659]: `panic` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
--> <::std::macros::panic macros>:1:13
--> $DIR/local-modularized-tricky-fail-1.rs:45:5
|
LL | ( ) => ( { panic ! ( "explicit panic" ) } ) ; ( $ msg : expr ) => (
| ^^^^^ ambiguous name
LL | panic!(); //~ ERROR `panic` is ambiguous
| ^^^^^^^^^ ambiguous name
|
= note: `panic` could refer to a macro from prelude
note: `panic` could also refer to the macro defined here
Expand All @@ -76,7 +76,8 @@ LL | | }
...
LL | define_panic!();
| ---------------- in this macro invocation
= help: use `self::panic` to refer to this macro unambiguously
= help: use `crate::panic` to refer to this macro unambiguously
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to 4 previous errors

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/imports/macro-paths.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ LL | / pub mod baz {
LL | | pub use two_macros::m;
LL | | }
| |_^
= help: use `self::baz` to refer to this module unambiguously
= help: use `crate::baz` to refer to this module unambiguously

error: aborting due to 2 previous errors

Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/macros/restricted-shadowing-legacy.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
|
LL | m!(); //~ ERROR `m` is ambiguous
| ^ ambiguous name
...
LL | include!();
| ----------- in this macro invocation
|
note: `m` could refer to the macro defined here
--> $DIR/restricted-shadowing-legacy.rs:88:9
Expand All @@ -26,6 +29,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
|
LL | macro_rules! gen_invoc { () => { m!() } } //~ ERROR `m` is ambiguous
| ^ ambiguous name
...
LL | include!();
| ----------- in this macro invocation
|
note: `m` could refer to the macro defined here
--> $DIR/restricted-shadowing-legacy.rs:88:9
Expand All @@ -49,6 +55,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
|
LL | m!(); //~ ERROR `m` is ambiguous
| ^ ambiguous name
...
LL | include!();
| ----------- in this macro invocation
|
note: `m` could refer to the macro defined here
--> $DIR/restricted-shadowing-legacy.rs:88:9
Expand All @@ -72,6 +81,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
|
LL | m!(); //~ ERROR `m` is ambiguous
| ^ ambiguous name
...
LL | include!();
| ----------- in this macro invocation
|
note: `m` could refer to the macro defined here
--> $DIR/restricted-shadowing-legacy.rs:88:9
Expand All @@ -95,6 +107,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
|
LL | m!(); //~ ERROR `m` is ambiguous
| ^ ambiguous name
...
LL | include!();
| ----------- in this macro invocation
|
note: `m` could refer to the macro defined here
--> $DIR/restricted-shadowing-legacy.rs:88:9
Expand All @@ -118,6 +133,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
|
LL | macro_rules! gen_invoc { () => { m!() } } //~ ERROR `m` is ambiguous
| ^ ambiguous name
...
LL | include!();
| ----------- in this macro invocation
|
note: `m` could refer to the macro defined here
--> $DIR/restricted-shadowing-legacy.rs:88:9
Expand All @@ -141,6 +159,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
|
LL | m!(); //~ ERROR `m` is ambiguous
| ^ ambiguous name
...
LL | include!();
| ----------- in this macro invocation
|
note: `m` could refer to the macro defined here
--> $DIR/restricted-shadowing-legacy.rs:88:9
Expand All @@ -164,6 +185,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
|
LL | macro_rules! gen_invoc { () => { m!() } } //~ ERROR `m` is ambiguous
| ^ ambiguous name
...
LL | include!();
| ----------- in this macro invocation
|
note: `m` could refer to the macro defined here
--> $DIR/restricted-shadowing-legacy.rs:88:9
Expand Down
Loading

0 comments on commit 581b683

Please sign in to comment.