Skip to content

Commit d0623cf

Browse files
authored
Auto merge of #36678 - TimNN:fix-dist, r=alexcrichton
emit feature help in cheat mode (fix nightlies) This should fix the `distcheck` failure in the latest nightly. cc #36539 It's probably not ideal to check the environment that often and the code ist duplicated from `librustc/session/config.rs` but this was the easiest fix I could think of. A cleaner solution would probably be to move the `unstable_features` from `Options` to `ParseSess` and change the `diag` parameter of `emit_feature_err` to take `ParseSess` instead of a `Handler`.
2 parents 388c3f2 + f0e1738 commit d0623cf

File tree

19 files changed

+79
-78
lines changed

19 files changed

+79
-78
lines changed

src/librustc/middle/stability.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,8 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
411411
&feature, &r),
412412
None => format!("use of unstable library feature '{}'", &feature)
413413
};
414-
emit_feature_err(&self.tcx.sess.parse_sess.span_diagnostic,
415-
&feature, span, GateIssue::Library(Some(issue)), &msg);
414+
emit_feature_err(&self.tcx.sess.parse_sess, &feature, span,
415+
GateIssue::Library(Some(issue)), &msg);
416416
}
417417
}
418418
Some(&Stability { ref level, ref feature, .. }) => {

src/librustc/session/config.rs

+5-26
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use std::collections::btree_map::Iter as BTreeMapIter;
3737
use std::collections::btree_map::Keys as BTreeMapKeysIter;
3838
use std::collections::btree_map::Values as BTreeMapValuesIter;
3939

40-
use std::env;
4140
use std::fmt;
4241
use std::hash::{Hasher, SipHasher};
4342
use std::iter::FromIterator;
@@ -1525,27 +1524,12 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
15251524
crate_name: crate_name,
15261525
alt_std_name: None,
15271526
libs: libs,
1528-
unstable_features: get_unstable_features_setting(),
1527+
unstable_features: UnstableFeatures::from_environment(),
15291528
debug_assertions: debug_assertions,
15301529
},
15311530
cfg)
15321531
}
15331532

1534-
pub fn get_unstable_features_setting() -> UnstableFeatures {
1535-
// Whether this is a feature-staged build, i.e. on the beta or stable channel
1536-
let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some();
1537-
// The secret key needed to get through the rustc build itself by
1538-
// subverting the unstable features lints
1539-
let bootstrap_secret_key = option_env!("CFG_BOOTSTRAP_KEY");
1540-
// The matching key to the above, only known by the build system
1541-
let bootstrap_provided_key = env::var("RUSTC_BOOTSTRAP_KEY").ok();
1542-
match (disable_unstable_features, bootstrap_secret_key, bootstrap_provided_key) {
1543-
(_, Some(ref s), Some(ref p)) if s == p => UnstableFeatures::Cheat,
1544-
(true, ..) => UnstableFeatures::Disallow,
1545-
(false, ..) => UnstableFeatures::Allow
1546-
}
1547-
}
1548-
15491533
pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateType>, String> {
15501534
let mut crate_types: Vec<CrateType> = Vec::new();
15511535
for unparsed_crate_type in &list_list {
@@ -1575,26 +1559,21 @@ pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateTy
15751559
pub mod nightly_options {
15761560
use getopts;
15771561
use syntax::feature_gate::UnstableFeatures;
1578-
use super::{ErrorOutputType, OptionStability, RustcOptGroup, get_unstable_features_setting};
1562+
use super::{ErrorOutputType, OptionStability, RustcOptGroup};
15791563
use session::{early_error, early_warn};
15801564

15811565
pub fn is_unstable_enabled(matches: &getopts::Matches) -> bool {
15821566
is_nightly_build() && matches.opt_strs("Z").iter().any(|x| *x == "unstable-options")
15831567
}
15841568

15851569
pub fn is_nightly_build() -> bool {
1586-
match get_unstable_features_setting() {
1587-
UnstableFeatures::Allow | UnstableFeatures::Cheat => true,
1588-
_ => false,
1589-
}
1570+
UnstableFeatures::from_environment().is_nightly_build()
15901571
}
15911572

15921573
pub fn check_nightly_options(matches: &getopts::Matches, flags: &[RustcOptGroup]) {
15931574
let has_z_unstable_option = matches.opt_strs("Z").iter().any(|x| *x == "unstable-options");
1594-
let really_allows_unstable_options = match get_unstable_features_setting() {
1595-
UnstableFeatures::Disallow => false,
1596-
_ => true,
1597-
};
1575+
let really_allows_unstable_options = UnstableFeatures::from_environment()
1576+
.is_nightly_build();
15981577

15991578
for opt in flags.iter() {
16001579
if opt.stability == OptionStability::Stable {

src/librustc_driver/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use rustc_trans::back::write::{create_target_machine, RELOC_MODEL_ARGS, CODE_GEN
7373
use rustc::dep_graph::DepGraph;
7474
use rustc::session::{self, config, Session, build_session, CompileResult};
7575
use rustc::session::config::{Input, PrintRequest, OutputType, ErrorOutputType};
76-
use rustc::session::config::{get_unstable_features_setting, nightly_options};
76+
use rustc::session::config::nightly_options;
7777
use rustc::lint::Lint;
7878
use rustc::lint;
7979
use rustc_metadata::loader;
@@ -649,10 +649,8 @@ impl RustcDefaultCalls {
649649
}
650650
}
651651
PrintRequest::Cfg => {
652-
let allow_unstable_cfg = match get_unstable_features_setting() {
653-
UnstableFeatures::Disallow => false,
654-
_ => true,
655-
};
652+
let allow_unstable_cfg = UnstableFeatures::from_environment()
653+
.is_nightly_build();
656654

657655
for cfg in cfg {
658656
if !allow_unstable_cfg && GatedCfg::gate(&*cfg).is_some() {

src/librustc_passes/static_recursion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'a, 'ast: 'a> CheckItemRecursionVisitor<'a, 'ast> {
143143
});
144144
if any_static {
145145
if !self.sess.features.borrow().static_recursion {
146-
emit_feature_err(&self.sess.parse_sess.span_diagnostic,
146+
emit_feature_err(&self.sess.parse_sess,
147147
"static_recursion",
148148
*self.root_span,
149149
GateIssue::Language,

src/librustc_resolve/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ impl<'a> Resolver<'a> {
149149

150150
fn insert_custom_derive(&mut self, name: &str, ext: Rc<MultiItemModifier>, sp: Span) {
151151
if !self.session.features.borrow().rustc_macro {
152-
let diagnostic = &self.session.parse_sess.span_diagnostic;
152+
let sess = &self.session.parse_sess;
153153
let msg = "loading custom derive macro crates is experimentally supported";
154-
emit_feature_err(diagnostic, "rustc_macro", sp, feature_gate::GateIssue::Language, msg);
154+
emit_feature_err(sess, "rustc_macro", sp, feature_gate::GateIssue::Language, msg);
155155
}
156156
if self.derive_modes.insert(token::intern(name), ext).is_some() {
157157
self.session.span_err(sp, &format!("cannot shadow existing derive mode `{}`", name));

src/librustc_typeck/astconv.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
795795
// For now, require that parenthetical notation be used
796796
// only with `Fn()` etc.
797797
if !self.tcx().sess.features.borrow().unboxed_closures && trait_def.paren_sugar {
798-
emit_feature_err(&self.tcx().sess.parse_sess.span_diagnostic,
798+
emit_feature_err(&self.tcx().sess.parse_sess,
799799
"unboxed_closures", span, GateIssue::Language,
800800
"\
801801
the precise format of `Fn`-family traits' \
@@ -807,7 +807,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
807807
// For now, require that parenthetical notation be used
808808
// only with `Fn()` etc.
809809
if !self.tcx().sess.features.borrow().unboxed_closures && !trait_def.paren_sugar {
810-
emit_feature_err(&self.tcx().sess.parse_sess.span_diagnostic,
810+
emit_feature_err(&self.tcx().sess.parse_sess,
811811
"unboxed_closures", span, GateIssue::Language,
812812
"\
813813
parenthetical notation is only stable when used with `Fn`-family traits");

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3255,7 +3255,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
32553255
if let Some((def_id, variant)) = variant {
32563256
if variant.kind == ty::VariantKind::Tuple &&
32573257
!self.tcx.sess.features.borrow().relaxed_adts {
3258-
emit_feature_err(&self.tcx.sess.parse_sess.span_diagnostic,
3258+
emit_feature_err(&self.tcx.sess.parse_sess,
32593259
"relaxed_adts", span, GateIssue::Language,
32603260
"tuple structs and variants in struct patterns are unstable");
32613261
}

src/librustdoc/html/markdown.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#![allow(non_camel_case_types)]
2828

2929
use libc;
30-
use rustc::session::config::get_unstable_features_setting;
3130
use std::ascii::AsciiExt;
3231
use std::cell::RefCell;
3332
use std::default::Default;
@@ -478,13 +477,10 @@ impl LangString {
478477
let mut data = LangString::all_false();
479478
let mut allow_compile_fail = false;
480479
let mut allow_error_code_check = false;
481-
match get_unstable_features_setting() {
482-
UnstableFeatures::Allow | UnstableFeatures::Cheat => {
483-
allow_compile_fail = true;
484-
allow_error_code_check = true;
485-
}
486-
_ => {},
487-
};
480+
if UnstableFeatures::from_environment().is_nightly_build() {
481+
allow_compile_fail = true;
482+
allow_error_code_check = true;
483+
}
488484

489485
let tokens = string.split(|c: char|
490486
!(c == '_' || c == '-' || c.is_alphanumeric())

src/librustdoc/html/render.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ use syntax::feature_gate::UnstableFeatures;
5858
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, LOCAL_CRATE};
5959
use rustc::middle::privacy::AccessLevels;
6060
use rustc::middle::stability;
61-
use rustc::session::config::get_unstable_features_setting;
6261
use rustc::hir;
6362
use rustc::util::nodemap::{FnvHashMap, FnvHashSet};
6463
use rustc_data_structures::flock;
@@ -1971,7 +1970,7 @@ fn item_static(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19711970
fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
19721971
f: &clean::Function) -> fmt::Result {
19731972
// FIXME(#24111): remove when `const_fn` is stabilized
1974-
let vis_constness = match get_unstable_features_setting() {
1973+
let vis_constness = match UnstableFeatures::from_environment() {
19751974
UnstableFeatures::Allow => f.constness,
19761975
_ => hir::Constness::NotConst
19771976
};
@@ -2250,7 +2249,7 @@ fn render_assoc_item(w: &mut fmt::Formatter,
22502249
}
22512250
};
22522251
// FIXME(#24111): remove when `const_fn` is stabilized
2253-
let vis_constness = match get_unstable_features_setting() {
2252+
let vis_constness = match UnstableFeatures::from_environment() {
22542253
UnstableFeatures::Allow => constness,
22552254
_ => hir::Constness::NotConst
22562255
};

src/librustdoc/test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ use rustc_lint;
2525
use rustc::dep_graph::DepGraph;
2626
use rustc::hir::map as hir_map;
2727
use rustc::session::{self, config};
28-
use rustc::session::config::{get_unstable_features_setting, OutputType,
29-
OutputTypes, Externs};
28+
use rustc::session::config::{OutputType, OutputTypes, Externs};
3029
use rustc::session::search_paths::{SearchPaths, PathKind};
3130
use rustc_back::dynamic_lib::DynamicLibrary;
3231
use rustc_back::tempdir::TempDir;
@@ -35,6 +34,7 @@ use rustc_driver::driver::phase_2_configure_and_expand;
3534
use rustc_metadata::cstore::CStore;
3635
use rustc_resolve::MakeGlobMap;
3736
use syntax::codemap::CodeMap;
37+
use syntax::feature_gate::UnstableFeatures;
3838
use errors;
3939
use errors::emitter::ColorConfig;
4040

@@ -68,7 +68,7 @@ pub fn run(input: &str,
6868
search_paths: libs.clone(),
6969
crate_types: vec!(config::CrateTypeDylib),
7070
externs: externs.clone(),
71-
unstable_features: get_unstable_features_setting(),
71+
unstable_features: UnstableFeatures::from_environment(),
7272
..config::basic_options().clone()
7373
};
7474

@@ -197,7 +197,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
197197
.. config::basic_codegen_options()
198198
},
199199
test: as_test_harness,
200-
unstable_features: get_unstable_features_setting(),
200+
unstable_features: UnstableFeatures::from_environment(),
201201
..config::basic_options().clone()
202202
};
203203

src/libsyntax/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<'a> StripUnconfigured<'a> {
157157
// flag the offending attributes
158158
for attr in attrs.iter() {
159159
if !self.features.map(|features| features.stmt_expr_attributes).unwrap_or(true) {
160-
emit_feature_err(&self.sess.span_diagnostic,
160+
emit_feature_err(&self.sess,
161161
"stmt_expr_attributes",
162162
attr.span,
163163
GateIssue::Language,

src/libsyntax/ext/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
344344
// Detect use of feature-gated or invalid attributes on macro invoations
345345
// since they will not be detected after macro expansion.
346346
for attr in attrs.iter() {
347-
feature_gate::check_attribute(&attr, &self.cx.parse_sess.span_diagnostic,
347+
feature_gate::check_attribute(&attr, &self.cx.parse_sess,
348348
&self.cx.parse_sess.codemap(),
349349
&self.cx.ecfg.features.unwrap());
350350
}

src/libsyntax/feature_gate.rs

+43-17
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use parse::ParseSess;
3636
use parse::token::InternedString;
3737

3838
use std::ascii::AsciiExt;
39+
use std::env;
3940

4041
macro_rules! setter {
4142
($field: ident) => {{
@@ -679,16 +680,15 @@ impl GatedCfg {
679680
pub fn check_and_emit(&self, sess: &ParseSess, features: &Features) {
680681
let (cfg, feature, has_feature) = GATED_CFGS[self.index];
681682
if !has_feature(features) && !sess.codemap().span_allows_unstable(self.span) {
682-
let diagnostic = &sess.span_diagnostic;
683683
let explain = format!("`cfg({})` is experimental and subject to change", cfg);
684-
emit_feature_err(diagnostic, feature, self.span, GateIssue::Language, &explain);
684+
emit_feature_err(sess, feature, self.span, GateIssue::Language, &explain);
685685
}
686686
}
687687
}
688688

689689
struct Context<'a> {
690690
features: &'a Features,
691-
span_handler: &'a Handler,
691+
parse_sess: &'a ParseSess,
692692
cm: &'a CodeMap,
693693
plugin_attributes: &'a [(String, AttributeType)],
694694
}
@@ -699,7 +699,7 @@ macro_rules! gate_feature_fn {
699699
let has_feature: bool = has_feature(&$cx.features);
700700
debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature);
701701
if !has_feature && !cx.cm.span_allows_unstable(span) {
702-
emit_feature_err(cx.span_handler, name, span, GateIssue::Language, explain);
702+
emit_feature_err(cx.parse_sess, name, span, GateIssue::Language, explain);
703703
}
704704
}}
705705
}
@@ -756,10 +756,10 @@ impl<'a> Context<'a> {
756756
}
757757
}
758758

759-
pub fn check_attribute(attr: &ast::Attribute, handler: &Handler,
759+
pub fn check_attribute(attr: &ast::Attribute, parse_sess: &ParseSess,
760760
cm: &CodeMap, features: &Features) {
761761
let cx = Context {
762-
features: features, span_handler: handler,
762+
features: features, parse_sess: parse_sess,
763763
cm: cm, plugin_attributes: &[]
764764
};
765765
cx.check_attribute(attr, true);
@@ -788,8 +788,10 @@ pub enum GateIssue {
788788
Library(Option<u32>)
789789
}
790790

791-
pub fn emit_feature_err(diag: &Handler, feature: &str, span: Span, issue: GateIssue,
791+
pub fn emit_feature_err(sess: &ParseSess, feature: &str, span: Span, issue: GateIssue,
792792
explain: &str) {
793+
let diag = &sess.span_diagnostic;
794+
793795
let issue = match issue {
794796
GateIssue::Language => find_lang_feature_issue(feature),
795797
GateIssue::Library(lib) => lib,
@@ -802,13 +804,12 @@ pub fn emit_feature_err(diag: &Handler, feature: &str, span: Span, issue: GateIs
802804
};
803805

804806
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
805-
if option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some() {
806-
err.emit();
807-
return;
807+
if sess.unstable_features.is_nightly_build() {
808+
err.help(&format!("add #![feature({})] to the \
809+
crate attributes to enable",
810+
feature));
808811
}
809-
err.help(&format!("add #![feature({})] to the \
810-
crate attributes to enable",
811-
feature));
812+
812813
err.emit();
813814
}
814815

@@ -962,9 +963,10 @@ impl<'a> Visitor for PostExpansionVisitor<'a> {
962963
if attr::contains_name(&i.attrs[..], "simd") {
963964
gate_feature_post!(&self, simd, i.span,
964965
"SIMD types are experimental and possibly buggy");
965-
self.context.span_handler.span_warn(i.span,
966-
"the `#[simd]` attribute is deprecated, \
967-
use `#[repr(simd)]` instead");
966+
self.context.parse_sess.span_diagnostic.span_warn(i.span,
967+
"the `#[simd]` attribute \
968+
is deprecated, use \
969+
`#[repr(simd)]` instead");
968970
}
969971
for attr in &i.attrs {
970972
if attr.name() == "repr" {
@@ -1273,7 +1275,7 @@ pub fn check_crate(krate: &ast::Crate,
12731275
maybe_stage_features(&sess.span_diagnostic, krate, unstable);
12741276
let ctx = Context {
12751277
features: features,
1276-
span_handler: &sess.span_diagnostic,
1278+
parse_sess: sess,
12771279
cm: sess.codemap(),
12781280
plugin_attributes: plugin_attributes,
12791281
};
@@ -1294,6 +1296,30 @@ pub enum UnstableFeatures {
12941296
Cheat
12951297
}
12961298

1299+
impl UnstableFeatures {
1300+
pub fn from_environment() -> UnstableFeatures {
1301+
// Whether this is a feature-staged build, i.e. on the beta or stable channel
1302+
let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some();
1303+
// The secret key needed to get through the rustc build itself by
1304+
// subverting the unstable features lints
1305+
let bootstrap_secret_key = option_env!("CFG_BOOTSTRAP_KEY");
1306+
// The matching key to the above, only known by the build system
1307+
let bootstrap_provided_key = env::var("RUSTC_BOOTSTRAP_KEY").ok();
1308+
match (disable_unstable_features, bootstrap_secret_key, bootstrap_provided_key) {
1309+
(_, Some(ref s), Some(ref p)) if s == p => UnstableFeatures::Cheat,
1310+
(true, _, _) => UnstableFeatures::Disallow,
1311+
(false, _, _) => UnstableFeatures::Allow
1312+
}
1313+
}
1314+
1315+
pub fn is_nightly_build(&self) -> bool {
1316+
match *self {
1317+
UnstableFeatures::Allow | UnstableFeatures::Cheat => true,
1318+
_ => false,
1319+
}
1320+
}
1321+
}
1322+
12971323
fn maybe_stage_features(span_handler: &Handler, krate: &ast::Crate,
12981324
unstable: UnstableFeatures) {
12991325
let allow_features = match unstable {

0 commit comments

Comments
 (0)