Skip to content

Commit 0dd2c0d

Browse files
committed
Auto merge of rust-lang#15209 - Veykril:ast-id, r=Veykril
internal: Stronger typing for AstId and AstIdMap
2 parents 45272ef + 8886d70 commit 0dd2c0d

File tree

10 files changed

+102
-50
lines changed

10 files changed

+102
-50
lines changed

crates/hir-def/src/data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ impl<'a> AssocItemCollector<'a> {
599599
if !attrs.is_cfg_enabled(self.expander.cfg_options()) {
600600
self.diagnostics.push(DefDiagnostic::unconfigured_code(
601601
self.module_id.local_id,
602-
InFile::new(self.expander.current_file_id(), item.ast_id(item_tree).upcast()),
602+
InFile::new(self.expander.current_file_id(), item.ast_id(item_tree).erase()),
603603
attrs.cfg().unwrap(),
604604
self.expander.cfg_options().clone(),
605605
));

crates/hir-def/src/data/adt.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl EnumData {
330330
} else {
331331
diagnostics.push(DefDiagnostic::unconfigured_code(
332332
loc.container.local_id,
333-
InFile::new(loc.id.file_id(), var.ast_id.upcast()),
333+
InFile::new(loc.id.file_id(), var.ast_id.erase()),
334334
attrs.cfg().unwrap(),
335335
cfg_options.clone(),
336336
))
@@ -540,8 +540,8 @@ fn lower_fields(
540540
InFile::new(
541541
current_file_id,
542542
match field.ast_id {
543-
FieldAstId::Record(it) => it.upcast(),
544-
FieldAstId::Tuple(it) => it.upcast(),
543+
FieldAstId::Record(it) => it.erase(),
544+
FieldAstId::Tuple(it) => it.erase(),
545545
},
546546
),
547547
attrs.cfg().unwrap(),
@@ -564,8 +564,8 @@ fn lower_fields(
564564
InFile::new(
565565
current_file_id,
566566
match field.ast_id {
567-
FieldAstId::Record(it) => it.upcast(),
568-
FieldAstId::Tuple(it) => it.upcast(),
567+
FieldAstId::Record(it) => it.erase(),
568+
FieldAstId::Tuple(it) => it.erase(),
569569
},
570570
),
571571
attrs.cfg().unwrap(),

crates/hir-def/src/item_tree.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use ast::{AstNode, HasName, StructKind};
4646
use base_db::CrateId;
4747
use either::Either;
4848
use hir_expand::{
49-
ast_id_map::FileAstId,
49+
ast_id_map::{AstIdNode, FileAstId},
5050
attrs::RawAttrs,
5151
hygiene::Hygiene,
5252
name::{name, AsName, Name},
@@ -314,7 +314,7 @@ from_attrs!(ModItem(ModItem), Variant(Idx<Variant>), Field(Idx<Field>), Param(Id
314314

315315
/// Trait implemented by all item nodes in the item tree.
316316
pub trait ItemTreeNode: Clone {
317-
type Source: AstNode + Into<ast::Item>;
317+
type Source: AstIdNode + Into<ast::Item>;
318318

319319
fn ast_id(&self) -> FileAstId<Self::Source>;
320320

crates/hir-def/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use std::{
6464

6565
use base_db::{impl_intern_key, salsa, CrateId, ProcMacroKind};
6666
use hir_expand::{
67-
ast_id_map::FileAstId,
67+
ast_id_map::{AstIdNode, FileAstId},
6868
attrs::{Attr, AttrId, AttrInput},
6969
builtin_attr_macro::BuiltinAttrExpander,
7070
builtin_derive_macro::BuiltinDeriveExpander,
@@ -1124,12 +1124,12 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
11241124

11251125
/// Helper wrapper for `AstId` with `ModPath`
11261126
#[derive(Clone, Debug, Eq, PartialEq)]
1127-
struct AstIdWithPath<T: ast::AstNode> {
1127+
struct AstIdWithPath<T: AstIdNode> {
11281128
ast_id: AstId<T>,
11291129
path: path::ModPath,
11301130
}
11311131

1132-
impl<T: ast::AstNode> AstIdWithPath<T> {
1132+
impl<T: AstIdNode> AstIdWithPath<T> {
11331133
fn new(file_id: HirFileId, ast_id: FileAstId<T>, path: path::ModPath) -> AstIdWithPath<T> {
11341134
AstIdWithPath { ast_id: AstId::new(file_id, ast_id), path }
11351135
}

crates/hir-def/src/lower.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//! Context for lowering paths.
2-
use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, AstId, HirFileId, InFile};
2+
use hir_expand::{
3+
ast_id_map::{AstIdMap, AstIdNode},
4+
hygiene::Hygiene,
5+
AstId, HirFileId, InFile,
6+
};
37
use once_cell::unsync::OnceCell;
48
use syntax::ast;
59
use triomphe::Arc;
@@ -37,7 +41,7 @@ impl<'a> LowerCtx<'a> {
3741
Path::from_src(ast, self)
3842
}
3943

40-
pub(crate) fn ast_id<N: syntax::AstNode>(&self, item: &N) -> Option<AstId<N>> {
44+
pub(crate) fn ast_id<N: AstIdNode>(&self, item: &N) -> Option<AstId<N>> {
4145
let &(file_id, ref ast_id_map) = self.ast_id_map.as_ref()?;
4246
let ast_id_map = ast_id_map.get_or_init(|| self.db.ast_id_map(file_id));
4347
Some(InFile::new(file_id, ast_id_map.ast_id(item)))

crates/hir-def/src/nameres/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,7 @@ impl ModCollector<'_, '_> {
22802280
fn emit_unconfigured_diagnostic(&mut self, item: ModItem, cfg: &CfgExpr) {
22812281
let ast_id = item.ast_id(self.item_tree);
22822282

2283-
let ast_id = InFile::new(self.file_id(), ast_id.upcast());
2283+
let ast_id = InFile::new(self.file_id(), ast_id.erase());
22842284
self.def_collector.def_map.diagnostics.push(DefDiagnostic::unconfigured_code(
22852285
self.module_id,
22862286
ast_id,

crates/hir-def/src/nameres/diagnostics.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
33
use base_db::CrateId;
44
use cfg::{CfgExpr, CfgOptions};
5-
use hir_expand::{attrs::AttrId, MacroCallKind};
5+
use hir_expand::{attrs::AttrId, ErasedAstId, MacroCallKind};
66
use la_arena::Idx;
7-
use syntax::{
8-
ast::{self, AnyHasAttrs},
9-
SyntaxError,
10-
};
7+
use syntax::{ast, SyntaxError};
118

129
use crate::{
1310
item_tree::{self, ItemTreeId},
@@ -24,7 +21,7 @@ pub enum DefDiagnosticKind {
2421

2522
UnresolvedImport { id: ItemTreeId<item_tree::Import>, index: Idx<ast::UseTree> },
2623

27-
UnconfiguredCode { ast: AstId<AnyHasAttrs>, cfg: CfgExpr, opts: CfgOptions },
24+
UnconfiguredCode { ast: ErasedAstId, cfg: CfgExpr, opts: CfgOptions },
2825

2926
UnresolvedProcMacro { ast: MacroCallKind, krate: CrateId },
3027

@@ -81,7 +78,7 @@ impl DefDiagnostic {
8178

8279
pub fn unconfigured_code(
8380
container: LocalModuleId,
84-
ast: AstId<ast::AnyHasAttrs>,
81+
ast: ErasedAstId,
8582
cfg: CfgExpr,
8683
opts: CfgOptions,
8784
) -> Self {

crates/hir-expand/src/ast_id_map.rs

+59-20
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,89 @@ use rustc_hash::FxHasher;
1818
use syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};
1919

2020
/// `AstId` points to an AST node in a specific file.
21-
pub struct FileAstId<N: AstNode> {
21+
pub struct FileAstId<N: AstIdNode> {
2222
raw: ErasedFileAstId,
2323
covariant: PhantomData<fn() -> N>,
2424
}
2525

26-
impl<N: AstNode> Clone for FileAstId<N> {
26+
impl<N: AstIdNode> Clone for FileAstId<N> {
2727
fn clone(&self) -> FileAstId<N> {
2828
*self
2929
}
3030
}
31-
impl<N: AstNode> Copy for FileAstId<N> {}
31+
impl<N: AstIdNode> Copy for FileAstId<N> {}
3232

33-
impl<N: AstNode> PartialEq for FileAstId<N> {
33+
impl<N: AstIdNode> PartialEq for FileAstId<N> {
3434
fn eq(&self, other: &Self) -> bool {
3535
self.raw == other.raw
3636
}
3737
}
38-
impl<N: AstNode> Eq for FileAstId<N> {}
39-
impl<N: AstNode> Hash for FileAstId<N> {
38+
impl<N: AstIdNode> Eq for FileAstId<N> {}
39+
impl<N: AstIdNode> Hash for FileAstId<N> {
4040
fn hash<H: Hasher>(&self, hasher: &mut H) {
4141
self.raw.hash(hasher);
4242
}
4343
}
4444

45-
impl<N: AstNode> fmt::Debug for FileAstId<N> {
45+
impl<N: AstIdNode> fmt::Debug for FileAstId<N> {
4646
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4747
write!(f, "FileAstId::<{}>({})", type_name::<N>(), self.raw.into_raw())
4848
}
4949
}
5050

51-
impl<N: AstNode> FileAstId<N> {
51+
impl<N: AstIdNode> FileAstId<N> {
5252
// Can't make this a From implementation because of coherence
53-
pub fn upcast<M: AstNode>(self) -> FileAstId<M>
53+
pub fn upcast<M: AstIdNode>(self) -> FileAstId<M>
5454
where
5555
N: Into<M>,
5656
{
5757
FileAstId { raw: self.raw, covariant: PhantomData }
5858
}
59+
60+
pub fn erase(self) -> ErasedFileAstId {
61+
self.raw
62+
}
5963
}
6064

61-
type ErasedFileAstId = Idx<SyntaxNodePtr>;
65+
pub type ErasedFileAstId = Idx<SyntaxNodePtr>;
66+
67+
pub trait AstIdNode: AstNode {}
68+
macro_rules! register_ast_id_node {
69+
(impl AstIdNode for $($ident:ident),+ ) => {
70+
$(
71+
impl AstIdNode for ast::$ident {}
72+
)+
73+
fn should_alloc_id(kind: syntax::SyntaxKind) -> bool {
74+
$(
75+
ast::$ident::can_cast(kind)
76+
)||+
77+
}
78+
};
79+
}
80+
register_ast_id_node! {
81+
impl AstIdNode for
82+
Item,
83+
Adt,
84+
Enum,
85+
Struct,
86+
Union,
87+
Const,
88+
ExternBlock,
89+
ExternCrate,
90+
Fn,
91+
Impl,
92+
Macro,
93+
MacroDef,
94+
MacroRules,
95+
MacroCall,
96+
Module,
97+
Static,
98+
Trait,
99+
TraitAlias,
100+
TypeAlias,
101+
Use,
102+
AssocItem, BlockExpr, Variant, RecordField, TupleField, ConstArg
103+
}
62104

63105
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.
64106
#[derive(Default)]
@@ -92,14 +134,7 @@ impl AstIdMap {
92134
// change parent's id. This means that, say, adding a new function to a
93135
// trait does not change ids of top-level items, which helps caching.
94136
bdfs(node, |it| {
95-
let kind = it.kind();
96-
if ast::Item::can_cast(kind)
97-
|| ast::BlockExpr::can_cast(kind)
98-
|| ast::Variant::can_cast(kind)
99-
|| ast::RecordField::can_cast(kind)
100-
|| ast::TupleField::can_cast(kind)
101-
|| ast::ConstArg::can_cast(kind)
102-
{
137+
if should_alloc_id(it.kind()) {
103138
res.alloc(&it);
104139
true
105140
} else {
@@ -120,15 +155,19 @@ impl AstIdMap {
120155
res
121156
}
122157

123-
pub fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
158+
pub fn ast_id<N: AstIdNode>(&self, item: &N) -> FileAstId<N> {
124159
let raw = self.erased_ast_id(item.syntax());
125160
FileAstId { raw, covariant: PhantomData }
126161
}
127162

128-
pub fn get<N: AstNode>(&self, id: FileAstId<N>) -> AstPtr<N> {
163+
pub fn get<N: AstIdNode>(&self, id: FileAstId<N>) -> AstPtr<N> {
129164
AstPtr::try_from_raw(self.arena[id.raw].clone()).unwrap()
130165
}
131166

167+
pub(crate) fn get_raw(&self, id: ErasedFileAstId) -> SyntaxNodePtr {
168+
self.arena[id].clone()
169+
}
170+
132171
fn erased_ast_id(&self, item: &SyntaxNode) -> ErasedFileAstId {
133172
let ptr = SyntaxNodePtr::new(item);
134173
let hash = hash_ptr(&ptr);

crates/hir-expand/src/lib.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ use either::Either;
3737
use syntax::{
3838
algo::{self, skip_trivia_token},
3939
ast::{self, AstNode, HasDocComments},
40-
Direction, SyntaxNode, SyntaxToken,
40+
AstPtr, Direction, SyntaxNode, SyntaxNodePtr, SyntaxToken,
4141
};
4242

4343
use crate::{
44-
ast_id_map::FileAstId,
44+
ast_id_map::{AstIdNode, ErasedFileAstId, FileAstId},
4545
attrs::AttrId,
4646
builtin_attr_macro::BuiltinAttrExpander,
4747
builtin_derive_macro::BuiltinDeriveExpander,
@@ -551,9 +551,9 @@ impl MacroCallKind {
551551
};
552552

553553
let range = match kind {
554-
MacroCallKind::FnLike { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
555-
MacroCallKind::Derive { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
556-
MacroCallKind::Attr { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
554+
MacroCallKind::FnLike { ast_id, .. } => ast_id.to_ptr(db).text_range(),
555+
MacroCallKind::Derive { ast_id, .. } => ast_id.to_ptr(db).text_range(),
556+
MacroCallKind::Attr { ast_id, .. } => ast_id.to_ptr(db).text_range(),
557557
};
558558

559559
FileRange { range, file_id }
@@ -801,13 +801,25 @@ impl ExpansionInfo {
801801
/// It is stable across reparses, and can be used as salsa key/value.
802802
pub type AstId<N> = InFile<FileAstId<N>>;
803803

804-
impl<N: AstNode> AstId<N> {
804+
impl<N: AstIdNode> AstId<N> {
805805
pub fn to_node(&self, db: &dyn db::ExpandDatabase) -> N {
806-
let root = db.parse_or_expand(self.file_id);
807-
db.ast_id_map(self.file_id).get(self.value).to_node(&root)
806+
self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id))
807+
}
808+
pub fn to_ptr(&self, db: &dyn db::ExpandDatabase) -> AstPtr<N> {
809+
db.ast_id_map(self.file_id).get(self.value)
808810
}
809811
}
810812

813+
pub type ErasedAstId = InFile<ErasedFileAstId>;
814+
815+
impl ErasedAstId {
816+
pub fn to_node(&self, db: &dyn db::ExpandDatabase) -> SyntaxNode {
817+
self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id))
818+
}
819+
pub fn to_ptr(&self, db: &dyn db::ExpandDatabase) -> SyntaxNodePtr {
820+
db.ast_id_map(self.file_id).get_raw(self.value)
821+
}
822+
}
811823
/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
812824
///
813825
/// Typical usages are:

crates/hir/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ fn emit_def_diagnostic_(
754754
let item = ast.to_node(db.upcast());
755755
acc.push(
756756
InactiveCode {
757-
node: ast.with_value(AstPtr::new(&item).into()),
757+
node: ast.with_value(SyntaxNodePtr::new(&item).into()),
758758
cfg: cfg.clone(),
759759
opts: opts.clone(),
760760
}

0 commit comments

Comments
 (0)