Skip to content

Commit 506131e

Browse files
bors[bot]kjeremy
andauthored
Merge #2357
2357: Expand file! to dummy "" r=edwin0cheng a=kjeremy See #2355 (comment) Co-authored-by: kjeremy <[email protected]>
2 parents 404493e + 786544f commit 506131e

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

crates/ra_hir/src/ty/tests.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4857,3 +4857,22 @@ fn main() {
48574857
"###
48584858
);
48594859
}
4860+
4861+
#[test]
4862+
fn infer_builtin_macros_file() {
4863+
assert_snapshot!(
4864+
infer(r#"
4865+
#[rustc_builtin_macro]
4866+
macro_rules! file {() => {}}
4867+
4868+
fn main() {
4869+
let x = file!();
4870+
}
4871+
"#),
4872+
@r###"
4873+
![0; 2) '""': &str
4874+
[64; 88) '{ ...!(); }': ()
4875+
[74; 75) 'x': &str
4876+
"###
4877+
);
4878+
}

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::quote;
1010

1111
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1212
pub enum BuiltinExpander {
13+
File,
1314
Line,
1415
Stringify,
1516
}
@@ -22,6 +23,7 @@ impl BuiltinExpander {
2223
tt: &tt::Subtree,
2324
) -> Result<tt::Subtree, mbe::ExpandError> {
2425
match self {
26+
BuiltinExpander::File => file_expand(db, id, tt),
2527
BuiltinExpander::Line => line_expand(db, id, tt),
2628
BuiltinExpander::Stringify => stringify_expand(db, id, tt),
2729
}
@@ -34,7 +36,9 @@ pub fn find_builtin_macro(
3436
ast_id: AstId<ast::MacroCall>,
3537
) -> Option<MacroDefId> {
3638
// 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 {
3842
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) })
3943
} else if ident == &name::STRINGIFY_MACRO {
4044
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Stringify) })
@@ -105,3 +109,23 @@ fn stringify_expand(
105109

106110
Ok(expanded)
107111
}
112+
113+
fn 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+
}

crates/ra_hir_expand/src/name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,6 @@ pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
142142
pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");
143143

144144
// Builtin Macros
145+
pub const FILE_MACRO: Name = Name::new_inline_ascii(4, b"file");
145146
pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line");
146147
pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify");

0 commit comments

Comments
 (0)