diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-07 05:04:36 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-07 05:04:36 +0000 |
commit | 8218494b530be43e4baa045176a3e8b6ad0558fa (patch) | |
tree | 7b9a9b39256f829f7f2eb1961ed880833f0367d6 | |
parent | 48bb1c51721b7736b6d6f2e5cb82126c91934433 (diff) | |
parent | 2e178b5475b156e1876e4e6971c0abd3bdf6c889 (diff) |
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 <[email protected]>
-rw-r--r-- | crates/ra_hir_expand/src/builtin_macro.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/macros.rs | 27 |
2 files changed, 32 insertions, 2 deletions
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index 9fc33e4b1..a57e9285f 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs | |||
@@ -142,7 +142,10 @@ fn env_expand( | |||
142 | _tt: &tt::Subtree, | 142 | _tt: &tt::Subtree, |
143 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 143 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
144 | // dummy implementation for type-checking purposes | 144 | // 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__" }; | ||
146 | 149 | ||
147 | Ok(expanded) | 150 | Ok(expanded) |
148 | } | 151 | } |
@@ -394,7 +397,7 @@ mod tests { | |||
394 | "#, | 397 | "#, |
395 | ); | 398 | ); |
396 | 399 | ||
397 | assert_eq!(expanded, "\"\""); | 400 | assert_eq!(expanded, "\"__RA_UNIMPLEMENTATED__\""); |
398 | } | 401 | } |
399 | 402 | ||
400 | #[test] | 403 | #[test] |
diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs index 42814941f..c86733ac3 100644 --- a/crates/ra_hir_ty/src/tests/macros.rs +++ b/crates/ra_hir_ty/src/tests/macros.rs | |||
@@ -484,6 +484,33 @@ fn bar() -> u32 {0} | |||
484 | } | 484 | } |
485 | 485 | ||
486 | #[test] | 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 | |||
513 | #[test] | ||
487 | fn infer_builtin_macros_concat_with_lazy() { | 514 | fn infer_builtin_macros_concat_with_lazy() { |
488 | assert_snapshot!( | 515 | assert_snapshot!( |
489 | infer(r#" | 516 | infer(r#" |