Skip to content

Commit 6f13708

Browse files
committed
resolve: Suggest crate:: for resolving ambiguities when appropriate
More precise spans for ambiguities from macros
1 parent d1862b4 commit 6f13708

16 files changed

+111
-50
lines changed

src/librustc_resolve/lib.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,7 @@ impl AmbiguityKind {
12921292
/// Miscellaneous bits of metadata for better ambiguity error reporting.
12931293
#[derive(Clone, Copy, PartialEq)]
12941294
enum AmbiguityErrorMisc {
1295+
SuggestCrate,
12951296
SuggestSelf,
12961297
FromPrelude,
12971298
None,
@@ -4870,12 +4871,21 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
48704871
`{ident}` to disambiguate", ident = ident))
48714872
}
48724873
if b.is_extern_crate() && ident.span.rust_2018() {
4873-
help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously",
4874-
ident = ident, thing = b.descr()))
4875-
}
4876-
if misc == AmbiguityErrorMisc::SuggestSelf {
4877-
help_msgs.push(format!("use `self::{ident}` to refer to this {thing} unambiguously",
4878-
ident = ident, thing = b.descr()))
4874+
help_msgs.push(format!(
4875+
"use `::{ident}` to refer to this {thing} unambiguously",
4876+
ident = ident, thing = b.descr(),
4877+
))
4878+
}
4879+
if misc == AmbiguityErrorMisc::SuggestCrate {
4880+
help_msgs.push(format!(
4881+
"use `crate::{ident}` to refer to this {thing} unambiguously",
4882+
ident = ident, thing = b.descr(),
4883+
))
4884+
} else if misc == AmbiguityErrorMisc::SuggestSelf {
4885+
help_msgs.push(format!(
4886+
"use `self::{ident}` to refer to this {thing} unambiguously",
4887+
ident = ident, thing = b.descr(),
4888+
))
48794889
}
48804890

48814891
if b.span.is_dummy() {

src/librustc_resolve/macros.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use syntax_pos::{Span, DUMMY_SP};
4242
use errors::Applicability;
4343

4444
use std::cell::Cell;
45-
use std::mem;
45+
use std::{mem, ptr};
4646
use rustc_data_structures::sync::Lrc;
4747

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

595595
bitflags! {
596596
struct Flags: u8 {
597-
const MACRO_RULES = 1 << 0;
598-
const MODULE = 1 << 1;
599-
const PRELUDE = 1 << 2;
600-
const MISC_SUGGEST_SELF = 1 << 3;
601-
const MISC_FROM_PRELUDE = 1 << 4;
597+
const MACRO_RULES = 1 << 0;
598+
const MODULE = 1 << 1;
599+
const PRELUDE = 1 << 2;
600+
const MISC_SUGGEST_CRATE = 1 << 3;
601+
const MISC_SUGGEST_SELF = 1 << 4;
602+
const MISC_FROM_PRELUDE = 1 << 5;
602603
}
603604
}
604605

@@ -684,7 +685,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
684685
path_span,
685686
);
686687
match binding {
687-
Ok(binding) => Ok((binding, Flags::MODULE)),
688+
Ok(binding) => Ok((binding, Flags::MODULE | Flags::MISC_SUGGEST_CRATE)),
688689
Err((Determinacy::Undetermined, Weak::No)) =>
689690
return Err(Determinacy::determined(force)),
690691
Err((Determinacy::Undetermined, Weak::Yes)) =>
@@ -706,7 +707,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
706707
self.current_module = orig_current_module;
707708
match binding {
708709
Ok(binding) => {
709-
let misc_flags = if module.is_normal() {
710+
let misc_flags = if ptr::eq(module, self.graph_root) {
711+
Flags::MISC_SUGGEST_CRATE
712+
} else if module.is_normal() {
710713
Flags::MISC_SUGGEST_SELF
711714
} else {
712715
Flags::empty()
@@ -857,7 +860,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
857860
None
858861
};
859862
if let Some(kind) = ambiguity_error_kind {
860-
let misc = |f: Flags| if f.contains(Flags::MISC_SUGGEST_SELF) {
863+
let misc = |f: Flags| if f.contains(Flags::MISC_SUGGEST_CRATE) {
864+
AmbiguityErrorMisc::SuggestCrate
865+
} else if f.contains(Flags::MISC_SUGGEST_SELF) {
861866
AmbiguityErrorMisc::SuggestSelf
862867
} else if f.contains(Flags::MISC_FROM_PRELUDE) {
863868
AmbiguityErrorMisc::FromPrelude
@@ -866,7 +871,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
866871
};
867872
self.ambiguity_errors.push(AmbiguityError {
868873
kind,
869-
ident,
874+
ident: orig_ident,
870875
b1: innermost_binding,
871876
b2: binding,
872877
misc1: misc(innermost_flags),

src/test/ui-fulldeps/custom-derive/helper-attr-blocked-by-import-ambig.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ note: `helper` could also refer to the attribute macro imported here
1414
|
1515
LL | use plugin::helper;
1616
| ^^^^^^^^^^^^^^
17-
= help: use `self::helper` to refer to this attribute macro unambiguously
17+
= help: use `crate::helper` to refer to this attribute macro unambiguously
1818

1919
error: aborting due to previous error
2020

src/test/ui-fulldeps/proc-macro/ambiguous-builtin-attrs.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ note: `repr` could also refer to the attribute macro imported here
1616
|
1717
LL | use builtin_attrs::*;
1818
| ^^^^^^^^^^^^^^^^
19-
= help: use `self::repr` to refer to this attribute macro unambiguously
19+
= help: use `crate::repr` to refer to this attribute macro unambiguously
2020

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

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

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

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

7777
error: aborting due to 6 previous errors
7878

src/test/ui-fulldeps/proc-macro/derive-helper-shadowing.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ note: `my_attr` could also refer to the attribute macro imported here
1414
|
1515
LL | use derive_helper_shadowing::*;
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
17-
= help: use `self::my_attr` to refer to this attribute macro unambiguously
17+
= help: use `crate::my_attr` to refer to this attribute macro unambiguously
1818

1919
error: aborting due to previous error
2020

src/test/ui/editions/edition-imports-virtual-2015-ambiguity.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// edition:2018
22
// compile-flags:--extern edition_imports_2015
33
// aux-build:edition-imports-2015.rs
4-
// error-pattern: `Ambiguous` is ambiguous
5-
// error-pattern: `edition_imports_2015` is ambiguous
64

75
mod edition_imports_2015 {
86
pub struct Path;
@@ -14,7 +12,8 @@ mod check {
1412
pub struct Ambiguous {}
1513

1614
fn check() {
17-
edition_imports_2015::gen_ambiguous!();
15+
edition_imports_2015::gen_ambiguous!(); //~ ERROR `Ambiguous` is ambiguous
16+
//~| ERROR `edition_imports_2015` is ambiguous
1817
}
1918
}
2019

src/test/ui/editions/edition-imports-virtual-2015-ambiguity.stderr

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
error[E0659]: `Ambiguous` is ambiguous (name vs any other name during import resolution)
2-
--> <::edition_imports_2015::gen_ambiguous macros>:1:15
2+
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:15:9
33
|
4-
LL | ( ) => { use Ambiguous ; type A = :: edition_imports_2015 :: Path ; }
5-
| ^^^^^^^^^ ambiguous name
4+
LL | edition_imports_2015::gen_ambiguous!(); //~ ERROR `Ambiguous` is ambiguous
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ambiguous name
66
|
77
note: `Ambiguous` could refer to the struct defined here
8-
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:11:1
8+
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:9:1
99
|
1010
LL | pub struct Ambiguous {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^
12+
= help: use `crate::Ambiguous` to refer to this struct unambiguously
1213
note: `Ambiguous` could also refer to the struct defined here
13-
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:14:5
14+
--> $DIR/edition-imports-virtual-2015-ambiguity.rs:12:5
1415
|
1516
LL | pub struct Ambiguous {}
1617
| ^^^^^^^^^^^^^^^^^^^^^^^
1718
= help: use `self::Ambiguous` to refer to this struct unambiguously
19+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
1820

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

3538
error: aborting due to 2 previous errors
3639

src/test/ui/imports/local-modularized-tricky-fail-1.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod inner2 {
4343

4444
fn main() {
4545
panic!(); //~ ERROR `panic` is ambiguous
46+
//~| ERROR `panic` is ambiguous
4647
}
4748

4849
mod inner3 {

src/test/ui/imports/local-modularized-tricky-fail-1.stderr

+8-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LL | use inner1::*;
2222
= help: consider adding an explicit import of `exported` to disambiguate
2323

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

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

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

8182
error: aborting due to 4 previous errors
8283

src/test/ui/imports/macro-paths.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ LL | / pub mod baz {
3434
LL | | pub use two_macros::m;
3535
LL | | }
3636
| |_^
37-
= help: use `self::baz` to refer to this module unambiguously
37+
= help: use `crate::baz` to refer to this module unambiguously
3838

3939
error: aborting due to 2 previous errors
4040

src/test/ui/macros/restricted-shadowing-legacy.stderr

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
33
|
44
LL | m!(); //~ ERROR `m` is ambiguous
55
| ^ ambiguous name
6+
...
7+
LL | include!();
8+
| ----------- in this macro invocation
69
|
710
note: `m` could refer to the macro defined here
811
--> $DIR/restricted-shadowing-legacy.rs:88:9
@@ -26,6 +29,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
2629
|
2730
LL | macro_rules! gen_invoc { () => { m!() } } //~ ERROR `m` is ambiguous
2831
| ^ ambiguous name
32+
...
33+
LL | include!();
34+
| ----------- in this macro invocation
2935
|
3036
note: `m` could refer to the macro defined here
3137
--> $DIR/restricted-shadowing-legacy.rs:88:9
@@ -49,6 +55,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
4955
|
5056
LL | m!(); //~ ERROR `m` is ambiguous
5157
| ^ ambiguous name
58+
...
59+
LL | include!();
60+
| ----------- in this macro invocation
5261
|
5362
note: `m` could refer to the macro defined here
5463
--> $DIR/restricted-shadowing-legacy.rs:88:9
@@ -72,6 +81,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
7281
|
7382
LL | m!(); //~ ERROR `m` is ambiguous
7483
| ^ ambiguous name
84+
...
85+
LL | include!();
86+
| ----------- in this macro invocation
7587
|
7688
note: `m` could refer to the macro defined here
7789
--> $DIR/restricted-shadowing-legacy.rs:88:9
@@ -95,6 +107,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
95107
|
96108
LL | m!(); //~ ERROR `m` is ambiguous
97109
| ^ ambiguous name
110+
...
111+
LL | include!();
112+
| ----------- in this macro invocation
98113
|
99114
note: `m` could refer to the macro defined here
100115
--> $DIR/restricted-shadowing-legacy.rs:88:9
@@ -118,6 +133,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
118133
|
119134
LL | macro_rules! gen_invoc { () => { m!() } } //~ ERROR `m` is ambiguous
120135
| ^ ambiguous name
136+
...
137+
LL | include!();
138+
| ----------- in this macro invocation
121139
|
122140
note: `m` could refer to the macro defined here
123141
--> $DIR/restricted-shadowing-legacy.rs:88:9
@@ -141,6 +159,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
141159
|
142160
LL | m!(); //~ ERROR `m` is ambiguous
143161
| ^ ambiguous name
162+
...
163+
LL | include!();
164+
| ----------- in this macro invocation
144165
|
145166
note: `m` could refer to the macro defined here
146167
--> $DIR/restricted-shadowing-legacy.rs:88:9
@@ -164,6 +185,9 @@ error[E0659]: `m` is ambiguous (macro-expanded name vs less macro-expanded name
164185
|
165186
LL | macro_rules! gen_invoc { () => { m!() } } //~ ERROR `m` is ambiguous
166187
| ^ ambiguous name
188+
...
189+
LL | include!();
190+
| ----------- in this macro invocation
167191
|
168192
note: `m` could refer to the macro defined here
169193
--> $DIR/restricted-shadowing-legacy.rs:88:9

0 commit comments

Comments
 (0)