Skip to content

Expand file! to dummy "" #2357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions crates/ra_hir/src/ty/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4857,3 +4857,22 @@ fn main() {
"###
);
}

#[test]
fn infer_builtin_macros_file() {
assert_snapshot!(
infer(r#"
#[rustc_builtin_macro]
macro_rules! file {() => {}}

fn main() {
let x = file!();
}
"#),
@r###"
![0; 2) '""': &str
[64; 88) '{ ...!(); }': ()
[74; 75) 'x': &str
"###
);
}
26 changes: 25 additions & 1 deletion crates/ra_hir_expand/src/builtin_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::quote;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BuiltinExpander {
File,
Line,
Stringify,
}
Expand All @@ -22,6 +23,7 @@ impl BuiltinExpander {
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
match self {
BuiltinExpander::File => file_expand(db, id, tt),
BuiltinExpander::Line => line_expand(db, id, tt),
BuiltinExpander::Stringify => stringify_expand(db, id, tt),
}
Expand All @@ -34,7 +36,9 @@ pub fn find_builtin_macro(
ast_id: AstId<ast::MacroCall>,
) -> Option<MacroDefId> {
// FIXME: Better registering method
if ident == &name::LINE_MACRO {
if ident == &name::FILE_MACRO {
Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::File) })
} else if ident == &name::LINE_MACRO {
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) })
Expand Down Expand Up @@ -105,3 +109,23 @@ fn stringify_expand(

Ok(expanded)
}

fn file_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_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?;

// FIXME: RA purposefully lacks knowledge of absolute file names
// so just return "".
let file_name = "";

let expanded = quote! {
#file_name
};

Ok(expanded)
}
1 change: 1 addition & 0 deletions crates/ra_hir_expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@ pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");

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