Skip to content

Commit 9cbe924

Browse files
Auto merge of #149195 - petrochenkov:globamberr, r=<try>
resolve: Partially convert `ambiguous_glob_imports` lint into a hard error
2 parents 3391c01 + 7e2e10d commit 9cbe924

File tree

60 files changed

+785
-830
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+785
-830
lines changed

‎compiler/rustc_codegen_gcc/build_system/src/test.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,6 @@ fn valid_ui_error_pattern_test(file: &str) -> bool {
826826
"type-alias-impl-trait/auxiliary/cross_crate_ice.rs",
827827
"type-alias-impl-trait/auxiliary/cross_crate_ice2.rs",
828828
"macros/rfc-2011-nicer-assert-messages/auxiliary/common.rs",
829-
"imports/ambiguous-1.rs",
830-
"imports/ambiguous-4-extern.rs",
831829
"entry-point/auxiliary/bad_main_functions.rs",
832830
]
833831
.iter()

‎compiler/rustc_lint_defs/src/builtin.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4437,7 +4437,7 @@ declare_lint! {
44374437
///
44384438
/// ### Example
44394439
///
4440-
/// ```rust,compile_fail
4440+
/// ```rust,ignore (needs extern crate)
44414441
/// #![deny(ambiguous_glob_imports)]
44424442
/// pub fn foo() -> u32 {
44434443
/// use sub::*;
@@ -4453,8 +4453,6 @@ declare_lint! {
44534453
/// }
44544454
/// ```
44554455
///
4456-
/// {{produces}}
4457-
///
44584456
/// ### Explanation
44594457
///
44604458
/// Previous versions of Rust compile it successfully because it

‎compiler/rustc_resolve/src/build_reduced_graph.rs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
5353
ns: Namespace,
5454
binding: NameBinding<'ra>,
5555
) {
56-
if let Err(old_binding) = self.try_define_local(parent, ident, ns, binding, false) {
56+
if let Err(old_binding) = self.try_define_local(parent, ident, ns, binding) {
5757
self.report_conflict(parent, ident, ns, old_binding, binding);
5858
}
5959
}
@@ -82,13 +82,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
8282
vis: Visibility<DefId>,
8383
span: Span,
8484
expansion: LocalExpnId,
85-
ambiguity: Option<(NameBinding<'ra>, AmbiguityKind)>,
85+
ambiguity: Option<(NameBinding<'ra>, AmbiguityKind, bool)>,
8686
) {
8787
let binding = self.arenas.alloc_name_binding(NameBindingData {
8888
kind: NameBindingKind::Res(res),
8989
ambiguity,
90-
// External ambiguities always report the `AMBIGUOUS_GLOB_IMPORTS` lint at the moment.
91-
warn_ambiguity: true,
9290
vis,
9391
span,
9492
expansion,
@@ -288,7 +286,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
288286
AmbigModChildKind::GlobVsGlob => AmbiguityKind::GlobVsGlob,
289287
AmbigModChildKind::GlobVsExpanded => AmbiguityKind::GlobVsExpanded,
290288
};
291-
(self.arenas.new_res_binding(res, vis, span, expansion), ambig_kind)
289+
// External ambiguities always report the `AMBIGUOUS_GLOB_IMPORTS` lint at the moment.
290+
(self.arenas.new_res_binding(res, vis, span, expansion), ambig_kind, true)
292291
});
293292

294293
// Record primary definitions.

‎compiler/rustc_resolve/src/effective_visibilities.rs‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,13 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
125125
// If the binding is ambiguous, put the root ambiguity binding and all reexports
126126
// leading to it into the table. They are used by the `ambiguous_glob_reexports`
127127
// lint. For all bindings added to the table this way `is_ambiguity` returns true.
128-
let is_ambiguity =
129-
|binding: NameBinding<'ra>, warn: bool| binding.ambiguity.is_some() && !warn;
130128
let mut parent_id = ParentId::Def(module_id);
131-
let mut warn_ambiguity = binding.warn_ambiguity;
132129
while let NameBindingKind::Import { binding: nested_binding, .. } = binding.kind {
133130
self.update_import(binding, parent_id);
134-
135-
if is_ambiguity(binding, warn_ambiguity) {
136-
// Stop at the root ambiguity, further bindings in the chain should not
137-
// be reexported because the root ambiguity blocks any access to them.
138-
// (Those further bindings are most likely not ambiguities themselves.)
139-
break;
140-
}
141-
142131
parent_id = ParentId::Import(binding);
143132
binding = nested_binding;
144-
warn_ambiguity |= nested_binding.warn_ambiguity;
145133
}
146-
if !is_ambiguity(binding, warn_ambiguity)
147-
&& let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local())
148-
{
134+
if let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local()) {
149135
self.update_def(def_id, binding.vis.expect_local(), parent_id);
150136
}
151137
}

‎compiler/rustc_resolve/src/imports.rs‎

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
325325
self.arenas.alloc_name_binding(NameBindingData {
326326
kind: NameBindingKind::Import { binding, import },
327327
ambiguity: None,
328-
warn_ambiguity: false,
329328
span: import.span,
330329
vis,
331330
expansion: import.parent_scope.expansion,
@@ -339,7 +338,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
339338
ident: Ident,
340339
ns: Namespace,
341340
binding: NameBinding<'ra>,
342-
warn_ambiguity: bool,
343341
) -> Result<(), NameBinding<'ra>> {
344342
let res = binding.res();
345343
self.check_reserved_macro_name(ident, res);
@@ -351,7 +349,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
351349
module.underscore_disambiguator.update_unchecked(|d| d + 1);
352350
module.underscore_disambiguator.get()
353351
});
354-
self.update_local_resolution(module, key, warn_ambiguity, |this, resolution| {
352+
self.update_local_resolution(module, key, |this, resolution| {
355353
if let Some(old_binding) = resolution.best_binding() {
356354
if res == Res::Err && old_binding.res() != Res::Err {
357355
// Do not override real bindings with `Res::Err`s from error recovery.
@@ -360,30 +358,31 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
360358
match (old_binding.is_glob_import(), binding.is_glob_import()) {
361359
(true, true) => {
362360
let (glob_binding, old_glob_binding) = (binding, old_binding);
363-
// FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
364-
if !binding.is_ambiguity_recursive()
365-
&& let NameBindingKind::Import { import: old_import, .. } =
366-
old_glob_binding.kind
367-
&& let NameBindingKind::Import { import, .. } = glob_binding.kind
368-
&& old_import == import
369-
{
370-
// When imported from the same glob-import statement, we should replace
371-
// `old_glob_binding` with `glob_binding`, regardless of whether
372-
// they have the same resolution or not.
373-
resolution.glob_binding = Some(glob_binding);
374-
} else if res != old_glob_binding.res() {
361+
if res != old_glob_binding.res() {
362+
let (primary_binding, secondary_binding, warning) = if !binding
363+
.is_ambiguity_recursive()
364+
&& let NameBindingKind::Import { import: old_import, .. } =
365+
old_glob_binding.kind
366+
&& let NameBindingKind::Import { import, .. } = glob_binding.kind
367+
&& old_import == import
368+
{
369+
// When imported from the same import, we have to replace
370+
// `old_glob_binding` with `glob_binding` to avoid cascade errors.
371+
(glob_binding, old_glob_binding, true)
372+
} else {
373+
(old_glob_binding, glob_binding, false)
374+
};
375375
resolution.glob_binding = Some(this.new_ambiguity_binding(
376376
AmbiguityKind::GlobVsGlob,
377-
old_glob_binding,
378-
glob_binding,
379-
warn_ambiguity,
377+
primary_binding,
378+
secondary_binding,
379+
warning,
380380
));
381-
} else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
381+
} else if !old_glob_binding.vis.is_at_least(glob_binding.vis, this.tcx)
382+
|| glob_binding.is_ambiguity_recursive()
383+
{
382384
// We are glob-importing the same item but with greater visibility.
383385
resolution.glob_binding = Some(glob_binding);
384-
} else if binding.is_ambiguity_recursive() {
385-
resolution.glob_binding =
386-
Some(this.new_warn_ambiguity_binding(glob_binding));
387386
}
388387
}
389388
(old_glob @ true, false) | (old_glob @ false, true) => {
@@ -412,7 +411,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
412411
glob_binding,
413412
false,
414413
));
415-
} else if !old_glob_binding.vis.is_at_least(binding.vis, this.tcx) {
414+
} else if !old_glob_binding.vis.is_at_least(glob_binding.vis, this.tcx)
415+
|| glob_binding.is_ambiguity_recursive()
416+
{
417+
// We are glob-importing the same item but with greater visibility.
416418
resolution.glob_binding = Some(glob_binding);
417419
}
418420
} else {
@@ -440,33 +442,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
440442
ambiguity_kind: AmbiguityKind,
441443
primary_binding: NameBinding<'ra>,
442444
secondary_binding: NameBinding<'ra>,
443-
warn_ambiguity: bool,
445+
warning: bool,
444446
) -> NameBinding<'ra> {
445-
let ambiguity = Some((secondary_binding, ambiguity_kind));
446-
let data = NameBindingData { ambiguity, warn_ambiguity, ..*primary_binding };
447+
let ambiguity = Some((secondary_binding, ambiguity_kind, warning));
448+
let data = NameBindingData { ambiguity, ..*primary_binding };
447449
self.arenas.alloc_name_binding(data)
448450
}
449451

450-
fn new_warn_ambiguity_binding(&self, binding: NameBinding<'ra>) -> NameBinding<'ra> {
451-
assert!(binding.is_ambiguity_recursive());
452-
self.arenas.alloc_name_binding(NameBindingData { warn_ambiguity: true, ..*binding })
453-
}
454-
455452
// Use `f` to mutate the resolution of the name in the module.
456453
// If the resolution becomes a success, define it in the module's glob importers.
457-
fn update_local_resolution<T, F>(
458-
&mut self,
459-
module: Module<'ra>,
460-
key: BindingKey,
461-
warn_ambiguity: bool,
462-
f: F,
463-
) -> T
454+
fn update_local_resolution<T, F>(&mut self, module: Module<'ra>, key: BindingKey, f: F) -> T
464455
where
465456
F: FnOnce(&Resolver<'ra, 'tcx>, &mut NameResolution<'ra>) -> T,
466457
{
467458
// Ensure that `resolution` isn't borrowed when defining in the module's glob importers,
468459
// during which the resolution might end up getting re-defined via a glob cycle.
469-
let (binding, t, warn_ambiguity) = {
460+
let (binding, t) = {
470461
let resolution = &mut *self.resolution_or_default(module, key).borrow_mut_unchecked();
471462
let old_binding = resolution.binding();
472463

@@ -475,7 +466,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
475466
if let Some(binding) = resolution.binding()
476467
&& old_binding != Some(binding)
477468
{
478-
(binding, t, warn_ambiguity || old_binding.is_some())
469+
(binding, t)
479470
} else {
480471
return t;
481472
}
@@ -500,7 +491,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
500491
ident.0,
501492
key.ns,
502493
imported_binding,
503-
warn_ambiguity,
504494
);
505495
}
506496
}
@@ -521,11 +511,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
521511
let dummy_binding = self.import(dummy_binding, import);
522512
self.per_ns(|this, ns| {
523513
let module = import.parent_scope.module;
524-
let _ = this.try_define_local(module, target, ns, dummy_binding, false);
514+
let _ = this.try_define_local(module, target, ns, dummy_binding);
525515
// Don't remove underscores from `single_imports`, they were never added.
526516
if target.name != kw::Underscore {
527517
let key = BindingKey::new(target, ns);
528-
this.update_local_resolution(module, key, false, |_, resolution| {
518+
this.update_local_resolution(module, key, |_, resolution| {
529519
resolution.single_imports.swap_remove(&import);
530520
})
531521
}
@@ -658,7 +648,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
658648
let Some(binding) = resolution.best_binding() else { continue };
659649

660650
if let NameBindingKind::Import { import, .. } = binding.kind
661-
&& let Some((amb_binding, _)) = binding.ambiguity
651+
&& let Some((amb_binding, ..)) = binding.ambiguity
662652
&& binding.res() != Res::Err
663653
&& exported_ambiguities.contains(&binding)
664654
{
@@ -917,7 +907,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
917907
this.get_mut_unchecked().update_local_resolution(
918908
parent,
919909
key,
920-
false,
921910
|_, resolution| {
922911
resolution.single_imports.swap_remove(&import);
923912
},
@@ -1523,16 +1512,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15231512
};
15241513
if self.is_accessible_from(binding.vis, scope) {
15251514
let imported_binding = self.import(binding, import);
1526-
let warn_ambiguity = self
1527-
.resolution(import.parent_scope.module, key)
1528-
.and_then(|r| r.binding())
1529-
.is_some_and(|binding| binding.warn_ambiguity_recursive());
15301515
let _ = self.try_define_local(
15311516
import.parent_scope.module,
15321517
key.ident.0,
15331518
key.ns,
15341519
imported_binding,
1535-
warn_ambiguity,
15361520
);
15371521
}
15381522
}

‎compiler/rustc_resolve/src/lib.rs‎

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,7 @@ impl<'ra> fmt::Debug for Module<'ra> {
792792
#[derive(Clone, Copy, Debug)]
793793
struct NameBindingData<'ra> {
794794
kind: NameBindingKind<'ra>,
795-
ambiguity: Option<(NameBinding<'ra>, AmbiguityKind)>,
796-
/// Produce a warning instead of an error when reporting ambiguities inside this binding.
797-
/// May apply to indirect ambiguities under imports, so `ambiguity.is_some()` is not required.
798-
warn_ambiguity: bool,
795+
ambiguity: Option<(NameBinding<'ra>, AmbiguityKind, bool /*warning*/)>,
799796
expansion: LocalExpnId,
800797
span: Span,
801798
vis: Visibility<DefId>,
@@ -932,7 +929,7 @@ impl<'ra> NameBindingData<'ra> {
932929
self: NameBinding<'ra>,
933930
) -> Option<(NameBinding<'ra>, NameBinding<'ra>, AmbiguityKind)> {
934931
match self.ambiguity {
935-
Some((ambig_binding, ambig_kind)) => Some((self, ambig_binding, ambig_kind)),
932+
Some((ambig_binding, ambig_kind, _)) => Some((self, ambig_binding, ambig_kind)),
936933
None => match self.kind {
937934
NameBindingKind::Import { binding, .. } => binding.descent_to_ambiguity(),
938935
_ => None,
@@ -948,14 +945,6 @@ impl<'ra> NameBindingData<'ra> {
948945
}
949946
}
950947

951-
fn warn_ambiguity_recursive(&self) -> bool {
952-
self.warn_ambiguity
953-
|| match self.kind {
954-
NameBindingKind::Import { binding, .. } => binding.warn_ambiguity_recursive(),
955-
_ => false,
956-
}
957-
}
958-
959948
fn is_possibly_imported_variant(&self) -> bool {
960949
match self.kind {
961950
NameBindingKind::Import { binding, .. } => binding.is_possibly_imported_variant(),
@@ -1340,7 +1329,6 @@ impl<'ra> ResolverArenas<'ra> {
13401329
self.alloc_name_binding(NameBindingData {
13411330
kind: NameBindingKind::Res(res),
13421331
ambiguity: None,
1343-
warn_ambiguity: false,
13441332
vis,
13451333
span,
13461334
expansion,
@@ -2045,25 +2033,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20452033
}
20462034

20472035
fn record_use(&mut self, ident: Ident, used_binding: NameBinding<'ra>, used: Used) {
2048-
self.record_use_inner(ident, used_binding, used, used_binding.warn_ambiguity);
2049-
}
2050-
2051-
fn record_use_inner(
2052-
&mut self,
2053-
ident: Ident,
2054-
used_binding: NameBinding<'ra>,
2055-
used: Used,
2056-
warn_ambiguity: bool,
2057-
) {
2058-
if let Some((b2, kind)) = used_binding.ambiguity {
2036+
if let Some((b2, kind, warning)) = used_binding.ambiguity {
20592037
let ambiguity_error = AmbiguityError {
20602038
kind,
20612039
ident,
20622040
b1: used_binding,
20632041
b2,
20642042
misc1: AmbiguityErrorMisc::None,
20652043
misc2: AmbiguityErrorMisc::None,
2066-
warning: warn_ambiguity,
2044+
warning,
20672045
};
20682046
if !self.matches_previous_ambiguity_error(&ambiguity_error) {
20692047
// avoid duplicated span information to be emit out
@@ -2112,12 +2090,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21122090
self.used_imports.insert(id);
21132091
}
21142092
self.add_to_glob_map(import, ident);
2115-
self.record_use_inner(
2116-
ident,
2117-
binding,
2118-
Used::Other,
2119-
warn_ambiguity || binding.warn_ambiguity,
2120-
);
2093+
self.record_use(ident, binding, Used::Other);
21212094
}
21222095
}
21232096

‎tests/ui/imports/ambiguous-1.rs‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
//@ check-pass
21
// https://github.com/rust-lang/rust/pull/112743#issuecomment-1601986883
32

4-
#![warn(ambiguous_glob_imports)]
5-
63
macro_rules! m {
74
() => {
85
pub fn id() {}
@@ -27,6 +24,5 @@ pub use openssl::*;
2724

2825
fn main() {
2926
id();
30-
//~^ WARNING `id` is ambiguous
31-
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
27+
//~^ ERROR `id` is ambiguous
3228
}

0 commit comments

Comments
 (0)