From: Pierre-Emmanuel Patry <[email protected]>
Macros from extern crate should be accessible, their identifier should
be brought into scope as shadowable identifiers when using the macro_use
attribute.
gcc/rust/ChangeLog:
* expand/rust-extern-crate-loader.cc (ExternCrateLoaderVisitor::visit):
Identify macro_use on extern crates.
* resolve/rust-forever-stack.h: Add macro_use prelude to forever stack.
Add function to add macros to the macro_use prelude.
* resolve/rust-forever-stack.hxx: Add macros to macro_use prelude and
dump macro_use prelude.
* resolve/rust-name-resolution-context.cc
(NameResolutionContext::NameResolutionContext):
Initialize prelude.
(NameResolutionContext::merge): Add all extern crate macros to
macro_use prelude when the macro_use attribute is used.
* resolve/rust-name-resolution-context.h: Add macro_use_prelude node.
* rust-session-manager.cc (Session::expansion): Forward macro_use
attribute status.
(Session::load_extern_crate): Likewise.
* rust-session-manager.h: Likewise.
Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/198e458a537e8823a3f2de0b02ca66450b13bf6d
The commit has NOT been mentioned in any issue.
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/5105
gcc/rust/expand/rust-extern-crate-loader.cc | 13 +++++++++-
gcc/rust/resolve/rust-forever-stack.h | 26 ++++++++++++++++---
gcc/rust/resolve/rust-forever-stack.hxx | 13 ++++++++++
.../resolve/rust-name-resolution-context.cc | 24 +++++++++++++----
.../resolve/rust-name-resolution-context.h | 3 ++-
gcc/rust/rust-session-manager.cc | 9 ++++---
gcc/rust/rust-session-manager.h | 9 ++++---
7 files changed, 80 insertions(+), 17 deletions(-)
diff --git a/gcc/rust/expand/rust-extern-crate-loader.cc
b/gcc/rust/expand/rust-extern-crate-loader.cc
index ba048bcf0..cc41f5561 100644
--- a/gcc/rust/expand/rust-extern-crate-loader.cc
+++ b/gcc/rust/expand/rust-extern-crate-loader.cc
@@ -18,6 +18,7 @@
#include "rust-extern-crate-loader.h"
#include "rust-session-manager.h"
+#include "rust-attribute-values.h"
namespace Rust {
ExternCrateLoaderVisitor::ExternCrateLoaderVisitor (
@@ -38,8 +39,18 @@ ExternCrateLoaderVisitor::visit (AST::ExternCrate
&extern_crate)
return;
Session &session = Session::get_instance ();
+ bool macro_use = false;
+ if (extern_crate.has_outer_attrs ())
+ {
+ auto &outer_attrs = extern_crate.get_outer_attrs ();
+ macro_use = std::any_of (outer_attrs.begin (), outer_attrs.end (),
+ [] (AST::Attribute attr) {
+ return attr.get_path ().as_string ()
+ == Values::Attributes::MACRO_USE;
+ });
+ }
auto crate = session.load_extern_crate (extern_crate.get_referenced_crate (),
- extern_crate.get_locus ());
+ macro_use, extern_crate.get_locus ());
if (crate)
loaded_crates.push_back (std::move (crate.value ()));
}
diff --git a/gcc/rust/resolve/rust-forever-stack.h
b/gcc/rust/resolve/rust-forever-stack.h
index 5a0cd22b2..5d6a7e564 100644
--- a/gcc/rust/resolve/rust-forever-stack.h
+++ b/gcc/rust/resolve/rust-forever-stack.h
@@ -579,8 +579,10 @@ public:
tl::optional<Node &> parent; // `None` only if the node is a root
};
- ForeverStackBase (Node &root, Node &lang_prelude, Node &extern_prelude)
- : root (root), lang_prelude (lang_prelude), extern_prelude (extern_prelude)
+ ForeverStackBase (Node &root, Node &lang_prelude, Node &extern_prelude,
+ Node ¯o_use_prelude)
+ : root (root), lang_prelude (lang_prelude), extern_prelude
(extern_prelude),
+ macro_use_prelude (macro_use_prelude)
{}
/* The forever stack's actual nodes */
@@ -597,13 +599,20 @@ public:
* The extern prelude, used for resolving external crates
*/
Node &extern_prelude;
+
+ /*
+ * The macro use prelude, used for resolving macros from external crates.
+ * https://doc.rust-lang.org/reference/names/preludes.html#macro_use-prelude
+ */
+ Node ¯o_use_prelude;
};
template <Namespace N> class ForeverStack : public ForeverStackBase
{
public:
- ForeverStack (Node &root, Node &lang_prelude, Node &extern_prelude)
- : ForeverStackBase (root, lang_prelude, extern_prelude),
+ ForeverStack (Node &root, Node &lang_prelude, Node &extern_prelude,
+ Node ¯o_use_prelude)
+ : ForeverStackBase (root, lang_prelude, extern_prelude, macro_use_prelude),
cursor_reference (root)
{
rust_assert (root.is_root ());
@@ -617,6 +626,7 @@ public:
crate_mappings.get_current_crate ());
extern_prelude.id = mappings.get_next_node_id ();
lang_prelude.id = mappings.get_next_node_id ();
+ macro_use_prelude.id = mappings.get_next_node_id ();
}
/**
@@ -654,8 +664,16 @@ public:
tl::expected<NodeId, DuplicateNameError> insert_variant (Identifier name,
NodeId id);
+ /*
+ * Insert a crate in the extern crate prelude.
+ */
void insert_crate_in_prelude (Identifier crate_name, NodeId crate_id);
+ /*
+ Insert a macro in the macro_use prelude.
+ */
+ void insert_macro_in_prelude (Identifier macro_name, NodeId macro_id);
+
/**
* Insert a new shadowable definition in the innermost `Rib` in this stack
*
diff --git a/gcc/rust/resolve/rust-forever-stack.hxx
b/gcc/rust/resolve/rust-forever-stack.hxx
index 5f4397aca..28979f97f 100644
--- a/gcc/rust/resolve/rust-forever-stack.hxx
+++ b/gcc/rust/resolve/rust-forever-stack.hxx
@@ -213,6 +213,17 @@ ForeverStack<N>::insert_crate_in_prelude (Identifier
crate_name,
insert_inner (extern_prelude_rib, crate_name, definition);
}
+template <Namespace N>
+void
+ForeverStack<N>::insert_macro_in_prelude (Identifier macro_name,
+ NodeId macro_id)
+{
+ rust_assert (N == Namespace::Macros);
+ auto ¯o_use_prelude_rib = macro_use_prelude.rib (Namespace::Macros);
+ auto definition = Rib::Definition::Shadowable (macro_id);
+ insert_inner (macro_use_prelude_rib, macro_name, definition);
+}
+
template <Namespace N>
inline void
ForeverStack<N>::insert_lang_prelude (Identifier name, NodeId id)
@@ -771,6 +782,8 @@ ForeverStack<N>::as_debug_string () const
stream_node (stream, 0, root);
stream << "Extern prelude \n";
stream_node (stream, 0, extern_prelude);
+ stream << "Macro use prelude \n";
+ stream_node (stream, 0, macro_use_prelude);
stream << "Lang prelude \n";
stream_node (stream, 0, lang_prelude);
diff --git a/gcc/rust/resolve/rust-name-resolution-context.cc
b/gcc/rust/resolve/rust-name-resolution-context.cc
index 318cf2ca5..3902db029 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.cc
+++ b/gcc/rust/resolve/rust-name-resolution-context.cc
@@ -204,10 +204,12 @@ NameResolutionContext::NameResolutionContext ()
std::make_unique<Node> (Rib::Kind::Prelude, UNKNOWN_NODEID, *root)),
extern_prelude (
std::make_unique<Node> (Rib::Kind::Prelude, UNKNOWN_NODEID)),
- values (*root, *lang_prelude, *extern_prelude),
- types (*root, *lang_prelude, *extern_prelude),
- macros (*root, *lang_prelude, *extern_prelude),
- labels (*root, *lang_prelude, *extern_prelude),
+ macro_use_prelude (
+ std::make_unique<Node> (Rib::Kind::Prelude, UNKNOWN_NODEID)),
+ values (*root, *lang_prelude, *extern_prelude, *macro_use_prelude),
+ types (*root, *lang_prelude, *extern_prelude, *macro_use_prelude),
+ macros (*root, *lang_prelude, *extern_prelude, *macro_use_prelude),
+ labels (*root, *lang_prelude, *extern_prelude, *macro_use_prelude),
mappings (Analysis::Mappings::get ()), canonical_ctx (*this)
{}
@@ -410,7 +412,8 @@ NameResolutionContext::scoped (Rib::Kind rib_kind,
Namespace ns,
}
void
-NameResolutionContext::merge (NameResolutionContext &other, NodeId at)
+NameResolutionContext::merge (NameResolutionContext &other, NodeId at,
+ bool macro_use)
{
// TODO: merge once, for all namespaces at once
@@ -439,6 +442,17 @@ NameResolutionContext::merge (NameResolutionContext
&other, NodeId at)
merge_fstack (values, other.values, Namespace::Values);
merge_fstack (types, other.types, Namespace::Types);
+
+ if (macro_use)
+ {
+ for (auto kv : other.macros.root.rib_macros.get_values ())
+ {
+ auto &name = kv.first;
+ auto &definition = kv.second;
+ macros.insert_macro_in_prelude (name, definition.get_node_id ());
+ }
+ }
+
merge_fstack (macros, other.macros, Namespace::Macros);
merge_fstack (labels, other.labels, Namespace::Labels);
canonical_ctx.merge (std::move (other.canonical_ctx));
diff --git a/gcc/rust/resolve/rust-name-resolution-context.h
b/gcc/rust/resolve/rust-name-resolution-context.h
index 697dfd823..997969431 100644
--- a/gcc/rust/resolve/rust-name-resolution-context.h
+++ b/gcc/rust/resolve/rust-name-resolution-context.h
@@ -535,6 +535,7 @@ public:
std::unique_ptr<Node> root;
std::unique_ptr<Node> lang_prelude;
std::unique_ptr<Node> extern_prelude;
+ std::unique_ptr<Node> macro_use_prelude;
ForeverStack<Namespace::Values> values;
ForeverStack<Namespace::Types> types;
@@ -840,7 +841,7 @@ public:
* @param at The node id of the container were the nr context should be
* merged. Usually an extern crate node.
*/
- void merge (NameResolutionContext &other, NodeId at);
+ void merge (NameResolutionContext &other, NodeId at, bool macro_use);
// We disable this function for now as it causes regressions, but I think it
// is important for a more proper final nameres context - need to investigate
diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index 8d7dde39b..75b855108 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -1090,7 +1090,8 @@ Session::expansion (AST::Crate &crate,
Resolver2_0::NameResolutionContext &ctx)
Resolver2_0::Early early (ctx);
early.go (crate);
for (auto &loaded_crate : loaded_crates)
- ctx.merge (loaded_crate.ctx, loaded_crate.node_id);
+ ctx.merge (loaded_crate.ctx, loaded_crate.node_id,
+ loaded_crate.macro_use);
macro_errors = early.get_macro_resolve_errors ();
if (saw_errors ())
@@ -1253,7 +1254,8 @@ Session::dump_hir_pretty (HIR::Crate &crate) const
// imports
tl::expected<Session::LoadedCrate, Session::LoadingError>
-Session::load_extern_crate (const std::string &crate_name, location_t locus)
+Session::load_extern_crate (const std::string &crate_name, bool macro_use,
+ location_t locus)
{
auto &crate_mapping = mappings.crate;
// has it already been loaded?
@@ -1371,7 +1373,8 @@ Session::load_extern_crate (const std::string
&crate_name, location_t locus)
// always restore the crate_num
crate_mapping.set_current_crate (saved_crate_num);
- return LoadedCrate{crate_name, parsed_crate.get_node_id (), std::move (ctx)};
+ return LoadedCrate{crate_name, macro_use, parsed_crate.get_node_id (),
+ std::move (ctx)};
}
//
diff --git a/gcc/rust/rust-session-manager.h b/gcc/rust/rust-session-manager.h
index 9d7dc0bba..f80e13e33 100644
--- a/gcc/rust/rust-session-manager.h
+++ b/gcc/rust/rust-session-manager.h
@@ -448,9 +448,10 @@ public:
struct LoadedCrate
{
- LoadedCrate (std::string name, NodeId node_id,
+ LoadedCrate (std::string name, bool macro_use, NodeId node_id,
Resolver2_0::NameResolutionContext ctx)
- : name (std::move (name)), node_id (node_id), ctx (std::move (ctx))
+ : name (std::move (name)), node_id (node_id), macro_use (macro_use),
+ ctx (std::move (ctx))
{}
LoadedCrate (const LoadedCrate &) = delete;
@@ -459,6 +460,7 @@ public:
std::string name;
NodeId node_id;
+ bool macro_use;
Resolver2_0::NameResolutionContext ctx;
};
@@ -490,7 +492,8 @@ public:
};
tl::expected<LoadedCrate, LoadingError>
- load_extern_crate (const std::string &crate_name, location_t locus);
+ load_extern_crate (const std::string &crate_name, bool macro_use,
+ location_t locus);
int get_compat_version () const { return options.get_compat_version (); }
base-commit: 7627d860f12ff0bbd9c86e18f58504608850c6ad
--
2.56.1