-
Notifications
You must be signed in to change notification settings - Fork 208
session: Add basic prelude injection #4383
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
CohenArthur
wants to merge
1
commit into
Rust-GCC:master
Choose a base branch
from
CohenArthur:inject-prelude
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // 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/>. | ||
|
|
||
| #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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's 2026! 🎉