Skip to content

Commit bd12d15

Browse files
author
Yiming Lei
committed
add function to tell if the current ambiguity error matches a previous one in ambiguity_errors
if 2 errors of the kind and ident and span of the ident, b1, b2 and misc1 misc2 are the same then these 2 ambiguity errors matched prevent identical ambiguity error from pushing into vector of ambiguity_errors this will fix #105177
1 parent ba64ba8 commit bd12d15

File tree

5 files changed

+32
-56
lines changed

5 files changed

+32
-56
lines changed

compiler/rustc_resolve/src/lib.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -1686,21 +1686,43 @@ impl<'a> Resolver<'a> {
16861686
.or_insert_with(|| self.arenas.alloc_name_resolution())
16871687
}
16881688

1689+
// Test if AmbiguityError ambi is any identical to any one inside ambiguity_errors
1690+
fn matches_previous_ambiguity_error(&mut self, ambi: &AmbiguityError<'_>) -> bool {
1691+
for ambiguity_error in &self.ambiguity_errors {
1692+
// if the span location and ident as well as its span are the same
1693+
if ambiguity_error.kind == ambi.kind
1694+
&& ambiguity_error.ident == ambi.ident
1695+
&& ambiguity_error.ident.span == ambi.ident.span
1696+
&& ambiguity_error.b1.span == ambi.b1.span
1697+
&& ambiguity_error.b2.span == ambi.b2.span
1698+
&& ambiguity_error.misc1 == ambi.misc1
1699+
&& ambiguity_error.misc2 == ambi.misc2
1700+
{
1701+
return true;
1702+
}
1703+
}
1704+
false
1705+
}
1706+
16891707
fn record_use(
16901708
&mut self,
16911709
ident: Ident,
16921710
used_binding: &'a NameBinding<'a>,
16931711
is_lexical_scope: bool,
16941712
) {
16951713
if let Some((b2, kind)) = used_binding.ambiguity {
1696-
self.ambiguity_errors.push(AmbiguityError {
1714+
let ambiguity_error = AmbiguityError {
16971715
kind,
16981716
ident,
16991717
b1: used_binding,
17001718
b2,
17011719
misc1: AmbiguityErrorMisc::None,
17021720
misc2: AmbiguityErrorMisc::None,
1703-
});
1721+
};
1722+
if !self.matches_previous_ambiguity_error(&ambiguity_error) {
1723+
// avoid dumplicated span information to be emitt out
1724+
self.ambiguity_errors.push(ambiguity_error);
1725+
}
17041726
}
17051727
if let NameBindingKind::Import { import, binding, ref used } = used_binding.kind {
17061728
// Avoid marking `extern crate` items that refer to a name from extern prelude,

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

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ mod inner1 {
2626
}
2727

2828
exported!(); //~ ERROR `exported` is ambiguous
29-
//~| ERROR `exported` is ambiguous
3029

3130
mod inner2 {
3231
define_exported!();

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

+3-28
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,8 @@ LL | use inner1::*;
2323
= help: consider adding an explicit import of `exported` to disambiguate
2424
= note: this error originates in the macro `define_exported` (in Nightly builds, run with -Z macro-backtrace for more info)
2525

26-
error[E0659]: `exported` is ambiguous
27-
--> $DIR/local-modularized-tricky-fail-1.rs:28:1
28-
|
29-
LL | exported!();
30-
| ^^^^^^^^ ambiguous name
31-
|
32-
= note: ambiguous because of a conflict between a name from a glob import and a macro-expanded name in the same module during import or macro resolution
33-
note: `exported` could refer to the macro defined here
34-
--> $DIR/local-modularized-tricky-fail-1.rs:5:5
35-
|
36-
LL | / macro_rules! exported {
37-
LL | | () => ()
38-
LL | | }
39-
| |_____^
40-
...
41-
LL | define_exported!();
42-
| ------------------ in this macro invocation
43-
note: `exported` could also refer to the macro imported here
44-
--> $DIR/local-modularized-tricky-fail-1.rs:22:5
45-
|
46-
LL | use inner1::*;
47-
| ^^^^^^^^^
48-
= help: consider adding an explicit import of `exported` to disambiguate
49-
= note: this error originates in the macro `define_exported` (in Nightly builds, run with -Z macro-backtrace for more info)
50-
5126
error[E0659]: `panic` is ambiguous
52-
--> $DIR/local-modularized-tricky-fail-1.rs:36:5
27+
--> $DIR/local-modularized-tricky-fail-1.rs:35:5
5328
|
5429
LL | panic!();
5530
| ^^^^^ ambiguous name
@@ -70,7 +45,7 @@ LL | define_panic!();
7045
= note: this error originates in the macro `define_panic` (in Nightly builds, run with -Z macro-backtrace for more info)
7146

7247
error[E0659]: `include` is ambiguous
73-
--> $DIR/local-modularized-tricky-fail-1.rs:47:1
48+
--> $DIR/local-modularized-tricky-fail-1.rs:46:1
7449
|
7550
LL | include!();
7651
| ^^^^^^^ ambiguous name
@@ -90,6 +65,6 @@ LL | define_include!();
9065
= help: use `crate::include` to refer to this macro unambiguously
9166
= note: this error originates in the macro `define_include` (in Nightly builds, run with -Z macro-backtrace for more info)
9267

93-
error: aborting due to 4 previous errors
68+
error: aborting due to 3 previous errors
9469

9570
For more information about this error, try `rustc --explain E0659`.

src/test/ui/imports/macros.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ mod m1 {
1414
mod m2 {
1515
use two_macros::*;
1616
m! { //~ ERROR ambiguous
17-
//~| ERROR ambiguous
1817
use foo::m;
1918
}
2019
}

src/test/ui/imports/macros.stderr

+5-24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | m! {
66
|
77
= note: ambiguous because of a conflict between a name from a glob import and a macro-expanded name in the same module during import or macro resolution
88
note: `m` could refer to the macro imported here
9-
--> $DIR/macros.rs:18:13
9+
--> $DIR/macros.rs:17:13
1010
|
1111
LL | use foo::m;
1212
| ^^^^^^
@@ -18,43 +18,24 @@ LL | use two_macros::*;
1818
= help: consider adding an explicit import of `m` to disambiguate
1919

2020
error[E0659]: `m` is ambiguous
21-
--> $DIR/macros.rs:16:5
22-
|
23-
LL | m! {
24-
| ^ ambiguous name
25-
|
26-
= note: ambiguous because of a conflict between a name from a glob import and a macro-expanded name in the same module during import or macro resolution
27-
note: `m` could refer to the macro imported here
28-
--> $DIR/macros.rs:18:13
29-
|
30-
LL | use foo::m;
31-
| ^^^^^^
32-
note: `m` could also refer to the macro imported here
33-
--> $DIR/macros.rs:15:9
34-
|
35-
LL | use two_macros::*;
36-
| ^^^^^^^^^^^^^
37-
= help: consider adding an explicit import of `m` to disambiguate
38-
39-
error[E0659]: `m` is ambiguous
40-
--> $DIR/macros.rs:30:9
21+
--> $DIR/macros.rs:29:9
4122
|
4223
LL | m! {
4324
| ^ ambiguous name
4425
|
4526
= note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
4627
note: `m` could refer to the macro imported here
47-
--> $DIR/macros.rs:31:17
28+
--> $DIR/macros.rs:30:17
4829
|
4930
LL | use two_macros::n as m;
5031
| ^^^^^^^^^^^^^^^^^^
5132
note: `m` could also refer to the macro imported here
52-
--> $DIR/macros.rs:23:9
33+
--> $DIR/macros.rs:22:9
5334
|
5435
LL | use two_macros::m;
5536
| ^^^^^^^^^^^^^
5637
= help: use `self::m` to refer to this macro unambiguously
5738

58-
error: aborting due to 3 previous errors
39+
error: aborting due to 2 previous errors
5940

6041
For more information about this error, try `rustc --explain E0659`.

0 commit comments

Comments
 (0)