From: Pierre-Emmanuel Patry <[email protected]>

gcc/rust/ChangeLog:

        * expand/rust-macro-expand.cc: Forward invocation tree locus to
        substitution context.
        * expand/rust-macro-substitute-ctx.cc: Use origin location for expanded
        tokens.
        * expand/rust-macro-substitute-ctx.h (class SubstituteCtx): Save
        invocation location.

gcc/testsuite/ChangeLog:

        * rust/compile/macros/mbe/macro58.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
---
 gcc/rust/expand/rust-macro-expand.cc             |  5 +++--
 gcc/rust/expand/rust-macro-substitute-ctx.cc     |  8 ++++----
 gcc/rust/expand/rust-macro-substitute-ctx.h      |  6 ++++--
 gcc/testsuite/rust/compile/macros/mbe/macro58.rs | 12 ++++++++++++
 4 files changed, 23 insertions(+), 8 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/macros/mbe/macro58.rs

diff --git a/gcc/rust/expand/rust-macro-expand.cc 
b/gcc/rust/expand/rust-macro-expand.cc
index ec4f666dc7b..52f8e2b10e3 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -1108,8 +1108,9 @@ MacroExpander::transcribe_rule (
   auto invoc_stream = invoc_token_tree.to_token_stream ();
   auto macro_rule_tokens = transcribe_tree.to_token_stream ();
 
-  auto substitute_context = SubstituteCtx (invoc_stream, macro_rule_tokens,
-                                          matched_fragments, definition);
+  auto substitute_context
+    = SubstituteCtx (invoc_stream, macro_rule_tokens, matched_fragments,
+                    definition, invoc_token_tree.get_locus ());
   std::vector<std::unique_ptr<AST::Token>> substituted_tokens
     = substitute_context.substitute_tokens ();
 
diff --git a/gcc/rust/expand/rust-macro-substitute-ctx.cc 
b/gcc/rust/expand/rust-macro-substitute-ctx.cc
index 0b78fa535cb..36bae5bf45a 100644
--- a/gcc/rust/expand/rust-macro-substitute-ctx.cc
+++ b/gcc/rust/expand/rust-macro-substitute-ctx.cc
@@ -40,7 +40,7 @@ SubstituteCtx::substitute_dollar_crate (
   if (*def_crate == current_crate)
     {
       expanded.push_back (std::make_unique<AST::Token> (
-       Rust::Token::make_identifier (UNKNOWN_LOCATION, "crate")));
+       Rust::Token::make_identifier (origin, "crate")));
     }
   else
     {
@@ -49,9 +49,9 @@ SubstituteCtx::substitute_dollar_crate (
       rust_assert (name);
 
       expanded.push_back (std::make_unique<AST::Token> (
-       Rust::Token::make (SCOPE_RESOLUTION, UNKNOWN_LOCATION)));
+       Rust::Token::make (SCOPE_RESOLUTION, origin)));
       expanded.push_back (std::make_unique<AST::Token> (
-       Rust::Token::make_identifier (UNKNOWN_LOCATION, std::string (*name))));
+       Rust::Token::make_identifier (origin, std::string (*name))));
     }
 
   return true;
@@ -237,7 +237,7 @@ SubstituteCtx::substitute_repetition (
        }
 
       auto substitute_context
-       = SubstituteCtx (input, new_macro, sub_map, definition);
+       = SubstituteCtx (input, new_macro, sub_map, definition, origin);
       auto new_tokens = substitute_context.substitute_tokens ();
 
       // Skip the first repetition, but add the separator to the expanded
diff --git a/gcc/rust/expand/rust-macro-substitute-ctx.h 
b/gcc/rust/expand/rust-macro-substitute-ctx.h
index c5c4956bc8e..3829a5a81fa 100644
--- a/gcc/rust/expand/rust-macro-substitute-ctx.h
+++ b/gcc/rust/expand/rust-macro-substitute-ctx.h
@@ -27,6 +27,8 @@ class SubstituteCtx
   std::vector<std::unique_ptr<AST::Token>> &macro;
   std::map<std::string, MatchedFragmentContainer *> &fragments;
   AST::MacroRulesDefinition &definition;
+  // Macro invocation location
+  location_t origin;
 
   /**
    * Find the repetition amount to use when expanding a repetition, and
@@ -43,9 +45,9 @@ public:
   SubstituteCtx (std::vector<std::unique_ptr<AST::Token>> &input,
                 std::vector<std::unique_ptr<AST::Token>> &macro,
                 std::map<std::string, MatchedFragmentContainer *> &fragments,
-                AST::MacroRulesDefinition &definition)
+                AST::MacroRulesDefinition &definition, location_t origin)
     : input (input), macro (macro), fragments (fragments),
-      definition (definition)
+      definition (definition), origin (origin)
   {}
 
   /**
diff --git a/gcc/testsuite/rust/compile/macros/mbe/macro58.rs 
b/gcc/testsuite/rust/compile/macros/mbe/macro58.rs
new file mode 100644
index 00000000000..d8f75995e57
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macros/mbe/macro58.rs
@@ -0,0 +1,12 @@
+pub fn print(a: *const u8) {}
+#[macro_export]
+macro_rules! pr_warn (
+    ($($arg:tt)*) => (
+        $($crate::print($arg))*
+    )
+);
+
+fn main() {
+    pr_warn!("test\0", "test\0");
+    // { dg-error "expecting .;. but .identifier. found" "" { target *-*-* } 
.-1 }
+}
-- 
2.50.1

Reply via email to