Skip to content

rustc: target_features: allow for cfg-only stable target_features#155962

Open
romancardenas wants to merge 1 commit intorust-lang:mainfrom
romancardenas:cfg-only-stable-target-feature
Open

rustc: target_features: allow for cfg-only stable target_features#155962
romancardenas wants to merge 1 commit intorust-lang:mainfrom
romancardenas:cfg-only-stable-target-feature

Conversation

@romancardenas
Copy link
Copy Markdown

@romancardenas romancardenas commented Apr 29, 2026

This PR introduces a new stabilization level for target_features: CfgOnlyStable. The motivation is allowing the Rust compiler to expose target_features of targets so users can use cfg(target_feature = "feature") for conditional blocks depending on target features. However, CfgOnlyStable cannot be used for #[target_feature(enable = "feature")], as this is still considered unstable. Accordingly, the compiler will still raise an error if these expressions are used on stable.

This PR relates partially to #150257. As discussed, for RISC-V targets, having the "d", "e", and "f" target features exposed will allow baremetal developers to adapt the code depending on the target's properties.

r? @RalfJung

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 29, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 29, 2026

r? @nikomatsakis

rustbot has assigned @nikomatsakis.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 20 candidates

@rustbot rustbot assigned RalfJung and unassigned nikomatsakis Apr 29, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 29, 2026

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

Copy link
Copy Markdown
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Thanks! This is probably the easiest way to do it. It's not super clean since it cannot represent "feature is allowed in cfg but strictly forbidden otherwise", so the entire thing will need a refactor eventually... but I can't ask you to do that now so let's just go with this.

@rustbot author

View changes since this review

reason,
});
} else if stability.requires_nightly().is_some() {
} else if stability.requires_nightly(true).is_some() {
Copy link
Copy Markdown
Member

@RalfJung RalfJung Apr 30, 2026

Choose a reason for hiding this comment

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

Suggested change
} else if stability.requires_nightly(true).is_some() {
} else if stability.requires_nightly(/* in_cfg */ false).is_some() {

This is where we emit warnings for unstable things in -Ctarget-feature. Yes it's pretty hacky to do that as a side-effect of constructing the cfg data... codegen backend initialization is a terrible spaghetti mess and someone decided this was the place to put that check. 🤷 (It's probably here to avoid emitting the warnings multiple times, or something like that.)

reason,
});
} else if let Some(nightly_feature) = stability.requires_nightly()
} else if let Some(nightly_feature) = stability.requires_nightly(false)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
} else if let Some(nightly_feature) = stability.requires_nightly(false)
} else if let Some(nightly_feature) = stability.requires_nightly(/* in_cfg */ false)

if allow_unstable
|| (gate.in_cfg()
&& (sess.is_nightly_build() || gate.requires_nightly().is_none()))
&& (sess.is_nightly_build() || gate.requires_nightly(true).is_none()))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
&& (sess.is_nightly_build() || gate.requires_nightly(true).is_none()))
&& (sess.is_nightly_build() || gate.requires_nightly(/* in_cfg */ true).is_none()))

CfgOnlyStable(
/// This must be a *language* feature, or else rustc will ICE when reporting a missing
/// feature gate!
Symbol,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

CfgOnlyStable(feature_name) will look odd.

Maybe CfgStableToggleUnstable?

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 30, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 30, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants