From 99f629a9313a6ac5d7c57fdfa9127a336d0731a8 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 8 May 2017 13:36:26 -0700 Subject: [PATCH] rustc: Add a new `-Z force-unstable-if-unmarked` flag This commit adds a new `-Z` flag to the compiler for use when bootstrapping the compiler itself. We want to be able to use crates.io crates, but we also want the usage of such crates to be as ergonomic as possible! To that end compiler crates are a little tricky in that the crates.io crates are not annotated as unstable, nor do they expect to pull in unstable dependencies. To cover all these situations it's intended that the compiler will forever now bootstrap with `-Z force-unstable-if-unmarked`. This flags serves a dual purpose of forcing crates.io crates to themselves be unstable while also allowing them to use other "unstable" crates.io crates. This should mean that adding a dependency to compiler no longer requires upstream modification with unstable/staged_api attributes for inclusion! --- src/librustc/middle/cstore.rs | 2 - src/librustc/middle/stability.rs | 81 ++++++++++++++++++++-------- src/librustc/session/config.rs | 2 + src/librustc_driver/driver.rs | 2 +- src/librustc_driver/test.rs | 2 +- src/librustc_metadata/cstore.rs | 9 ---- src/librustc_metadata/cstore_impl.rs | 5 -- 7 files changed, 62 insertions(+), 41 deletions(-) diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index 16b3fcd2f8c..53e684e34bd 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -239,7 +239,6 @@ fn dylib_dependency_formats(&self, cnum: CrateNum) fn export_macros(&self, cnum: CrateNum); fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>; fn missing_lang_items(&self, cnum: CrateNum) -> Vec; - fn is_staged_api(&self, cnum: CrateNum) -> bool; fn is_allocator(&self, cnum: CrateNum) -> bool; fn is_panic_runtime(&self, cnum: CrateNum) -> bool; fn is_compiler_builtins(&self, cnum: CrateNum) -> bool; @@ -368,7 +367,6 @@ fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)> { bug!("lang_items") } fn missing_lang_items(&self, cnum: CrateNum) -> Vec { bug!("missing_lang_items") } - fn is_staged_api(&self, cnum: CrateNum) -> bool { bug!("is_staged_api") } fn dep_kind(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") } fn export_macros(&self, cnum: CrateNum) { bug!("export_macros") } fn is_allocator(&self, cnum: CrateNum) -> bool { bug!("is_allocator") } diff --git a/src/librustc/middle/stability.rs b/src/librustc/middle/stability.rs index 198f7420f5d..d74877e355a 100644 --- a/src/librustc/middle/stability.rs +++ b/src/librustc/middle/stability.rs @@ -13,12 +13,12 @@ pub use self::StabilityLevel::*; -use hir::map as hir_map; use lint; use hir::def::Def; use hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, DefIndex, LOCAL_CRATE}; use ty::{self, TyCtxt}; use middle::privacy::AccessLevels; +use session::Session; use syntax::symbol::Symbol; use syntax_pos::{Span, DUMMY_SP}; use syntax::ast; @@ -123,7 +123,7 @@ fn annotate(&mut self, id: NodeId, attrs: &[Attribute], item_sp: Span, kind: AnnotationKind, visit_children: F) where F: FnOnce(&mut Self) { - if self.index.staged_api[&LOCAL_CRATE] && self.tcx.sess.features.borrow().staged_api { + if self.index.staged_api[&LOCAL_CRATE] { debug!("annotate(id = {:?}, attrs = {:?})", id, attrs); if let Some(..) = attr::find_deprecation(self.tcx.sess.diagnostic(), attrs, item_sp) { self.tcx.sess.span_err(item_sp, "`#[deprecated]` cannot be used in staged api, \ @@ -390,20 +390,36 @@ pub fn build(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>) { parent_depr: None, in_trait_impl: false, }; + + // If the `-Z force-unstable-if-unmarked` flag is passed then we provide + // a parent stability annotation which indicates that this is private + // with the `rustc_private` feature. This is intended for use when + // compiling librustc crates themselves so we can leverage crates.io + // while maintaining the invariant that all sysroot crates are unstable + // by default and are unable to be used. + if tcx.sess.opts.debugging_opts.force_unstable_if_unmarked { + let reason = "this crate is being loaded from the sysroot, and \ + unstable location; did you mean to load this crate \ + from crates.io via `Cargo.toml` instead?"; + let stability = tcx.intern_stability(Stability { + level: attr::StabilityLevel::Unstable { + reason: Some(Symbol::intern(reason)), + issue: 27812, + }, + feature: Symbol::intern("rustc_private"), + rustc_depr: None, + }); + annotator.parent_stab = Some(stability); + } + annotator.annotate(ast::CRATE_NODE_ID, &krate.attrs, krate.span, AnnotationKind::Required, |v| intravisit::walk_crate(v, krate)); } - pub fn new(hir_map: &hir_map::Map) -> Index<'tcx> { - let krate = hir_map.krate(); - - let mut is_staged_api = false; - for attr in &krate.attrs { - if attr.path == "stable" || attr.path == "unstable" { - is_staged_api = true; - break - } - } + pub fn new(sess: &Session) -> Index<'tcx> { + let is_staged_api = + sess.opts.debugging_opts.force_unstable_if_unmarked || + sess.features.borrow().staged_api; let mut staged_api = FxHashMap(); staged_api.insert(LOCAL_CRATE, is_staged_api); @@ -496,8 +512,10 @@ pub fn check_stability(self, def_id: DefId, id: NodeId, span: Span) { } } - let is_staged_api = *self.stability.borrow_mut().staged_api.entry(def_id.krate) - .or_insert_with(|| self.sess.cstore.is_staged_api(def_id.krate)); + let is_staged_api = self.lookup_stability(DefId { + index: CRATE_DEF_INDEX, + ..def_id + }).is_some(); if !is_staged_api { return; } @@ -530,15 +548,32 @@ pub fn check_stability(self, def_id: DefId, id: NodeId, span: Span) { match stability { Some(&Stability { level: attr::Unstable {ref reason, issue}, ref feature, .. }) => { - if !self.stability.borrow().active_features.contains(feature) { - let msg = match *reason { - Some(ref r) => format!("use of unstable library feature '{}': {}", - feature.as_str(), &r), - None => format!("use of unstable library feature '{}'", &feature) - }; - emit_feature_err(&self.sess.parse_sess, &feature.as_str(), span, - GateIssue::Library(Some(issue)), &msg); + if self.stability.borrow().active_features.contains(feature) { + return } + + // When we're compiling the compiler itself we may pull in + // crates from crates.io, but those crates may depend on other + // crates also pulled in from crates.io. We want to ideally be + // able to compile everything without requiring upstream + // modifications, so in the case that this looks like a + // rustc_private crate (e.g. a compiler crate) and we also have + // the `-Z force-unstable-if-unmarked` flag present (we're + // compiling a compiler crate), then let this missing feature + // annotation slide. + if *feature == "rustc_private" && issue == 27812 { + if self.sess.opts.debugging_opts.force_unstable_if_unmarked { + return + } + } + + let msg = match *reason { + Some(ref r) => format!("use of unstable library feature '{}': {}", + feature.as_str(), &r), + None => format!("use of unstable library feature '{}'", &feature) + }; + emit_feature_err(&self.sess.parse_sess, &feature.as_str(), span, + GateIssue::Library(Some(issue)), &msg); } Some(_) => { // Stable APIs are always ok to call and deprecated APIs are @@ -658,7 +693,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) { let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE); - if tcx.stability.borrow().staged_api[&LOCAL_CRATE] && tcx.sess.features.borrow().staged_api { + if tcx.stability.borrow().staged_api[&LOCAL_CRATE] { let krate = tcx.hir.krate(); let mut missing = MissingStabilityAnnotations { tcx: tcx, diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 75bc940625d..884a71f0d32 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -1027,6 +1027,8 @@ fn parse_optimization_fuel(slot: &mut Option<(String, u64)>, v: Option<&str>) -> "add a source pattern to the file path remapping config"), remap_path_prefix_to: Vec = (vec![], parse_string_push, [TRACKED], "add a mapping target to the file path remapping config"), + force_unstable_if_unmarked: bool = (false, parse_bool, [TRACKED], + "force all crates to be `rustc_private` unstable"), } pub fn default_lib_output() -> CrateType { diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 5f14890665c..8fddbe110b0 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -882,7 +882,7 @@ macro_rules! try_with_f { "static item recursion checking", || static_recursion::check_crate(sess, &hir_map))?; - let index = stability::Index::new(&hir_map); + let index = stability::Index::new(&sess); let mut local_providers = ty::maps::Providers::default(); borrowck::provide(&mut local_providers); diff --git a/src/librustc_driver/test.rs b/src/librustc_driver/test.rs index 8b95be00fa7..3b4f2560fc5 100644 --- a/src/librustc_driver/test.rs +++ b/src/librustc_driver/test.rs @@ -138,7 +138,7 @@ fn test_env(source_string: &str, // run just enough stuff to build a tcx: let lang_items = lang_items::collect_language_items(&sess, &hir_map); let named_region_map = resolve_lifetime::krate(&sess, &hir_map); - let index = stability::Index::new(&hir_map); + let index = stability::Index::new(&sess); TyCtxt::create_and_enter(&sess, ty::maps::Providers::default(), ty::maps::Providers::default(), diff --git a/src/librustc_metadata/cstore.rs b/src/librustc_metadata/cstore.rs index 8d53e7d49ee..c12b4209675 100644 --- a/src/librustc_metadata/cstore.rs +++ b/src/librustc_metadata/cstore.rs @@ -269,15 +269,6 @@ pub fn disambiguator(&self) -> Symbol { self.root.disambiguator } - pub fn is_staged_api(&self, dep_graph: &DepGraph) -> bool { - for attr in self.get_item_attrs(CRATE_DEF_INDEX, dep_graph).iter() { - if attr.path == "stable" || attr.path == "unstable" { - return true; - } - } - false - } - pub fn is_allocator(&self, dep_graph: &DepGraph) -> bool { let attrs = self.get_item_attrs(CRATE_DEF_INDEX, dep_graph); attr::contains_name(&attrs, "allocator") diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index 6fa6a868605..c7da535faa2 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -267,11 +267,6 @@ fn missing_lang_items(&self, cnum: CrateNum) self.get_crate_data(cnum).get_missing_lang_items(&self.dep_graph) } - fn is_staged_api(&self, cnum: CrateNum) -> bool - { - self.get_crate_data(cnum).is_staged_api(&self.dep_graph) - } - fn is_allocator(&self, cnum: CrateNum) -> bool { self.get_crate_data(cnum).is_allocator(&self.dep_graph) -- 2.44.0