Skip to content

Commit

Permalink
Auto merge of #16145 - Veykril:span-crate, r=Veykril
Browse files Browse the repository at this point in the history
internal: Split out a span crate

Allows getting rid of some of the dependency injection generics that my rewrite introduced
  • Loading branch information
bors committed Dec 18, 2023
2 parents cfc959d + ec61623 commit 0380869
Show file tree
Hide file tree
Showing 51 changed files with 588 additions and 514 deletions.
20 changes: 19 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ proc-macro-srv-cli = { path = "./crates/proc-macro-srv-cli", version = "0.0.0" }
profile = { path = "./crates/profile", version = "0.0.0" }
project-model = { path = "./crates/project-model", version = "0.0.0" }
sourcegen = { path = "./crates/sourcegen", version = "0.0.0" }
span = { path = "./crates/span", version = "0.0.0" }
stdx = { path = "./crates/stdx", version = "0.0.0" }
syntax = { path = "./crates/syntax", version = "0.0.0" }
test-utils = { path = "./crates/test-utils", version = "0.0.0" }
Expand Down
2 changes: 1 addition & 1 deletion crates/base-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ profile.workspace = true
stdx.workspace = true
syntax.workspace = true
test-utils.workspace = true
tt.workspace = true
vfs.workspace = true
span.workspace = true
16 changes: 2 additions & 14 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

mod input;
mod change;
pub mod span;

use std::panic;

use rustc_hash::FxHashSet;
use syntax::{ast, Parse, SourceFile, TextRange, TextSize};
use syntax::{ast, Parse, SourceFile};
use triomphe::Arc;

pub use crate::{
Expand All @@ -21,6 +20,7 @@ pub use crate::{
},
};
pub use salsa::{self, Cancelled};
pub use span::{FilePosition, FileRange};
pub use vfs::{file_set::FileSet, AnchoredPath, AnchoredPathBuf, FileId, VfsPath};

#[macro_export]
Expand All @@ -41,18 +41,6 @@ pub trait Upcast<T: ?Sized> {
fn upcast(&self) -> &T;
}

#[derive(Clone, Copy, Debug)]
pub struct FilePosition {
pub file_id: FileId,
pub offset: TextSize,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct FileRange {
pub file_id: FileId,
pub range: TextRange,
}

pub const DEFAULT_PARSE_LRU_CAP: usize = 128;

pub trait FileLoader {
Expand Down
1 change: 1 addition & 0 deletions crates/hir-def/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mbe.workspace = true
cfg.workspace = true
tt.workspace = true
limit.workspace = true
span.workspace = true


[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/attr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! Currently, it tests `#[doc(hidden)]` and `#[doc(alias)]`.
use base_db::FileId;
use hir_expand::span::{RealSpanMap, SpanMapRef};
use hir_expand::span_map::{RealSpanMap, SpanMapRef};
use mbe::syntax_node_to_token_tree;
use syntax::{ast, AstNode};

Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use base_db::CrateId;
use cfg::CfgOptions;
use drop_bomb::DropBomb;
use hir_expand::{
attrs::RawAttrs, mod_path::ModPath, span::SpanMap, ExpandError, ExpandResult, HirFileId,
attrs::RawAttrs, mod_path::ModPath, span_map::SpanMap, ExpandError, ExpandResult, HirFileId,
InFile, MacroCallId,
};
use limit::Limit;
Expand Down
3 changes: 2 additions & 1 deletion crates/hir-def/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use std::{
};

use ast::{AstNode, HasName, StructKind};
use base_db::{span::SyntaxContextId, CrateId};
use base_db::CrateId;
use either::Either;
use hir_expand::{
ast_id_map::{AstIdNode, FileAstId},
Expand All @@ -55,6 +55,7 @@ use la_arena::{Arena, Idx, IdxRange, RawIdx};
use profile::Count;
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use span::SyntaxContextId;
use stdx::never;
use syntax::{ast, match_ast, SyntaxKind};
use triomphe::Arc;
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::collections::hash_map::Entry;

use hir_expand::{ast_id_map::AstIdMap, span::SpanMapRef, HirFileId};
use hir_expand::{ast_id_map::AstIdMap, span_map::SpanMapRef, HirFileId};
use syntax::ast::{self, HasModuleItem, HasTypeBounds};

use crate::{
Expand Down
3 changes: 2 additions & 1 deletion crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use std::{
panic::{RefUnwindSafe, UnwindSafe},
};

use base_db::{impl_intern_key, salsa, span::SyntaxContextId, CrateId};
use base_db::{impl_intern_key, salsa, CrateId};
use hir_expand::{
ast_id_map::{AstIdNode, FileAstId},
attrs::{Attr, AttrId, AttrInput},
Expand All @@ -80,6 +80,7 @@ use hir_expand::{
use item_tree::ExternBlock;
use la_arena::Idx;
use nameres::DefMap;
use span::SyntaxContextId;
use stdx::impl_from;
use syntax::{ast, AstNode};

Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cell::OnceCell;

use hir_expand::{
ast_id_map::{AstIdMap, AstIdNode},
span::{SpanMap, SpanMapRef},
span_map::{SpanMap, SpanMapRef},
AstId, HirFileId, InFile,
};
use syntax::ast;
Expand Down
11 changes: 6 additions & 5 deletions crates/hir-def/src/macro_expansion_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ mod proc_macros;

use std::{iter, ops::Range, sync};

use base_db::{span::SpanData, SourceDatabase};
use base_db::SourceDatabase;
use expect_test::Expect;
use hir_expand::{
db::ExpandDatabase,
fixture::WithFixture,
proc_macro::{ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind},
span::SpanMapRef,
span_map::SpanMapRef,
InFile, MacroFileId, MacroFileIdExt,
};
use span::Span;
use stdx::format_to;
use syntax::{
ast::{self, edit::IndentLevel},
Expand Down Expand Up @@ -319,9 +320,9 @@ impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
subtree: &Subtree,
_: Option<&Subtree>,
_: &base_db::Env,
_: SpanData,
_: SpanData,
_: SpanData,
_: Span,
_: Span,
_: Span,
) -> Result<Subtree, ProcMacroExpansionError> {
let (parse, _) =
::mbe::token_tree_to_syntax_node(subtree, ::mbe::TopEntryPoint::MacroItems);
Expand Down
15 changes: 8 additions & 7 deletions crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::{cmp::Ordering, iter, mem};

use base_db::{span::SyntaxContextId, CrateId, Dependency, Edition, FileId};
use base_db::{CrateId, Dependency, Edition, FileId};
use cfg::{CfgExpr, CfgOptions};
use either::Either;
use hir_expand::{
Expand All @@ -23,6 +23,7 @@ use itertools::{izip, Itertools};
use la_arena::Idx;
use limit::Limit;
use rustc_hash::{FxHashMap, FxHashSet};
use span::{Span, SyntaxContextId};
use stdx::always;
use syntax::{ast, SmolStr};
use triomphe::Arc;
Expand Down Expand Up @@ -86,11 +87,11 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI
// FIXME: a hacky way to create a Name from string.
let name = tt::Ident {
text: it.name.clone(),
span: tt::SpanData {
span: Span {
range: syntax::TextRange::empty(syntax::TextSize::new(0)),
anchor: base_db::span::SpanAnchor {
anchor: span::SpanAnchor {
file_id: FileId::BOGUS,
ast_id: base_db::span::ROOT_ERASED_FILE_AST_ID,
ast_id: span::ROOT_ERASED_FILE_AST_ID,
},
ctx: SyntaxContextId::ROOT,
},
Expand Down Expand Up @@ -2095,11 +2096,11 @@ impl ModCollector<'_, '_> {
// FIXME: a hacky way to create a Name from string.
name = tt::Ident {
text: it.clone(),
span: tt::SpanData {
span: Span {
range: syntax::TextRange::empty(syntax::TextSize::new(0)),
anchor: base_db::span::SpanAnchor {
anchor: span::SpanAnchor {
file_id: FileId::BOGUS,
ast_id: base_db::span::ROOT_ERASED_FILE_AST_ID,
ast_id: span::ROOT_ERASED_FILE_AST_ID,
},
ctx: SyntaxContextId::ROOT,
},
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::iter;

use hir_expand::{span::SpanMapRef, InFile};
use hir_expand::{span_map::SpanMapRef, InFile};
use la_arena::ArenaMap;
use syntax::ast;
use triomphe::Arc;
Expand Down
1 change: 1 addition & 0 deletions crates/hir-expand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ profile.workspace = true
tt.workspace = true
mbe.workspace = true
limit.workspace = true
span.workspace = true
test-utils.workspace = true

[dev-dependencies]
Expand Down
14 changes: 8 additions & 6 deletions crates/hir-expand/src/ast_id_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! 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,
Expand All @@ -17,31 +19,31 @@ use profile::Count;
use rustc_hash::FxHasher;
use syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};

use crate::db;
use crate::db::ExpandDatabase;

pub use base_db::span::ErasedFileAstId;
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<N> = crate::InFile<FileAstId<N>>;

impl<N: AstIdNode> AstId<N> {
pub fn to_node(&self, db: &dyn db::ExpandDatabase) -> N {
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 db::ExpandDatabase) -> crate::InFile<N> {
pub fn to_in_file_node(&self, db: &dyn ExpandDatabase) -> crate::InFile<N> {
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 db::ExpandDatabase) -> AstPtr<N> {
pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> AstPtr<N> {
db.ast_id_map(self.file_id).get(self.value)
}
}

pub type ErasedAstId = crate::InFile<ErasedFileAstId>;

impl ErasedAstId {
pub fn to_ptr(&self, db: &dyn db::ExpandDatabase) -> SyntaxNodePtr {
pub fn to_ptr(&self, db: &dyn ExpandDatabase) -> SyntaxNodePtr {
db.ast_id_map(self.file_id).get_erased(self.value)
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/hir-expand/src/attrs.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
//! A higher level attributes based on TokenTree, with also some shortcuts.
use std::{fmt, ops};

use base_db::{span::SyntaxContextId, CrateId};
use base_db::CrateId;
use cfg::CfgExpr;
use either::Either;
use intern::Interned;
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
use smallvec::{smallvec, SmallVec};
use span::SyntaxContextId;
use syntax::{ast, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
use triomphe::Arc;

use crate::{
db::ExpandDatabase,
mod_path::ModPath,
span::SpanMapRef,
span_map::SpanMapRef,
tt::{self, Subtree},
InFile,
};
Expand Down
Loading

0 comments on commit 0380869

Please sign in to comment.