]> git.lizzy.rs Git - rust.git/commitdiff
Fixed nested eager macro bug
authorEdwin Cheng <edwin0cheng@gmail.com>
Sun, 3 Jan 2021 09:56:59 +0000 (17:56 +0800)
committerEdwin Cheng <edwin0cheng@gmail.com>
Sun, 3 Jan 2021 09:56:59 +0000 (17:56 +0800)
crates/hir_expand/src/eager.rs
crates/hir_ty/src/tests/macros.rs

index 6354b090d83cc2bcb32eaa2edf7c5c326e464a25..ae7b51a088167813c2f5f9d97879d6e74e2429b6 100644 (file)
@@ -218,6 +218,12 @@ fn eager_macro_recur(
             }
         };
 
+        // check if the whole original sytnax is replaced
+        // Note that SyntaxRewriter cannot replace the root node itself
+        if child.syntax() == &original {
+            return Ok(insert);
+        }
+
         rewriter.replace(child.syntax(), &insert);
     }
 
index 23b79abc45f2437250f4df50b41f2ecddd755263..c64f0b5b53a8d100c955263baeb8c4e9ae568160 100644 (file)
@@ -570,6 +570,52 @@ fn bar() -> u32 {0}
     );
 }
 
+#[test]
+fn infer_builtin_macros_include_str() {
+    check_types(
+        r#"
+//- /main.rs
+#[rustc_builtin_macro]
+macro_rules! include_str {() => {}}
+
+fn main() {
+    let a = include_str!("foo.rs");
+    a;
+} //^ &str
+
+//- /foo.rs
+hello
+"#,
+    );
+}
+
+#[test]
+fn infer_builtin_macros_include_str_with_lazy_nested() {
+    check_types(
+        r#"
+//- /main.rs
+#[rustc_builtin_macro]
+macro_rules! concat {() => {}}
+#[rustc_builtin_macro]
+macro_rules! include_str {() => {}}
+
+macro_rules! m {
+    ($x:expr) => {
+        concat!("foo", $x)
+    };
+}
+
+fn main() {
+    let a = include_str!(m!(".rs"));
+    a;
+} //^ &str
+
+//- /foo.rs
+hello
+"#,
+    );
+}
+
 #[test]
 #[ignore]
 fn include_accidentally_quadratic() {