Skip to content

Commit 9f7e281

Browse files
vishadGoyalMark-Simulacrum
authored andcommitted
delay error for enabling unstable lib features
If #![feature] is used outside the nightly channel for only lib features, the check will be delayed to the stability pass after parsing. This is done so that appropriate help messages can be shown if the #![feature] has been used needlessly
1 parent d1d8145 commit 9f7e281

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+6
Original file line numberDiff line numberDiff line change
@@ -702,10 +702,16 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
702702
}
703703

704704
fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
705+
// checks if `#![feature]` has been used to enable any lang feature
706+
// does not check the same for lib features unless there's at least one
707+
// declared lang feature
705708
use rustc_errors::Applicability;
706709

707710
if !sess.opts.unstable_features.is_nightly_build() {
708711
let lang_features = &sess.features_untracked().declared_lang_features;
712+
if lang_features.len() == 0 {
713+
return;
714+
}
709715
for attr in krate.attrs.iter().filter(|attr| attr.has_name(sym::feature)) {
710716
let mut err = struct_span_err!(
711717
sess.parse_sess.span_diagnostic,

compiler/rustc_passes/src/stability.rs

+10
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,16 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
929929
let declared_lib_features = &tcx.features().declared_lib_features;
930930
let mut remaining_lib_features = FxHashMap::default();
931931
for (feature, span) in declared_lib_features {
932+
if !tcx.sess.opts.unstable_features.is_nightly_build() {
933+
struct_span_err!(
934+
tcx.sess,
935+
*span,
936+
E0554,
937+
"`#![feature]` may not be used on the {} release channel",
938+
env!("CFG_RELEASE_CHANNEL")
939+
)
940+
.emit();
941+
}
932942
if remaining_lib_features.contains_key(&feature) {
933943
// Warn if the user enables a lib feature multiple times.
934944
duplicate_feature_err(tcx.sess, *span, *feature);

0 commit comments

Comments
 (0)