Skip to content

Commit 446cb60

Browse files
Auto merge of #149195 - petrochenkov:globamberr, r=<try>
resolve: Convert `ambiguous_glob_imports` lint into a hard error
2 parents e22dab3 + 99f8131 commit 446cb60

Some content is hidden

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

58 files changed

+646
-752
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
@@ -840,8 +840,6 @@ fn valid_ui_error_pattern_test(file: &str) -> bool {
840840
"type-alias-impl-trait/auxiliary/cross_crate_ice.rs",
841841
"type-alias-impl-trait/auxiliary/cross_crate_ice2.rs",
842842
"macros/rfc-2011-nicer-assert-messages/auxiliary/common.rs",
843-
"imports/ambiguous-1.rs",
844-
"imports/ambiguous-4-extern.rs",
845843
"entry-point/auxiliary/bad_main_functions.rs",
846844
]
847845
.iter()

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ declare_lint_pass! {
1919
AARCH64_SOFTFLOAT_NEON,
2020
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
2121
AMBIGUOUS_ASSOCIATED_ITEMS,
22-
AMBIGUOUS_GLOB_IMPORTS,
2322
AMBIGUOUS_GLOB_REEXPORTS,
2423
ARITHMETIC_OVERFLOW,
2524
ASM_SUB_REGISTER,
@@ -4445,48 +4444,6 @@ declare_lint! {
44454444
Warn,
44464445
"detects diagnostic attribute with malformed diagnostic format literals",
44474446
}
4448-
declare_lint! {
4449-
/// The `ambiguous_glob_imports` lint detects glob imports that should report ambiguity
4450-
/// errors, but previously didn't do that due to rustc bugs.
4451-
///
4452-
/// ### Example
4453-
///
4454-
/// ```rust,compile_fail
4455-
/// #![deny(ambiguous_glob_imports)]
4456-
/// pub fn foo() -> u32 {
4457-
/// use sub::*;
4458-
/// C
4459-
/// }
4460-
///
4461-
/// mod sub {
4462-
/// mod mod1 { pub const C: u32 = 1; }
4463-
/// mod mod2 { pub const C: u32 = 2; }
4464-
///
4465-
/// pub use mod1::*;
4466-
/// pub use mod2::*;
4467-
/// }
4468-
/// ```
4469-
///
4470-
/// {{produces}}
4471-
///
4472-
/// ### Explanation
4473-
///
4474-
/// Previous versions of Rust compile it successfully because it
4475-
/// had lost the ambiguity error when resolve `use sub::mod2::*`.
4476-
///
4477-
/// This is a [future-incompatible] lint to transition this to a
4478-
/// hard error in the future.
4479-
///
4480-
/// [future-incompatible]: ../index.md#future-incompatible-lints
4481-
pub AMBIGUOUS_GLOB_IMPORTS,
4482-
Deny,
4483-
"detects certain glob imports that require reporting an ambiguity error",
4484-
@future_incompatible = FutureIncompatibleInfo {
4485-
reason: FutureIncompatibilityReason::FutureReleaseError,
4486-
reference: "issue #114095 <https://github.com/rust-lang/rust/issues/114095>",
4487-
report_in_deps: true,
4488-
};
4489-
}
44904447

44914448
declare_lint! {
44924449
/// The `refining_impl_trait_reachable` lint detects `impl Trait` return

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
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
}

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use rustc_middle::bug;
2121
use rustc_middle::ty::TyCtxt;
2222
use rustc_session::Session;
2323
use rustc_session::lint::builtin::{
24-
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE, AMBIGUOUS_GLOB_IMPORTS,
25-
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
24+
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE, MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
2625
};
2726
use rustc_session::lint::{AmbiguityErrorDiag, BuiltinLintDiag};
2827
use rustc_session::utils::was_invoked_from_cargo;
@@ -144,21 +143,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
144143

145144
for ambiguity_error in &self.ambiguity_errors {
146145
let diag = self.ambiguity_diagnostics(ambiguity_error);
147-
if ambiguity_error.warning {
148-
let NameBindingKind::Import { import, .. } = ambiguity_error.b1.0.kind else {
149-
unreachable!()
150-
};
151-
self.lint_buffer.buffer_lint(
152-
AMBIGUOUS_GLOB_IMPORTS,
153-
import.root_id,
154-
ambiguity_error.ident.span,
155-
BuiltinLintDiag::AmbiguousGlobImports { diag },
156-
);
157-
} else {
158-
let mut err = struct_span_code_err!(self.dcx(), diag.span, E0659, "{}", diag.msg);
159-
report_ambiguity_error(&mut err, diag);
160-
err.emit();
161-
}
146+
let mut err = struct_span_code_err!(self.dcx(), diag.span, E0659, "{}", diag.msg);
147+
report_ambiguity_error(&mut err, diag);
148+
err.emit();
162149
}
163150

164151
let mut reported_spans = FxHashSet::default();

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/ident.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
728728
ident: orig_ident,
729729
b1: innermost_binding,
730730
b2: binding,
731-
warning: false,
732731
misc1: misc(innermost_flags),
733732
misc2: misc(flags),
734733
});
@@ -1072,7 +1071,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10721071
ident,
10731072
b1: binding,
10741073
b2: shadowed_glob,
1075-
warning: false,
10761074
misc1: AmbiguityErrorMisc::None,
10771075
misc2: AmbiguityErrorMisc::None,
10781076
});

compiler/rustc_resolve/src/imports.rs

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
326326
self.arenas.alloc_name_binding(NameBindingData {
327327
kind: NameBindingKind::Import { binding, import },
328328
ambiguity: None,
329-
warn_ambiguity: false,
330329
span: import.span,
331330
vis,
332331
expansion: import.parent_scope.expansion,
@@ -340,7 +339,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
340339
ident: Ident,
341340
ns: Namespace,
342341
binding: NameBinding<'ra>,
343-
warn_ambiguity: bool,
344342
) -> Result<(), NameBinding<'ra>> {
345343
let res = binding.res();
346344
self.check_reserved_macro_name(ident, res);
@@ -352,7 +350,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
352350
module.underscore_disambiguator.update_unchecked(|d| d + 1);
353351
module.underscore_disambiguator.get()
354352
});
355-
self.update_local_resolution(module, key, warn_ambiguity, |this, resolution| {
353+
self.update_local_resolution(module, key, |this, resolution| {
356354
if let Some(old_binding) = resolution.best_binding() {
357355
if res == Res::Err && old_binding.res() != Res::Err {
358356
// Do not override real bindings with `Res::Err`s from error recovery.
@@ -361,30 +359,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
361359
match (old_binding.is_glob_import(), binding.is_glob_import()) {
362360
(true, true) => {
363361
let (glob_binding, old_glob_binding) = (binding, old_binding);
364-
// FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
365-
if !binding.is_ambiguity_recursive()
366-
&& let NameBindingKind::Import { import: old_import, .. } =
367-
old_glob_binding.kind
368-
&& let NameBindingKind::Import { import, .. } = glob_binding.kind
369-
&& old_import == import
370-
{
371-
// When imported from the same glob-import statement, we should replace
372-
// `old_glob_binding` with `glob_binding`, regardless of whether
373-
// they have the same resolution or not.
374-
resolution.glob_binding = Some(glob_binding);
375-
} else if res != old_glob_binding.res() {
362+
if res != old_glob_binding.res() {
376363
resolution.glob_binding = Some(this.new_ambiguity_binding(
377364
AmbiguityKind::GlobVsGlob,
378365
old_glob_binding,
379366
glob_binding,
380-
warn_ambiguity,
381367
));
382-
} else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
368+
} else if !old_glob_binding.vis.is_at_least(glob_binding.vis, this.tcx)
369+
|| glob_binding.is_ambiguity_recursive()
370+
{
383371
// We are glob-importing the same item but with greater visibility.
384372
resolution.glob_binding = Some(glob_binding);
385-
} else if binding.is_ambiguity_recursive() {
386-
resolution.glob_binding =
387-
Some(this.new_warn_ambiguity_binding(glob_binding));
388373
}
389374
}
390375
(old_glob @ true, false) | (old_glob @ false, true) => {
@@ -398,7 +383,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
398383
AmbiguityKind::GlobVsExpanded,
399384
non_glob_binding,
400385
glob_binding,
401-
false,
402386
));
403387
} else {
404388
resolution.non_glob_binding = Some(non_glob_binding);
@@ -411,9 +395,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
411395
AmbiguityKind::GlobVsGlob,
412396
old_glob_binding,
413397
glob_binding,
414-
false,
415398
));
416-
} else if !old_glob_binding.vis.is_at_least(binding.vis, this.tcx) {
399+
} else if !old_glob_binding.vis.is_at_least(glob_binding.vis, this.tcx)
400+
|| glob_binding.is_ambiguity_recursive()
401+
{
402+
// We are glob-importing the same item but with greater visibility.
417403
resolution.glob_binding = Some(glob_binding);
418404
}
419405
} else {
@@ -441,33 +427,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
441427
ambiguity_kind: AmbiguityKind,
442428
primary_binding: NameBinding<'ra>,
443429
secondary_binding: NameBinding<'ra>,
444-
warn_ambiguity: bool,
445430
) -> NameBinding<'ra> {
446431
let ambiguity = Some((secondary_binding, ambiguity_kind));
447-
let data = NameBindingData { ambiguity, warn_ambiguity, ..*primary_binding };
432+
let data = NameBindingData { ambiguity, ..*primary_binding };
448433
self.arenas.alloc_name_binding(data)
449434
}
450435

451-
fn new_warn_ambiguity_binding(&self, binding: NameBinding<'ra>) -> NameBinding<'ra> {
452-
assert!(binding.is_ambiguity_recursive());
453-
self.arenas.alloc_name_binding(NameBindingData { warn_ambiguity: true, ..*binding })
454-
}
455-
456436
// Use `f` to mutate the resolution of the name in the module.
457437
// If the resolution becomes a success, define it in the module's glob importers.
458-
fn update_local_resolution<T, F>(
459-
&mut self,
460-
module: Module<'ra>,
461-
key: BindingKey,
462-
warn_ambiguity: bool,
463-
f: F,
464-
) -> T
438+
fn update_local_resolution<T, F>(&mut self, module: Module<'ra>, key: BindingKey, f: F) -> T
465439
where
466440
F: FnOnce(&Resolver<'ra, 'tcx>, &mut NameResolution<'ra>) -> T,
467441
{
468442
// Ensure that `resolution` isn't borrowed when defining in the module's glob importers,
469443
// during which the resolution might end up getting re-defined via a glob cycle.
470-
let (binding, t, warn_ambiguity) = {
444+
let (binding, t) = {
471445
let resolution = &mut *self.resolution_or_default(module, key).borrow_mut_unchecked();
472446
let old_binding = resolution.binding();
473447

@@ -476,7 +450,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
476450
if let Some(binding) = resolution.binding()
477451
&& old_binding != Some(binding)
478452
{
479-
(binding, t, warn_ambiguity || old_binding.is_some())
453+
(binding, t)
480454
} else {
481455
return t;
482456
}
@@ -501,7 +475,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
501475
ident.0,
502476
key.ns,
503477
imported_binding,
504-
warn_ambiguity,
505478
);
506479
}
507480
}
@@ -522,11 +495,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
522495
let dummy_binding = self.import(dummy_binding, import);
523496
self.per_ns(|this, ns| {
524497
let module = import.parent_scope.module;
525-
let _ = this.try_define_local(module, target, ns, dummy_binding, false);
498+
let _ = this.try_define_local(module, target, ns, dummy_binding);
526499
// Don't remove underscores from `single_imports`, they were never added.
527500
if target.name != kw::Underscore {
528501
let key = BindingKey::new(target, ns);
529-
this.update_local_resolution(module, key, false, |_, resolution| {
502+
this.update_local_resolution(module, key, |_, resolution| {
530503
resolution.single_imports.swap_remove(&import);
531504
})
532505
}
@@ -916,7 +889,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
916889
this.get_mut_unchecked().update_local_resolution(
917890
parent,
918891
key,
919-
false,
920892
|_, resolution| {
921893
resolution.single_imports.swap_remove(&import);
922894
},
@@ -945,8 +917,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
945917
ImportKind::Single { bindings, .. } => bindings[TypeNS].get().binding(),
946918
_ => None,
947919
};
948-
let ambiguity_errors_len =
949-
|errors: &Vec<AmbiguityError<'_>>| errors.iter().filter(|error| !error.warning).count();
920+
let ambiguity_errors_len = |errors: &Vec<AmbiguityError<'_>>| errors.iter().count();
950921
let prev_ambiguity_errors_len = ambiguity_errors_len(&self.ambiguity_errors);
951922
let finalize = Finalize::with_root_span(import.root_id, import.span, import.root_span);
952923

@@ -1160,9 +1131,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11601131
initial_binding.res()
11611132
});
11621133
let res = binding.res();
1163-
let has_ambiguity_error =
1164-
this.ambiguity_errors.iter().any(|error| !error.warning);
1165-
if res == Res::Err || has_ambiguity_error {
1134+
if res == Res::Err || !this.ambiguity_errors.is_empty() {
11661135
this.dcx()
11671136
.span_delayed_bug(import.span, "some error happened for an import");
11681137
return;
@@ -1520,16 +1489,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15201489
};
15211490
if self.is_accessible_from(binding.vis, scope) {
15221491
let imported_binding = self.import(binding, import);
1523-
let warn_ambiguity = self
1524-
.resolution(import.parent_scope.module, key)
1525-
.and_then(|r| r.binding())
1526-
.is_some_and(|binding| binding.warn_ambiguity_recursive());
15271492
let _ = self.try_define_local(
15281493
import.parent_scope.module,
15291494
key.ident.0,
15301495
key.ns,
15311496
imported_binding,
1532-
warn_ambiguity,
15331497
);
15341498
}
15351499
}
@@ -1554,8 +1518,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15541518

15551519
module.for_each_child(self, |this, ident, _, binding| {
15561520
let res = binding.res().expect_non_local();
1557-
let error_ambiguity = binding.is_ambiguity_recursive() && !binding.warn_ambiguity;
1558-
if res != def::Res::Err && !error_ambiguity {
1521+
if res != def::Res::Err && !binding.is_ambiguity_recursive() {
15591522
let mut reexport_chain = SmallVec::new();
15601523
let mut next_binding = binding;
15611524
while let NameBindingKind::Import { binding, import, .. } = next_binding.kind {

0 commit comments

Comments
 (0)