Skip to content

Commit 2043b9c

Browse files
Report glob errors in first phase of commit.
1 parent 08235d4 commit 2043b9c

File tree

9 files changed

+90
-131
lines changed

9 files changed

+90
-131
lines changed

compiler/rustc_resolve/src/imports.rs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ enum SideEffectBindings<'ra> {
5353
import_bindings: PerNS<Option<Option<NameBinding<'ra>>>>,
5454
},
5555
Glob {
56-
import_bindings: Vec<(NameBinding<'ra>, BindingKey, bool /* warn_ambiguity */)>,
56+
import_bindings: Vec<(NameBinding<'ra>, BindingKey)>,
5757
},
5858
}
5959

@@ -607,17 +607,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
607607
!self.assert_speculative,
608608
"`commit_import_resolutions` should not be called during speculative resolution"
609609
);
610-
self.determined_imports.reserve(self.determined_imports.len());
611610
for (import, side_effect) in import_resolutions.iter() {
612-
self.determined_imports.push(*import);
613611
let SideEffect { imported_module, .. } = side_effect;
614612
import.imported_module.set_unchecked(Some(*imported_module));
615613

616-
if import.is_glob()
617-
&& let ModuleOrUniformRoot::Module(module) = imported_module
618-
&& import.parent_scope.module != *module
619-
{
620-
module.glob_importers.borrow_mut(self).push(*import);
614+
if import.is_glob() {
615+
let ModuleOrUniformRoot::Module(module) = imported_module else {
616+
self.dcx().emit_err(CannotGlobImportAllCrates { span: import.span });
617+
continue;
618+
};
619+
if import.parent_scope.module != *module {
620+
module.glob_importers.borrow_mut_unchecked().push(*import);
621+
}
621622
}
622623
}
623624

@@ -668,7 +669,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
668669
}
669670
(ImportKind::Glob { id, .. }, SideEffectBindings::Glob { import_bindings }) => {
670671
let ModuleOrUniformRoot::Module(module) = imported_module else {
671-
self.dcx().emit_err(CannotGlobImportAllCrates { span: import.span });
672672
continue;
673673
};
674674

@@ -683,12 +683,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
683683
.emit();
684684
}
685685

686-
for (binding, key, warn_ambiguity) in import_bindings {
686+
for (binding, key) in import_bindings {
687+
let imported_binding = self.import(binding, import);
688+
let warn_ambiguity = self
689+
.resolution(import.parent_scope.module, key)
690+
.and_then(|r| r.binding())
691+
.is_some_and(|binding| binding.warn_ambiguity_recursive());
687692
let _ = self.try_define_local(
688693
parent,
689694
key.ident.0,
690695
key.ns,
691-
binding,
696+
imported_binding,
692697
warn_ambiguity,
693698
);
694699
}
@@ -970,7 +975,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
970975
///
971976
/// Meanwhile, if resolution is successful, the side effect of the resolution is returned.
972977
fn resolve_import<'r>(
973-
self: &mut CmResolver<'r, 'ra, 'tcx>,
978+
mut self: CmResolver<'r, 'ra, 'tcx>,
974979
import: Import<'ra>,
975980
) -> (Option<SideEffect<'ra>>, usize) {
976981
debug!(
@@ -1015,12 +1020,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10151020

10161021
let mut import_bindings = PerNS::default();
10171022
let mut indeterminate_count = 0;
1018-
self.reborrow().per_ns_cm(|this, ns| {
1023+
1024+
// HACK: Use array of namespaces in the same order as `per_ns_mut`.
1025+
// We can't use `per_ns_cm` because of the invariance on CmResolver (RefOrMut).
1026+
for ns in [TypeNS, ValueNS, MacroNS] {
10191027
if !type_ns_only || ns == TypeNS {
10201028
if bindings[ns].get() != PendingBinding::Pending {
1021-
return;
1029+
continue;
10221030
};
1023-
let binding_result = this.reborrow().maybe_resolve_ident_in_module(
1031+
let binding_result = self.reborrow().maybe_resolve_ident_in_module(
10241032
module,
10251033
source,
10261034
ns,
@@ -1030,7 +1038,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10301038
let pending_binding = match binding_result {
10311039
Ok(binding) => {
10321040
// We need the `target`, `source` can be extracted.
1033-
let imported_binding = this.import(binding, import);
1041+
let imported_binding = self.import(binding, import);
10341042
Some(Some(imported_binding))
10351043
}
10361044
Err(Determinacy::Determined) => Some(None),
@@ -1042,8 +1050,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10421050
// FIXME(batched): Will be fixed in batched import resolution.
10431051
import_bindings[ns] = pending_binding;
10441052
}
1045-
});
1046-
1053+
}
10471054
(
10481055
Some(SideEffect {
10491056
imported_module: module,
@@ -1593,47 +1600,40 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15931600
false
15941601
}
15951602

1596-
fn resolve_glob_import<'r>(
1597-
self: &mut CmResolver<'r, 'ra, 'tcx>,
1603+
fn resolve_glob_import(
1604+
&self,
15981605
import: Import<'ra>,
15991606
imported_module: ModuleOrUniformRoot<'ra>,
16001607
) -> SideEffectBindings<'ra> {
1601-
// This function is only called for glob imports.
1602-
let ImportKind::Glob { .. } = import.kind else { unreachable!() };
1608+
match imported_module {
1609+
ModuleOrUniformRoot::Module(module) if module != import.parent_scope.module => {
1610+
let import_bindings = self
1611+
.resolutions(module)
1612+
.borrow()
1613+
.iter()
1614+
.filter_map(|(key, resolution)| {
1615+
let binding = resolution.borrow().binding()?;
1616+
let mut key = *key;
1617+
let scope = match key
1618+
.ident
1619+
.0
1620+
.span
1621+
.reverse_glob_adjust(module.expansion, import.span)
1622+
{
1623+
Some(Some(def)) => self.expn_def_scope(def),
1624+
Some(None) => import.parent_scope.module,
1625+
None => return None,
1626+
};
1627+
self.is_accessible_from(binding.vis, scope).then(|| (binding, key))
1628+
})
1629+
.collect::<Vec<_>>();
16031630

1604-
let ModuleOrUniformRoot::Module(module) = imported_module else {
1605-
return SideEffectBindings::None;
1606-
};
1631+
SideEffectBindings::Glob { import_bindings }
1632+
}
16071633

1608-
if module == import.parent_scope.module {
1609-
return SideEffectBindings::None;
1634+
// Errors are reported in `commit_imports_resolutions`
1635+
_ => SideEffectBindings::None,
16101636
}
1611-
1612-
let import_bindings = self
1613-
.resolutions(module)
1614-
.borrow()
1615-
.iter()
1616-
.filter_map(|(key, resolution)| {
1617-
let binding = resolution.borrow().binding()?;
1618-
let mut key = *key;
1619-
let scope =
1620-
match key.ident.0.span.reverse_glob_adjust(module.expansion, import.span) {
1621-
Some(Some(def)) => self.expn_def_scope(def),
1622-
Some(None) => import.parent_scope.module,
1623-
None => return None,
1624-
};
1625-
self.is_accessible_from(binding.vis, scope).then(|| {
1626-
let imported_binding = self.import(binding, import);
1627-
let warn_ambiguity = self
1628-
.resolution(import.parent_scope.module, key)
1629-
.and_then(|r| r.binding())
1630-
.is_some_and(|binding| binding.warn_ambiguity_recursive());
1631-
(imported_binding, key, warn_ambiguity)
1632-
})
1633-
})
1634-
.collect::<Vec<_>>();
1635-
1636-
SideEffectBindings::Glob { import_bindings }
16371637
}
16381638

16391639
// Miscellaneous post-processing, including recording re-exports,

compiler/rustc_resolve/src/lib.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,15 +1841,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18411841
f(self, MacroNS);
18421842
}
18431843

1844-
fn per_ns_cm<'r, F: FnMut(&mut CmResolver<'r, 'ra, 'tcx>, Namespace)>(
1845-
mut self: CmResolver<'r, 'ra, 'tcx>,
1846-
mut f: F,
1847-
) {
1848-
f(&mut self, TypeNS);
1849-
f(&mut self, ValueNS);
1850-
f(&mut self, MacroNS);
1851-
}
1852-
18531844
fn is_builtin_macro(&self, res: Res) -> bool {
18541845
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name.is_some())
18551846
}

tests/ui/imports/ambiguous-9.stderr

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,12 @@ LL | pub use super::dsl::*;
2424
| ^^^^^^^^^^^^^
2525
= help: consider adding an explicit import of `date_range` to disambiguate
2626
note: `date_range` could also refer to the function imported here
27-
--> $DIR/ambiguous-9.rs:8:9
28-
|
29-
LL | use super::prelude::*;
30-
| ^^^^^^^^^^^^^^^^^
31-
= help: consider adding an explicit import of `date_range` to disambiguate
32-
= note: `#[deny(ambiguous_glob_imports)]` (part of `#[deny(future_incompatible)]`) on by default
33-
34-
warning: ambiguous glob re-exports
3527
--> $DIR/ambiguous-9.rs:15:13
3628
|
3729
LL | pub use self::t::*;
3830
| ^^^^^^^^^^
3931
= help: consider adding an explicit import of `date_range` to disambiguate
40-
= note: `#[deny(ambiguous_glob_imports)]` on by default
32+
= note: `#[deny(ambiguous_glob_imports)]` (part of `#[deny(future_incompatible)]`) on by default
4133

4234
error: aborting due to 1 previous error; 1 warning emitted
4335

@@ -60,32 +52,8 @@ LL | pub use super::dsl::*;
6052
note: `date_range` could also refer to the function imported here
6153
--> $DIR/ambiguous-9.rs:15:13
6254
|
63-
LL | use super::prelude::*;
64-
| ^^^^^^^^^^^^^^^^^
65-
= help: consider adding an explicit import of `date_range` to disambiguate
66-
= note: `#[deny(ambiguous_glob_imports)]` (part of `#[deny(future_incompatible)]`) on by default
67-
68-
Future breakage diagnostic:
69-
error: `date_range` is ambiguous
70-
--> $DIR/ambiguous-9.rs:23:5
71-
|
72-
LL | date_range();
73-
| ^^^^^^^^^^ ambiguous name
74-
|
75-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
76-
= note: for more information, see issue #114095 <https://github.com/rust-lang/rust/issues/114095>
77-
= note: ambiguous because of multiple glob imports of a name in the same module
78-
note: `date_range` could refer to the function imported here
79-
--> $DIR/ambiguous-9.rs:19:5
80-
|
81-
LL | use dsl::*;
82-
| ^^^^^^
83-
= help: consider adding an explicit import of `date_range` to disambiguate
84-
note: `date_range` could also refer to the function imported here
85-
--> $DIR/ambiguous-9.rs:20:5
86-
|
87-
LL | use prelude::*;
88-
| ^^^^^^^^^^
55+
LL | pub use self::t::*;
56+
| ^^^^^^^^^^
8957
= help: consider adding an explicit import of `date_range` to disambiguate
9058
= note: `#[deny(ambiguous_glob_imports)]` (part of `#[deny(future_incompatible)]`) on by default
9159

tests/ui/imports/import-loop-2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
mod a {
2-
pub use crate::b::x;
2+
pub use crate::b::x; //~ ERROR unresolved import `crate::b::x`
33
}
44

55
mod b {
6-
pub use crate::a::x; //~ ERROR unresolved import `crate::a::x`
6+
pub use crate::a::x;
77

88
fn main() { let y = x; }
99
}

tests/ui/imports/import-loop-2.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0432]: unresolved import `crate::a::x`
2-
--> $DIR/import-loop-2.rs:6:13
1+
error[E0432]: unresolved import `crate::b::x`
2+
--> $DIR/import-loop-2.rs:2:13
33
|
4-
LL | pub use crate::a::x;
5-
| ^^^^^^^^^^^ no `x` in `a`
4+
LL | pub use crate::b::x;
5+
| ^^^^^^^^^^^ no `x` in `b`
66

77
error: aborting due to 1 previous error
88

tests/ui/imports/import4.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
mod a { pub use crate::b::foo; }
2-
mod b { pub use crate::a::foo; } //~ ERROR unresolved import `crate::a::foo`
1+
mod a { pub use crate::b::foo; } //~ ERROR unresolved import `crate::b::foo`
2+
mod b { pub use crate::a::foo; }
33

44
fn main() { println!("loop"); }

tests/ui/imports/import4.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0432]: unresolved import `crate::a::foo`
2-
--> $DIR/import4.rs:2:17
1+
error[E0432]: unresolved import `crate::b::foo`
2+
--> $DIR/import4.rs:1:17
33
|
4-
LL | mod b { pub use crate::a::foo; }
5-
| ^^^^^^^^^^^^^ no `foo` in `a`
4+
LL | mod a { pub use crate::b::foo; }
5+
| ^^^^^^^^^^^^^ no `foo` in `b`
66

77
error: aborting due to 1 previous error
88

tests/ui/imports/reexports.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,33 @@ LL | pub use super::foo;
1111
| ^^^^^^^^^^
1212

1313
error[E0603]: module import `foo` is private
14-
--> $DIR/reexports.rs:37:22
14+
--> $DIR/reexports.rs:36:22
1515
|
16-
LL | use crate::b::b::foo::S as T;
16+
LL | use crate::b::a::foo::S;
1717
| ^^^ private module import
1818
|
1919
note: the module import `foo` is defined here...
20-
--> $DIR/reexports.rs:29:17
20+
--> $DIR/reexports.rs:24:17
2121
|
22-
LL | pub use super::*; // This is also OK since the value `foo` is visible enough.
23-
| ^^^^^^^^
22+
LL | pub use super::foo; // This is OK since the value `foo` is visible enough.
23+
| ^^^^^^^^^^
2424
note: ...and refers to the module `foo` which is defined here
2525
--> $DIR/reexports.rs:19:5
2626
|
2727
LL | mod foo {
2828
| ^^^^^^^
2929

3030
error[E0603]: module import `foo` is private
31-
--> $DIR/reexports.rs:36:22
31+
--> $DIR/reexports.rs:37:22
3232
|
33-
LL | use crate::b::a::foo::S;
33+
LL | use crate::b::b::foo::S as T;
3434
| ^^^ private module import
3535
|
3636
note: the module import `foo` is defined here...
37-
--> $DIR/reexports.rs:24:17
37+
--> $DIR/reexports.rs:29:17
3838
|
39-
LL | pub use super::foo; // This is OK since the value `foo` is visible enough.
40-
| ^^^^^^^^^^
39+
LL | pub use super::*; // This is also OK since the value `foo` is visible enough.
40+
| ^^^^^^^^
4141
note: ...and refers to the module `foo` which is defined here
4242
--> $DIR/reexports.rs:19:5
4343
|

tests/ui/privacy/privacy1.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ LL | mod baz {
2323
| ^^^^^^^
2424
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2525

26-
error[E0603]: module `baz` is private
27-
--> $DIR/privacy1.rs:147:18
28-
|
29-
LL | use bar::baz;
30-
| ^^^ private module
31-
|
32-
note: the module `baz` is defined here
33-
--> $DIR/privacy1.rs:56:5
34-
|
35-
LL | mod baz {
36-
| ^^^^^^^
37-
3826
error[E0603]: module `i` is private
3927
--> $DIR/privacy1.rs:171:20
4028
|
@@ -47,6 +35,18 @@ note: the module `i` is defined here
4735
LL | mod i {
4836
| ^^^^^
4937

38+
error[E0603]: module `baz` is private
39+
--> $DIR/privacy1.rs:147:18
40+
|
41+
LL | use bar::baz;
42+
| ^^^ private module
43+
|
44+
note: the module `baz` is defined here
45+
--> $DIR/privacy1.rs:56:5
46+
|
47+
LL | mod baz {
48+
| ^^^^^^^
49+
5050
error[E0603]: module `baz` is private
5151
--> $DIR/privacy1.rs:110:21
5252
|

0 commit comments

Comments
 (0)