Skip to content

Commit f14a0e2

Browse files
committed
Make a better error message for using #[feature] on stable rust
It now says '#[feature] may not be used on the stable release channel'. I had to convert this error from a lint to a normal compiler error. I left the lint previously-used for this in place since removing it is a breaking change. It will just go unused until the end of time. Fixes #24125
1 parent 9b29cbe commit f14a0e2

File tree

8 files changed

+50
-58
lines changed

8 files changed

+50
-58
lines changed

src/librustc/lint/context.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ use self::TargetLint::*;
2828
use middle::privacy::ExportedItems;
2929
use middle::ty::{self, Ty};
3030
use session::{early_error, Session};
31-
use session::config::UnstableFeatures;
3231
use lint::{Level, LevelSource, Lint, LintId, LintArray, LintPass, LintPassObject};
33-
use lint::{Default, CommandLine, Node, Allow, Warn, Deny, Forbid, ReleaseChannel};
32+
use lint::{Default, CommandLine, Node, Allow, Warn, Deny, Forbid};
3433
use lint::builtin;
3534
use util::nodemap::FnvHashMap;
3635

@@ -208,23 +207,6 @@ impl LintStore {
208207
}
209208
}
210209
}
211-
212-
fn maybe_stage_features(&mut self, sess: &Session) {
213-
let lvl = match sess.opts.unstable_features {
214-
UnstableFeatures::Default => return,
215-
UnstableFeatures::Disallow => Forbid,
216-
UnstableFeatures::Cheat => Allow
217-
};
218-
match self.by_name.get("unstable_features") {
219-
Some(&Id(lint_id)) => if self.get_level_source(lint_id).0 != Forbid {
220-
self.set_level(lint_id, (lvl, ReleaseChannel))
221-
},
222-
Some(&Renamed(_, lint_id)) => if self.get_level_source(lint_id).0 != Forbid {
223-
self.set_level(lint_id, (lvl, ReleaseChannel))
224-
},
225-
None => unreachable!()
226-
}
227-
}
228210
}
229211

230212
/// Context for lint checking.
@@ -308,7 +290,6 @@ pub fn raw_emit_lint(sess: &Session, lint: &'static Lint,
308290

309291
let name = lint.name_lower();
310292
let mut def = None;
311-
let mut note = None;
312293
let msg = match source {
313294
Default => {
314295
format!("{}, #[{}({})] on by default", msg,
@@ -325,12 +306,6 @@ pub fn raw_emit_lint(sess: &Session, lint: &'static Lint,
325306
def = Some(src);
326307
msg.to_string()
327308
}
328-
ReleaseChannel => {
329-
let release_channel = option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)");
330-
note = Some(format!("this feature may not be used in the {} release channel",
331-
release_channel));
332-
msg.to_string()
333-
}
334309
};
335310

336311
// For purposes of printing, we can treat forbid as deny.
@@ -344,10 +319,6 @@ pub fn raw_emit_lint(sess: &Session, lint: &'static Lint,
344319
_ => sess.bug("impossible level in raw_emit_lint"),
345320
}
346321

347-
if let Some(note) = note {
348-
sess.note(&note[..]);
349-
}
350-
351322
if let Some(span) = def {
352323
sess.span_note(span, "lint level defined here");
353324
}
@@ -689,9 +660,6 @@ impl LintPass for GatherNodeLevels {
689660
pub fn check_crate(tcx: &ty::ctxt,
690661
exported_items: &ExportedItems) {
691662

692-
// If this is a feature-staged build of rustc then flip several lints to 'forbid'
693-
tcx.sess.lint_store.borrow_mut().maybe_stage_features(&tcx.sess);
694-
695663
let krate = tcx.map.krate();
696664
let mut cx = Context::new(tcx, krate, exported_items);
697665

src/librustc/lint/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,6 @@ pub enum LintSource {
247247

248248
/// Lint level was set by a command-line flag.
249249
CommandLine,
250-
251-
/// Lint level was set by the release channel.
252-
ReleaseChannel
253250
}
254251

255252
pub type LevelSource = (Level, LintSource);

src/librustc/session/config.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use syntax::attr::AttrMetaMethods;
3232
use syntax::diagnostic::{ColorConfig, Auto, Always, Never, SpanHandler};
3333
use syntax::parse;
3434
use syntax::parse::token::InternedString;
35+
use syntax::feature_gate::UnstableFeatures;
3536

3637
use getopts;
3738
use std::collections::HashMap;
@@ -119,21 +120,6 @@ pub struct Options {
119120
pub unstable_features: UnstableFeatures
120121
}
121122

122-
#[derive(Clone, Copy)]
123-
pub enum UnstableFeatures {
124-
/// Hard errors for unstable features are active, as on
125-
/// beta/stable channels.
126-
Disallow,
127-
/// Use the default lint levels
128-
Default,
129-
/// Errors are bypassed for bootstrapping. This is required any time
130-
/// during the build that feature-related lints are set to warn or above
131-
/// because the build turns on warnings-as-errors and uses lots of unstable
132-
/// features. As a result, this this is always required for building Rust
133-
/// itself.
134-
Cheat
135-
}
136-
137123
#[derive(Clone, PartialEq, Eq)]
138124
pub enum PrintRequest {
139125
FileNames,
@@ -1074,7 +1060,7 @@ pub fn get_unstable_features_setting() -> UnstableFeatures {
10741060
match (disable_unstable_features, bootstrap_secret_key, bootstrap_provided_key) {
10751061
(_, Some(ref s), Some(ref p)) if s == p => UnstableFeatures::Cheat,
10761062
(true, _, _) => UnstableFeatures::Disallow,
1077-
(false, _, _) => UnstableFeatures::Default
1063+
(false, _, _) => UnstableFeatures::Allow
10781064
}
10791065
}
10801066

src/librustc_driver/driver.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
513513
let features =
514514
syntax::feature_gate::check_crate(sess.codemap(),
515515
&sess.parse_sess.span_diagnostic,
516-
&krate, &attributes);
516+
&krate, &attributes,
517+
sess.opts.unstable_features);
517518
*sess.features.borrow_mut() = features;
518519
sess.abort_if_errors();
519520
});
@@ -543,7 +544,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
543544
let features =
544545
syntax::feature_gate::check_crate(sess.codemap(),
545546
&sess.parse_sess.span_diagnostic,
546-
&krate, &attributes);
547+
&krate, &attributes,
548+
sess.opts.unstable_features);
547549
*sess.features.borrow_mut() = features;
548550
sess.abort_if_errors();
549551
});

src/librustc_driver/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use syntax::codemap;
3636
use syntax::codemap::{Span, CodeMap, DUMMY_SP};
3737
use syntax::diagnostic::{Level, RenderSpan, Bug, Fatal, Error, Warning, Note, Help};
3838
use syntax::parse::token;
39+
use syntax::feature_gate::UnstableFeatures;
3940

4041
struct Env<'a, 'tcx: 'a> {
4142
infcx: &'a infer::InferCtxt<'a, 'tcx>,
@@ -103,6 +104,7 @@ fn test_env<F>(source_string: &str,
103104
let mut options =
104105
config::basic_options();
105106
options.debugging_opts.verbose = true;
107+
options.unstable_features = UnstableFeatures::Allow;
106108
let codemap =
107109
CodeMap::new();
108110
let diagnostic_handler =

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,7 @@ pub struct UnstableFeatures;
22152215
declare_lint! {
22162216
UNSTABLE_FEATURES,
22172217
Allow,
2218-
"enabling unstable features"
2218+
"enabling unstable features (deprecated. do not use)"
22192219
}
22202220

22212221
impl LintPass for UnstableFeatures {

src/librustdoc/core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ pub use self::MaybeTyped::*;
1212
use rustc_lint;
1313
use rustc_driver::driver;
1414
use rustc::session::{self, config};
15-
use rustc::session::config::UnstableFeatures;
1615
use rustc::middle::{privacy, ty};
1716
use rustc::ast_map;
1817
use rustc::lint;
1918
use rustc_trans::back::link;
2019
use rustc_resolve as resolve;
2120

2221
use syntax::{ast, codemap, diagnostic};
22+
use syntax::feature_gate::UnstableFeatures;
2323

2424
use std::cell::{RefCell, Cell};
2525
use std::collections::{HashMap, HashSet};
@@ -106,7 +106,7 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
106106
target_triple: triple.unwrap_or(config::host_triple().to_string()),
107107
cfg: config::parse_cfgspecs(cfgs),
108108
// Ensure that rustdoc works even if rustc is feature-staged
109-
unstable_features: UnstableFeatures::Default,
109+
unstable_features: UnstableFeatures::Allow,
110110
..config::basic_options().clone()
111111
};
112112

src/libsyntax/feature_gate.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,46 @@ pub fn check_crate_macros(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast:
799799
}
800800

801801
pub fn check_crate(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::Crate,
802-
plugin_attributes: &[(String, AttributeType)]) -> Features
802+
plugin_attributes: &[(String, AttributeType)],
803+
unstable: UnstableFeatures) -> Features
803804
{
805+
maybe_stage_features(span_handler, krate, unstable);
806+
804807
check_crate_inner(cm, span_handler, krate, plugin_attributes,
805808
|ctx, krate| visit::walk_crate(&mut PostExpansionVisitor { context: ctx },
806809
krate))
807810
}
811+
812+
#[derive(Clone, Copy)]
813+
pub enum UnstableFeatures {
814+
/// Hard errors for unstable features are active, as on
815+
/// beta/stable channels.
816+
Disallow,
817+
/// Allow features to me activated, as on nightly.
818+
Allow,
819+
/// Errors are bypassed for bootstrapping. This is required any time
820+
/// during the build that feature-related lints are set to warn or above
821+
/// because the build turns on warnings-as-errors and uses lots of unstable
822+
/// features. As a result, this this is always required for building Rust
823+
/// itself.
824+
Cheat
825+
}
826+
827+
fn maybe_stage_features(span_handler: &SpanHandler, krate: &ast::Crate,
828+
unstable: UnstableFeatures) {
829+
let allow_features = match unstable {
830+
UnstableFeatures::Allow => true,
831+
UnstableFeatures::Disallow => false,
832+
UnstableFeatures::Cheat => true
833+
};
834+
if !allow_features {
835+
for attr in &krate.attrs {
836+
if attr.check_name("feature") {
837+
let release_channel = option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)");
838+
let ref msg = format!("#[feature] may not be used on the {} release channel",
839+
release_channel);
840+
span_handler.span_err(attr.span, msg);
841+
}
842+
}
843+
}
844+
}

0 commit comments

Comments
 (0)