Skip to content

Commit c512e2e

Browse files
committed
Auto merge of rust-lang#16728 - Veykril:span-docs, r=Veykril
minor: Move span stuff around
2 parents 9efa23c + efd76ec commit c512e2e

File tree

17 files changed

+266
-210
lines changed

17 files changed

+266
-210
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir-def/src/body/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use std::mem;
66
use base_db::CrateId;
77
use either::Either;
88
use hir_expand::{
9-
ast_id_map::AstIdMap,
109
name::{name, AsName, Name},
1110
ExpandError, InFile,
1211
};
1312
use intern::Interned;
1413
use profile::Count;
1514
use rustc_hash::FxHashMap;
1615
use smallvec::SmallVec;
16+
use span::AstIdMap;
1717
use syntax::{
1818
ast::{
1919
self, ArrayExprKind, AstChildren, BlockExpr, HasArgList, HasAttrs, HasLoopBody, HasName,

crates/hir-def/src/item_tree.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,13 @@ use std::{
4747
use ast::{AstNode, StructKind};
4848
use base_db::CrateId;
4949
use either::Either;
50-
use hir_expand::{
51-
ast_id_map::{AstIdNode, FileAstId},
52-
attrs::RawAttrs,
53-
name::Name,
54-
ExpandTo, HirFileId, InFile,
55-
};
50+
use hir_expand::{attrs::RawAttrs, name::Name, ExpandTo, HirFileId, InFile};
5651
use intern::Interned;
5752
use la_arena::{Arena, Idx, IdxRange, RawIdx};
5853
use profile::Count;
5954
use rustc_hash::FxHashMap;
6055
use smallvec::SmallVec;
61-
use span::Span;
56+
use span::{AstIdNode, FileAstId, Span};
6257
use stdx::never;
6358
use syntax::{ast, match_ast, SyntaxKind};
6459
use triomphe::Arc;

crates/hir-def/src/item_tree/lower.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
33
use std::collections::hash_map::Entry;
44

5-
use hir_expand::{
6-
ast_id_map::AstIdMap, mod_path::path, name, name::AsName, span_map::SpanMapRef, HirFileId,
7-
};
5+
use hir_expand::{mod_path::path, name, name::AsName, span_map::SpanMapRef, HirFileId};
86
use la_arena::Arena;
7+
use span::AstIdMap;
98
use syntax::{
109
ast::{self, HasModuleItem, HasName, HasTypeBounds, IsString},
1110
AstNode,

crates/hir-def/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ use base_db::{
7676
CrateId, Edition,
7777
};
7878
use hir_expand::{
79-
ast_id_map::{AstIdNode, FileAstId},
8079
builtin_attr_macro::BuiltinAttrExpander,
8180
builtin_derive_macro::BuiltinDeriveExpander,
8281
builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
@@ -91,7 +90,7 @@ use hir_expand::{
9190
use item_tree::ExternBlock;
9291
use la_arena::Idx;
9392
use nameres::DefMap;
94-
use span::{FileId, Span};
93+
use span::{AstIdNode, FileAstId, FileId, Span};
9594
use stdx::impl_from;
9695
use syntax::{ast, AstNode};
9796

crates/hir-def/src/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
use std::cell::OnceCell;
33

44
use hir_expand::{
5-
ast_id_map::{AstIdMap, AstIdNode},
65
span_map::{SpanMap, SpanMapRef},
76
AstId, HirFileId, InFile,
87
};
8+
use span::{AstIdMap, AstIdNode};
99
use syntax::ast;
1010
use triomphe::Arc;
1111

crates/hir-def/src/nameres.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ use std::ops::Deref;
6161

6262
use base_db::{CrateId, Edition, FileId};
6363
use hir_expand::{
64-
ast_id_map::FileAstId, name::Name, proc_macro::ProcMacroKind, HirFileId, InFile, MacroCallId,
65-
MacroDefId,
64+
name::Name, proc_macro::ProcMacroKind, HirFileId, InFile, MacroCallId, MacroDefId,
6665
};
6766
use itertools::Itertools;
6867
use la_arena::Arena;
6968
use profile::Count;
7069
use rustc_hash::{FxHashMap, FxHashSet};
70+
use span::FileAstId;
7171
use stdx::format_to;
7272
use syntax::{ast, SmolStr};
7373
use triomphe::Arc;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use base_db::{CrateId, Dependency, Edition, FileId};
99
use cfg::{CfgExpr, CfgOptions};
1010
use either::Either;
1111
use hir_expand::{
12-
ast_id_map::FileAstId,
1312
attrs::{Attr, AttrId},
1413
builtin_attr_macro::{find_builtin_attr, BuiltinAttrExpander},
1514
builtin_derive_macro::find_builtin_derive,
@@ -23,7 +22,7 @@ use itertools::{izip, Itertools};
2322
use la_arena::Idx;
2423
use limit::Limit;
2524
use rustc_hash::{FxHashMap, FxHashSet};
26-
use span::{ErasedFileAstId, Span, SyntaxContextId};
25+
use span::{ErasedFileAstId, FileAstId, Span, SyntaxContextId};
2726
use stdx::always;
2827
use syntax::{ast, SmolStr};
2928
use triomphe::Arc;

crates/hir-expand/src/db.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,20 @@ use either::Either;
55
use limit::Limit;
66
use mbe::{syntax_node_to_token_tree, ValueResult};
77
use rustc_hash::FxHashSet;
8-
use span::SyntaxContextId;
8+
use span::{AstIdMap, SyntaxContextData, SyntaxContextId};
99
use syntax::{
1010
ast::{self, HasAttrs},
1111
AstNode, Parse, SyntaxError, SyntaxNode, SyntaxToken, T,
1212
};
1313
use triomphe::Arc;
1414

1515
use crate::{
16-
ast_id_map::AstIdMap,
1716
attrs::collect_attrs,
1817
builtin_attr_macro::pseudo_derive_attr_expansion,
1918
builtin_fn_macro::EagerExpander,
2019
declarative::DeclarativeMacroExpander,
2120
fixup::{self, reverse_fixups, SyntaxFixupUndoInfo},
22-
hygiene::{
23-
span_with_call_site_ctxt, span_with_def_site_ctxt, span_with_mixed_site_ctxt,
24-
SyntaxContextData,
25-
},
21+
hygiene::{span_with_call_site_ctxt, span_with_def_site_ctxt, span_with_mixed_site_ctxt},
2622
proc_macro::ProcMacros,
2723
span_map::{RealSpanMap, SpanMap, SpanMapRef},
2824
tt, AstId, BuiltinAttrExpander, BuiltinDeriveExpander, BuiltinFnLikeExpander,
@@ -61,7 +57,6 @@ pub trait ExpandDatabase: SourceDatabase {
6157
#[salsa::input]
6258
fn proc_macros(&self) -> Arc<ProcMacros>;
6359

64-
#[salsa::invoke(AstIdMap::new)]
6560
fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
6661

6762
/// Main public API -- parses a hir file, not caring whether it's a real
@@ -256,6 +251,10 @@ pub fn expand_speculative(
256251
Some((node.syntax_node(), token))
257252
}
258253

254+
fn ast_id_map(db: &dyn ExpandDatabase, file_id: span::HirFileId) -> triomphe::Arc<AstIdMap> {
255+
triomphe::Arc::new(AstIdMap::from_source(&db.parse_or_expand(file_id)))
256+
}
257+
259258
fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> SyntaxNode {
260259
match file_id.repr() {
261260
HirFileIdRepr::FileId(file_id) => db.parse(file_id).syntax_node(),

crates/hir-expand/src/files.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
use std::iter;
33

44
use either::Either;
5-
use span::{FileId, FileRange, HirFileId, HirFileIdRepr, MacroFileId, SyntaxContextId};
6-
use syntax::{AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize};
5+
use span::{
6+
AstIdNode, ErasedFileAstId, FileAstId, FileId, FileRange, HirFileId, HirFileIdRepr,
7+
MacroFileId, SyntaxContextId,
8+
};
9+
use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize};
710

8-
use crate::{db, map_node_range_up, span_for_offset, MacroFileIdExt};
11+
use crate::{
12+
db::{self, ExpandDatabase},
13+
map_node_range_up, span_for_offset, MacroFileIdExt,
14+
};
915

1016
/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
1117
///
@@ -23,6 +29,31 @@ pub type InFile<T> = InFileWrapper<HirFileId, T>;
2329
pub type InMacroFile<T> = InFileWrapper<MacroFileId, T>;
2430
pub type InRealFile<T> = InFileWrapper<FileId, T>;
2531

32+
/// `AstId` points to an AST node in any file.
33+
///
34+
/// It is stable across reparses, and can be used as salsa key/value.
35+
pub type AstId<N> = crate::InFile<FileAstId<N>>;
36+
37+
impl<N: AstIdNode> AstId<N> {
38+
pub fn to_node(&self, db: &dyn ExpandDatabase) -> N {
39+
self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id))
40+
}
41+
pub fn to_in_file_node(&self, db: &dyn ExpandDatabase) -> crate::InFile<N> {
42+
crate::InFile::new(self.file_id, self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id)))
43+
}
44+
pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> AstPtr<N> {
45+
db.ast_id_map(self.file_id).get(self.value)
46+
}
47+
}
48+
49+
pub type ErasedAstId = crate::InFile<ErasedFileAstId>;
50+
51+
impl ErasedAstId {
52+
pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> SyntaxNodePtr {
53+
db.ast_id_map(self.file_id).get_erased(self.value)
54+
}
55+
}
56+
2657
impl<FileKind, T> InFileWrapper<FileKind, T> {
2758
pub fn new(file_id: FileKind, value: T) -> Self {
2859
Self { file_id, value }

0 commit comments

Comments
 (0)