Skip to content

Commit

Permalink
fix: Fix item tree lowering pub(self) to pub()
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Dec 8, 2023
1 parent 2d879e0 commit d54745a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
12 changes: 12 additions & 0 deletions crates/hir-def/src/item_tree/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,15 @@ struct S<#[cfg(never)] T>;
"#]],
)
}

#[test]
fn pub_self() {
check(
r#"
pub(self) struct S;
"#,
expect![[r#"
pub(self) struct S;
"#]],
)
}
14 changes: 5 additions & 9 deletions crates/hir-def/src/nameres/path_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,11 @@ impl DefMap {
if remaining.is_some() {
return None;
}
let types = result.take_types();

match (types, path.kind) {
(Some(ModuleDefId::ModuleId(m)), _) => Visibility::Module(m),
// resolve_path doesn't find any values for a plan pathkind of a private function
(None, PathKind::Plain | PathKind::Crate) => {
Visibility::Module(self.module_id(original_module))
}
(_, _) => {
let types = result.take_types()?;
match types {
ModuleDefId::ModuleId(m) => Visibility::Module(m),
// error: visibility needs to refer to module
_ => {
return None;
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl RawVisibility {
RawVisibility::Module(path)
}
ast::VisibilityKind::PubSelf => {
let path = ModPath::from_kind(PathKind::Plain);
let path = ModPath::from_kind(PathKind::Super(0));
RawVisibility::Module(path)
}
ast::VisibilityKind::Pub => RawVisibility::Public,
Expand Down
29 changes: 9 additions & 20 deletions crates/ide-completion/src/completions/expr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Completion of names from the current scope in expression position.
use hir::{HasVisibility, Module, ScopeDef};
use hir::ScopeDef;
use syntax::ast;

use crate::{
Expand All @@ -9,23 +9,6 @@ use crate::{
CompletionContext, Completions,
};

fn scope_def_applicable(
def: ScopeDef,
ctx: &CompletionContext<'_>,
module: Option<&Module>,
) -> bool {
match (def, module) {
(ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) | ScopeDef::Label(_), _) => {
false
}
(ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)), _) => mac.is_fn_like(ctx.db),
(ScopeDef::ModuleDef(hir::ModuleDef::Function(f)), Some(m)) => {
f.is_visible_from(ctx.db, *m)
}
_ => true,
}
}

pub(crate) fn complete_expr_path(
acc: &mut Completions,
ctx: &CompletionContext<'_>,
Expand Down Expand Up @@ -54,6 +37,12 @@ pub(crate) fn complete_expr_path(
let wants_mut_token =
ref_expr_parent.as_ref().map(|it| it.mut_token().is_none()).unwrap_or(false);

let scope_def_applicable = |def| match def {
ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) | ScopeDef::Label(_) => false,
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => mac.is_fn_like(ctx.db),
_ => true,
};

let add_assoc_item = |acc: &mut Completions, item| match item {
hir::AssocItem::Function(func) => acc.add_function(ctx, path_ctx, func, None),
hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
Expand Down Expand Up @@ -98,7 +87,7 @@ pub(crate) fn complete_expr_path(
hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
let module_scope = module.scope(ctx.db, Some(ctx.module));
for (name, def) in module_scope {
if scope_def_applicable(def, ctx, Some(module)) {
if scope_def_applicable(def) {
acc.add_path_resolution(
ctx,
path_ctx,
Expand Down Expand Up @@ -244,7 +233,7 @@ pub(crate) fn complete_expr_path(
[..] => acc.add_path_resolution(ctx, path_ctx, name, def, doc_aliases),
}
}
_ if scope_def_applicable(def, ctx, None) => {
_ if scope_def_applicable(def) => {
acc.add_path_resolution(ctx, path_ctx, name, def, doc_aliases)
}
_ => (),
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-completion/src/tests/special.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ fn completes_only_public() {
r#"
//- /e.rs
pub(self) fn i_should_be_hidden() {}
pub(in crate::krate) fn i_should_also_be_hidden() {}
pub(in crate::e) fn i_should_also_be_hidden() {}
pub fn i_am_public () {}
//- /lib.rs crate:krate
Expand Down

0 comments on commit d54745a

Please sign in to comment.