Skip to content

Commit 09bc7ca

Browse files
committed
Reduce visibility
1 parent c3a4c44 commit 09bc7ca

File tree

3 files changed

+81
-76
lines changed

3 files changed

+81
-76
lines changed

crates/ra_hir/src/from_id.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ from_id![
4040
(hir_def::ConstId, crate::Const),
4141
(hir_def::FunctionId, crate::Function),
4242
(hir_def::ImplId, crate::ImplBlock),
43+
(hir_def::TypeParamId, crate::TypeParam),
4344
(hir_expand::MacroDefId, crate::MacroDef)
4445
];
4546

crates/ra_hir/src/semantics.rs

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ use std::{cell::RefCell, fmt, iter::successors};
44

55
use hir_def::{
66
resolver::{self, HasResolver, Resolver},
7-
TraitId,
7+
DefWithBodyId, TraitId,
88
};
99
use ra_db::{FileId, FileRange};
10-
use ra_syntax::{ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextUnit};
10+
use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextUnit};
1111
use rustc_hash::{FxHashMap, FxHashSet};
1212

1313
use crate::{
1414
db::HirDatabase,
1515
source_analyzer::{resolve_hir_path, ReferenceDescriptor, SourceAnalyzer},
16-
source_binder::{ChildContainer, SourceBinder, ToDef},
16+
source_binder::{ChildContainer, SourceBinder},
1717
Function, HirFileId, InFile, Local, MacroDef, Module, Name, Origin, Path, PathResolution,
1818
ScopeDef, StructField, Trait, Type, TypeParam, VariantDef,
1919
};
@@ -129,9 +129,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
129129
// pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>;
130130

131131
pub fn to_def<T: ToDef + Clone>(&self, src: &T) -> Option<T::Def> {
132-
let src = self.find_file(src.syntax().clone()).with_value(src.clone());
133-
let mut sb = self.sb.borrow_mut();
134-
T::to_def(self.db, &mut sb, src)
132+
T::to_def(self, src)
135133
}
136134

137135
pub fn to_module_def(&self, file: FileId) -> Option<Module> {
@@ -227,6 +225,68 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
227225
}
228226
}
229227

228+
pub trait ToDef: Sized + AstNode + 'static {
229+
type Def;
230+
fn to_def<DB: HirDatabase>(sema: &Semantics<DB>, src: &Self) -> Option<Self::Def>;
231+
}
232+
233+
macro_rules! to_def_impls {
234+
($(($def:path, $ast:path)),* ,) => {$(
235+
impl ToDef for $ast {
236+
type Def = $def;
237+
fn to_def<DB: HirDatabase>(sema: &Semantics<DB>, src: &Self)
238+
-> Option<Self::Def>
239+
{
240+
let src = sema.find_file(src.syntax().clone()).with_value(src);
241+
sema.sb.borrow_mut().to_id(sema.db, src.cloned()).map(Into::into)
242+
}
243+
}
244+
)*}
245+
}
246+
247+
to_def_impls![
248+
(crate::Module, ast::Module),
249+
(crate::Struct, ast::StructDef),
250+
(crate::Enum, ast::EnumDef),
251+
(crate::Union, ast::UnionDef),
252+
(crate::Trait, ast::TraitDef),
253+
(crate::ImplBlock, ast::ImplBlock),
254+
(crate::TypeAlias, ast::TypeAliasDef),
255+
(crate::Const, ast::ConstDef),
256+
(crate::Static, ast::StaticDef),
257+
(crate::Function, ast::FnDef),
258+
(crate::StructField, ast::RecordFieldDef),
259+
(crate::EnumVariant, ast::EnumVariant),
260+
(crate::TypeParam, ast::TypeParam),
261+
(crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros
262+
];
263+
264+
impl ToDef for ast::BindPat {
265+
type Def = Local;
266+
267+
fn to_def<DB: HirDatabase>(sema: &Semantics<DB>, src: &Self) -> Option<Local> {
268+
let src = sema.find_file(src.syntax().clone()).with_value(src);
269+
let file_id = src.file_id;
270+
let mut sb = sema.sb.borrow_mut();
271+
let db = sema.db;
272+
let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| {
273+
let res = match_ast! {
274+
match it {
275+
ast::ConstDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
276+
ast::StaticDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
277+
ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
278+
_ => return None,
279+
}
280+
};
281+
Some(res)
282+
})?;
283+
let (_body, source_map) = db.body_with_source_map(parent);
284+
let src = src.cloned().map(ast::Pat::from);
285+
let pat_id = source_map.node_pat(src.as_ref())?;
286+
Some(Local { parent: parent.into(), pat_id })
287+
}
288+
}
289+
230290
fn find_root(node: &SyntaxNode) -> SyntaxNode {
231291
node.ancestors().last().unwrap()
232292
}

crates/ra_hir/src/source_binder.rs

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hir_def::{
66
dyn_map::DynMap,
77
keys::{self, Key},
88
ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId,
9-
StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId,
9+
StaticId, StructFieldId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
1010
};
1111
use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind};
1212
use ra_db::FileId;
@@ -17,9 +17,9 @@ use ra_syntax::{
1717
};
1818
use rustc_hash::FxHashMap;
1919

20-
use crate::{db::HirDatabase, Local, Module, TypeParam};
20+
use crate::{db::HirDatabase, Module};
2121

22-
pub struct SourceBinder {
22+
pub(crate) struct SourceBinder {
2323
child_by_source_cache: FxHashMap<ChildContainer, DynMap>,
2424
}
2525

@@ -38,7 +38,11 @@ impl SourceBinder {
3838
Some(Module { id: ModuleId { krate, local_id } })
3939
}
4040

41-
fn to_id<T: ToId>(&mut self, db: &impl HirDatabase, src: InFile<T>) -> Option<T::ID> {
41+
pub(crate) fn to_id<T: ToId>(
42+
&mut self,
43+
db: &impl HirDatabase,
44+
src: InFile<T>,
45+
) -> Option<T::ID> {
4246
T::to_id(db, self, src)
4347
}
4448

@@ -118,42 +122,6 @@ pub(crate) trait ToId: Sized {
118122
) -> Option<Self::ID>;
119123
}
120124

121-
pub trait ToDef: Sized + AstNode + 'static {
122-
type Def;
123-
fn to_def<DB: HirDatabase>(
124-
db: &DB,
125-
sb: &mut SourceBinder,
126-
src: InFile<Self>,
127-
) -> Option<Self::Def>;
128-
}
129-
130-
macro_rules! to_def_impls {
131-
($(($def:path, $ast:path)),* ,) => {$(
132-
impl ToDef for $ast {
133-
type Def = $def;
134-
fn to_def<DB: HirDatabase>(db: &DB, sb: &mut SourceBinder, src: InFile<Self>)
135-
-> Option<Self::Def>
136-
{ sb.to_id(db, src).map(Into::into) }
137-
}
138-
)*}
139-
}
140-
141-
to_def_impls![
142-
(crate::Module, ast::Module),
143-
(crate::Struct, ast::StructDef),
144-
(crate::Enum, ast::EnumDef),
145-
(crate::Union, ast::UnionDef),
146-
(crate::Trait, ast::TraitDef),
147-
(crate::ImplBlock, ast::ImplBlock),
148-
(crate::TypeAlias, ast::TypeAliasDef),
149-
(crate::Const, ast::ConstDef),
150-
(crate::Static, ast::StaticDef),
151-
(crate::Function, ast::FnDef),
152-
(crate::StructField, ast::RecordFieldDef),
153-
(crate::EnumVariant, ast::EnumVariant),
154-
(crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros
155-
];
156-
157125
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
158126
pub(crate) enum ChildContainer {
159127
DefWithBodyId(DefWithBodyId),
@@ -245,37 +213,14 @@ impl ToId for ast::MacroCall {
245213
}
246214
}
247215

248-
impl ToDef for ast::BindPat {
249-
type Def = Local;
216+
impl ToId for ast::TypeParam {
217+
type ID = TypeParamId;
250218

251-
fn to_def<DB: HirDatabase>(db: &DB, sb: &mut SourceBinder, src: InFile<Self>) -> Option<Local> {
252-
let file_id = src.file_id;
253-
let parent: DefWithBodyId = src.value.syntax().ancestors().find_map(|it| {
254-
let res = match_ast! {
255-
match it {
256-
ast::ConstDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
257-
ast::StaticDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
258-
ast::FnDef(value) => { sb.to_id(db, InFile { value, file_id})?.into() },
259-
_ => return None,
260-
}
261-
};
262-
Some(res)
263-
})?;
264-
let (_body, source_map) = db.body_with_source_map(parent);
265-
let src = src.map(ast::Pat::from);
266-
let pat_id = source_map.node_pat(src.as_ref())?;
267-
Some(Local { parent: parent.into(), pat_id })
268-
}
269-
}
270-
271-
impl ToDef for ast::TypeParam {
272-
type Def = TypeParam;
273-
274-
fn to_def<DB: HirDatabase>(
219+
fn to_id<DB: HirDatabase>(
275220
db: &DB,
276221
sb: &mut SourceBinder,
277-
src: InFile<ast::TypeParam>,
278-
) -> Option<TypeParam> {
222+
src: InFile<Self>,
223+
) -> Option<Self::ID> {
279224
let file_id = src.file_id;
280225
let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| {
281226
let res = match_ast! {
@@ -291,8 +236,7 @@ impl ToDef for ast::TypeParam {
291236
};
292237
Some(res)
293238
})?;
294-
let &id = sb.child_by_source(db, parent.into())[keys::TYPE_PARAM].get(&src)?;
295-
Some(TypeParam { id })
239+
sb.child_by_source(db, parent.into())[keys::TYPE_PARAM].get(&src).copied()
296240
}
297241
}
298242

0 commit comments

Comments
 (0)