Skip to content

Commit 1473190

Browse files
Auto merge of #149195 - petrochenkov:globamberr, r=<try>
resolve: Partially convert `ambiguous_glob_imports` lint into a hard error
2 parents bad5026 + 2ecd27b commit 1473190

File tree

59 files changed

+667
-831
lines changed

Some content is hidden

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

59 files changed

+667
-831
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_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: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,12 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
124124
//
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`
127-
// 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;
127+
// lint.
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);
134131

135-
if is_ambiguity(binding, warn_ambiguity) {
132+
if binding.ambiguity.is_some() {
136133
// Stop at the root ambiguity, further bindings in the chain should not
137134
// be reexported because the root ambiguity blocks any access to them.
138135
// (Those further bindings are most likely not ambiguities themselves.)
@@ -141,9 +138,8 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
141138

142139
parent_id = ParentId::Import(binding);
143140
binding = nested_binding;
144-
warn_ambiguity |= nested_binding.warn_ambiguity;
145141
}
146-
if !is_ambiguity(binding, warn_ambiguity)
142+
if binding.ambiguity.is_none()
147143
&& let Some(def_id) = binding.res().opt_def_id().and_then(|id| id.as_local())
148144
{
149145
self.update_def(def_id, binding.vis.expect_local(), parent_id);

‎compiler/rustc_resolve/src/imports.rs‎

Lines changed: 23 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ use crate::errors::{
3232
};
3333
use crate::ref_mut::CmCell;
3434
use crate::{
35-
AmbiguityError, AmbiguityKind, BindingKey, CmResolver, Determinacy, Finalize, ImportSuggestion,
36-
Module, ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind, ParentScope,
37-
PathResult, PerNS, ResolutionError, Resolver, ScopeSet, Segment, Used, module_to_string,
38-
names_to_string,
35+
AmbiguityKind, BindingKey, CmResolver, Determinacy, Finalize, ImportSuggestion, Module,
36+
ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind, ParentScope, PathResult,
37+
PerNS, ResolutionError, Resolver, ScopeSet, Segment, Used, module_to_string, names_to_string,
3938
};
4039

4140
type Res = def::Res<NodeId>;
@@ -325,7 +324,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
325324
self.arenas.alloc_name_binding(NameBindingData {
326325
kind: NameBindingKind::Import { binding, import },
327326
ambiguity: None,
328-
warn_ambiguity: false,
329327
span: import.span,
330328
vis,
331329
expansion: import.parent_scope.expansion,
@@ -339,7 +337,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
339337
ident: Ident,
340338
ns: Namespace,
341339
binding: NameBinding<'ra>,
342-
warn_ambiguity: bool,
343340
) -> Result<(), NameBinding<'ra>> {
344341
let res = binding.res();
345342
self.check_reserved_macro_name(ident, res);
@@ -351,7 +348,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
351348
module.underscore_disambiguator.update_unchecked(|d| d + 1);
352349
module.underscore_disambiguator.get()
353350
});
354-
self.update_local_resolution(module, key, warn_ambiguity, |this, resolution| {
351+
self.update_local_resolution(module, key, |this, resolution| {
355352
if let Some(old_binding) = resolution.best_binding() {
356353
if res == Res::Err && old_binding.res() != Res::Err {
357354
// Do not override real bindings with `Res::Err`s from error recovery.
@@ -360,30 +357,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
360357
match (old_binding.is_glob_import(), binding.is_glob_import()) {
361358
(true, true) => {
362359
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() {
360+
if res != old_glob_binding.res() {
375361
resolution.glob_binding = Some(this.new_ambiguity_binding(
376362
AmbiguityKind::GlobVsGlob,
377363
old_glob_binding,
378364
glob_binding,
379-
warn_ambiguity,
380365
));
381-
} else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
366+
} else if !old_glob_binding.vis.is_at_least(glob_binding.vis, this.tcx)
367+
|| glob_binding.is_ambiguity_recursive()
368+
{
382369
// We are glob-importing the same item but with greater visibility.
383370
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));
387371
}
388372
}
389373
(old_glob @ true, false) | (old_glob @ false, true) => {
@@ -397,7 +381,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
397381
AmbiguityKind::GlobVsExpanded,
398382
non_glob_binding,
399383
glob_binding,
400-
false,
401384
));
402385
} else {
403386
resolution.non_glob_binding = Some(non_glob_binding);
@@ -410,9 +393,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
410393
AmbiguityKind::GlobVsGlob,
411394
old_glob_binding,
412395
glob_binding,
413-
false,
414396
));
415-
} else if !old_glob_binding.vis.is_at_least(binding.vis, this.tcx) {
397+
} else if !old_glob_binding.vis.is_at_least(glob_binding.vis, this.tcx)
398+
|| glob_binding.is_ambiguity_recursive()
399+
{
400+
// We are glob-importing the same item but with greater visibility.
416401
resolution.glob_binding = Some(glob_binding);
417402
}
418403
} else {
@@ -440,33 +425,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
440425
ambiguity_kind: AmbiguityKind,
441426
primary_binding: NameBinding<'ra>,
442427
secondary_binding: NameBinding<'ra>,
443-
warn_ambiguity: bool,
444428
) -> NameBinding<'ra> {
445-
let ambiguity = Some((secondary_binding, ambiguity_kind));
446-
let data = NameBindingData { ambiguity, warn_ambiguity, ..*primary_binding };
429+
let ambiguity = Some((secondary_binding, ambiguity_kind, false));
430+
let data = NameBindingData { ambiguity, ..*primary_binding };
447431
self.arenas.alloc_name_binding(data)
448432
}
449433

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-
455434
// Use `f` to mutate the resolution of the name in the module.
456435
// 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
436+
fn update_local_resolution<T, F>(&mut self, module: Module<'ra>, key: BindingKey, f: F) -> T
464437
where
465438
F: FnOnce(&Resolver<'ra, 'tcx>, &mut NameResolution<'ra>) -> T,
466439
{
467440
// Ensure that `resolution` isn't borrowed when defining in the module's glob importers,
468441
// during which the resolution might end up getting re-defined via a glob cycle.
469-
let (binding, t, warn_ambiguity) = {
442+
let (binding, t) = {
470443
let resolution = &mut *self.resolution_or_default(module, key).borrow_mut_unchecked();
471444
let old_binding = resolution.binding();
472445

@@ -475,7 +448,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
475448
if let Some(binding) = resolution.binding()
476449
&& old_binding != Some(binding)
477450
{
478-
(binding, t, warn_ambiguity || old_binding.is_some())
451+
(binding, t)
479452
} else {
480453
return t;
481454
}
@@ -500,7 +473,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
500473
ident.0,
501474
key.ns,
502475
imported_binding,
503-
warn_ambiguity,
504476
);
505477
}
506478
}
@@ -521,11 +493,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
521493
let dummy_binding = self.import(dummy_binding, import);
522494
self.per_ns(|this, ns| {
523495
let module = import.parent_scope.module;
524-
let _ = this.try_define_local(module, target, ns, dummy_binding, false);
496+
let _ = this.try_define_local(module, target, ns, dummy_binding);
525497
// Don't remove underscores from `single_imports`, they were never added.
526498
if target.name != kw::Underscore {
527499
let key = BindingKey::new(target, ns);
528-
this.update_local_resolution(module, key, false, |_, resolution| {
500+
this.update_local_resolution(module, key, |_, resolution| {
529501
resolution.single_imports.swap_remove(&import);
530502
})
531503
}
@@ -658,7 +630,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
658630
let Some(binding) = resolution.best_binding() else { continue };
659631

660632
if let NameBindingKind::Import { import, .. } = binding.kind
661-
&& let Some((amb_binding, _)) = binding.ambiguity
633+
&& let Some((amb_binding, ..)) = binding.ambiguity
662634
&& binding.res() != Res::Err
663635
&& exported_ambiguities.contains(&binding)
664636
{
@@ -917,7 +889,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
917889
this.get_mut_unchecked().update_local_resolution(
918890
parent,
919891
key,
920-
false,
921892
|_, resolution| {
922893
resolution.single_imports.swap_remove(&import);
923894
},
@@ -946,9 +917,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
946917
ImportKind::Single { bindings, .. } => bindings[TypeNS].get().binding(),
947918
_ => None,
948919
};
949-
let ambiguity_errors_len =
950-
|errors: &Vec<AmbiguityError<'_>>| errors.iter().filter(|error| !error.warning).count();
951-
let prev_ambiguity_errors_len = ambiguity_errors_len(&self.ambiguity_errors);
920+
let prev_ambiguity_errors_len = self.ambiguity_errors.len();
952921
let finalize = Finalize::with_root_span(import.root_id, import.span, import.root_span);
953922

954923
// We'll provide more context to the privacy errors later, up to `len`.
@@ -963,8 +932,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
963932
Some(import),
964933
);
965934

966-
let no_ambiguity =
967-
ambiguity_errors_len(&self.ambiguity_errors) == prev_ambiguity_errors_len;
935+
let no_ambiguity = self.ambiguity_errors.len() == prev_ambiguity_errors_len;
968936

969937
let module = match path_res {
970938
PathResult::Module(module) => {
@@ -1162,9 +1130,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11621130
initial_binding.res()
11631131
});
11641132
let res = binding.res();
1165-
let has_ambiguity_error =
1166-
this.ambiguity_errors.iter().any(|error| !error.warning);
1167-
if res == Res::Err || has_ambiguity_error {
1133+
if res == Res::Err || !this.ambiguity_errors.is_empty() {
11681134
this.dcx()
11691135
.span_delayed_bug(import.span, "some error happened for an import");
11701136
return;
@@ -1523,16 +1489,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15231489
};
15241490
if self.is_accessible_from(binding.vis, scope) {
15251491
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());
15301492
let _ = self.try_define_local(
15311493
import.parent_scope.module,
15321494
key.ident.0,
15331495
key.ns,
15341496
imported_binding,
1535-
warn_ambiguity,
15361497
);
15371498
}
15381499
}

‎compiler/rustc_resolve/src/lib.rs‎

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

950-
fn warn_ambiguity_recursive(&self) -> bool {
951-
self.warn_ambiguity
952-
|| match self.kind {
953-
NameBindingKind::Import { binding, .. } => binding.warn_ambiguity_recursive(),
954-
_ => false,
955-
}
956-
}
957-
958947
fn is_possibly_imported_variant(&self) -> bool {
959948
match self.kind {
960949
NameBindingKind::Import { binding, .. } => binding.is_possibly_imported_variant(),
@@ -1339,7 +1328,6 @@ impl<'ra> ResolverArenas<'ra> {
13391328
self.alloc_name_binding(NameBindingData {
13401329
kind: NameBindingKind::Res(res),
13411330
ambiguity: None,
1342-
warn_ambiguity: false,
13431331
vis,
13441332
span,
13451333
expansion,
@@ -2044,25 +2032,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20442032
}
20452033

20462034
fn record_use(&mut self, ident: Ident, used_binding: NameBinding<'ra>, used: Used) {
2047-
self.record_use_inner(ident, used_binding, used, used_binding.warn_ambiguity);
2048-
}
2049-
2050-
fn record_use_inner(
2051-
&mut self,
2052-
ident: Ident,
2053-
used_binding: NameBinding<'ra>,
2054-
used: Used,
2055-
warn_ambiguity: bool,
2056-
) {
2057-
if let Some((b2, kind)) = used_binding.ambiguity {
2035+
if let Some((b2, kind, warning)) = used_binding.ambiguity {
20582036
let ambiguity_error = AmbiguityError {
20592037
kind,
20602038
ident,
20612039
b1: used_binding,
20622040
b2,
20632041
misc1: AmbiguityErrorMisc::None,
20642042
misc2: AmbiguityErrorMisc::None,
2065-
warning: warn_ambiguity,
2043+
warning,
20662044
};
20672045
if !self.matches_previous_ambiguity_error(&ambiguity_error) {
20682046
// avoid duplicated span information to be emit out
@@ -2111,12 +2089,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21112089
self.used_imports.insert(id);
21122090
}
21132091
self.add_to_glob_map(import, ident);
2114-
self.record_use_inner(
2115-
ident,
2116-
binding,
2117-
Used::Other,
2118-
warn_ambiguity || binding.warn_ambiguity,
2119-
);
2092+
self.record_use(ident, binding, Used::Other);
21202093
}
21212094
}
21222095

0 commit comments

Comments
 (0)