mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 22:31:43 +00:00
2348: Add support for stringify! builtin macro r=matklad a=piotr-szpetkowski Refs #2212 First time ever contributing here, hopefully it's ok. 2352: Move TypeAlias to hir_def r=matklad a=matklad Co-authored-by: Piotr Szpetkowski <piotr.szpetkowski@pyquest.space> Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
5be7bd605a
8 changed files with 44 additions and 20 deletions
|
@ -937,7 +937,7 @@ impl TypeAlias {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> {
|
pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> {
|
||||||
db.type_alias_data(self).type_ref.clone()
|
db.type_alias_data(self.id).type_ref.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ty(self, db: &impl HirDatabase) -> Ty {
|
pub fn ty(self, db: &impl HirDatabase) -> Ty {
|
||||||
|
@ -945,7 +945,7 @@ impl TypeAlias {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(self, db: &impl DefDatabase) -> Name {
|
pub fn name(self, db: &impl DefDatabase) -> Name {
|
||||||
db.type_alias_data(self).name.clone()
|
db.type_alias_data(self.id).name.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,16 +16,15 @@ use crate::{
|
||||||
CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef,
|
CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef,
|
||||||
TypeCtor,
|
TypeCtor,
|
||||||
},
|
},
|
||||||
type_alias::TypeAliasData,
|
|
||||||
Const, ConstData, Crate, DefWithBody, FnData, Function, GenericDef, ImplBlock, Module, Static,
|
Const, ConstData, Crate, DefWithBody, FnData, Function, GenericDef, ImplBlock, Module, Static,
|
||||||
StructField, Trait, TypeAlias,
|
StructField, Trait,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use hir_def::db::{
|
pub use hir_def::db::{
|
||||||
BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage,
|
BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage,
|
||||||
EnumDataQuery, ExprScopesQuery, GenericParamsQuery, ImplDataQuery, InternDatabase,
|
EnumDataQuery, ExprScopesQuery, GenericParamsQuery, ImplDataQuery, InternDatabase,
|
||||||
InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery,
|
InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery,
|
||||||
TraitDataQuery,
|
TraitDataQuery, TypeAliasDataQuery,
|
||||||
};
|
};
|
||||||
pub use hir_expand::db::{
|
pub use hir_expand::db::{
|
||||||
AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
|
AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
|
||||||
|
@ -39,9 +38,6 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
|
||||||
#[salsa::invoke(FnData::fn_data_query)]
|
#[salsa::invoke(FnData::fn_data_query)]
|
||||||
fn fn_data(&self, func: Function) -> Arc<FnData>;
|
fn fn_data(&self, func: Function) -> Arc<FnData>;
|
||||||
|
|
||||||
#[salsa::invoke(TypeAliasData::type_alias_data_query)]
|
|
||||||
fn type_alias_data(&self, typ: TypeAlias) -> Arc<TypeAliasData>;
|
|
||||||
|
|
||||||
#[salsa::invoke(ConstData::const_data_query)]
|
#[salsa::invoke(ConstData::const_data_query)]
|
||||||
fn const_data(&self, konst: Const) -> Arc<ConstData>;
|
fn const_data(&self, konst: Const) -> Arc<ConstData>;
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,6 @@ pub mod db;
|
||||||
pub mod source_binder;
|
pub mod source_binder;
|
||||||
|
|
||||||
mod ids;
|
mod ids;
|
||||||
mod type_alias;
|
|
||||||
mod ty;
|
mod ty;
|
||||||
mod impl_block;
|
mod impl_block;
|
||||||
mod expr;
|
mod expr;
|
||||||
|
|
|
@ -15,7 +15,8 @@ use crate::{
|
||||||
CrateDefMap,
|
CrateDefMap,
|
||||||
},
|
},
|
||||||
traits::TraitData,
|
traits::TraitData,
|
||||||
DefWithBodyId, EnumId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId,
|
type_alias::TypeAliasData,
|
||||||
|
DefWithBodyId, EnumId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, TypeAliasId,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[salsa::query_group(InternDatabaseStorage)]
|
#[salsa::query_group(InternDatabaseStorage)]
|
||||||
|
@ -64,6 +65,9 @@ pub trait DefDatabase2: InternDatabase + AstDatabase {
|
||||||
#[salsa::invoke(TraitData::trait_data_query)]
|
#[salsa::invoke(TraitData::trait_data_query)]
|
||||||
fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
|
fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
|
||||||
|
|
||||||
|
#[salsa::invoke(TypeAliasData::type_alias_data_query)]
|
||||||
|
fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>;
|
||||||
|
|
||||||
#[salsa::invoke(Body::body_with_source_map_query)]
|
#[salsa::invoke(Body::body_with_source_map_query)]
|
||||||
fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
|
fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ pub mod body;
|
||||||
pub mod generics;
|
pub mod generics;
|
||||||
pub mod traits;
|
pub mod traits;
|
||||||
pub mod resolver;
|
pub mod resolver;
|
||||||
|
pub mod type_alias;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test_db;
|
mod test_db;
|
||||||
|
|
|
@ -2,28 +2,24 @@
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use hir_def::type_ref::TypeRef;
|
|
||||||
use hir_expand::name::{AsName, Name};
|
use hir_expand::name::{AsName, Name};
|
||||||
|
|
||||||
use ra_syntax::ast::NameOwner;
|
use ra_syntax::ast::NameOwner;
|
||||||
|
|
||||||
use crate::{
|
use crate::{db::DefDatabase2, type_ref::TypeRef, HasSource, Lookup, TypeAliasId};
|
||||||
db::{AstDatabase, DefDatabase},
|
|
||||||
HasSource, TypeAlias,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct TypeAliasData {
|
pub struct TypeAliasData {
|
||||||
pub(crate) name: Name,
|
pub name: Name,
|
||||||
pub(crate) type_ref: Option<TypeRef>,
|
pub type_ref: Option<TypeRef>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TypeAliasData {
|
impl TypeAliasData {
|
||||||
pub(crate) fn type_alias_data_query(
|
pub(crate) fn type_alias_data_query(
|
||||||
db: &(impl DefDatabase + AstDatabase),
|
db: &impl DefDatabase2,
|
||||||
typ: TypeAlias,
|
typ: TypeAliasId,
|
||||||
) -> Arc<TypeAliasData> {
|
) -> Arc<TypeAliasData> {
|
||||||
let node = typ.source(db).value;
|
let node = typ.lookup(db).source(db).value;
|
||||||
let name = node.name().map_or_else(Name::missing, |n| n.as_name());
|
let name = node.name().map_or_else(Name::missing, |n| n.as_name());
|
||||||
let type_ref = node.type_ref().map(TypeRef::from_ast);
|
let type_ref = node.type_ref().map(TypeRef::from_ast);
|
||||||
Arc::new(TypeAliasData { name, type_ref })
|
Arc::new(TypeAliasData { name, type_ref })
|
|
@ -11,6 +11,7 @@ use crate::quote;
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub enum BuiltinExpander {
|
pub enum BuiltinExpander {
|
||||||
Line,
|
Line,
|
||||||
|
Stringify,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BuiltinExpander {
|
impl BuiltinExpander {
|
||||||
|
@ -22,6 +23,7 @@ impl BuiltinExpander {
|
||||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||||
match self {
|
match self {
|
||||||
BuiltinExpander::Line => line_expand(db, id, tt),
|
BuiltinExpander::Line => line_expand(db, id, tt),
|
||||||
|
BuiltinExpander::Stringify => stringify_expand(db, id, tt),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +36,8 @@ pub fn find_builtin_macro(
|
||||||
// FIXME: Better registering method
|
// FIXME: Better registering method
|
||||||
if ident == &name::LINE_MACRO {
|
if ident == &name::LINE_MACRO {
|
||||||
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) })
|
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) })
|
||||||
|
} else if ident == &name::STRINGIFY_MACRO {
|
||||||
|
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Stringify) })
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -78,3 +82,26 @@ fn line_expand(
|
||||||
|
|
||||||
Ok(expanded)
|
Ok(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn stringify_expand(
|
||||||
|
db: &dyn AstDatabase,
|
||||||
|
id: MacroCallId,
|
||||||
|
_tt: &tt::Subtree,
|
||||||
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
||||||
|
let loc = db.lookup_intern_macro(id);
|
||||||
|
let macro_call = loc.ast_id.to_node(db);
|
||||||
|
|
||||||
|
let macro_content = {
|
||||||
|
let arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
|
||||||
|
let macro_args = arg.syntax().clone();
|
||||||
|
let text = macro_args.text();
|
||||||
|
let without_parens = TextUnit::of_char('(')..text.len() - TextUnit::of_char(')');
|
||||||
|
text.slice(without_parens).to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
let expanded = quote! {
|
||||||
|
#macro_content
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(expanded)
|
||||||
|
}
|
||||||
|
|
|
@ -143,3 +143,4 @@ pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
|
||||||
|
|
||||||
// Builtin Macros
|
// Builtin Macros
|
||||||
pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
|
pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
|
||||||
|
pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue