Use InFile for AstId

This commit is contained in:
Aleksey Kladov 2019-11-28 16:00:03 +03:00
parent 16030528b5
commit e823c578c9
6 changed files with 16 additions and 47 deletions

View file

@ -105,7 +105,7 @@ impl HasSource for TypeAlias {
impl HasSource for MacroDef {
type Ast = ast::MacroCall;
fn source(self, db: &impl DefDatabase) -> InFile<ast::MacroCall> {
InFile { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) }
InFile { file_id: self.id.ast_id.file_id, value: self.id.ast_id.to_node(db) }
}
}
impl HasSource for ImplBlock {

View file

@ -157,7 +157,7 @@ where
N: ast::AttrsOwner,
D: DefDatabase,
{
let src = InFile::new(src.file_id(), src.to_node(db));
let src = InFile::new(src.file_id, src.to_node(db));
Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner))
}

View file

@ -108,7 +108,7 @@ pub trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
fn source(self, db: &(impl AstDatabase + InternDatabase)) -> InFile<N> {
let loc = self.lookup_intern(db);
let value = loc.ast_id.to_node(db);
InFile { file_id: loc.ast_id.file_id(), value }
InFile { file_id: loc.ast_id.file_id, value }
}
fn module(self, db: &impl InternDatabase) -> ModuleId {
let loc = self.lookup_intern(db);
@ -525,7 +525,7 @@ impl HasSource for FunctionLoc {
fn source(&self, db: &impl db::DefDatabase) -> InFile<ast::FnDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id(), node)
InFile::new(self.ast_id.file_id, node)
}
}
@ -534,7 +534,7 @@ impl HasSource for TypeAliasLoc {
fn source(&self, db: &impl db::DefDatabase) -> InFile<ast::TypeAliasDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id(), node)
InFile::new(self.ast_id.file_id, node)
}
}
@ -543,7 +543,7 @@ impl HasSource for ConstLoc {
fn source(&self, db: &impl db::DefDatabase) -> InFile<ast::ConstDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id(), node)
InFile::new(self.ast_id.file_id, node)
}
}
@ -552,7 +552,7 @@ impl HasSource for StaticLoc {
fn source(&self, db: &impl db::DefDatabase) -> InFile<ast::StaticDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id(), node)
InFile::new(self.ast_id.file_id, node)
}
}

View file

@ -267,7 +267,7 @@ impl ModuleData {
return InFile::new(file_id.into(), Either::A(sf));
}
let decl = self.declaration.unwrap();
InFile::new(decl.file_id(), Either::B(decl.to_node(db)))
InFile::new(decl.file_id, Either::B(decl.to_node(db)))
}
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
@ -275,7 +275,7 @@ impl ModuleData {
pub fn declaration_source(&self, db: &impl DefDatabase) -> Option<InFile<ast::Module>> {
let decl = self.declaration?;
let value = decl.to_node(db);
Some(InFile { file_id: decl.file_id(), value })
Some(InFile { file_id: decl.file_id, value })
}
}
@ -309,7 +309,7 @@ mod diagnostics {
}
let decl = declaration.to_node(db);
sink.push(UnresolvedModule {
file: declaration.file_id(),
file: declaration.file_id,
decl: AstPtr::new(&decl),
candidate: candidate.clone(),
})

View file

@ -668,7 +668,7 @@ fn unresolved_module_diagnostics() {
module: LocalModuleId(
0,
),
declaration: AstId {
declaration: InFile {
file_id: HirFileId(
FileId(
FileId(
@ -676,7 +676,7 @@ fn unresolved_module_diagnostics() {
),
),
),
file_ast_id: FileAstId {
value: FileAstId {
raw: ErasedFileAstId(
1,
),

View file

@ -13,7 +13,7 @@ pub mod diagnostics;
pub mod builtin_macro;
pub mod quote;
use std::hash::{Hash, Hasher};
use std::hash::Hash;
use std::sync::Arc;
use ra_db::{salsa, CrateId, FileId};
@ -70,7 +70,7 @@ impl HirFileId {
HirFileIdRepr::FileId(file_id) => file_id,
HirFileIdRepr::MacroFile(macro_file) => {
let loc = db.lookup_intern_macro(macro_file.macro_call_id);
loc.ast_id.file_id().original_file(db)
loc.ast_id.file_id.original_file(db)
}
}
}
@ -214,43 +214,12 @@ impl ExpansionInfo {
///
/// It is stable across reparses, and can be used as salsa key/value.
// FIXME: isn't this just a `Source<FileAstId<N>>` ?
#[derive(Debug)]
pub struct AstId<N: AstNode> {
file_id: HirFileId,
file_ast_id: FileAstId<N>,
}
impl<N: AstNode> Clone for AstId<N> {
fn clone(&self) -> AstId<N> {
*self
}
}
impl<N: AstNode> Copy for AstId<N> {}
impl<N: AstNode> PartialEq for AstId<N> {
fn eq(&self, other: &Self) -> bool {
(self.file_id, self.file_ast_id) == (other.file_id, other.file_ast_id)
}
}
impl<N: AstNode> Eq for AstId<N> {}
impl<N: AstNode> Hash for AstId<N> {
fn hash<H: Hasher>(&self, hasher: &mut H) {
(self.file_id, self.file_ast_id).hash(hasher);
}
}
pub type AstId<N> = InFile<FileAstId<N>>;
impl<N: AstNode> AstId<N> {
pub fn new(file_id: HirFileId, file_ast_id: FileAstId<N>) -> AstId<N> {
AstId { file_id, file_ast_id }
}
pub fn file_id(&self) -> HirFileId {
self.file_id
}
pub fn to_node(&self, db: &dyn db::AstDatabase) -> N {
let root = db.parse_or_expand(self.file_id).unwrap();
db.ast_id_map(self.file_id).get(self.file_ast_id).to_node(&root)
db.ast_id_map(self.file_id).get(self.value).to_node(&root)
}
}