5102: Add support for include_bytes! r=edwin0cheng a=lnicola



Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
This commit is contained in:
bors[bot] 2020-06-27 19:10:56 +00:00 committed by GitHub
commit 513924a7e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View file

@ -99,6 +99,7 @@ register_builtin! {
EAGER:
(concat, Concat) => concat_expand,
(include, Include) => include_expand,
(include_bytes, IncludeBytes) => include_bytes_expand,
(include_str, IncludeStr) => include_str_expand,
(env, Env) => env_expand,
(option_env, OptionEnv) => option_env_expand
@ -337,6 +338,24 @@ fn include_expand(
Ok((res, FragmentKind::Items))
}
fn include_bytes_expand(
_db: &dyn AstDatabase,
_arg_id: EagerMacroId,
tt: &tt::Subtree,
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
let _path = parse_string(tt)?;
// FIXME: actually read the file here if the user asked for macro expansion
let res = tt::Subtree {
delimiter: None,
token_trees: vec![tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
text: r#"b"""#.into(),
id: tt::TokenId::unspecified(),
}))],
};
Ok((res, FragmentKind::Expr))
}
fn include_str_expand(
db: &dyn AstDatabase,
arg_id: EagerMacroId,
@ -611,4 +630,20 @@ mod tests {
r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#
);
}
#[test]
fn test_include_bytes_expand() {
let expanded = expand_builtin_macro(
r#"
#[rustc_builtin_macro]
macro_rules! include_bytes {
($file:expr) => {{ /* compiler built-in */ }};
($file:expr,) => {{ /* compiler built-in */ }};
}
include_bytes("foo");
"#,
);
assert_eq!(expanded, r#"b"""#);
}
}

View file

@ -191,6 +191,7 @@ pub mod known {
stringify,
concat,
include,
include_bytes,
include_str,
format_args,
format_args_nl,