Skip to content

Commit 53a1105

Browse files
committed
On stable, suggest removing #![feature] for features that have been stabilized
I don't know how to test this. I confirmed locally that this gives the appropriate help with `channel = "beta"`: ``` error[E0554]: `#![feature]` may not be used on the beta release channel --> src/lib.rs:2:1 | 2 | #![feature(min_const_generics)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove the attribute | = help: the feature `min_const_generics` has been stable since 1.51.0 and no longer requires an attribute to enable error[E0554]: `#![feature]` may not be used on the beta release channel --> src/lib.rs:3:1 | 3 | #![feature(min_const_generics, min_specialization)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: the feature `min_const_generics` has been stable since 1.51.0 and no longer requires an attribute to enable error[E0554]: `#![feature]` may not be used on the beta release channel --> src/lib.rs:4:1 | 4 | #![feature(box_patterns)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ ```
1 parent bba4088 commit 53a1105

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+33-3
Original file line numberDiff line numberDiff line change
@@ -740,16 +740,46 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
740740
}
741741

742742
fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
743+
use rustc_errors::Applicability;
744+
743745
if !sess.opts.unstable_features.is_nightly_build() {
746+
let lang_features = &sess.features_untracked().declared_lang_features;
744747
for attr in krate.attrs.iter().filter(|attr| sess.check_name(attr, sym::feature)) {
745-
struct_span_err!(
748+
let mut err = struct_span_err!(
746749
sess.parse_sess.span_diagnostic,
747750
attr.span,
748751
E0554,
749752
"`#![feature]` may not be used on the {} release channel",
750753
option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)")
751-
)
752-
.emit();
754+
);
755+
let mut all_stable = true;
756+
for ident in
757+
attr.meta_item_list().into_iter().flatten().map(|nested| nested.ident()).flatten()
758+
{
759+
let name = ident.name;
760+
let stable_since = lang_features
761+
.iter()
762+
.flat_map(|&(feature, _, since)| if feature == name { since } else { None })
763+
.next();
764+
if let Some(since) = stable_since {
765+
err.help(&format!(
766+
"the feature `{}` has been stable since {} and no longer requires \
767+
an attribute to enable",
768+
name, since
769+
));
770+
} else {
771+
all_stable = false;
772+
}
773+
}
774+
if all_stable {
775+
err.span_suggestion(
776+
attr.span,
777+
"remove the attribute",
778+
String::new(),
779+
Applicability::MachineApplicable,
780+
);
781+
}
782+
err.emit();
753783
}
754784
}
755785
}

0 commit comments

Comments
 (0)