aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-22 16:07:37 +0000
committerGitHub <[email protected]>2019-11-22 16:07:37 +0000
commit506131e3e01a81546ab218e1764073061875ea2e (patch)
treed9f4f83de92e8b4a38ceb9ad6003f55d544373e1 /crates/ra_hir_expand
parent404493e9df8cc34fab5ff0c605eef7700e687e80 (diff)
parent786544f022418ed7813e401aded31e4bd5451391 (diff)
Merge #2357
2357: Expand file! to dummy "" r=edwin0cheng a=kjeremy See https://github.com/rust-analyzer/rust-analyzer/pull/2355#issuecomment-557541873 Co-authored-by: kjeremy <[email protected]>
Diffstat (limited to 'crates/ra_hir_expand')
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs26
-rw-r--r--crates/ra_hir_expand/src/name.rs1
2 files changed, 26 insertions, 1 deletions
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index 9628666d4..d7057e005 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -10,6 +10,7 @@ use crate::quote;
10 10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub enum BuiltinExpander { 12pub enum BuiltinExpander {
13 File,
13 Line, 14 Line,
14 Stringify, 15 Stringify,
15} 16}
@@ -22,6 +23,7 @@ impl BuiltinExpander {
22 tt: &tt::Subtree, 23 tt: &tt::Subtree,
23 ) -> Result<tt::Subtree, mbe::ExpandError> { 24 ) -> Result<tt::Subtree, mbe::ExpandError> {
24 match self { 25 match self {
26 BuiltinExpander::File => file_expand(db, id, tt),
25 BuiltinExpander::Line => line_expand(db, id, tt), 27 BuiltinExpander::Line => line_expand(db, id, tt),
26 BuiltinExpander::Stringify => stringify_expand(db, id, tt), 28 BuiltinExpander::Stringify => stringify_expand(db, id, tt),
27 } 29 }
@@ -34,7 +36,9 @@ pub fn find_builtin_macro(
34 ast_id: AstId<ast::MacroCall>, 36 ast_id: AstId<ast::MacroCall>,
35) -> Option<MacroDefId> { 37) -> Option<MacroDefId> {
36 // FIXME: Better registering method 38 // FIXME: Better registering method
37 if ident == &name::LINE_MACRO { 39 if ident == &name::FILE_MACRO {
40 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::File) })
41 } else if ident == &name::LINE_MACRO {
38 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) }) 42 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) })
39 } else if ident == &name::STRINGIFY_MACRO { 43 } else if ident == &name::STRINGIFY_MACRO {
40 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Stringify) }) 44 Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Stringify) })
@@ -105,3 +109,23 @@ fn stringify_expand(
105 109
106 Ok(expanded) 110 Ok(expanded)
107} 111}
112
113fn file_expand(
114 db: &dyn AstDatabase,
115 id: MacroCallId,
116 _tt: &tt::Subtree,
117) -> Result<tt::Subtree, mbe::ExpandError> {
118 let loc = db.lookup_intern_macro(id);
119 let macro_call = loc.ast_id.to_node(db);
120 let _ = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;
121
122 // FIXME: RA purposefully lacks knowledge of absolute file names
123 // so just return "".
124 let file_name = "";
125
126 let expanded = quote! {
127 #file_name
128 };
129
130 Ok(expanded)
131}
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs
index c3f7e77a5..05fe6afd9 100644
--- a/crates/ra_hir_expand/src/name.rs
+++ b/crates/ra_hir_expand/src/name.rs
@@ -142,5 +142,6 @@ pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
142pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box"); 142pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
143 143
144// Builtin Macros 144// Builtin Macros
145pub const FILE_MACRO: Name = Name::new_inline_ascii(4, b"file");
145pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line"); 146pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
146pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify"); 147pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify");