-
Notifications
You must be signed in to change notification settings - Fork 209
Improve feature collection and permit macro attribute input #4425
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
Open
P-E-P
wants to merge
13
commits into
Rust-GCC:master
Choose a base branch
from
P-E-P:feature_collector
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.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ca9191e
Rename is_builtin and change return type
P-E-P fb01eaf
Split feature collection from feature gating
P-E-P 319d3d4
Remove unused function
P-E-P 77fac84
Add early cfg strip step before feature collection
P-E-P 7d4fa03
Bump feature definition version
P-E-P ecec7e3
Collect feature gate error at parse time
P-E-P 214b773
Collect early feature gating errors in a store
P-E-P c5354cb
Move early gate feature store to it's own TU
P-E-P c980f8a
Add crate_name and crate_type to attribute values
P-E-P 189aee8
Add multiple missing builtin attribute values
P-E-P db38ae8
Make attribute a proper AST node
P-E-P ada760c
Add node id to attribute node.
P-E-P 16b913d
WIP
P-E-P 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
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
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
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
117 changes: 117 additions & 0 deletions
117
gcc/rust/checks/errors/feature/rust-feature-collector.cc
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,117 @@ | ||
| // Copyright (C) 2026 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-feature-collector.h" | ||
| #include "rust-attribute-values.h" | ||
|
|
||
| namespace Rust { | ||
| namespace Features { | ||
|
|
||
| FeatureCollector::FeatureCollector () : features (CrateFeatures{UNKNOWN_NODEID}) | ||
| {} | ||
|
|
||
| CrateFeatures | ||
| FeatureCollector::collect (AST::Crate &crate) | ||
| { | ||
| features.valid_lang_features.clear (); | ||
| features.valid_lib_features.clear (); | ||
| features.crate_id = crate.get_node_id (); | ||
|
|
||
| visit (crate); | ||
|
|
||
| return features; | ||
| } | ||
|
|
||
| namespace { | ||
| bool | ||
| is_feature_attribute (const AST::Attribute &attribute) | ||
| { | ||
| return Values::Attributes::FEATURE == attribute.get_path ().as_string (); | ||
| } | ||
|
|
||
| // check for empty feature, such as `#![feature], this is an error | ||
| bool | ||
| is_valid_feature_attribute (const AST::Attribute &attribute) | ||
| { | ||
| return !attribute.empty_input (); | ||
| } | ||
| } // namespace | ||
|
|
||
| void | ||
| FeatureCollector::add_features_from_token_tree ( | ||
| const AST::DelimTokenTree &delim_ttree) | ||
| { | ||
| std::unique_ptr<AST::AttrInputMetaItemContainer> meta_item ( | ||
| delim_ttree.parse_to_meta_item ()); | ||
| add_features_from_meta_item_container (*meta_item); | ||
| } | ||
|
|
||
| void | ||
| FeatureCollector::add_features_from_meta_item_container ( | ||
| const AST::AttrInputMetaItemContainer &meta_item_container) | ||
| { | ||
| for (const auto &item : meta_item_container.get_items ()) | ||
| { | ||
| const auto &name_str = item->as_string (); | ||
|
|
||
| // TODO: detect duplicates | ||
| if (auto tname = Feature::as_name (name_str)) | ||
| features.valid_lang_features.insert (*tname); | ||
| else | ||
| features.valid_lib_features.emplace (name_str, item->get_locus ()); | ||
| } | ||
| } | ||
|
|
||
| void | ||
| FeatureCollector::identify_feature (const AST::Attribute &attribute) | ||
| { | ||
| if (is_feature_attribute (attribute)) | ||
| { | ||
| if (!is_valid_feature_attribute (attribute)) | ||
| { | ||
| rust_error_at (attribute.get_locus (), ErrorCode::E0556, | ||
| "malformed %<feature%> attribute input"); | ||
| return; | ||
| } | ||
| const auto &attr_input = attribute.get_attr_input (); | ||
| auto type = attr_input.get_attr_input_type (); | ||
| if (type == AST::AttrInput::AttrInputType::TOKEN_TREE) | ||
| { | ||
| const auto &delim_ttree = static_cast<const AST::DelimTokenTree &> ( | ||
| attribute.get_attr_input ()); | ||
| add_features_from_token_tree (delim_ttree); | ||
| } | ||
| else if (type == AST::AttrInput::AttrInputType::META_ITEM) | ||
| { | ||
| // We can find a meta item in #[cfg(toto),feature(xxxx)] | ||
| const auto &meta_item_container | ||
| = static_cast<const AST::AttrInputMetaItemContainer &> (attr_input); | ||
| add_features_from_meta_item_container (meta_item_container); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void | ||
| FeatureCollector::visit (AST::Crate &crate) | ||
| { | ||
| for (const auto &attribute : crate.inner_attrs) | ||
| identify_feature (attribute); | ||
| } | ||
|
|
||
| } // namespace Features | ||
| } // 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,62 @@ | ||
| // Copyright (C) 2026 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_FEATURE_COLLECTOR_H | ||
| #define RUST_FEATURE_COLLECTOR_H | ||
|
|
||
| #include "rust-feature.h" | ||
| #include "rust-ast-visitor.h" | ||
|
|
||
| namespace Rust { | ||
| namespace Features { | ||
|
|
||
| /** Helper structure gathering all features enabled within a given crate | ||
| * using the #![feature(XXXXX)] syntax. | ||
| **/ | ||
| struct CrateFeatures | ||
| { | ||
| // The node id identifying the crate those features belong to. | ||
| NodeId crate_id; | ||
| // Language features that have been declared within the crate. | ||
| std::set<Feature::Name> valid_lang_features; | ||
| // Library features that have been declared within the crate. | ||
| std::map<std::string, location_t> valid_lib_features; | ||
|
|
||
| CrateFeatures (NodeId crate_id) : crate_id (crate_id) {} | ||
| }; | ||
|
|
||
| class FeatureCollector : public AST::DefaultASTVisitor | ||
| { | ||
| public: | ||
| FeatureCollector (); | ||
|
|
||
| CrateFeatures collect (AST::Crate &crate); | ||
|
|
||
| private: | ||
| CrateFeatures features; | ||
|
|
||
| using AST::DefaultASTVisitor::visit; | ||
| void visit (AST::Crate &crate) override; | ||
| void identify_feature (const AST::Attribute &attribute); | ||
| void add_features_from_token_tree (const AST::DelimTokenTree &delim_ttree); | ||
| void add_features_from_meta_item_container ( | ||
| const AST::AttrInputMetaItemContainer &meta_item_container); | ||
| }; | ||
| } // namespace Features | ||
| } // namespace Rust | ||
|
|
||
| #endif /* ! RUST_FEATURE_COLLECTOR_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
Oops, something went wrong.
Oops, something went wrong.
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.
1.49 libraries need features new to the 1.50 compiler?
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.
I'm experimenting with rust for linux, initially I thought this would be fine because we only had a superset of features but since the regeneration removes a feature I'll make a separate header with whatever features I need for linux.