Skip to content

Commit 8218494

Browse files
Merge #3508
3508: Use a not so dummy implementation of env macro r=edwin0cheng a=edwin0cheng Currently we have a dummy `env` macro implementation which expand to an empty string, such that a `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become `include!("/foo.rs")`, and here may be a infinite loop. :) This PR use a not so dummy version of `env` macro to prevent this infinite loop. Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2 parents 48bb1c5 + 2e178b5 commit 8218494

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ fn env_expand(
142142
_tt: &tt::Subtree,
143143
) -> Result<tt::Subtree, mbe::ExpandError> {
144144
// dummy implementation for type-checking purposes
145-
let expanded = quote! { "" };
145+
// we cannot use an empty string here, because for
146+
// `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become
147+
// `include!("foo.rs"), which maybe infinite loop
148+
let expanded = quote! { "__RA_UNIMPLEMENTATED__" };
146149

147150
Ok(expanded)
148151
}
@@ -394,7 +397,7 @@ mod tests {
394397
"#,
395398
);
396399

397-
assert_eq!(expanded, "\"\"");
400+
assert_eq!(expanded, "\"__RA_UNIMPLEMENTATED__\"");
398401
}
399402

400403
#[test]

crates/ra_hir_ty/src/tests/macros.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,33 @@ fn bar() -> u32 {0}
483483
assert_eq!("u32", type_at_pos(&db, pos));
484484
}
485485

486+
#[test]
487+
fn infer_builtin_macros_include_concat_with_bad_env_should_failed() {
488+
let (db, pos) = TestDB::with_position(
489+
r#"
490+
//- /main.rs
491+
#[rustc_builtin_macro]
492+
macro_rules! include {() => {}}
493+
494+
#[rustc_builtin_macro]
495+
macro_rules! concat {() => {}}
496+
497+
#[rustc_builtin_macro]
498+
macro_rules! env {() => {}}
499+
500+
include!(concat!(env!("OUT_DIR"), "/foo.rs"));
501+
502+
fn main() {
503+
bar()<|>;
504+
}
505+
506+
//- /foo.rs
507+
fn bar() -> u32 {0}
508+
"#,
509+
);
510+
assert_eq!("{unknown}", type_at_pos(&db, pos));
511+
}
512+
486513
#[test]
487514
fn infer_builtin_macros_concat_with_lazy() {
488515
assert_snapshot!(

0 commit comments

Comments
 (0)