Skip to content

Commit 8ae5d6f

Browse files
committed
Add support for Stringify builtin macro
1 parent c927382 commit 8ae5d6f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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)