Add dummy implementations of env! and option_env! builtins

They don't do anything except return the correct type.

Also refactor the builtin macro tests a bit.
This commit is contained in:
Florian Diebold 2019-12-21 13:33:44 +01:00
parent cdc9d682b0
commit 2058f704cb
3 changed files with 99 additions and 43 deletions

View file

@ -26,6 +26,13 @@ macro_rules! register_builtin {
}; };
expander(db, id, tt) expander(db, id, tt)
} }
fn by_name(ident: &name::Name) -> Option<BuiltinFnLikeExpander> {
match ident {
$( id if id == &name::name![$name] => Some(BuiltinFnLikeExpander::$kind), )*
_ => return None,
}
}
} }
pub fn find_builtin_macro( pub fn find_builtin_macro(
@ -33,10 +40,7 @@ macro_rules! register_builtin {
krate: CrateId, krate: CrateId,
ast_id: AstId<ast::MacroCall>, ast_id: AstId<ast::MacroCall>,
) -> Option<MacroDefId> { ) -> Option<MacroDefId> {
let kind = match ident { let kind = BuiltinFnLikeExpander::by_name(ident)?;
$( id if id == &name::name![$name] => BuiltinFnLikeExpander::$kind, )*
_ => return None,
};
Some(MacroDefId { krate: Some(krate), ast_id: Some(ast_id), kind: MacroDefKind::BuiltIn(kind) }) Some(MacroDefId { krate: Some(krate), ast_id: Some(ast_id), kind: MacroDefKind::BuiltIn(kind) })
} }
@ -50,6 +54,8 @@ register_builtin! {
(line, Line) => line_expand, (line, Line) => line_expand,
(stringify, Stringify) => stringify_expand, (stringify, Stringify) => stringify_expand,
(format_args, FormatArgs) => format_args_expand, (format_args, FormatArgs) => format_args_expand,
(env, Env) => env_expand,
(option_env, OptionEnv) => option_env_expand,
// format_args_nl only differs in that it adds a newline in the end, // format_args_nl only differs in that it adds a newline in the end,
// so we use the same stub expansion for now // so we use the same stub expansion for now
(format_args_nl, FormatArgsNl) => format_args_expand (format_args_nl, FormatArgsNl) => format_args_expand
@ -121,6 +127,28 @@ fn stringify_expand(
Ok(expanded) Ok(expanded)
} }
fn env_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
// dummy implementation for type-checking purposes
let expanded = quote! { "" };
Ok(expanded)
}
fn option_env_expand(
_db: &dyn AstDatabase,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
// dummy implementation for type-checking purposes
let expanded = quote! { std::option::Option::None::<&str> };
Ok(expanded)
}
fn to_col_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize { fn to_col_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize {
// FIXME: Use expansion info // FIXME: Use expansion info
let file_id = file.original_file(db); let file_id = file.original_file(db);
@ -248,10 +276,11 @@ fn format_args_expand(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{test_db::TestDB, MacroCallKind, MacroCallLoc}; use crate::{name::AsName, test_db::TestDB, MacroCallKind, MacroCallLoc};
use ra_db::{fixture::WithFixture, SourceDatabase}; use ra_db::{fixture::WithFixture, SourceDatabase};
use ra_syntax::ast::NameOwner;
fn expand_builtin_macro(s: &str, expander: BuiltinFnLikeExpander) -> String { fn expand_builtin_macro(s: &str) -> String {
let (db, file_id) = TestDB::with_single_file(&s); let (db, file_id) = TestDB::with_single_file(&s);
let parsed = db.parse(file_id); let parsed = db.parse(file_id);
let macro_calls: Vec<_> = let macro_calls: Vec<_> =
@ -259,6 +288,9 @@ mod tests {
let ast_id_map = db.ast_id_map(file_id.into()); let ast_id_map = db.ast_id_map(file_id.into());
let expander =
BuiltinFnLikeExpander::by_name(&macro_calls[0].name().unwrap().as_name()).unwrap();
// the first one should be a macro_rules // the first one should be a macro_rules
let def = MacroDefId { let def = MacroDefId {
krate: Some(CrateId(0)), krate: Some(CrateId(0)),
@ -288,10 +320,9 @@ mod tests {
macro_rules! column {() => {}} macro_rules! column {() => {}}
column!() column!()
"#, "#,
BuiltinFnLikeExpander::Column,
); );
assert_eq!(expanded, "9"); assert_eq!(expanded, "13");
} }
#[test] #[test]
@ -302,7 +333,6 @@ mod tests {
macro_rules! line {() => {}} macro_rules! line {() => {}}
line!() line!()
"#, "#,
BuiltinFnLikeExpander::Line,
); );
assert_eq!(expanded, "4"); assert_eq!(expanded, "4");
@ -316,12 +346,37 @@ mod tests {
macro_rules! stringify {() => {}} macro_rules! stringify {() => {}}
stringify!(a b c) stringify!(a b c)
"#, "#,
BuiltinFnLikeExpander::Stringify,
); );
assert_eq!(expanded, "\"a b c\""); assert_eq!(expanded, "\"a b c\"");
} }
#[test]
fn test_env_expand() {
let expanded = expand_builtin_macro(
r#"
#[rustc_builtin_macro]
macro_rules! env {() => {}}
env!("TEST_ENV_VAR")
"#,
);
assert_eq!(expanded, "\"\"");
}
#[test]
fn test_option_env_expand() {
let expanded = expand_builtin_macro(
r#"
#[rustc_builtin_macro]
macro_rules! option_env {() => {}}
option_env!("TEST_ENV_VAR")
"#,
);
assert_eq!(expanded, "std::option::Option::None:: <&str>");
}
#[test] #[test]
fn test_file_expand() { fn test_file_expand() {
let expanded = expand_builtin_macro( let expanded = expand_builtin_macro(
@ -330,7 +385,6 @@ mod tests {
macro_rules! file {() => {}} macro_rules! file {() => {}}
file!() file!()
"#, "#,
BuiltinFnLikeExpander::File,
); );
assert_eq!(expanded, "\"\""); assert_eq!(expanded, "\"\"");
@ -347,7 +401,6 @@ mod tests {
} }
compile_error!("error!"); compile_error!("error!");
"#, "#,
BuiltinFnLikeExpander::CompileError,
); );
assert_eq!(expanded, r#"loop{"error!"}"#); assert_eq!(expanded, r#"loop{"error!"}"#);
@ -364,7 +417,6 @@ mod tests {
} }
format_args!("{} {:?}", arg1(a, b, c), arg2); format_args!("{} {:?}", arg1(a, b, c), arg2);
"#, "#,
BuiltinFnLikeExpander::FormatArgs,
); );
assert_eq!( assert_eq!(

View file

@ -170,6 +170,8 @@ pub mod known {
stringify, stringify,
format_args, format_args,
format_args_nl, format_args_nl,
env,
option_env,
// Builtin derives // Builtin derives
Copy, Copy,
Clone, Clone,

View file

@ -102,6 +102,8 @@ macro_rules! __quote {
( : ) => {$crate::__quote!(@PUNCT ':')}; ( : ) => {$crate::__quote!(@PUNCT ':')};
( :: ) => {$crate::__quote!(@PUNCT ':', ':')}; ( :: ) => {$crate::__quote!(@PUNCT ':', ':')};
( . ) => {$crate::__quote!(@PUNCT '.')}; ( . ) => {$crate::__quote!(@PUNCT '.')};
( < ) => {$crate::__quote!(@PUNCT '<')};
( > ) => {$crate::__quote!(@PUNCT '>')};
( $first:tt $($tail:tt)+ ) => { ( $first:tt $($tail:tt)+ ) => {
{ {