Skip to content

Commit 0b3e018

Browse files
authored
Rollup merge of #103249 - petrochenkov:revaddids, r=oli-obk
resolve: Revert "Set effective visibilities for imports more precisely" In theory the change was correct, but in practice the use of import items in HIR is limited and hacky, and it expects that (effective) visibilities for all (up to) 3 IDs of the import are set to the value reflecting (effective) visibility of the whole syntactic `use` item rather than its individual components. Fixes #102352 r? `@oli-obk`
2 parents 8440b09 + ba4834c commit 0b3e018

File tree

4 files changed

+26
-36
lines changed

4 files changed

+26
-36
lines changed

compiler/rustc_resolve/src/access_levels.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::NameBindingKind;
2-
use crate::Resolver;
1+
use crate::{ImportKind, NameBindingKind, Resolver};
32
use rustc_ast::ast;
43
use rustc_ast::visit;
54
use rustc_ast::visit::Visitor;
@@ -45,31 +44,40 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
4544
let module = self.r.get_module(module_id.to_def_id()).unwrap();
4645
let resolutions = self.r.resolutions(module);
4746

48-
for (key, name_resolution) in resolutions.borrow().iter() {
47+
for (_, name_resolution) in resolutions.borrow().iter() {
4948
if let Some(mut binding) = name_resolution.borrow().binding() && !binding.is_ambiguity() {
5049
// Set the given binding access level to `AccessLevel::Public` and
5150
// sets the rest of the `use` chain to `AccessLevel::Exported` until
5251
// we hit the actual exported item.
5352

54-
// FIXME: tag and is_public() condition must be deleted,
55-
// but assertion fail occurs in import_id_for_ns
53+
// FIXME: tag and is_public() condition should be removed, but assertions occur.
5654
let tag = if binding.is_import() { AccessLevel::Exported } else { AccessLevel::Public };
5755
if binding.vis.is_public() {
5856
let mut prev_parent_id = module_id;
5957
let mut level = AccessLevel::Public;
6058
while let NameBindingKind::Import { binding: nested_binding, import, .. } =
6159
binding.kind
6260
{
63-
let id = self.r.local_def_id(self.r.import_id_for_ns(import, key.ns));
64-
self.update(
65-
id,
61+
let mut update = |node_id| self.update(
62+
self.r.local_def_id(node_id),
6663
binding.vis.expect_local(),
6764
prev_parent_id,
6865
level,
6966
);
67+
// In theory all the import IDs have individual visibilities and effective
68+
// visibilities, but in practice these IDs go straigth to HIR where all
69+
// their few uses assume that their (effective) visibility applies to the
70+
// whole syntactic `use` item. So we update them all to the maximum value
71+
// among the potential individual effective visibilities. Maybe HIR for
72+
// imports shouldn't use three IDs at all.
73+
update(import.id);
74+
if let ImportKind::Single { additional_ids, .. } = import.kind {
75+
update(additional_ids.0);
76+
update(additional_ids.1);
77+
}
7078

7179
level = AccessLevel::Exported;
72-
prev_parent_id = id;
80+
prev_parent_id = self.r.local_def_id(import.id);
7381
binding = nested_binding;
7482
}
7583
}

compiler/rustc_resolve/src/imports.rs

+1-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::diagnostics::{import_candidates, Suggestion};
44
use crate::Determinacy::{self, *};
5-
use crate::Namespace::{self, *};
5+
use crate::Namespace::*;
66
use crate::{module_to_string, names_to_string, ImportSuggestion};
77
use crate::{AmbiguityKind, BindingKey, ModuleKind, ResolutionError, Resolver, Segment};
88
use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet};
@@ -371,31 +371,6 @@ impl<'a> Resolver<'a> {
371371
self.used_imports.insert(import.id);
372372
}
373373
}
374-
375-
/// Take primary and additional node IDs from an import and select one that corresponds to the
376-
/// given namespace. The logic must match the corresponding logic from `fn lower_use_tree` that
377-
/// assigns resolutons to IDs.
378-
pub(crate) fn import_id_for_ns(&self, import: &Import<'_>, ns: Namespace) -> NodeId {
379-
if let ImportKind::Single { additional_ids: (id1, id2), .. } = import.kind {
380-
if let Some(resolutions) = self.import_res_map.get(&import.id) {
381-
assert!(resolutions[ns].is_some(), "incorrectly finalized import");
382-
return match ns {
383-
TypeNS => import.id,
384-
ValueNS => match resolutions.type_ns {
385-
Some(_) => id1,
386-
None => import.id,
387-
},
388-
MacroNS => match (resolutions.type_ns, resolutions.value_ns) {
389-
(Some(_), Some(_)) => id2,
390-
(Some(_), None) | (None, Some(_)) => id1,
391-
(None, None) => import.id,
392-
},
393-
};
394-
}
395-
}
396-
397-
import.id
398-
}
399374
}
400375

401376
/// An error that may be transformed into a diagnostic later. Used to combine multiple unresolved

src/test/ui/privacy/access_levels.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,6 @@ mod half_public_import {
7070

7171
#[rustc_effective_visibility]
7272
pub use half_public_import::HalfPublicImport; //~ ERROR Public: pub, Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
73+
//~^ ERROR Public: pub, Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
7374

7475
fn main() {}

src/test/ui/privacy/access_levels.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ error: Public: pub, Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
112112
LL | pub use half_public_import::HalfPublicImport;
113113
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114114

115+
error: Public: pub, Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
116+
--> $DIR/access_levels.rs:72:9
117+
|
118+
LL | pub use half_public_import::HalfPublicImport;
119+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120+
115121
error: Public: pub(crate), Exported: pub, Reachable: pub, ReachableFromImplTrait: pub
116122
--> $DIR/access_levels.rs:14:13
117123
|
@@ -124,5 +130,5 @@ error: Public: pub(crate), Exported: pub, Reachable: pub, ReachableFromImplTrait
124130
LL | type B;
125131
| ^^^^^^
126132

127-
error: aborting due to 21 previous errors
133+
error: aborting due to 22 previous errors
128134

0 commit comments

Comments
 (0)