Skip to content

Commit c25ef5c

Browse files
committed
Implicitely inject extern crate core
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 <[email protected]>
1 parent d47331f commit c25ef5c

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

gcc/rust/ast/rust-ast.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ Crate::as_string () const
235235
return str + "\n";
236236
}
237237

238+
void
239+
Crate::inject_extern_crate (std::string name)
240+
{
241+
items.push_back (std::make_unique<AST::ExternCrate> (
242+
AST::ExternCrate (name, AST::Visibility::create_public (UNKNOWN_LOCATION),
243+
{}, UNKNOWN_LOCATION)));
244+
}
245+
238246
std::string
239247
Attribute::as_string () const
240248
{

gcc/rust/ast/rust-ast.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,8 @@ struct Crate : public GlobContainer
21162116
// TODO: is this the best way to do this?
21172117
}
21182118

2119+
void inject_extern_crate (std::string name);
2120+
21192121
NodeId get_node_id () const { return node_id; }
21202122
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
21212123
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }

gcc/rust/rust-session-manager.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ validate_crate_name (const std::string &crate_name, Error &error)
162162
return true;
163163
}
164164

165+
static bool
166+
has_attribute (AST::Crate crate, std::string attribute)
167+
{
168+
auto &crate_attrs = crate.get_inner_attrs ();
169+
auto has_attr = [&attribute] (AST::Attribute &attr) {
170+
return attr.as_string () == attribute;
171+
};
172+
return std::any_of (crate_attrs.begin (), crate_attrs.end (), has_attr);
173+
}
174+
165175
void
166176
Session::init ()
167177
{
@@ -658,6 +668,11 @@ Session::compile_crate (const char *filename)
658668

659669
Analysis::AttributeChecker ().go (parsed_crate);
660670

671+
if (!has_attribute (parsed_crate, std::string (Values::Attributes::NO_CORE)))
672+
{
673+
parsed_crate.inject_extern_crate ("core");
674+
}
675+
661676
if (last_step == CompileOptions::CompileStep::Expansion)
662677
return;
663678

gcc/rust/util/rust-attribute-values.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Attributes
3737
static constexpr auto &MUST_USE = "must_use";
3838
static constexpr auto &LANG = "lang";
3939
static constexpr auto &LINK_NAME = "link_name";
40+
static constexpr auto &NO_CORE = "no_core";
4041
static constexpr auto &LINK_SECTION = "link_section";
4142
static constexpr auto &NO_MANGLE = "no_mangle";
4243
static constexpr auto &EXPORT_NAME = "export_name";

0 commit comments

Comments
 (0)