Skip to content

Commit d58b0c4

Browse files
committed
resolve: Improve diagnostics for ambiguities in extern prelude
1 parent f8849ed commit d58b0c4

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18881888
"consider adding an explicit import of `{ident}` to disambiguate"
18891889
))
18901890
}
1891-
if b.is_extern_crate() && ident.span.at_least_rust_2018() {
1891+
if kind != AmbiguityKind::ExternPrelude
1892+
&& b.is_extern_crate()
1893+
&& ident.span.at_least_rust_2018()
1894+
{
18921895
help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously"))
18931896
}
18941897
match misc {

compiler/rustc_resolve/src/ident.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
402402
struct Flags: u8 {
403403
const MACRO_RULES = 1 << 0;
404404
const MODULE = 1 << 1;
405-
const MISC_SUGGEST_CRATE = 1 << 2;
406-
const MISC_SUGGEST_SELF = 1 << 3;
407-
const MISC_FROM_PRELUDE = 1 << 4;
405+
const EXTERN_PRELUDE = 1 << 2;
406+
const MISC_SUGGEST_CRATE = 1 << 3;
407+
const MISC_SUGGEST_SELF = 1 << 4;
408+
const MISC_FROM_PRELUDE = 1 << 5;
408409
}
409410
}
410411

@@ -570,15 +571,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
570571
},
571572
Scope::ExternPreludeItems => {
572573
match this.reborrow().extern_prelude_get_item(ident, finalize.is_some()) {
573-
Some(binding) => Ok((binding, Flags::empty())),
574+
Some(binding) => Ok((binding, Flags::EXTERN_PRELUDE)),
574575
None => Err(Determinacy::determined(
575576
this.graph_root.unexpanded_invocations.borrow().is_empty(),
576577
)),
577578
}
578579
}
579580
Scope::ExternPreludeFlags => {
580581
match this.extern_prelude_get_flag(ident, finalize.is_some()) {
581-
Some(binding) => Ok((binding, Flags::empty())),
582+
Some(binding) => Ok((binding, Flags::EXTERN_PRELUDE)),
582583
None => Err(Determinacy::Determined),
583584
}
584585
}
@@ -690,7 +691,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
690691
} else if innermost_binding
691692
.may_appear_after(parent_scope.expansion, binding)
692693
{
693-
Some(AmbiguityKind::MoreExpandedVsOuter)
694+
if flags.contains(Flags::EXTERN_PRELUDE)
695+
&& innermost_flags.contains(Flags::EXTERN_PRELUDE)
696+
{
697+
Some(AmbiguityKind::ExternPrelude)
698+
} else {
699+
Some(AmbiguityKind::MoreExpandedVsOuter)
700+
}
694701
} else {
695702
None
696703
};

compiler/rustc_resolve/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ enum AmbiguityKind {
863863
GlobVsGlob,
864864
GlobVsExpanded,
865865
MoreExpandedVsOuter,
866+
ExternPrelude,
866867
}
867868

868869
impl AmbiguityKind {
@@ -883,6 +884,9 @@ impl AmbiguityKind {
883884
AmbiguityKind::MoreExpandedVsOuter => {
884885
"a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution"
885886
}
887+
AmbiguityKind::ExternPrelude => {
888+
"a conflict between a macro-expanded `extern crate` and `--extern` flag during import or macro resolution"
889+
}
886890
}
887891
}
888892
}

tests/ui/imports/issue-109148.stderr

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ error[E0659]: `std` is ambiguous
3636
LL | use ::std::mem as _;
3737
| ^^^ ambiguous name
3838
|
39-
= 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
39+
= note: ambiguous because of a conflict between a macro-expanded `extern crate` and `--extern` flag during import or macro resolution
4040
= note: `std` could refer to a built-in crate
41-
= help: use `::std` to refer to this crate unambiguously
4241
note: `std` could also refer to the crate imported here
4342
--> $DIR/issue-109148.rs:6:9
4443
|
@@ -47,7 +46,6 @@ LL | extern crate core as std;
4746
...
4847
LL | m!();
4948
| ---- in this macro invocation
50-
= help: use `::std` to refer to this crate unambiguously
5149
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
5250

5351
error: aborting due to 3 previous errors

tests/ui/macros/issue-78325-inconsistent-resolution.stderr

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ error[E0659]: `core` is ambiguous
3636
LL | ::core::panic!();
3737
| ^^^^ ambiguous name
3838
|
39-
= 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
39+
= note: ambiguous because of a conflict between a macro-expanded `extern crate` and `--extern` flag during import or macro resolution
4040
= note: `core` could refer to a built-in crate
41-
= help: use `::core` to refer to this crate unambiguously
4241
note: `core` could also refer to the crate imported here
4342
--> $DIR/issue-78325-inconsistent-resolution.rs:5:9
4443
|
@@ -47,7 +46,6 @@ LL | extern crate std as core;
4746
...
4847
LL | define_other_core!();
4948
| -------------------- in this macro invocation
50-
= help: use `::core` to refer to this crate unambiguously
5149
= note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info)
5250

5351
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)