Rollup merge of #105208 - chenyukang:yukang/fix-105069, r=cjgillot

Add AmbiguityError for inconsistent resolution for an import

Fixes #105069
Fixes #83950
This commit is contained in:
Matthias Krüger 2022-12-03 17:37:45 +01:00 committed by GitHub
commit af8f72292b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 5 deletions

View File

@ -4,7 +4,10 @@ use crate::diagnostics::{import_candidates, Suggestion};
use crate::Determinacy::{self, *};
use crate::Namespace::*;
use crate::{module_to_string, names_to_string, ImportSuggestion};
use crate::{AmbiguityKind, BindingKey, ModuleKind, ResolutionError, Resolver, Segment};
use crate::{
AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingKey, ModuleKind, ResolutionError,
Resolver, Segment,
};
use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet};
use crate::{NameBinding, NameBindingKind, PathResult};
@ -791,7 +794,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
match binding {
Ok(binding) => {
// Consistency checks, analogous to `finalize_macro_resolutions`.
let initial_res = source_bindings[ns].get().map(|initial_binding| {
let initial_binding = source_bindings[ns].get().map(|initial_binding| {
all_ns_err = false;
if let Some(target_binding) = target_bindings[ns].get() {
if target.name == kw::Underscore
@ -805,12 +808,20 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
);
}
}
initial_binding.res()
initial_binding
});
let res = binding.res();
if let Ok(initial_res) = initial_res {
if let Ok(initial_binding) = initial_binding {
let initial_res = initial_binding.res();
if res != initial_res && this.ambiguity_errors.is_empty() {
span_bug!(import.span, "inconsistent resolution for an import");
this.ambiguity_errors.push(AmbiguityError {
kind: AmbiguityKind::Import,
ident,
b1: initial_binding,
b2: binding,
misc1: AmbiguityErrorMisc::None,
misc2: AmbiguityErrorMisc::None,
});
}
} else if res != Res::Err
&& this.ambiguity_errors.is_empty()

View File

@ -0,0 +1,11 @@
use self::A::*;
use V; //~ ERROR `V` is ambiguous
use self::B::*;
enum A {
V
}
enum B {
V
}
fn main() {}

View File

@ -0,0 +1,21 @@
error[E0659]: `V` is ambiguous
--> $DIR/issue-105069.rs:2:5
|
LL | use V;
| ^ ambiguous name
|
= note: ambiguous because of multiple potential import sources
note: `V` could refer to the variant imported here
--> $DIR/issue-105069.rs:1:5
|
LL | use self::A::*;
| ^^^^^^^^^^
note: `V` could also refer to the variant imported here
--> $DIR/issue-105069.rs:3:5
|
LL | use self::B::*;
| ^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0659`.