Skip to content

Commit 513924a

Browse files
bors[bot]lnicola
andauthored
Merge #5102
5102: Add support for include_bytes! r=edwin0cheng a=lnicola Co-authored-by: Laurențiu Nicola <[email protected]>
2 parents 446fd3f + f8d37ff commit 513924a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ register_builtin! {
9999
EAGER:
100100
(concat, Concat) => concat_expand,
101101
(include, Include) => include_expand,
102+
(include_bytes, IncludeBytes) => include_bytes_expand,
102103
(include_str, IncludeStr) => include_str_expand,
103104
(env, Env) => env_expand,
104105
(option_env, OptionEnv) => option_env_expand
@@ -337,6 +338,24 @@ fn include_expand(
337338
Ok((res, FragmentKind::Items))
338339
}
339340

341+
fn include_bytes_expand(
342+
_db: &dyn AstDatabase,
343+
_arg_id: EagerMacroId,
344+
tt: &tt::Subtree,
345+
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
346+
let _path = parse_string(tt)?;
347+
348+
// FIXME: actually read the file here if the user asked for macro expansion
349+
let res = tt::Subtree {
350+
delimiter: None,
351+
token_trees: vec![tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
352+
text: r#"b"""#.into(),
353+
id: tt::TokenId::unspecified(),
354+
}))],
355+
};
356+
Ok((res, FragmentKind::Expr))
357+
}
358+
340359
fn include_str_expand(
341360
db: &dyn AstDatabase,
342361
arg_id: EagerMacroId,
@@ -611,4 +630,20 @@ mod tests {
611630
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),])"#
612631
);
613632
}
633+
634+
#[test]
635+
fn test_include_bytes_expand() {
636+
let expanded = expand_builtin_macro(
637+
r#"
638+
#[rustc_builtin_macro]
639+
macro_rules! include_bytes {
640+
($file:expr) => {{ /* compiler built-in */ }};
641+
($file:expr,) => {{ /* compiler built-in */ }};
642+
}
643+
include_bytes("foo");
644+
"#,
645+
);
646+
647+
assert_eq!(expanded, r#"b"""#);
648+
}
614649
}

crates/ra_hir_expand/src/name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ pub mod known {
191191
stringify,
192192
concat,
193193
include,
194+
include_bytes,
194195
include_str,
195196
format_args,
196197
format_args_nl,

0 commit comments

Comments
 (0)