]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Implicitely inject `extern crate core`
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fri, 23 Jan 2026 04:34:24 +0000 (05:34 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Fri, 27 Feb 2026 14:57:09 +0000 (15:57 +0100)
The core library is made accessible through the `core` identifier in
every crate unless the crate opt out with the `#![no_core]` attribute.
This commit implicitely inject this extern crate when required.

gcc/rust/ChangeLog:

* ast/rust-ast.cc (Crate::inject_extern_crate): Add a function to
inject an extern crate item to a crate.
* ast/rust-ast.h: Add function prototype for inject_extern_crate.
* rust-session-manager.cc (has_attribute): Add helper to determine if
a crate has a given inner attribute.
(Session::compile_crate): Add a step to inject the core extern crate
when the no_core attribute is missing.
* util/rust-attribute-values.h: Add the no_core attribute value.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/ast/rust-ast.cc
gcc/rust/ast/rust-ast.h
gcc/rust/rust-session-manager.cc
gcc/rust/util/rust-attribute-values.h

index f550edb088d8c975e1f3afb553f45f18c13e55e8..37c3849496879756f8ade9d4ee6c2464f9ed23f9 100644 (file)
@@ -235,6 +235,14 @@ Crate::as_string () const
   return str + "\n";
 }
 
+void
+Crate::inject_extern_crate (std::string name)
+{
+  items.push_back (std::make_unique<AST::ExternCrate> (
+    AST::ExternCrate (name, AST::Visibility::create_public (UNKNOWN_LOCATION),
+                     {}, UNKNOWN_LOCATION)));
+}
+
 std::string
 Attribute::as_string () const
 {
index 65d1c3f0a2addb2d6506fe688e2818c25b12c9b8..02fbb52a2173d243330febb74f7d09729499dd3f 100644 (file)
@@ -2116,6 +2116,8 @@ public:
     // TODO: is this the best way to do this?
   }
 
+  void inject_extern_crate (std::string name);
+
   NodeId get_node_id () const { return node_id; }
   const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
   std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
index 2235876c59d6a09e071f47ef1ed91ee5c46d40ab..4a2f5a5ad706c6c63f7423ac41b3be9a7ecd55a0 100644 (file)
@@ -162,6 +162,16 @@ validate_crate_name (const std::string &crate_name, Error &error)
   return true;
 }
 
+static bool
+has_attribute (AST::Crate crate, std::string attribute)
+{
+  auto &crate_attrs = crate.get_inner_attrs ();
+  auto has_attr = [&attribute] (AST::Attribute &attr) {
+    return attr.as_string () == attribute;
+  };
+  return std::any_of (crate_attrs.begin (), crate_attrs.end (), has_attr);
+}
+
 void
 Session::init ()
 {
@@ -658,6 +668,11 @@ Session::compile_crate (const char *filename)
 
   Analysis::AttributeChecker ().go (parsed_crate);
 
+  if (!has_attribute (parsed_crate, std::string (Values::Attributes::NO_CORE)))
+    {
+      parsed_crate.inject_extern_crate ("core");
+    }
+
   if (last_step == CompileOptions::CompileStep::Expansion)
     return;
 
index 45a81e4418ad7c6a8c60320562d546ff19010b1b..85d3bac924299cc6d96427a366fe66d5bd35fed6 100644 (file)
@@ -37,6 +37,7 @@ public:
   static constexpr auto &MUST_USE = "must_use";
   static constexpr auto &LANG = "lang";
   static constexpr auto &LINK_NAME = "link_name";
+  static constexpr auto &NO_CORE = "no_core";
   static constexpr auto &LINK_SECTION = "link_section";
   static constexpr auto &NO_MANGLE = "no_mangle";
   static constexpr auto &EXPORT_NAME = "export_name";