Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 302bffd

Browse files
authoredJun 30, 2023
Rollup merge of rust-lang#112086 - petrochenkov:impambig, r=oli-obk
resolve: Remove artificial import ambiguity errors Fixes rust-lang#56414. FCP report: rust-lang#112086 (comment)
2 parents b0ed475 + 4dcce38 commit 302bffd

22 files changed

+53
-230
lines changed
 

‎compiler/rustc_resolve/src/diagnostics.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14031403
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
14041404
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
14051405
ident,
1406-
ScopeSet::All(ns, false),
1406+
ScopeSet::All(ns),
14071407
&parent_scope,
14081408
None,
14091409
false,
@@ -1841,10 +1841,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18411841
_ => None,
18421842
}
18431843
} else {
1844-
let scopes = ScopeSet::All(ns_to_try, opt_ns.is_none());
18451844
self.early_resolve_ident_in_lexical_scope(
18461845
ident,
1847-
scopes,
1846+
ScopeSet::All(ns_to_try),
18481847
parent_scope,
18491848
None,
18501849
false,

‎compiler/rustc_resolve/src/ident.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
8888

8989
let rust_2015 = ctxt.edition().is_rust_2015();
9090
let (ns, macro_kind, is_absolute_path) = match scope_set {
91-
ScopeSet::All(ns, _) => (ns, None, false),
91+
ScopeSet::All(ns) => (ns, None, false),
9292
ScopeSet::AbsolutePath(ns) => (ns, None, true),
9393
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
9494
ScopeSet::Late(ns, ..) => (ns, None, false),
@@ -397,11 +397,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
397397
return Err(Determinacy::Determined);
398398
}
399399

400-
let (ns, macro_kind, is_import) = match scope_set {
401-
ScopeSet::All(ns, is_import) => (ns, None, is_import),
402-
ScopeSet::AbsolutePath(ns) => (ns, None, false),
403-
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
404-
ScopeSet::Late(ns, ..) => (ns, None, false),
400+
let (ns, macro_kind) = match scope_set {
401+
ScopeSet::All(ns) => (ns, None),
402+
ScopeSet::AbsolutePath(ns) => (ns, None),
403+
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
404+
ScopeSet::Late(ns, ..) => (ns, None),
405405
};
406406

407407
// This is *the* result, resolution from the scope closest to the resolved identifier.
@@ -631,9 +631,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
631631
let derive_helper_compat =
632632
Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
633633

634-
let ambiguity_error_kind = if is_import {
635-
Some(AmbiguityKind::Import)
636-
} else if is_builtin(innermost_res) || is_builtin(res) {
634+
let ambiguity_error_kind = if is_builtin(innermost_res)
635+
|| is_builtin(res)
636+
{
637637
Some(AmbiguityKind::BuiltinAttr)
638638
} else if innermost_res == derive_helper_compat
639639
|| res == derive_helper_compat && innermost_res != derive_helper
@@ -853,10 +853,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
853853
}
854854
}
855855

856-
let scopes = ScopeSet::All(ns, true);
857856
let binding = self.early_resolve_ident_in_lexical_scope(
858857
ident,
859-
scopes,
858+
ScopeSet::All(ns),
860859
parent_scope,
861860
finalize,
862861
finalize.is_some(),
@@ -1497,7 +1496,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14971496
} else {
14981497
self.early_resolve_ident_in_lexical_scope(
14991498
ident,
1500-
ScopeSet::All(ns, opt_ns.is_none()),
1499+
ScopeSet::All(ns),
15011500
parent_scope,
15021501
finalize,
15031502
finalize.is_some(),

‎compiler/rustc_resolve/src/imports.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ use crate::errors::{
1010
use crate::Determinacy::{self, *};
1111
use crate::{fluent_generated as fluent, Namespace::*};
1212
use crate::{module_to_string, names_to_string, ImportSuggestion};
13-
use crate::{
14-
AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingKey, ModuleKind, ResolutionError,
15-
Resolver, Segment,
16-
};
13+
use crate::{AmbiguityKind, BindingKey, ModuleKind, ResolutionError, Resolver, Segment};
1714
use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet};
1815
use crate::{NameBinding, NameBindingKind, PathResult};
1916

@@ -984,7 +981,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
984981
match binding {
985982
Ok(binding) => {
986983
// Consistency checks, analogous to `finalize_macro_resolutions`.
987-
let initial_binding = source_bindings[ns].get().map(|initial_binding| {
984+
let initial_res = source_bindings[ns].get().map(|initial_binding| {
988985
all_ns_err = false;
989986
if let Some(target_binding) = target_bindings[ns].get() {
990987
if target.name == kw::Underscore
@@ -998,20 +995,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
998995
);
999996
}
1000997
}
1001-
initial_binding
998+
initial_binding.res()
1002999
});
10031000
let res = binding.res();
1004-
if let Ok(initial_binding) = initial_binding {
1005-
let initial_res = initial_binding.res();
1001+
if let Ok(initial_res) = initial_res {
10061002
if res != initial_res && this.ambiguity_errors.is_empty() {
1007-
this.ambiguity_errors.push(AmbiguityError {
1008-
kind: AmbiguityKind::Import,
1009-
ident,
1010-
b1: initial_binding,
1011-
b2: binding,
1012-
misc1: AmbiguityErrorMisc::None,
1013-
misc2: AmbiguityErrorMisc::None,
1014-
});
1003+
span_bug!(import.span, "inconsistent resolution for an import");
10151004
}
10161005
} else if res != Res::Err
10171006
&& this.ambiguity_errors.is_empty()
@@ -1283,7 +1272,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12831272

12841273
match this.early_resolve_ident_in_lexical_scope(
12851274
target,
1286-
ScopeSet::All(ns, false),
1275+
ScopeSet::All(ns),
12871276
&import.parent_scope,
12881277
None,
12891278
false,

‎compiler/rustc_resolve/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ enum Scope<'a> {
131131
#[derive(Clone, Copy)]
132132
enum ScopeSet<'a> {
133133
/// All scopes with the given namespace.
134-
All(Namespace, /*is_import*/ bool),
134+
All(Namespace),
135135
/// Crate root, then extern prelude (used for mixed 2015-2018 mode in macros).
136136
AbsolutePath(Namespace),
137137
/// All scopes with macro namespace and the given macro kind restriction.
@@ -718,7 +718,6 @@ struct UseError<'a> {
718718

719719
#[derive(Clone, Copy, PartialEq, Debug)]
720720
enum AmbiguityKind {
721-
Import,
722721
BuiltinAttr,
723722
DeriveHelper,
724723
MacroRulesVsModularized,
@@ -731,7 +730,6 @@ enum AmbiguityKind {
731730
impl AmbiguityKind {
732731
fn descr(self) -> &'static str {
733732
match self {
734-
AmbiguityKind::Import => "multiple potential import sources",
735733
AmbiguityKind::BuiltinAttr => "a name conflict with a builtin attribute",
736734
AmbiguityKind::DeriveHelper => "a name conflict with a derive helper attribute",
737735
AmbiguityKind::MacroRulesVsModularized => {
@@ -1557,7 +1555,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15571555
}
15581556
}
15591557

1560-
self.visit_scopes(ScopeSet::All(TypeNS, false), parent_scope, ctxt, |this, scope, _, _| {
1558+
self.visit_scopes(ScopeSet::All(TypeNS), parent_scope, ctxt, |this, scope, _, _| {
15611559
match scope {
15621560
Scope::Module(module, _) => {
15631561
this.traits_in_module(module, assoc_item, &mut found_traits);

‎compiler/rustc_resolve/src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
645645
self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
646646
res
647647
} else {
648-
let scope_set = kind.map_or(ScopeSet::All(MacroNS, false), ScopeSet::Macro);
648+
let scope_set = kind.map_or(ScopeSet::All(MacroNS), ScopeSet::Macro);
649649
let binding = self.early_resolve_ident_in_lexical_scope(
650650
path[0].ident,
651651
scope_set,

‎tests/ui/imports/issue-56125.stderr

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,50 @@ error[E0659]: `issue_56125` is ambiguous
2222
LL | use issue_56125::last_segment::*;
2323
| ^^^^^^^^^^^ ambiguous name
2424
|
25-
= note: ambiguous because of multiple potential import sources
25+
= note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution
2626
= note: `issue_56125` could refer to a crate passed with `--extern`
2727
= help: use `::issue_56125` to refer to this crate unambiguously
2828
note: `issue_56125` could also refer to the module imported here
2929
--> $DIR/issue-56125.rs:6:9
3030
|
3131
LL | use issue_56125::last_segment::*;
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33-
= help: use `self::issue_56125` to refer to this module unambiguously
33+
= help: consider adding an explicit import of `issue_56125` to disambiguate
34+
= help: or use `self::issue_56125` to refer to this module unambiguously
3435

3536
error[E0659]: `issue_56125` is ambiguous
3637
--> $DIR/issue-56125.rs:11:9
3738
|
3839
LL | use issue_56125::non_last_segment::non_last_segment::*;
3940
| ^^^^^^^^^^^ ambiguous name
4041
|
41-
= note: ambiguous because of multiple potential import sources
42+
= note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution
4243
= note: `issue_56125` could refer to a crate passed with `--extern`
4344
= help: use `::issue_56125` to refer to this crate unambiguously
4445
note: `issue_56125` could also refer to the module imported here
4546
--> $DIR/issue-56125.rs:11:9
4647
|
4748
LL | use issue_56125::non_last_segment::non_last_segment::*;
4849
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
49-
= help: use `self::issue_56125` to refer to this module unambiguously
50+
= help: consider adding an explicit import of `issue_56125` to disambiguate
51+
= help: or use `self::issue_56125` to refer to this module unambiguously
5052

5153
error[E0659]: `issue_56125` is ambiguous
5254
--> $DIR/issue-56125.rs:18:9
5355
|
5456
LL | use issue_56125::*;
5557
| ^^^^^^^^^^^ ambiguous name
5658
|
57-
= note: ambiguous because of multiple potential import sources
59+
= note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution
5860
= note: `issue_56125` could refer to a crate passed with `--extern`
5961
= help: use `::issue_56125` to refer to this crate unambiguously
6062
note: `issue_56125` could also refer to the module imported here
6163
--> $DIR/issue-56125.rs:18:9
6264
|
6365
LL | use issue_56125::*;
6466
| ^^^^^^^^^^^^^^
65-
= help: use `self::issue_56125` to refer to this module unambiguously
67+
= help: consider adding an explicit import of `issue_56125` to disambiguate
68+
= help: or use `self::issue_56125` to refer to this module unambiguously
6669

6770
error: aborting due to 4 previous errors
6871

‎tests/ui/imports/issue-57539.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ error[E0659]: `core` is ambiguous
44
LL | use core;
55
| ^^^^ ambiguous name
66
|
7-
= note: ambiguous because of multiple potential import sources
7+
= note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution
88
= note: `core` could refer to a built-in crate
99
= help: use `::core` to refer to this crate unambiguously
1010
note: `core` could also refer to the module imported here
1111
--> $DIR/issue-57539.rs:5:9
1212
|
1313
LL | use crate::*;
1414
| ^^^^^^^^
15-
= help: use `self::core` to refer to this module unambiguously
15+
= help: consider adding an explicit import of `core` to disambiguate
16+
= help: or use `self::core` to refer to this module unambiguously
1617

1718
error: aborting due to previous error
1819

‎tests/ui/proc-macro/derive-helper-shadowing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ macro_rules! gen_helper_use {
2323
struct S {
2424
#[empty_helper] // OK, no ambiguity, derive helpers have highest priority
2525
field: [u8; {
26-
use empty_helper; //~ ERROR `empty_helper` is ambiguous
26+
use empty_helper; // OK, no ambiguity, derive helpers have highest priority
2727

2828
#[empty_helper] // OK, no ambiguity, derive helpers have highest priority
2929
struct U;

‎tests/ui/proc-macro/derive-helper-shadowing.stderr

+1-20
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,6 @@ help: consider importing this attribute macro through its public re-export
3737
LL + use crate::empty_helper;
3838
|
3939

40-
error[E0659]: `empty_helper` is ambiguous
41-
--> $DIR/derive-helper-shadowing.rs:26:13
42-
|
43-
LL | use empty_helper;
44-
| ^^^^^^^^^^^^ ambiguous name
45-
|
46-
= note: ambiguous because of multiple potential import sources
47-
note: `empty_helper` could refer to the derive helper attribute defined here
48-
--> $DIR/derive-helper-shadowing.rs:22:10
49-
|
50-
LL | #[derive(Empty)]
51-
| ^^^^^
52-
note: `empty_helper` could also refer to the attribute macro imported here
53-
--> $DIR/derive-helper-shadowing.rs:10:5
54-
|
55-
LL | use test_macros::empty_attr as empty_helper;
56-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
57-
= help: use `crate::empty_helper` to refer to this attribute macro unambiguously
58-
5940
error[E0659]: `empty_helper` is ambiguous
6041
--> $DIR/derive-helper-shadowing.rs:19:3
6142
|
@@ -88,6 +69,6 @@ LL | #[derive(Empty)]
8869
= note: for more information, see issue #79202 <https://github.com/rust-lang/rust/issues/79202>
8970
= note: `#[warn(legacy_derive_helpers)]` on by default
9071

91-
error: aborting due to 5 previous errors; 1 warning emitted
72+
error: aborting due to 4 previous errors; 1 warning emitted
9273

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

‎tests/ui/rust-2018/uniform-paths/ambiguity-macros-nested.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0659]: `std` is ambiguous
44
LL | pub use std::io;
55
| ^^^ ambiguous name
66
|
7-
= note: ambiguous because of multiple potential import sources
7+
= 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
88
= note: `std` could refer to a built-in crate
99
= help: use `::std` to refer to this crate unambiguously
1010
note: `std` could also refer to the module defined here

‎tests/ui/rust-2018/uniform-paths/ambiguity-macros.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0659]: `std` is ambiguous
44
LL | use std::io;
55
| ^^^ ambiguous name
66
|
7-
= note: ambiguous because of multiple potential import sources
7+
= 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
88
= note: `std` could refer to a built-in crate
99
= help: use `::std` to refer to this crate unambiguously
1010
note: `std` could also refer to the module defined here

‎tests/ui/rust-2018/uniform-paths/ambiguity-nested.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
// check-pass
12
// edition:2018
23

34
// This test is similar to `ambiguity.rs`, but nested in a module.
45

56
#![allow(non_camel_case_types)]
67

78
mod foo {
8-
pub use std::io;
9-
//~^ ERROR `std` is ambiguous
9+
pub use std::io; // OK
1010

1111
mod std {
1212
pub struct io;

‎tests/ui/rust-2018/uniform-paths/ambiguity-nested.stderr

-21
This file was deleted.

‎tests/ui/rust-2018/uniform-paths/ambiguity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
// check-pass
12
// edition:2018
23

34
#![allow(non_camel_case_types)]
45

5-
use std::io;
6-
//~^ ERROR `std` is ambiguous
6+
use std::io; // OK
77

88
mod std {
99
pub struct io;

‎tests/ui/rust-2018/uniform-paths/ambiguity.stderr

-21
This file was deleted.

‎tests/ui/rust-2018/uniform-paths/block-scoped-shadow-nested.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// check-pass
12
// edition:2018
23

34
mod my {
@@ -13,7 +14,7 @@ mod sub {
1314
fn foo() {
1415
use my::sub;
1516
{
16-
use sub::bar; //~ ERROR `sub` is ambiguous
17+
use sub::bar; // OK
1718
}
1819
}
1920

0 commit comments

Comments
 (0)
Please sign in to comment.