mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 13:33:31 +00:00
Fix associated item visibility in block-local impls
This commit is contained in:
parent
443801755c
commit
83e24fec98
5 changed files with 52 additions and 8 deletions
|
@ -666,8 +666,10 @@ impl DefCollector<'_> {
|
|||
macro_: Macro2Id,
|
||||
vis: &RawVisibility,
|
||||
) {
|
||||
let vis =
|
||||
self.def_map.resolve_visibility(self.db, module_id, vis).unwrap_or(Visibility::Public);
|
||||
let vis = self
|
||||
.def_map
|
||||
.resolve_visibility(self.db, module_id, vis, false)
|
||||
.unwrap_or(Visibility::Public);
|
||||
self.def_map.modules[module_id].scope.declare(macro_.into());
|
||||
self.update(
|
||||
module_id,
|
||||
|
@ -831,7 +833,7 @@ impl DefCollector<'_> {
|
|||
let mut def = directive.status.namespaces();
|
||||
let vis = self
|
||||
.def_map
|
||||
.resolve_visibility(self.db, module_id, &directive.import.visibility)
|
||||
.resolve_visibility(self.db, module_id, &directive.import.visibility, false)
|
||||
.unwrap_or(Visibility::Public);
|
||||
|
||||
match import.kind {
|
||||
|
@ -1547,7 +1549,7 @@ impl ModCollector<'_, '_> {
|
|||
};
|
||||
let resolve_vis = |def_map: &DefMap, visibility| {
|
||||
def_map
|
||||
.resolve_visibility(db, self.module_id, visibility)
|
||||
.resolve_visibility(db, self.module_id, visibility, false)
|
||||
.unwrap_or(Visibility::Public)
|
||||
};
|
||||
|
||||
|
@ -1823,7 +1825,7 @@ impl ModCollector<'_, '_> {
|
|||
) -> LocalModuleId {
|
||||
let def_map = &mut self.def_collector.def_map;
|
||||
let vis = def_map
|
||||
.resolve_visibility(self.def_collector.db, self.module_id, visibility)
|
||||
.resolve_visibility(self.def_collector.db, self.module_id, visibility, false)
|
||||
.unwrap_or(Visibility::Public);
|
||||
let modules = &mut def_map.modules;
|
||||
let origin = match definition {
|
||||
|
|
|
@ -78,6 +78,7 @@ impl DefMap {
|
|||
// pub(path)
|
||||
// ^^^^ this
|
||||
visibility: &RawVisibility,
|
||||
within_impl: bool,
|
||||
) -> Option<Visibility> {
|
||||
let mut vis = match visibility {
|
||||
RawVisibility::Module(path) => {
|
||||
|
@ -102,7 +103,8 @@ impl DefMap {
|
|||
// `super` to its parent (etc.). However, visibilities must only refer to a module in the
|
||||
// DefMap they're written in, so we restrict them when that happens.
|
||||
if let Visibility::Module(m) = vis {
|
||||
if self.block_id() != m.block {
|
||||
// ...unless we're resolving visibility for an associated item in an impl.
|
||||
if self.block_id() != m.block && !within_impl {
|
||||
cov_mark::hit!(adjust_vis_in_block_def_map);
|
||||
vis = Visibility::Module(self.module_id(self.root()));
|
||||
tracing::debug!("visibility {:?} points outside DefMap, adjusting to {:?}", m, vis);
|
||||
|
|
|
@ -214,10 +214,12 @@ impl Resolver {
|
|||
db: &dyn DefDatabase,
|
||||
visibility: &RawVisibility,
|
||||
) -> Option<Visibility> {
|
||||
let within_impl =
|
||||
self.scopes().find(|scope| matches!(scope, Scope::ImplDefScope(_))).is_some();
|
||||
match visibility {
|
||||
RawVisibility::Module(_) => {
|
||||
let (item_map, module) = self.item_scope();
|
||||
item_map.resolve_visibility(db, module, visibility)
|
||||
item_map.resolve_visibility(db, module, visibility, within_impl)
|
||||
}
|
||||
RawVisibility::Public => Some(Visibility::Public),
|
||||
}
|
||||
|
|
|
@ -120,7 +120,7 @@ impl Visibility {
|
|||
self,
|
||||
db: &dyn DefDatabase,
|
||||
def_map: &DefMap,
|
||||
mut from_module: crate::LocalModuleId,
|
||||
mut from_module: LocalModuleId,
|
||||
) -> bool {
|
||||
let mut to_module = match self {
|
||||
Visibility::Module(m) => m,
|
||||
|
|
|
@ -115,6 +115,44 @@ mod module {
|
|||
fn main(s: module::Struct) {
|
||||
s.method();
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_see_through_top_level_anonymous_const() {
|
||||
// regression test for #14046.
|
||||
check_diagnostics(
|
||||
r#"
|
||||
struct S;
|
||||
mod m {
|
||||
const _: () = {
|
||||
impl crate::S {
|
||||
pub(crate) fn method(self) {}
|
||||
pub(crate) const A: usize = 42;
|
||||
}
|
||||
};
|
||||
mod inner {
|
||||
const _: () = {
|
||||
impl crate::S {
|
||||
pub(crate) fn method2(self) {}
|
||||
pub(crate) const B: usize = 42;
|
||||
pub(super) fn private(self) {}
|
||||
pub(super) const PRIVATE: usize = 42;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
S.method();
|
||||
S::A;
|
||||
S.method2();
|
||||
S::B;
|
||||
S.private();
|
||||
//^^^^^^^^^^^ error: function `private` is private
|
||||
S::PRIVATE;
|
||||
//^^^^^^^^^^ error: const `PRIVATE` is private
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue