Skip to content

Commit 470ae47

Browse files
Report glob errors in first phase of commit.
1 parent 0b56fea commit 470ae47

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

@@ -605,17 +605,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
605605
!self.assert_speculative,
606606
"`commit_import_resolutions` should not be called during speculative resolution"
607607
);
608-
self.determined_imports.reserve(self.determined_imports.len());
609608
for (import, side_effect) in import_resolutions.iter() {
610-
self.determined_imports.push(*import);
611609
let SideEffect { imported_module, .. } = side_effect;
612610
import.imported_module.set_unchecked(Some(*imported_module));
613611

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

@@ -666,7 +667,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
666667
}
667668
(ImportKind::Glob { id, .. }, SideEffectBindings::Glob { import_bindings }) => {
668669
let ModuleOrUniformRoot::Module(module) = imported_module else {
669-
self.dcx().emit_err(CannotGlobImportAllCrates { span: import.span });
670670
continue;
671671
};
672672

@@ -681,12 +681,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
681681
.emit();
682682
}
683683

684-
for (binding, key, warn_ambiguity) in import_bindings {
684+
for (binding, key) in import_bindings {
685+
let imported_binding = self.import(binding, import);
686+
let warn_ambiguity = self
687+
.resolution(import.parent_scope.module, key)
688+
.and_then(|r| r.binding())
689+
.is_some_and(|binding| binding.warn_ambiguity_recursive());
685690
let _ = self.try_define_local(
686691
parent,
687692
key.ident.0,
688693
key.ns,
689-
binding,
694+
imported_binding,
690695
warn_ambiguity,
691696
);
692697
}
@@ -971,7 +976,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
971976
///
972977
/// Meanwhile, if resolution is successful, the side effect of the resolution is returned.
973978
fn resolve_import<'r>(
974-
self: &mut CmResolver<'r, 'ra, 'tcx>,
979+
mut self: CmResolver<'r, 'ra, 'tcx>,
975980
import: Import<'ra>,
976981
) -> (Option<SideEffect<'ra>>, usize) {
977982
debug!(
@@ -1016,12 +1021,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10161021

10171022
let mut import_bindings = PerNS::default();
10181023
let mut indeterminate_count = 0;
1019-
self.reborrow().per_ns_cm(|this, ns| {
1024+
1025+
// HACK: Use array of namespaces in the same order as `per_ns_mut`.
1026+
// We can't use `per_ns_cm` because of the invariance on CmResolver (RefOrMut).
1027+
for ns in [TypeNS, ValueNS, MacroNS] {
10201028
if !type_ns_only || ns == TypeNS {
10211029
if bindings[ns].get() != PendingBinding::Pending {
1022-
return;
1030+
continue;
10231031
};
1024-
let binding_result = this.reborrow().maybe_resolve_ident_in_module(
1032+
let binding_result = self.reborrow().maybe_resolve_ident_in_module(
10251033
module,
10261034
source,
10271035
ns,
@@ -1031,7 +1039,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10311039
let pending_binding = match binding_result {
10321040
Ok(binding) => {
10331041
// We need the `target`, `source` can be extracted.
1034-
let imported_binding = this.import(binding, import);
1042+
let imported_binding = self.import(binding, import);
10351043
Some(Some(imported_binding))
10361044
}
10371045
Err(Determinacy::Determined) => Some(None),
@@ -1043,8 +1051,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10431051
// FIXME(batched): Will be fixed in batched import resolution.
10441052
import_bindings[ns] = pending_binding;
10451053
}
1046-
});
1047-
1054+
}
10481055
(
10491056
Some(SideEffect {
10501057
imported_module: module,
@@ -1594,47 +1601,40 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15941601
false
15951602
}
15961603

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

1605-
let ModuleOrUniformRoot::Module(module) = imported_module else {
1606-
return SideEffectBindings::None;
1607-
};
1632+
SideEffectBindings::Glob { import_bindings }
1633+
}
16081634

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

16401640
// 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
@@ -1835,15 +1835,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18351835
f(self, MacroNS);
18361836
}
18371837

1838-
fn per_ns_cm<'r, F: FnMut(&mut CmResolver<'r, 'ra, 'tcx>, Namespace)>(
1839-
mut self: CmResolver<'r, 'ra, 'tcx>,
1840-
mut f: F,
1841-
) {
1842-
f(&mut self, TypeNS);
1843-
f(&mut self, ValueNS);
1844-
f(&mut self, MacroNS);
1845-
}
1846-
18471838
fn is_builtin_macro(&self, res: Res) -> bool {
18481839
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name.is_some())
18491840
}

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)