Skip to content

Commit 4d377d2

Browse files
committed
resolve: Improve diagnostics for ambiguities in extern prelude
1 parent 31c87c0 commit 4d377d2

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
@@ -1886,7 +1886,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18861886
"consider adding an explicit import of `{ident}` to disambiguate"
18871887
))
18881888
}
1889-
if b.is_extern_crate() && ident.span.at_least_rust_2018() {
1889+
if kind != AmbiguityKind::ExternPrelude
1890+
&& b.is_extern_crate()
1891+
&& ident.span.at_least_rust_2018()
1892+
{
18901893
help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously"))
18911894
}
18921895
match misc {

compiler/rustc_resolve/src/ident.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
396396
struct Flags: u8 {
397397
const MACRO_RULES = 1 << 0;
398398
const MODULE = 1 << 1;
399-
const MISC_SUGGEST_CRATE = 1 << 2;
400-
const MISC_SUGGEST_SELF = 1 << 3;
401-
const MISC_FROM_PRELUDE = 1 << 4;
399+
const EXTERN_PRELUDE = 1 << 2;
400+
const MISC_SUGGEST_CRATE = 1 << 3;
401+
const MISC_SUGGEST_SELF = 1 << 4;
402+
const MISC_FROM_PRELUDE = 1 << 5;
402403
}
403404
}
404405

@@ -564,15 +565,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
564565
},
565566
Scope::ExternPreludeItems => {
566567
match this.extern_prelude_get_item(ident, finalize.is_some()) {
567-
Some(binding) => Ok((binding, Flags::empty())),
568+
Some(binding) => Ok((binding, Flags::EXTERN_PRELUDE)),
568569
None => Err(Determinacy::determined(
569570
this.graph_root.unexpanded_invocations.borrow().is_empty(),
570571
)),
571572
}
572573
}
573574
Scope::ExternPreludeFlags => {
574575
match this.extern_prelude_get_flag(ident, finalize.is_some()) {
575-
Some(binding) => Ok((binding, Flags::empty())),
576+
Some(binding) => Ok((binding, Flags::EXTERN_PRELUDE)),
576577
None => Err(Determinacy::Determined),
577578
}
578579
}
@@ -684,7 +685,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
684685
} else if innermost_binding
685686
.may_appear_after(parent_scope.expansion, binding)
686687
{
687-
Some(AmbiguityKind::MoreExpandedVsOuter)
688+
if flags.contains(Flags::EXTERN_PRELUDE)
689+
&& innermost_flags.contains(Flags::EXTERN_PRELUDE)
690+
{
691+
Some(AmbiguityKind::ExternPrelude)
692+
} else {
693+
Some(AmbiguityKind::MoreExpandedVsOuter)
694+
}
688695
} else {
689696
None
690697
};

compiler/rustc_resolve/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ enum AmbiguityKind {
861861
GlobVsGlob,
862862
GlobVsExpanded,
863863
MoreExpandedVsOuter,
864+
ExternPrelude,
864865
}
865866

866867
impl AmbiguityKind {
@@ -881,6 +882,9 @@ impl AmbiguityKind {
881882
AmbiguityKind::MoreExpandedVsOuter => {
882883
"a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution"
883884
}
885+
AmbiguityKind::ExternPrelude => {
886+
"a conflict between a macro-expanded `extern crate` and `--extern` flag during import or macro resolution"
887+
}
884888
}
885889
}
886890
}

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)