Skip to content

[WIP] Compile libcore #3914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5e946de
stash expand
powerboat9 Jul 3, 2025
77e3f11
Make AttributeParser rely more on Parser
powerboat9 Jul 8, 2025
69159bc
stash checks
powerboat9 Jul 3, 2025
a690152
stash final
powerboat9 Jul 10, 2025
266b6e3
stash rest
powerboat9 Jul 3, 2025
21617f9
fix clobber parse
powerboat9 Jul 1, 2025
b7d34bf
fix late for-visitor
powerboat9 Jul 1, 2025
c50c6db
adjust module handling
powerboat9 Jul 1, 2025
651e704
improve path handling
powerboat9 Jul 1, 2025
f56cb16
fix path handling
powerboat9 Jul 2, 2025
72c037c
accept unhandled features
powerboat9 Jul 2, 2025
c837101
handle test attribute
powerboat9 Jul 3, 2025
ac578c1
rustc_allow_const_fn_unstable
powerboat9 Jul 3, 2025
266543a
Merge branch 'better-attr-3' into core-push
powerboat9 Jul 10, 2025
2ba3d92
fixup comment
powerboat9 Jul 10, 2025
49cf967
collect lang items later
powerboat9 Jul 10, 2025
b1c88c9
add rustc_args_required_const
powerboat9 Jul 12, 2025
663a097
handle attributes
powerboat9 Jul 12, 2025
3b7db4e
add some debug stuff
powerboat9 Jul 12, 2025
078f8d6
remove SIMD_TEST
powerboat9 Jul 12, 2025
5bf10e2
Revert "add some debug stuff"
powerboat9 Jul 13, 2025
4adbcc3
remove undefined behavior
powerboat9 Jul 15, 2025
bf2662f
stash
powerboat9 Jul 16, 2025
4c65214
format eager stash
powerboat9 Jul 17, 2025
217ea33
limit recursion for debug
powerboat9 Jul 17, 2025
073f2dc
mangle/link_name fixes
powerboat9 Jul 17, 2025
f44d6bc
hammer debug;
powerboat9 Jul 17, 2025
cb51e6d
stash expand expansion
powerboat9 Jul 17, 2025
b9ceb83
add saw_error call
powerboat9 Jul 17, 2025
24a9d04
Merge branch 'remove-nr1' into HEAD
powerboat9 Jul 16, 2025
a1d7ed4
fix changelog entry
powerboat9 Jul 31, 2025
9ba9897
handle rename
powerboat9 Aug 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gcc/rust/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2775,7 +2775,7 @@

2025-03-21 Owen Avery <[email protected]>

* resolve/rust-name-resolver.cc: Include options.txt.
* resolve/rust-name-resolver.cc: Include options.h.
(Resolver::insert_resolved_name): Assert that name resolution
2.0 is disabled.
(Resolver::lookup_resolved_name): Likewise.
Expand Down
10 changes: 0 additions & 10 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,7 @@ GRS_OBJS = \
rust/rust-ice-finalizer.o \
rust/rust-late-name-resolver-2.0.o \
rust/rust-immutable-name-resolution-context.o \
rust/rust-early-name-resolver.o \
rust/rust-name-resolver.o \
rust/rust-ast-resolve.o \
rust/rust-ast-resolve-base.o \
rust/rust-ast-resolve-item.o \
rust/rust-ast-resolve-pattern.o \
rust/rust-ast-resolve-expr.o \
rust/rust-ast-resolve-type.o \
rust/rust-ast-resolve-path.o \
rust/rust-ast-resolve-stmt.o \
rust/rust-ast-resolve-struct-expr-field.o \
rust/rust-forever-stack.o \
rust/rust-hir-type-check.o \
rust/rust-privacy-check.o \
Expand Down
84 changes: 83 additions & 1 deletion gcc/rust/ast/rust-ast-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,63 @@ Builder::type_path (LangItem::Kind lang_item) const
return type_path (type_path_segment (lang_item));
}

TypePath
Builder::type_path_core (std::vector<std::unique_ptr<TypePathSegment>> &&segments) const
{
bool opening_scope;

auto &mappings = Analysis::Mappings::get ();
if (mappings.get_current_crate_name () == "core")
{
segments.emplace (segments.cbegin (), type_path_segment ("crate"));
opening_scope = false;
}
else
{
segments.emplace (segments.cbegin (), type_path_segment ("core"));
opening_scope = true;
}

return TypePath (std::move (segments), loc, opening_scope);
}

TypePath
Builder::type_path_core (std::vector<std::string> &&segments) const
{
auto type_segments = std::vector<std::unique_ptr<TypePathSegment>> ();
type_segments.reserve (segments.size () + 1);
bool opening_scope;

auto &mappings = Analysis::Mappings::get ();
if (mappings.get_current_crate_name () == "core")
{
type_segments.emplace_back (type_path_segment ("crate"));
opening_scope = false;
}
else
{
type_segments.emplace_back (type_path_segment ("core"));
opening_scope = true;
}

for (auto &&seg : segments)
type_segments.emplace_back (type_path_segment (seg));

return TypePath (std::move (type_segments), loc, opening_scope);
}

TypePath
Builder::type_path_core (std::initializer_list<const char *> segments) const
{
std::vector<std::string> segments_out;
segments_out.reserve (segments.size ());

for (auto seg : segments)
segments_out.emplace_back (seg);

return type_path_core (std::move (segments_out));
}

std::unique_ptr<Type>
Builder::reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
bool mutability) const
Expand All @@ -301,6 +358,31 @@ Builder::path_in_expression (LangItem::Kind lang_item) const
return PathInExpression (lang_item, {}, loc);
}

PathInExpression
Builder::path_in_expression_core (std::vector<std::string> &&segments) const
{
auto path_segments = std::vector<PathExprSegment> ();
path_segments.reserve (segments.size () + 1);
bool opening_scope;

auto &mappings = Analysis::Mappings::get ();
if (mappings.get_current_crate_name () == "core")
{
path_segments.emplace_back (path_segment ("crate"));
opening_scope = false;
}
else
{
path_segments.emplace_back (path_segment ("core"));
opening_scope = true;
}

for (auto &&seg : segments)
path_segments.emplace_back (path_segment (seg));

return PathInExpression (std::move (path_segments), {}, loc, opening_scope);
}

PathInExpression
Builder::variant_path (const std::string &enum_path,
const std::string &variant) const
Expand Down Expand Up @@ -544,7 +626,7 @@ std::unique_ptr<Stmt>
Builder::discriminant_value (std::string binding_name, std::string instance)
{
auto intrinsic = ptrify (
path_in_expression ({"core", "intrinsics", "discriminant_value"}, true));
path_in_expression_core ({"intrinsics", "discriminant_value"}));

return let (identifier_pattern (binding_name), nullptr,
call (std::move (intrinsic), identifier (instance)));
Expand Down
19 changes: 14 additions & 5 deletions gcc/rust/ast/rust-ast-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ class Builder
TypePath type_path (std::string type) const;
TypePath type_path (LangItem::Kind lang_item) const;

/**
* Creates a type path relative to libcore
* Either `::core...` or `crate...`
*/
TypePath type_path_core (std::vector<std::unique_ptr<TypePathSegment>> &&segments) const;
TypePath type_path_core (std::vector<std::string> &&segments) const;
TypePath type_path_core (std::initializer_list<const char *> segments) const;

std::unique_ptr<Type>
reference_type (std::unique_ptr<TypeNoBounds> &&inner_type,
bool mutability = false) const;
Expand All @@ -237,6 +245,12 @@ class Builder
*/
PathInExpression path_in_expression (LangItem::Kind lang_item) const;

/**
* Create a path in expression relative to libcore
* Either `::core...` or `crate...`
*/
PathInExpression path_in_expression_core (std::vector<std::string> &&segments) const;

/* Create the path to an enum's variant (`Result::Ok`) */
PathInExpression variant_path (const std::string &enum_path,
const std::string &variant) const;
Expand Down Expand Up @@ -331,11 +345,6 @@ class Builder

/* Location of the generated AST nodes */
location_t loc;

private:
/* Some constexpr helpers for some of the builders */
static constexpr std::initializer_list<const char *> discriminant_value_path
= {"core", "intrinsics", "discriminant_value"};
};

} // namespace AST
Expand Down
15 changes: 11 additions & 4 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -840,13 +840,13 @@ TokenCollector::visit (MetaItemLitExpr &item)
}

void
TokenCollector::visit (MetaItemPathLit &item)
TokenCollector::visit (MetaItemPathExpr &item)
{
auto path = item.get_path ();
auto lit = item.get_literal ();
auto &expr = item.get_expr ();
visit (path);
push (Rust::Token::make (COLON, item.get_locus ()));
visit (lit);
push (Rust::Token::make (EQUAL, item.get_locus ()));
visit (expr);
}

void
Expand Down Expand Up @@ -3013,6 +3013,13 @@ TokenCollector::visit (AST::FormatArgs &fmt)
__FILE__, __LINE__);
}

void
TokenCollector::visit (AST::FormatArgsEager &fmt)
{
rust_sorry_at (fmt.get_locus (), "%s:%u: unimplemented FormatArgsEager visitor",
__FILE__, __LINE__);
}

void
TokenCollector::visit (AST::OffsetOf &offset_of)
{
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/ast/rust-ast-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class TokenCollector : public ASTVisitor
void visit (AttrInputLiteral &attr_input);
void visit (AttrInputMacro &attr_input);
void visit (MetaItemLitExpr &meta_item);
void visit (MetaItemPathLit &meta_item);
void visit (MetaItemPathExpr &meta_item);
void visit (BorrowExpr &expr);
void visit (DereferenceExpr &expr);
void visit (ErrorPropagationExpr &expr);
Expand Down Expand Up @@ -405,6 +405,7 @@ class TokenCollector : public ASTVisitor
void visit (BareFunctionType &type);

void visit (FormatArgs &fmt);
void visit (FormatArgsEager &fmt);
void visit (OffsetOf &offset_of);
};
} // namespace AST
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/ast/rust-ast-full-decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class LiteralExpr;
class AttrInputLiteral;
class AttrInputMacro;
class MetaItemLitExpr;
class MetaItemPathLit;
class MetaItemPathExpr;
class OperatorExpr;
class BorrowExpr;
class DereferenceExpr;
Expand Down Expand Up @@ -272,6 +272,7 @@ class BareFunctionType;

// rust-builtin-ast-nodes.h
class FormatArgs;
class FormatArgsEager;
} // namespace AST
} // namespace Rust

Expand Down
13 changes: 11 additions & 2 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ DefaultASTVisitor::visit (AST::SimplePath &path)
}

void
DefaultASTVisitor::visit (AST::MetaItemPathLit &meta_item)
DefaultASTVisitor::visit (AST::MetaItemPathExpr &meta_item)
{
visit (meta_item.get_path ());
visit (meta_item.get_literal ());
visit (meta_item.get_expr ());
}

void
Expand Down Expand Up @@ -1505,6 +1505,15 @@ DefaultASTVisitor::visit (AST::FormatArgs &)
// FIXME: Do we have anything to do? any subnodes to visit? Probably, right?
}

void
DefaultASTVisitor::visit (AST::FormatArgsEager &fmt)
{
// FIXME: Do we have anything to do? any subnodes to visit? Probably, right?

// we need this to resolve/expand macros, at least
visit (fmt.get_template ());
}

void
DefaultASTVisitor::visit (AST::OffsetOf &offset_of)
{
Expand Down
6 changes: 4 additions & 2 deletions gcc/rust/ast/rust-ast-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ASTVisitor
virtual void visit (AttrInputLiteral &attr_input) = 0;
virtual void visit (AttrInputMacro &attr_input) = 0;
virtual void visit (MetaItemLitExpr &meta_item) = 0;
virtual void visit (MetaItemPathLit &meta_item) = 0;
virtual void visit (MetaItemPathExpr &meta_item) = 0;
virtual void visit (BorrowExpr &expr) = 0;
virtual void visit (DereferenceExpr &expr) = 0;
virtual void visit (ErrorPropagationExpr &expr) = 0;
Expand Down Expand Up @@ -240,6 +240,7 @@ class ASTVisitor

// special AST nodes for certain builtin macros such as `asm!()`
virtual void visit (FormatArgs &fmt) = 0;
virtual void visit (FormatArgsEager &fmt) = 0;
virtual void visit (OffsetOf &fmt) = 0;

// TODO: rust-cond-compilation.h visiting? not currently used
Expand Down Expand Up @@ -270,7 +271,7 @@ class DefaultASTVisitor : public ASTVisitor
virtual void visit (AST::AttrInputLiteral &attr_input) override;
virtual void visit (AST::AttrInputMacro &attr_input) override;
virtual void visit (AST::MetaItemLitExpr &meta_item) override;
virtual void visit (AST::MetaItemPathLit &meta_item) override;
virtual void visit (AST::MetaItemPathExpr &meta_item) override;
virtual void visit (AST::BorrowExpr &expr) override;
virtual void visit (AST::DereferenceExpr &expr) override;
virtual void visit (AST::ErrorPropagationExpr &expr) override;
Expand Down Expand Up @@ -413,6 +414,7 @@ class DefaultASTVisitor : public ASTVisitor
virtual void visit (AST::FunctionParam &param) override;
virtual void visit (AST::VariadicParam &param) override;
virtual void visit (AST::FormatArgs &fmt) override;
virtual void visit (AST::FormatArgsEager &fmt) override;
virtual void visit (AST::OffsetOf &fmt) override;

template <typename T> void visit (T &node) { node.accept_vis (*this); }
Expand Down
Loading
Loading