Skip to content

Commit 6624978

Browse files
committed
core: Add -frust-compile-core flag
Add a specific flag for compiling core which changes compiler generated paths. This is important for derives, and especially for core, where we want to implement crate::marker::Copy rather than core::marker::Copy. gcc/rust/ChangeLog: * lang.opt: Add -frust-compile-core option. * ast/rust-ast-builder.h: Add get_path_start method which depends on -frust-compile-core * ast/rust-ast-builder.cc (Builder::discriminant_value): Use it. * expand/rust-derive-debug.cc (DeriveDebug::stub_debug_fn): Likewise. * expand/rust-derive-default.cc (DeriveDefault::default_call): Likewise. * expand/rust-derive-eq.cc (DeriveEq::assert_param_is_eq): Likewise. * expand/rust-derive-hash.cc (DeriveHash::hash_call): Likewise. (DeriveHash::hash_fn): Likewise. * expand/rust-derive-ord.cc (DeriveOrd::cmp_call): Likewise. (DeriveOrd::cmp_fn): Likewise. (DeriveOrd::make_equal): Likewise. (DeriveOrd::recursive_match): Likewise. * expand/rust-expand-format-args.cc (format_arg): Likewise. (expand_format_args): Likewise.
1 parent d2f8f64 commit 6624978

File tree

9 files changed

+60
-35
lines changed

9 files changed

+60
-35
lines changed

gcc/rust/ast/rust-ast-builder.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ std::unique_ptr<Stmt>
541541
Builder::discriminant_value (std::string binding_name, std::string instance)
542542
{
543543
auto intrinsic = ptrify (
544-
path_in_expression ({"core", "intrinsics", "discriminant_value"}, true));
544+
path_in_expression ({get_path_start (), "intrinsics", "discriminant_value"},
545+
true));
545546

546547
return let (identifier_pattern (binding_name), nullptr,
547548
call (std::move (intrinsic), identifier (instance)));

gcc/rust/ast/rust-ast-builder.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "rust-ast.h"
2525
#include "rust-item.h"
2626
#include "rust-operators.h"
27-
#include <initializer_list>
27+
#include "options.h"
2828

2929
namespace Rust {
3030
namespace AST {
@@ -332,10 +332,19 @@ class Builder
332332
/* Location of the generated AST nodes */
333333
location_t loc;
334334

335+
const char *get_path_start () const
336+
{
337+
if (flag_compile_core)
338+
return "crate";
339+
else
340+
return "core";
341+
}
342+
335343
private:
336-
/* Some constexpr helpers for some of the builders */
337-
static constexpr std::initializer_list<const char *> discriminant_value_path
338-
= {"core", "intrinsics", "discriminant_value"};
344+
// /* Some constexpr helpers for some of the builders */
345+
// static constexpr std::initializer_list<const char *>
346+
// discriminant_value_path
347+
// = {"core", "intrinsics", "discriminant_value"};
339348
};
340349

341350
} // namespace AST

gcc/rust/expand/rust-derive-debug.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ DeriveDebug::stub_debug_fn ()
5454

5555
auto self = builder.self_ref_param ();
5656

57-
auto return_type
58-
= ptrify (builder.type_path ({"core", "fmt", "Result"}, true));
57+
auto return_type = ptrify (
58+
builder.type_path ({builder.get_path_start (), "fmt", "Result"}, true));
5959

60-
auto mut_fmt_type_inner
61-
= ptrify (builder.type_path ({"core", "fmt", "Formatter"}, true));
60+
auto mut_fmt_type_inner = ptrify (
61+
builder.type_path ({builder.get_path_start (), "fmt", "Formatter"}, true));
6262

6363
auto mut_fmt_type
6464
= builder.reference_type (std::move (mut_fmt_type_inner), true);
@@ -81,7 +81,8 @@ DeriveDebug::stub_derive_impl (
8181
{
8282
auto trait_items = vec (stub_debug_fn ());
8383

84-
auto debug = builder.type_path ({"core", "fmt", "Debug"}, true);
84+
auto debug
85+
= builder.type_path ({builder.get_path_start (), "fmt", "Debug"}, true);
8586
auto generics
8687
= setup_impl_generics (name, type_generics, builder.trait_bound (debug));
8788

gcc/rust/expand/rust-derive-default.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ DeriveDefault::go (Item &item)
4242
std::unique_ptr<Expr>
4343
DeriveDefault::default_call (std::unique_ptr<Type> &&type)
4444
{
45-
auto default_trait = builder.type_path ({"core", "default", "Default"}, true);
45+
auto default_trait
46+
= builder.type_path ({builder.get_path_start (), "default", "Default"},
47+
true);
4648

4749
auto default_fn
4850
= builder.qualified_path_in_expression (std::move (type), default_trait,
@@ -69,7 +71,9 @@ DeriveDefault::default_impl (
6971
std::unique_ptr<AssociatedItem> &&default_fn, std::string name,
7072
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
7173
{
72-
auto default_path = builder.type_path ({"core", "default", "Default"}, true);
74+
auto default_path
75+
= builder.type_path ({builder.get_path_start (), "default", "Default"},
76+
true);
7377

7478
auto trait_items = vec (std::move (default_fn));
7579

gcc/rust/expand/rust-derive-eq.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace AST {
3030
static TypePath
3131
get_eq_trait_path (Builder &builder)
3232
{
33-
return builder.type_path ({"core", "cmp", "Eq"}, true);
33+
return builder.type_path ({builder.get_path_start (), "cmp", "Eq"}, true);
3434
}
3535

3636
DeriveEq::DeriveEq (location_t loc) : DeriveVisitor (loc) {}

gcc/rust/expand/rust-derive-hash.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ DeriveHash::go (Item &item)
4141
std::unique_ptr<Expr>
4242
DeriveHash::hash_call (std::unique_ptr<Expr> &&value)
4343
{
44-
auto hash
45-
= builder.path_in_expression ({"core", "hash", "Hash", "hash"}, true);
44+
auto hash = builder.path_in_expression ({builder.get_path_start (), "hash",
45+
"Hash", "hash"},
46+
true);
4647

4748
return builder.call (ptrify (hash),
4849
vec (std::move (value),
@@ -63,8 +64,8 @@ DeriveHash::hash_fn (std::unique_ptr<BlockExpr> &&block)
6364
true));
6465

6566
auto params = vec (builder.self_ref_param (), std::move (state_param));
66-
auto bounds = vec (
67-
builder.trait_bound (builder.type_path ({"core", "hash", "Hasher"}, true)));
67+
auto bounds = vec (builder.trait_bound (
68+
builder.type_path ({builder.get_path_start (), "hash", "Hasher"}, true)));
6869
auto generics = vec (
6970
builder.generic_type_param (DeriveHash::state_type, std::move (bounds)));
7071

@@ -77,7 +78,8 @@ DeriveHash::hash_impl (
7778
std::unique_ptr<AssociatedItem> &&hash_fn, std::string name,
7879
const std::vector<std::unique_ptr<GenericParam>> &type_generics)
7980
{
80-
auto hash_path = builder.type_path ({"core", "hash", "Hash"}, true);
81+
auto hash_path
82+
= builder.type_path ({builder.get_path_start (), "hash", "Hash"}, true);
8183

8284
auto trait_items = vec (std::move (hash_fn));
8385

gcc/rust/expand/rust-derive-ord.cc

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ DeriveOrd::cmp_call (std::unique_ptr<Expr> &&self_expr,
4343
std::unique_ptr<Expr> &&other_expr)
4444
{
4545
auto cmp_fn_path = builder.path_in_expression (
46-
{"core", "cmp", trait (ordering), fn (ordering)}, true);
46+
{builder.get_path_start (), "cmp", trait (ordering), fn (ordering)}, true);
4747

4848
return builder.call (ptrify (cmp_fn_path),
4949
vec (builder.ref (std::move (self_expr)),
@@ -58,10 +58,11 @@ DeriveOrd::cmp_impl (
5858
auto fn = cmp_fn (std::move (fn_block), type_name);
5959

6060
auto trait = ordering == Ordering::Partial ? "PartialOrd" : "Ord";
61-
auto trait_path = builder.type_path ({"core", "cmp", trait}, true);
61+
auto trait_path
62+
= builder.type_path ({builder.get_path_start (), "cmp", trait}, true);
6263

63-
auto trait_bound
64-
= builder.trait_bound (builder.type_path ({"core", "cmp", trait}, true));
64+
auto trait_bound = builder.trait_bound (
65+
builder.type_path ({builder.get_path_start (), "cmp", trait}, true));
6566

6667
auto trait_items = vec (std::move (fn));
6768

@@ -78,7 +79,8 @@ std::unique_ptr<AssociatedItem>
7879
DeriveOrd::cmp_fn (std::unique_ptr<BlockExpr> &&block, Identifier type_name)
7980
{
8081
// Ordering
81-
auto return_type = builder.type_path ({"core", "cmp", "Ordering"}, true);
82+
auto return_type
83+
= builder.type_path ({builder.get_path_start (), "cmp", "Ordering"}, true);
8284

8385
// In the case of PartialOrd, we return an Option<Ordering>
8486
if (ordering == Ordering::Partial)
@@ -87,7 +89,7 @@ DeriveOrd::cmp_fn (std::unique_ptr<BlockExpr> &&block, Identifier type_name)
8789

8890
auto generic_seg = builder.type_path_segment_generic (
8991
"Option", GenericArgs ({}, {generic}, {}, loc));
90-
auto core = builder.type_path_segment ("core");
92+
auto core = builder.type_path_segment (builder.get_path_start ());
9193
auto option = builder.type_path_segment ("option");
9294

9395
return_type
@@ -112,8 +114,8 @@ DeriveOrd::cmp_fn (std::unique_ptr<BlockExpr> &&block, Identifier type_name)
112114
std::unique_ptr<Pattern>
113115
DeriveOrd::make_equal ()
114116
{
115-
std::unique_ptr<Pattern> equal = ptrify (
116-
builder.path_in_expression ({"core", "cmp", "Ordering", "Equal"}, true));
117+
std::unique_ptr<Pattern> equal = ptrify (builder.path_in_expression (
118+
{builder.get_path_start (), "cmp", "Ordering", "Equal"}, true));
117119

118120
// We need to wrap the pattern in Option::Some if we are doing partial
119121
// ordering
@@ -147,9 +149,8 @@ DeriveOrd::recursive_match (std::vector<SelfOther> &&members)
147149
{
148150
if (members.empty ())
149151
{
150-
std::unique_ptr<Expr> value = ptrify (
151-
builder.path_in_expression ({"core", "cmp", "Ordering", "Equal"},
152-
true));
152+
std::unique_ptr<Expr> value = ptrify (builder.path_in_expression (
153+
{builder.get_path_start (), "cmp", "Ordering", "Equal"}, true));
153154

154155
if (ordering == Ordering::Partial)
155156
value = builder.call (ptrify (builder.path_in_expression (

gcc/rust/expand/rust-expand-format-args.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ static std::unique_ptr<AST::Expr>
3535
format_arg (const AST::Builder &builder, std::unique_ptr<AST::Expr> &&to_format,
3636
const std::string &trait)
3737
{
38-
auto formatter_fn = std::unique_ptr<AST::Expr> (new AST::PathInExpression (
39-
builder.path_in_expression ({"core", "fmt", trait, "fmt"})));
38+
auto formatter_fn = std::unique_ptr<AST::Expr> (
39+
new AST::PathInExpression (builder.path_in_expression (
40+
{builder.get_path_start (), "fmt", trait, "fmt"})));
4041

41-
auto path = std::unique_ptr<AST::Expr> (new AST::PathInExpression (
42-
builder.path_in_expression ({"core", "fmt", "ArgumentV1", "new"})));
42+
auto path = std::unique_ptr<AST::Expr> (
43+
new AST::PathInExpression (builder.path_in_expression (
44+
{builder.get_path_start (), "fmt", "ArgumentV1", "new"})));
4345

4446
auto args = std::vector<std::unique_ptr<AST::Expr>> ();
4547
args.emplace_back (std::move (to_format));
@@ -122,8 +124,9 @@ expand_format_args (AST::FormatArgs &fmt,
122124
auto pieces = builder.ref (builder.array (std::move (static_pieces)));
123125
auto args_slice = builder.ref (builder.array (std::move (args_array)));
124126

125-
auto final_path = std::make_unique<AST::PathInExpression> (
126-
builder.path_in_expression ({"core", "fmt", "Arguments", "new_v1"}));
127+
auto final_path
128+
= std::make_unique<AST::PathInExpression> (builder.path_in_expression (
129+
{builder.get_path_start (), "fmt", "Arguments", "new_v1"}));
127130
auto final_args = std::vector<std::unique_ptr<AST::Expr>> ();
128131
final_args.emplace_back (std::move (pieces));
129132
final_args.emplace_back (std::move (args_slice));

gcc/rust/lang.opt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,8 @@ frust-unused-check-2.0
237237
Rust Var(flag_unused_check_2_0)
238238
Use the new unused variable check implementation.
239239

240+
frust-compile-core
241+
Rust Var(flag_compile_core)
242+
Tell the compiler that we are currently compiling the core library
243+
240244
; This comment is to ensure we retain the blank line above.

0 commit comments

Comments
 (0)