Skip to content

Commit 1aac2c6

Browse files
authored
Merge pull request #18448 from Veykril/push-zwuuoytnmnqz
Move `child_by_source` from `hir-def` to `hir`
2 parents fb8ca32 + 70348fa commit 1aac2c6

File tree

9 files changed

+88
-90
lines changed

9 files changed

+88
-90
lines changed

crates/hir-def/src/dyn_map.rs

+73-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,79 @@
2121
//!
2222
//! This is a work of fiction. Any similarities to Kotlin's `BindingContext` are
2323
//! a coincidence.
24-
pub mod keys;
24+
25+
pub mod keys {
26+
use std::marker::PhantomData;
27+
28+
use hir_expand::{attrs::AttrId, MacroCallId};
29+
use rustc_hash::FxHashMap;
30+
use syntax::{ast, AstNode, AstPtr};
31+
32+
use crate::{
33+
dyn_map::{DynMap, Policy},
34+
BlockId, ConstId, EnumId, EnumVariantId, ExternCrateId, FieldId, FunctionId, ImplId,
35+
LifetimeParamId, Macro2Id, MacroRulesId, ProcMacroId, StaticId, StructId, TraitAliasId,
36+
TraitId, TypeAliasId, TypeOrConstParamId, UnionId, UseId,
37+
};
38+
39+
pub type Key<K, V> = crate::dyn_map::Key<AstPtr<K>, V, AstPtrPolicy<K, V>>;
40+
41+
pub const BLOCK: Key<ast::BlockExpr, BlockId> = Key::new();
42+
pub const FUNCTION: Key<ast::Fn, FunctionId> = Key::new();
43+
pub const CONST: Key<ast::Const, ConstId> = Key::new();
44+
pub const STATIC: Key<ast::Static, StaticId> = Key::new();
45+
pub const TYPE_ALIAS: Key<ast::TypeAlias, TypeAliasId> = Key::new();
46+
pub const IMPL: Key<ast::Impl, ImplId> = Key::new();
47+
pub const TRAIT: Key<ast::Trait, TraitId> = Key::new();
48+
pub const TRAIT_ALIAS: Key<ast::TraitAlias, TraitAliasId> = Key::new();
49+
pub const STRUCT: Key<ast::Struct, StructId> = Key::new();
50+
pub const UNION: Key<ast::Union, UnionId> = Key::new();
51+
pub const ENUM: Key<ast::Enum, EnumId> = Key::new();
52+
pub const EXTERN_CRATE: Key<ast::ExternCrate, ExternCrateId> = Key::new();
53+
pub const USE: Key<ast::Use, UseId> = Key::new();
54+
55+
pub const ENUM_VARIANT: Key<ast::Variant, EnumVariantId> = Key::new();
56+
pub const TUPLE_FIELD: Key<ast::TupleField, FieldId> = Key::new();
57+
pub const RECORD_FIELD: Key<ast::RecordField, FieldId> = Key::new();
58+
pub const TYPE_PARAM: Key<ast::TypeParam, TypeOrConstParamId> = Key::new();
59+
pub const CONST_PARAM: Key<ast::ConstParam, TypeOrConstParamId> = Key::new();
60+
pub const LIFETIME_PARAM: Key<ast::LifetimeParam, LifetimeParamId> = Key::new();
61+
62+
pub const MACRO_RULES: Key<ast::MacroRules, MacroRulesId> = Key::new();
63+
pub const MACRO2: Key<ast::MacroDef, Macro2Id> = Key::new();
64+
pub const PROC_MACRO: Key<ast::Fn, ProcMacroId> = Key::new();
65+
pub const MACRO_CALL: Key<ast::MacroCall, MacroCallId> = Key::new();
66+
pub const ATTR_MACRO_CALL: Key<ast::Item, MacroCallId> = Key::new();
67+
pub const DERIVE_MACRO_CALL: Key<ast::Attr, (AttrId, MacroCallId, Box<[Option<MacroCallId>]>)> =
68+
Key::new();
69+
70+
/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
71+
/// equal if they point to exactly the same object.
72+
///
73+
/// In general, we do not guarantee that we have exactly one instance of a
74+
/// syntax tree for each file. We probably should add such guarantee, but, for
75+
/// the time being, we will use identity-less AstPtr comparison.
76+
pub struct AstPtrPolicy<AST, ID> {
77+
_phantom: PhantomData<(AST, ID)>,
78+
}
79+
80+
impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> {
81+
type K = AstPtr<AST>;
82+
type V = ID;
83+
fn insert(map: &mut DynMap, key: AstPtr<AST>, value: ID) {
84+
map.map
85+
.entry::<FxHashMap<AstPtr<AST>, ID>>()
86+
.or_insert_with(Default::default)
87+
.insert(key, value);
88+
}
89+
fn get<'a>(map: &'a DynMap, key: &AstPtr<AST>) -> Option<&'a ID> {
90+
map.map.get::<FxHashMap<AstPtr<AST>, ID>>()?.get(key)
91+
}
92+
fn is_empty(map: &DynMap) -> bool {
93+
map.map.get::<FxHashMap<AstPtr<AST>, ID>>().map_or(true, |it| it.is_empty())
94+
}
95+
}
96+
}
2597

2698
use std::{
2799
hash::Hash,

crates/hir-def/src/dyn_map/keys.rs

-72
This file was deleted.

crates/hir-def/src/item_scope.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,7 @@ impl ItemScope {
361361
self.macro_invocations.get(&call).copied()
362362
}
363363

364-
pub(crate) fn iter_macro_invoc(
365-
&self,
366-
) -> impl Iterator<Item = (&AstId<ast::MacroCall>, &MacroCallId)> {
364+
pub fn iter_macro_invoc(&self) -> impl Iterator<Item = (&AstId<ast::MacroCall>, &MacroCallId)> {
367365
self.macro_invocations.iter()
368366
}
369367
}
@@ -401,9 +399,7 @@ impl ItemScope {
401399
self.macro_invocations.insert(call, call_id);
402400
}
403401

404-
pub(crate) fn attr_macro_invocs(
405-
&self,
406-
) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
402+
pub fn attr_macro_invocs(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
407403
self.attr_macros.iter().map(|(k, v)| (*k, *v))
408404
}
409405

@@ -440,7 +436,7 @@ impl ItemScope {
440436
});
441437
}
442438

443-
pub(crate) fn derive_macro_invocs(
439+
pub fn derive_macro_invocs(
444440
&self,
445441
) -> impl Iterator<
446442
Item = (

crates/hir-def/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub mod resolver;
4747

4848
pub mod nameres;
4949

50-
pub mod child_by_source;
5150
pub mod src;
5251

5352
pub mod find_path;
@@ -354,9 +353,9 @@ impl_loc!(ProcMacroLoc, id: Function, container: CrateRootModuleId);
354353
pub struct BlockId(ra_salsa::InternId);
355354
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
356355
pub struct BlockLoc {
357-
ast_id: AstId<ast::BlockExpr>,
356+
pub ast_id: AstId<ast::BlockExpr>,
358357
/// The containing module.
359-
module: ModuleId,
358+
pub module: ModuleId,
360359
}
361360
impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
362361

@@ -935,7 +934,7 @@ impl_from!(
935934
);
936935

937936
impl GenericDefId {
938-
fn file_id_and_params_of(
937+
pub fn file_id_and_params_of(
939938
self,
940939
db: &dyn DefDatabase,
941940
) -> (HirFileId, Option<ast::GenericParamList>) {

crates/hir/src/semantics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! See `Semantics`.
22
3+
mod child_by_source;
34
mod source_to_def;
45

56
use std::{

crates/hir-def/src/child_by_source.rs renamed to crates/hir/src/semantics/child_by_source.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use either::Either;
88
use hir_expand::{attrs::collect_attrs, HirFileId};
99
use syntax::{ast, AstPtr};
1010

11-
use crate::{
11+
use hir_def::{
1212
db::DefDatabase,
1313
dyn_map::{
1414
keys::{self, Key},
@@ -23,7 +23,7 @@ use crate::{
2323
VariantId,
2424
};
2525

26-
pub trait ChildBySource {
26+
pub(crate) trait ChildBySource {
2727
fn child_by_source(&self, db: &dyn DefDatabase, file_id: HirFileId) -> DynMap {
2828
let mut res = DynMap::default();
2929
self.child_by_source_to(db, &mut res, file_id);

crates/hir/src/semantics/source_to_def.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
8888
use either::Either;
8989
use hir_def::{
90-
child_by_source::ChildBySource,
9190
dyn_map::{
9291
keys::{self, Key},
9392
DynMap,
@@ -111,7 +110,10 @@ use syntax::{
111110
AstNode, AstPtr, SyntaxNode,
112111
};
113112

114-
use crate::{db::HirDatabase, InFile, InlineAsmOperand, SemanticsImpl};
113+
use crate::{
114+
db::HirDatabase, semantics::child_by_source::ChildBySource, InFile, InlineAsmOperand,
115+
SemanticsImpl,
116+
};
115117

116118
#[derive(Default)]
117119
pub(super) struct SourceToDefCache {

crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn edit_struct_def(
104104
ast::make::tokens::single_newline().text(),
105105
);
106106
edit.insert(tuple_fields_text_range.start(), w.syntax().text());
107-
if !w.syntax().last_token().is_some_and(|t| t.kind() == SyntaxKind::COMMA) {
107+
if w.syntax().last_token().is_none_or(|t| t.kind() != SyntaxKind::COMMA) {
108108
edit.insert(tuple_fields_text_range.start(), ",");
109109
}
110110
edit.insert(

crates/ide-diagnostics/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub fn semantic_diagnostics(
382382
// A bunch of parse errors in a file indicate some bigger structural parse changes in the
383383
// file, so we skip semantic diagnostics so we can show these faster.
384384
Some(m) => {
385-
if !db.parse_errors(file_id).as_deref().is_some_and(|es| es.len() >= 16) {
385+
if db.parse_errors(file_id).as_deref().is_none_or(|es| es.len() < 16) {
386386
m.diagnostics(db, &mut diags, config.style_lints);
387387
}
388388
}

0 commit comments

Comments
 (0)