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
This commit is contained in:
Brian Anderson 2015-06-17 17:48:16 -07:00
parent 9b29cbe1ea
commit f14a0e2de4
8 changed files with 50 additions and 58 deletions

View File

@ -28,9 +28,8 @@ use self::TargetLint::*;
use middle::privacy::ExportedItems;
use middle::ty::{self, Ty};
use session::{early_error, Session};
use session::config::UnstableFeatures;
use lint::{Level, LevelSource, Lint, LintId, LintArray, LintPass, LintPassObject};
use lint::{Default, CommandLine, Node, Allow, Warn, Deny, Forbid, ReleaseChannel};
use lint::{Default, CommandLine, Node, Allow, Warn, Deny, Forbid};
use lint::builtin;
use util::nodemap::FnvHashMap;
@ -208,23 +207,6 @@ impl LintStore {
}
}
}
fn maybe_stage_features(&mut self, sess: &Session) {
let lvl = match sess.opts.unstable_features {
UnstableFeatures::Default => return,
UnstableFeatures::Disallow => Forbid,
UnstableFeatures::Cheat => Allow
};
match self.by_name.get("unstable_features") {
Some(&Id(lint_id)) => if self.get_level_source(lint_id).0 != Forbid {
self.set_level(lint_id, (lvl, ReleaseChannel))
},
Some(&Renamed(_, lint_id)) => if self.get_level_source(lint_id).0 != Forbid {
self.set_level(lint_id, (lvl, ReleaseChannel))
},
None => unreachable!()
}
}
}
/// Context for lint checking.
@ -308,7 +290,6 @@ pub fn raw_emit_lint(sess: &Session, lint: &'static Lint,
let name = lint.name_lower();
let mut def = None;
let mut note = None;
let msg = match source {
Default => {
format!("{}, #[{}({})] on by default", msg,
@ -325,12 +306,6 @@ pub fn raw_emit_lint(sess: &Session, lint: &'static Lint,
def = Some(src);
msg.to_string()
}
ReleaseChannel => {
let release_channel = option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)");
note = Some(format!("this feature may not be used in the {} release channel",
release_channel));
msg.to_string()
}
};
// For purposes of printing, we can treat forbid as deny.
@ -344,10 +319,6 @@ pub fn raw_emit_lint(sess: &Session, lint: &'static Lint,
_ => sess.bug("impossible level in raw_emit_lint"),
}
if let Some(note) = note {
sess.note(&note[..]);
}
if let Some(span) = def {
sess.span_note(span, "lint level defined here");
}
@ -689,9 +660,6 @@ impl LintPass for GatherNodeLevels {
pub fn check_crate(tcx: &ty::ctxt,
exported_items: &ExportedItems) {
// If this is a feature-staged build of rustc then flip several lints to 'forbid'
tcx.sess.lint_store.borrow_mut().maybe_stage_features(&tcx.sess);
let krate = tcx.map.krate();
let mut cx = Context::new(tcx, krate, exported_items);

View File

@ -247,9 +247,6 @@ pub enum LintSource {
/// Lint level was set by a command-line flag.
CommandLine,
/// Lint level was set by the release channel.
ReleaseChannel
}
pub type LevelSource = (Level, LintSource);

View File

@ -32,6 +32,7 @@ use syntax::attr::AttrMetaMethods;
use syntax::diagnostic::{ColorConfig, Auto, Always, Never, SpanHandler};
use syntax::parse;
use syntax::parse::token::InternedString;
use syntax::feature_gate::UnstableFeatures;
use getopts;
use std::collections::HashMap;
@ -119,21 +120,6 @@ pub struct Options {
pub unstable_features: UnstableFeatures
}
#[derive(Clone, Copy)]
pub enum UnstableFeatures {
/// Hard errors for unstable features are active, as on
/// beta/stable channels.
Disallow,
/// Use the default lint levels
Default,
/// Errors are bypassed for bootstrapping. This is required any time
/// during the build that feature-related lints are set to warn or above
/// because the build turns on warnings-as-errors and uses lots of unstable
/// features. As a result, this this is always required for building Rust
/// itself.
Cheat
}
#[derive(Clone, PartialEq, Eq)]
pub enum PrintRequest {
FileNames,
@ -1074,7 +1060,7 @@ pub fn get_unstable_features_setting() -> UnstableFeatures {
match (disable_unstable_features, bootstrap_secret_key, bootstrap_provided_key) {
(_, Some(ref s), Some(ref p)) if s == p => UnstableFeatures::Cheat,
(true, _, _) => UnstableFeatures::Disallow,
(false, _, _) => UnstableFeatures::Default
(false, _, _) => UnstableFeatures::Allow
}
}

View File

@ -513,7 +513,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
let features =
syntax::feature_gate::check_crate(sess.codemap(),
&sess.parse_sess.span_diagnostic,
&krate, &attributes);
&krate, &attributes,
sess.opts.unstable_features);
*sess.features.borrow_mut() = features;
sess.abort_if_errors();
});
@ -543,7 +544,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
let features =
syntax::feature_gate::check_crate(sess.codemap(),
&sess.parse_sess.span_diagnostic,
&krate, &attributes);
&krate, &attributes,
sess.opts.unstable_features);
*sess.features.borrow_mut() = features;
sess.abort_if_errors();
});

View File

@ -36,6 +36,7 @@ use syntax::codemap;
use syntax::codemap::{Span, CodeMap, DUMMY_SP};
use syntax::diagnostic::{Level, RenderSpan, Bug, Fatal, Error, Warning, Note, Help};
use syntax::parse::token;
use syntax::feature_gate::UnstableFeatures;
struct Env<'a, 'tcx: 'a> {
infcx: &'a infer::InferCtxt<'a, 'tcx>,
@ -103,6 +104,7 @@ fn test_env<F>(source_string: &str,
let mut options =
config::basic_options();
options.debugging_opts.verbose = true;
options.unstable_features = UnstableFeatures::Allow;
let codemap =
CodeMap::new();
let diagnostic_handler =

View File

@ -2215,7 +2215,7 @@ pub struct UnstableFeatures;
declare_lint! {
UNSTABLE_FEATURES,
Allow,
"enabling unstable features"
"enabling unstable features (deprecated. do not use)"
}
impl LintPass for UnstableFeatures {

View File

@ -12,7 +12,6 @@ pub use self::MaybeTyped::*;
use rustc_lint;
use rustc_driver::driver;
use rustc::session::{self, config};
use rustc::session::config::UnstableFeatures;
use rustc::middle::{privacy, ty};
use rustc::ast_map;
use rustc::lint;
@ -20,6 +19,7 @@ use rustc_trans::back::link;
use rustc_resolve as resolve;
use syntax::{ast, codemap, diagnostic};
use syntax::feature_gate::UnstableFeatures;
use std::cell::{RefCell, Cell};
use std::collections::{HashMap, HashSet};
@ -106,7 +106,7 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
target_triple: triple.unwrap_or(config::host_triple().to_string()),
cfg: config::parse_cfgspecs(cfgs),
// Ensure that rustdoc works even if rustc is feature-staged
unstable_features: UnstableFeatures::Default,
unstable_features: UnstableFeatures::Allow,
..config::basic_options().clone()
};

View File

@ -799,9 +799,46 @@ pub fn check_crate_macros(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast:
}
pub fn check_crate(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::Crate,
plugin_attributes: &[(String, AttributeType)]) -> Features
plugin_attributes: &[(String, AttributeType)],
unstable: UnstableFeatures) -> Features
{
maybe_stage_features(span_handler, krate, unstable);
check_crate_inner(cm, span_handler, krate, plugin_attributes,
|ctx, krate| visit::walk_crate(&mut PostExpansionVisitor { context: ctx },
krate))
}
#[derive(Clone, Copy)]
pub enum UnstableFeatures {
/// Hard errors for unstable features are active, as on
/// beta/stable channels.
Disallow,
/// Allow features to me activated, as on nightly.
Allow,
/// Errors are bypassed for bootstrapping. This is required any time
/// during the build that feature-related lints are set to warn or above
/// because the build turns on warnings-as-errors and uses lots of unstable
/// features. As a result, this this is always required for building Rust
/// itself.
Cheat
}
fn maybe_stage_features(span_handler: &SpanHandler, krate: &ast::Crate,
unstable: UnstableFeatures) {
let allow_features = match unstable {
UnstableFeatures::Allow => true,
UnstableFeatures::Disallow => false,
UnstableFeatures::Cheat => true
};
if !allow_features {
for attr in &krate.attrs {
if attr.check_name("feature") {
let release_channel = option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)");
let ref msg = format!("#[feature] may not be used on the {} release channel",
release_channel);
span_handler.span_err(attr.span, msg);
}
}
}
}