diff --git a/Cargo.lock b/Cargo.lock index 92d5edea6f..9acace2fb3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1855,7 +1855,9 @@ dependencies = [ name = "span" version = "0.0.0" dependencies = [ + "hashbrown", "la-arena 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-hash", "salsa", "stdx", "syntax", diff --git a/crates/hir-def/src/body/lower.rs b/crates/hir-def/src/body/lower.rs index 5dc5fedd23..ad8782d3d1 100644 --- a/crates/hir-def/src/body/lower.rs +++ b/crates/hir-def/src/body/lower.rs @@ -6,7 +6,6 @@ use std::mem; use base_db::CrateId; use either::Either; use hir_expand::{ - ast_id_map::AstIdMap, name::{name, AsName, Name}, ExpandError, InFile, }; @@ -14,6 +13,7 @@ use intern::Interned; use profile::Count; use rustc_hash::FxHashMap; use smallvec::SmallVec; +use span::AstIdMap; use syntax::{ ast::{ self, ArrayExprKind, AstChildren, BlockExpr, HasArgList, HasAttrs, HasLoopBody, HasName, diff --git a/crates/hir-def/src/item_tree.rs b/crates/hir-def/src/item_tree.rs index bb36950f95..c7cf611589 100644 --- a/crates/hir-def/src/item_tree.rs +++ b/crates/hir-def/src/item_tree.rs @@ -47,18 +47,13 @@ use std::{ use ast::{AstNode, StructKind}; use base_db::CrateId; use either::Either; -use hir_expand::{ - ast_id_map::{AstIdNode, FileAstId}, - attrs::RawAttrs, - name::Name, - ExpandTo, HirFileId, InFile, -}; +use hir_expand::{attrs::RawAttrs, name::Name, ExpandTo, HirFileId, InFile}; use intern::Interned; use la_arena::{Arena, Idx, IdxRange, RawIdx}; use profile::Count; use rustc_hash::FxHashMap; use smallvec::SmallVec; -use span::Span; +use span::{AstIdNode, FileAstId, Span}; use stdx::never; use syntax::{ast, match_ast, SyntaxKind}; use triomphe::Arc; diff --git a/crates/hir-def/src/item_tree/lower.rs b/crates/hir-def/src/item_tree/lower.rs index 37fdece876..21cffafa95 100644 --- a/crates/hir-def/src/item_tree/lower.rs +++ b/crates/hir-def/src/item_tree/lower.rs @@ -2,10 +2,9 @@ use std::collections::hash_map::Entry; -use hir_expand::{ - ast_id_map::AstIdMap, mod_path::path, name, name::AsName, span_map::SpanMapRef, HirFileId, -}; +use hir_expand::{mod_path::path, name, name::AsName, span_map::SpanMapRef, HirFileId}; use la_arena::Arena; +use span::AstIdMap; use syntax::{ ast::{self, HasModuleItem, HasName, HasTypeBounds, IsString}, AstNode, diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs index 5670ebfa17..de3ab57a12 100644 --- a/crates/hir-def/src/lib.rs +++ b/crates/hir-def/src/lib.rs @@ -76,7 +76,6 @@ use base_db::{ CrateId, Edition, }; use hir_expand::{ - ast_id_map::{AstIdNode, FileAstId}, builtin_attr_macro::BuiltinAttrExpander, builtin_derive_macro::BuiltinDeriveExpander, builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander}, @@ -91,7 +90,7 @@ use hir_expand::{ use item_tree::ExternBlock; use la_arena::Idx; use nameres::DefMap; -use span::{FileId, Span}; +use span::{AstIdNode, FileAstId, FileId, Span}; use stdx::impl_from; use syntax::{ast, AstNode}; diff --git a/crates/hir-def/src/lower.rs b/crates/hir-def/src/lower.rs index 395b69d284..2fa6acdf17 100644 --- a/crates/hir-def/src/lower.rs +++ b/crates/hir-def/src/lower.rs @@ -2,10 +2,10 @@ use std::cell::OnceCell; use hir_expand::{ - ast_id_map::{AstIdMap, AstIdNode}, span_map::{SpanMap, SpanMapRef}, AstId, HirFileId, InFile, }; +use span::{AstIdMap, AstIdNode}; use syntax::ast; use triomphe::Arc; diff --git a/crates/hir-def/src/nameres.rs b/crates/hir-def/src/nameres.rs index 1a82782115..270468ad0a 100644 --- a/crates/hir-def/src/nameres.rs +++ b/crates/hir-def/src/nameres.rs @@ -61,13 +61,13 @@ use std::ops::Deref; use base_db::{CrateId, Edition, FileId}; use hir_expand::{ - ast_id_map::FileAstId, name::Name, proc_macro::ProcMacroKind, HirFileId, InFile, MacroCallId, - MacroDefId, + name::Name, proc_macro::ProcMacroKind, HirFileId, InFile, MacroCallId, MacroDefId, }; use itertools::Itertools; use la_arena::Arena; use profile::Count; use rustc_hash::{FxHashMap, FxHashSet}; +use span::FileAstId; use stdx::format_to; use syntax::{ast, SmolStr}; use triomphe::Arc; diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs index 3282540650..538e735688 100644 --- a/crates/hir-def/src/nameres/collector.rs +++ b/crates/hir-def/src/nameres/collector.rs @@ -9,7 +9,6 @@ use base_db::{CrateId, Dependency, Edition, FileId}; use cfg::{CfgExpr, CfgOptions}; use either::Either; use hir_expand::{ - ast_id_map::FileAstId, attrs::{Attr, AttrId}, builtin_attr_macro::{find_builtin_attr, BuiltinAttrExpander}, builtin_derive_macro::find_builtin_derive, @@ -23,7 +22,7 @@ use itertools::{izip, Itertools}; use la_arena::Idx; use limit::Limit; use rustc_hash::{FxHashMap, FxHashSet}; -use span::{ErasedFileAstId, Span, SyntaxContextId}; +use span::{ErasedFileAstId, FileAstId, Span, SyntaxContextId}; use stdx::always; use syntax::{ast, SmolStr}; use triomphe::Arc; diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 6aea676459..f1f0d8990f 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -5,7 +5,7 @@ use either::Either; use limit::Limit; use mbe::{syntax_node_to_token_tree, ValueResult}; use rustc_hash::FxHashSet; -use span::{SyntaxContextData, SyntaxContextId}; +use span::{AstIdMap, SyntaxContextData, SyntaxContextId}; use syntax::{ ast::{self, HasAttrs}, AstNode, Parse, SyntaxError, SyntaxNode, SyntaxToken, T, @@ -13,7 +13,6 @@ use syntax::{ use triomphe::Arc; use crate::{ - ast_id_map::AstIdMap, attrs::collect_attrs, builtin_attr_macro::pseudo_derive_attr_expansion, builtin_fn_macro::EagerExpander, @@ -58,7 +57,6 @@ pub trait ExpandDatabase: SourceDatabase { #[salsa::input] fn proc_macros(&self) -> Arc; - #[salsa::invoke(AstIdMap::new)] fn ast_id_map(&self, file_id: HirFileId) -> Arc; /// Main public API -- parses a hir file, not caring whether it's a real @@ -253,6 +251,10 @@ pub fn expand_speculative( Some((node.syntax_node(), token)) } +fn ast_id_map(db: &dyn ExpandDatabase, file_id: span::HirFileId) -> triomphe::Arc { + triomphe::Arc::new(AstIdMap::from_source(&db.parse_or_expand(file_id))) +} + fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> SyntaxNode { match file_id.repr() { HirFileIdRepr::FileId(file_id) => db.parse(file_id).syntax_node(), diff --git a/crates/hir-expand/src/files.rs b/crates/hir-expand/src/files.rs index 707daf0402..66ceb1b7d4 100644 --- a/crates/hir-expand/src/files.rs +++ b/crates/hir-expand/src/files.rs @@ -2,10 +2,16 @@ use std::iter; use either::Either; -use span::{FileId, FileRange, HirFileId, HirFileIdRepr, MacroFileId, SyntaxContextId}; -use syntax::{AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize}; +use span::{ + AstIdNode, ErasedFileAstId, FileAstId, FileId, FileRange, HirFileId, HirFileIdRepr, + MacroFileId, SyntaxContextId, +}; +use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize}; -use crate::{db, map_node_range_up, span_for_offset, MacroFileIdExt}; +use crate::{ + db::{self, ExpandDatabase}, + map_node_range_up, span_for_offset, MacroFileIdExt, +}; /// `InFile` stores a value of `T` inside a particular file/syntax tree. /// @@ -23,6 +29,31 @@ pub type InFile = InFileWrapper; pub type InMacroFile = InFileWrapper; pub type InRealFile = InFileWrapper; +/// `AstId` points to an AST node in any file. +/// +/// It is stable across reparses, and can be used as salsa key/value. +pub type AstId = crate::InFile>; + +impl AstId { + pub fn to_node(&self, db: &dyn ExpandDatabase) -> N { + self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id)) + } + pub fn to_in_file_node(&self, db: &dyn ExpandDatabase) -> crate::InFile { + crate::InFile::new(self.file_id, self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id))) + } + pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> AstPtr { + db.ast_id_map(self.file_id).get(self.value) + } +} + +pub type ErasedAstId = crate::InFile; + +impl ErasedAstId { + pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> SyntaxNodePtr { + db.ast_id_map(self.file_id).get_erased(self.value) + } +} + impl InFileWrapper { pub fn new(file_id: FileKind, value: T) -> Self { Self { file_id, value } diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs index e1a79e8b58..42dc8c12d6 100644 --- a/crates/hir-expand/src/lib.rs +++ b/crates/hir-expand/src/lib.rs @@ -6,7 +6,6 @@ #![warn(rust_2018_idioms, unused_lifetimes)] -pub mod ast_id_map; pub mod attrs; pub mod builtin_attr_macro; pub mod builtin_derive_macro; @@ -32,7 +31,7 @@ use std::{fmt, hash::Hash}; use base_db::{salsa::impl_intern_value_trivial, CrateId, Edition, FileId}; use either::Either; -use span::{FileRange, HirFileIdRepr, Span, SyntaxContextData, SyntaxContextId}; +use span::{ErasedFileAstId, FileRange, HirFileIdRepr, Span, SyntaxContextData, SyntaxContextId}; use syntax::{ ast::{self, AstNode}, SyntaxNode, SyntaxToken, TextRange, TextSize, @@ -49,8 +48,7 @@ use crate::{ span_map::{ExpansionSpanMap, SpanMap}, }; -pub use crate::ast_id_map::{AstId, ErasedAstId, ErasedFileAstId}; -pub use crate::files::{InFile, InMacroFile, InRealFile}; +pub use crate::files::{AstId, ErasedAstId, InFile, InMacroFile, InRealFile}; pub use mbe::ValueResult; pub use span::{HirFileId, MacroCallId, MacroFileId}; diff --git a/crates/span/Cargo.toml b/crates/span/Cargo.toml index 7093f3a691..cbda91f0a5 100644 --- a/crates/span/Cargo.toml +++ b/crates/span/Cargo.toml @@ -12,7 +12,8 @@ authors.workspace = true [dependencies] la-arena.workspace = true salsa.workspace = true - +rustc-hash.workspace = true +hashbrown.workspace = true # local deps vfs.workspace = true diff --git a/crates/hir-expand/src/ast_id_map.rs b/crates/span/src/ast_id.rs similarity index 85% rename from crates/hir-expand/src/ast_id_map.rs rename to crates/span/src/ast_id.rs index ab582741f5..2d98aa81e5 100644 --- a/crates/hir-expand/src/ast_id_map.rs +++ b/crates/span/src/ast_id.rs @@ -5,8 +5,6 @@ //! item as an ID. That way, id's don't change unless the set of items itself //! changes. -// FIXME: Consider moving this into the span crate - use std::{ any::type_name, fmt, @@ -15,38 +13,12 @@ use std::{ }; use la_arena::{Arena, Idx, RawIdx}; -use profile::Count; use rustc_hash::FxHasher; use syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr}; -use crate::db::ExpandDatabase; - -pub use span::ErasedFileAstId; - -/// `AstId` points to an AST node in any file. -/// -/// It is stable across reparses, and can be used as salsa key/value. -pub type AstId = crate::InFile>; - -impl AstId { - pub fn to_node(&self, db: &dyn ExpandDatabase) -> N { - self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id)) - } - pub fn to_in_file_node(&self, db: &dyn ExpandDatabase) -> crate::InFile { - crate::InFile::new(self.file_id, self.to_ptr(db).to_node(&db.parse_or_expand(self.file_id))) - } - pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> AstPtr { - db.ast_id_map(self.file_id).get(self.value) - } -} - -pub type ErasedAstId = crate::InFile; - -impl ErasedAstId { - pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> SyntaxNodePtr { - db.ast_id_map(self.file_id).get_erased(self.value) - } -} +/// See crates\hir-expand\src\ast_id_map.rs +/// This is a type erased FileAstId. +pub type ErasedFileAstId = la_arena::Idx; /// `AstId` points to an AST node in a specific file. pub struct FileAstId { @@ -138,7 +110,6 @@ pub struct AstIdMap { arena: Arena, /// Reverse: map ptr to id. map: hashbrown::HashMap, (), ()>, - _c: Count, } impl fmt::Debug for AstIdMap { @@ -155,14 +126,7 @@ impl PartialEq for AstIdMap { impl Eq for AstIdMap {} impl AstIdMap { - pub(crate) fn new( - db: &dyn ExpandDatabase, - file_id: span::HirFileId, - ) -> triomphe::Arc { - triomphe::Arc::new(AstIdMap::from_source(&db.parse_or_expand(file_id))) - } - - fn from_source(node: &SyntaxNode) -> AstIdMap { + pub fn from_source(node: &SyntaxNode) -> AstIdMap { assert!(node.parent().is_none()); let mut res = AstIdMap::default(); diff --git a/crates/span/src/lib.rs b/crates/span/src/lib.rs index 6b363e9050..0fe3275863 100644 --- a/crates/span/src/lib.rs +++ b/crates/span/src/lib.rs @@ -3,10 +3,12 @@ use std::fmt::{self, Write}; use salsa::InternId; +mod ast_id; mod hygiene; mod map; pub use self::{ + ast_id::{AstIdMap, AstIdNode, ErasedFileAstId, FileAstId}, hygiene::{SyntaxContextData, SyntaxContextId, Transparency}, map::{RealSpanMap, SpanMap}, }; @@ -26,10 +28,6 @@ pub struct FileRange { pub range: TextRange, } -/// See crates\hir-expand\src\ast_id_map.rs -/// This is a type erased FileAstId. -pub type ErasedFileAstId = la_arena::Idx; - // The first index is always the root node's AstId /// The root ast id always points to the encompassing file, using this in spans is discouraged as /// any range relative to it will be effectively absolute, ruining the entire point of anchored