Skip to content

Commit f5dc28e

Browse files
bors[bot]pszpetkowskimatklad
authored
Merge #2348 #2352
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 <[email protected]> Co-authored-by: Aleksey Kladov <[email protected]>
3 parents 5dd44a0 + 8ae5d6f + 360f9bf commit f5dc28e

File tree

8 files changed

+45
-20
lines changed

8 files changed

+45
-20
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,15 +937,15 @@ impl TypeAlias {
937937
}
938938

939939
pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> {
940-
db.type_alias_data(self).type_ref.clone()
940+
db.type_alias_data(self.id).type_ref.clone()
941941
}
942942

943943
pub fn ty(self, db: &impl HirDatabase) -> Ty {
944944
db.type_for_def(self.into(), Namespace::Types)
945945
}
946946

947947
pub fn name(self, db: &impl DefDatabase) -> Name {
948-
db.type_alias_data(self).name.clone()
948+
db.type_alias_data(self.id).name.clone()
949949
}
950950
}
951951

crates/ra_hir/src/db.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@ use crate::{
1616
CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef,
1717
TypeCtor,
1818
},
19-
type_alias::TypeAliasData,
2019
Const, ConstData, Crate, DefWithBody, FnData, Function, GenericDef, ImplBlock, Module, Static,
21-
StructField, Trait, TypeAlias,
20+
StructField, Trait,
2221
};
2322

2423
pub use hir_def::db::{
2524
BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage,
2625
EnumDataQuery, ExprScopesQuery, GenericParamsQuery, ImplDataQuery, InternDatabase,
2726
InternDatabaseStorage, RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery,
28-
TraitDataQuery,
27+
TraitDataQuery, TypeAliasDataQuery,
2928
};
3029
pub use hir_expand::db::{
3130
AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
@@ -39,9 +38,6 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
3938
#[salsa::invoke(FnData::fn_data_query)]
4039
fn fn_data(&self, func: Function) -> Arc<FnData>;
4140

42-
#[salsa::invoke(TypeAliasData::type_alias_data_query)]
43-
fn type_alias_data(&self, typ: TypeAlias) -> Arc<TypeAliasData>;
44-
4541
#[salsa::invoke(ConstData::const_data_query)]
4642
fn const_data(&self, konst: Const) -> Arc<ConstData>;
4743

crates/ra_hir/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub mod db;
3232
pub mod source_binder;
3333

3434
mod ids;
35-
mod type_alias;
3635
mod ty;
3736
mod impl_block;
3837
mod expr;

crates/ra_hir_def/src/db.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use crate::{
1515
CrateDefMap,
1616
},
1717
traits::TraitData,
18-
DefWithBodyId, EnumId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId,
18+
type_alias::TypeAliasData,
19+
DefWithBodyId, EnumId, GenericDefId, ImplId, ItemLoc, StructOrUnionId, TraitId, TypeAliasId,
1920
};
2021

2122
#[salsa::query_group(InternDatabaseStorage)]
@@ -64,6 +65,9 @@ pub trait DefDatabase2: InternDatabase + AstDatabase {
6465
#[salsa::invoke(TraitData::trait_data_query)]
6566
fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
6667

68+
#[salsa::invoke(TypeAliasData::type_alias_data_query)]
69+
fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>;
70+
6771
#[salsa::invoke(Body::body_with_source_map_query)]
6872
fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
6973

crates/ra_hir_def/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub mod body;
2020
pub mod generics;
2121
pub mod traits;
2222
pub mod resolver;
23+
pub mod type_alias;
24+
2325

2426
#[cfg(test)]
2527
mod test_db;

crates/ra_hir/src/type_alias.rs renamed to crates/ra_hir_def/src/type_alias.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,24 @@
22
33
use std::sync::Arc;
44

5-
use hir_def::type_ref::TypeRef;
65
use hir_expand::name::{AsName, Name};
76

87
use ra_syntax::ast::NameOwner;
98

10-
use crate::{
11-
db::{AstDatabase, DefDatabase},
12-
HasSource, TypeAlias,
13-
};
9+
use crate::{db::DefDatabase2, type_ref::TypeRef, HasSource, Lookup, TypeAliasId};
1410

1511
#[derive(Debug, Clone, PartialEq, Eq)]
1612
pub struct TypeAliasData {
17-
pub(crate) name: Name,
18-
pub(crate) type_ref: Option<TypeRef>,
13+
pub name: Name,
14+
pub type_ref: Option<TypeRef>,
1915
}
2016

2117
impl TypeAliasData {
2218
pub(crate) fn type_alias_data_query(
23-
db: &(impl DefDatabase + AstDatabase),
24-
typ: TypeAlias,
19+
db: &impl DefDatabase2,
20+
typ: TypeAliasId,
2521
) -> Arc<TypeAliasData> {
26-
let node = typ.source(db).value;
22+
let node = typ.lookup(db).source(db).value;
2723
let name = node.name().map_or_else(Name::missing, |n| n.as_name());
2824
let type_ref = node.type_ref().map(TypeRef::from_ast);
2925
Arc::new(TypeAliasData { name, type_ref })

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::quote;
1111
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1212
pub enum BuiltinExpander {
1313
Line,
14+
Stringify,
1415
}
1516

1617
impl BuiltinExpander {
@@ -22,6 +23,7 @@ impl BuiltinExpander {
2223
) -> Result<tt::Subtree, mbe::ExpandError> {
2324
match self {
2425
BuiltinExpander::Line => line_expand(db, id, tt),
26+
BuiltinExpander::Stringify => stringify_expand(db, id, tt),
2527
}
2628
}
2729
}
@@ -34,6 +36,8 @@ pub fn find_builtin_macro(
3436
// FIXME: Better registering method
3537
if ident == &name::LINE_MACRO {
3638
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) })
39+
} else if ident == &name::STRINGIFY_MACRO {
40+
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Stringify) })
3741
} else {
3842
None
3943
}
@@ -78,3 +82,26 @@ fn line_expand(
7882

7983
Ok(expanded)
8084
}
85+
86+
fn stringify_expand(
87+
db: &dyn AstDatabase,
88+
id: MacroCallId,
89+
_tt: &tt::Subtree,
90+
) -> Result<tt::Subtree, mbe::ExpandError> {
91+
let loc = db.lookup_intern_macro(id);
92+
let macro_call = loc.ast_id.to_node(db);
93+
94+
let macro_content = {
95+
let arg = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
96+
let macro_args = arg.syntax().clone();
97+
let text = macro_args.text();
98+
let without_parens = TextUnit::of_char('(')..text.len() - TextUnit::of_char(')');
99+
text.slice(without_parens).to_string()
100+
};
101+
102+
let expanded = quote! {
103+
#macro_content
104+
};
105+
106+
Ok(expanded)
107+
}

crates/ra_hir_expand/src/name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,4 @@ pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
143143

144144
// Builtin Macros
145145
pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
146+
pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify");

0 commit comments

Comments
 (0)