Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ GRS_OBJS = \
rust/rust-desugar-question-mark.o \
rust/rust-desugar-apit.o \
rust/rust-desugar-try-block.o \
rust/rust-std-prelude.o \
$(END)
# removed object files from here

Expand Down
61 changes: 61 additions & 0 deletions gcc/rust/resolve/rust-std-prelude.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (C) 2025 Free Software Foundation, Inc.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 2026! 🎉


// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-std-prelude.h"
#include "input.h"
#include "rust-ast-builder.h"
#include "rust-ast.h"
#include "rust-item.h"

namespace Rust {
namespace AST {

void
StdPrelude::inject (AST::Crate &crate)
{
// The actual logic is quite complicated, as this depends on editions and on
// whether or not we're compiling in #[no_std] mode or not. For now, we just
// want core's prelude, so that's what we'll inject

auto loc = UNKNOWN_LOCATION;

auto prelude_import
= Attribute (SimplePath::from_str ("prelude_import", loc), nullptr);
std::vector<Attribute> attributes = {prelude_import};

// core::prelude::rust_2018::*
auto core = SimplePathSegment ("core", loc);
auto prelude = SimplePathSegment ("prelude", loc);
auto rust_2018 = SimplePathSegment ("rust_2018", loc);

auto path = SimplePath ({core, prelude, rust_2018});

auto use
= std::make_unique<UseTreeGlob> (UseTreeGlob::PathType::PATH_PREFIXED, path,
loc);

auto final_import
= std::make_unique<UseDeclaration> (std::move (use),
Visibility::create_private (),
std::move (attributes), loc);

crate.items.insert (crate.items.begin (), std::move (final_import));
}

} // namespace AST
} // namespace Rust
39 changes: 39 additions & 0 deletions gcc/rust/resolve/rust-std-prelude.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2025 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#ifndef RUST_PRELUDE_INJECT_H
#define RUST_PRELUDE_INJECT_H

#include "rust-ast.h"

namespace Rust {
namespace AST {

class StdPrelude
{
public:
/**
* Inject the standard library prelude into a given crate
*/
static void inject (AST::Crate &crate);
};

} // namespace AST
} // namespace Rust

#endif // ! RUST_PRELUDE_INJECT_H
3 changes: 3 additions & 0 deletions gcc/rust/rust-session-manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "rust-desugar-for-loops.h"
#include "rust-desugar-question-mark.h"
#include "rust-desugar-apit.h"
#include "rust-std-prelude.h"
#include "rust-diagnostics.h"
#include "rust-expression-yeast.h"
#include "rust-hir-pattern-analysis.h"
Expand Down Expand Up @@ -974,6 +975,8 @@ Session::expansion (AST::Crate &crate, Resolver2_0::NameResolutionContext &ctx)
{
rust_debug ("started expansion");

// AST::StdPrelude::inject (crate);

/* rustc has a modification to windows PATH temporarily here, which may end
* up being required */

Expand Down