From b55453dbad4fb032d5b00c218146e1b053774a08 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 9 May 2022 15:18:53 -0700 Subject: add opt in attribute for stable-in-unstable items --- compiler/rustc_attr/src/builtin.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_attr/src') diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index ce7c0eb72cd..46643df84cc 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -137,7 +137,7 @@ impl ConstStability { pub enum StabilityLevel { // Reason for the current stability level and the relevant rust-lang issue Unstable { reason: Option, issue: Option, is_soft: bool }, - Stable { since: Symbol }, + Stable { since: Symbol, allowed_through_unstable_modules: bool }, } impl StabilityLevel { @@ -172,6 +172,7 @@ where let mut stab: Option<(Stability, Span)> = None; let mut const_stab: Option<(ConstStability, Span)> = None; let mut promotable = false; + let mut allowed_through_unstable_modules = false; let diagnostic = &sess.parse_sess.span_diagnostic; @@ -182,6 +183,7 @@ where sym::unstable, sym::stable, sym::rustc_promotable, + sym::rustc_allowed_through_unstable_modules, ] .iter() .any(|&s| attr.has_name(s)) @@ -193,6 +195,8 @@ where if attr.has_name(sym::rustc_promotable) { promotable = true; + } else if attr.has_name(sym::rustc_allowed_through_unstable_modules) { + allowed_through_unstable_modules = true; } // attributes with data else if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta { @@ -406,7 +410,7 @@ where match (feature, since) { (Some(feature), Some(since)) => { - let level = Stable { since }; + let level = Stable { since, allowed_through_unstable_modules: false }; if sym::stable == meta_name { stab = Some((Stability { level, feature }, attr.span)); } else { @@ -447,6 +451,27 @@ where } } + if allowed_through_unstable_modules { + if let Some(( + Stability { + level: StabilityLevel::Stable { ref mut allowed_through_unstable_modules, .. }, + .. + }, + _, + )) = stab + { + *allowed_through_unstable_modules = true; + } else { + struct_span_err!( + diagnostic, + item_sp, + E0788, + "`rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute" + ) + .emit(); + } + } + (stab, const_stab) } -- cgit 1.4.1-3-g733a5 From e4e6b1ebc6e75b44f5543eb72964f3716b0b7d07 Mon Sep 17 00:00:00 2001 From: Jane Losare-Lusby Date: Thu, 7 Jul 2022 22:29:38 +0000 Subject: fixes post rebase --- compiler/rustc_attr/src/builtin.rs | 2 +- compiler/rustc_error_codes/src/error_codes.rs | 2 +- compiler/rustc_middle/src/middle/stability.rs | 2 ++ compiler/rustc_passes/src/stability.rs | 2 +- src/test/ui-fulldeps/rustc-encodable-stability.rs | 12 ++++++++++++ .../stability-attribute/rustc-encodable-stability.rs | 12 ------------ .../ui/stability-attribute/allow-through-unstable-misuse.rs | 8 -------- .../ui/stability-attribute/allowed-through-unstable.stderr | 2 +- src/test/ui/stability-attribute/rustc-encodable-stability.rs | 8 -------- src/tools/tidy/src/error_codes_check.rs | 2 +- 10 files changed, 19 insertions(+), 33 deletions(-) create mode 100644 src/test/ui-fulldeps/rustc-encodable-stability.rs delete mode 100644 src/test/ui-fulldeps/stability-attribute/rustc-encodable-stability.rs delete mode 100644 src/test/ui/stability-attribute/allow-through-unstable-misuse.rs delete mode 100644 src/test/ui/stability-attribute/rustc-encodable-stability.rs (limited to 'compiler/rustc_attr/src') diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index 46643df84cc..6397d4e236a 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -465,7 +465,7 @@ where struct_span_err!( diagnostic, item_sp, - E0788, + E0789, "`rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute" ) .emit(); diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index 56f29dcc5ce..977318b8589 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -644,5 +644,5 @@ E0788: include_str!("./error_codes/E0788.md"), // E0721, // `await` keyword // E0723, // unstable feature in `const` context // E0738, // Removed; errored on `#[track_caller] fn`s in `extern "Rust" { ... }`. - E0788, // rustc_allowed_through_unstable_modules without stability attribute + E0789, // rustc_allowed_through_unstable_modules without stability attribute } diff --git a/compiler/rustc_middle/src/middle/stability.rs b/compiler/rustc_middle/src/middle/stability.rs index 9f00c6ac112..91f6cdd0877 100644 --- a/compiler/rustc_middle/src/middle/stability.rs +++ b/compiler/rustc_middle/src/middle/stability.rs @@ -492,6 +492,8 @@ impl<'tcx> TyCtxt<'tcx> { /// If so, and `id` is not `None`, a deprecated lint attached to `id` will be emitted. /// /// Pass `AllowUnstable::Yes` to `allow_unstable` to force an unstable item to be allowed. Deprecation warnings will be emitted normally. + /// + /// Returns `true` if item is allowed aka, stable or unstable under an enabled feature. pub fn check_stability_allow_unstable( self, def_id: DefId, diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index cf23e0ae5ef..ef7c3fc0966 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -858,7 +858,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> { } else { AllowUnstable::No }, - ) + ); } } } diff --git a/src/test/ui-fulldeps/rustc-encodable-stability.rs b/src/test/ui-fulldeps/rustc-encodable-stability.rs new file mode 100644 index 00000000000..072b9387119 --- /dev/null +++ b/src/test/ui-fulldeps/rustc-encodable-stability.rs @@ -0,0 +1,12 @@ +// edition:2018 +#![allow(deprecated)] +#![feature(rustc_private)] + +extern crate rustc_serialize; + +#[derive(RustcDecodable, RustcEncodable)] +struct ArbitraryTestType { + +} + +fn main() {} diff --git a/src/test/ui-fulldeps/stability-attribute/rustc-encodable-stability.rs b/src/test/ui-fulldeps/stability-attribute/rustc-encodable-stability.rs deleted file mode 100644 index 072b9387119..00000000000 --- a/src/test/ui-fulldeps/stability-attribute/rustc-encodable-stability.rs +++ /dev/null @@ -1,12 +0,0 @@ -// edition:2018 -#![allow(deprecated)] -#![feature(rustc_private)] - -extern crate rustc_serialize; - -#[derive(RustcDecodable, RustcEncodable)] -struct ArbitraryTestType { - -} - -fn main() {} diff --git a/src/test/ui/stability-attribute/allow-through-unstable-misuse.rs b/src/test/ui/stability-attribute/allow-through-unstable-misuse.rs deleted file mode 100644 index 7f942309e04..00000000000 --- a/src/test/ui/stability-attribute/allow-through-unstable-misuse.rs +++ /dev/null @@ -1,8 +0,0 @@ -#![crate_type = "lib"] -#![feature(staged_api)] -#![feature(rustc_attrs)] -#![stable(feature = "foo", since = "1.0.0")] - -#[unstable(feature = "bar", issue = "none")] -#[rustc_allowed_through_unstable_modules] -pub struct UnstableType(()); diff --git a/src/test/ui/stability-attribute/allowed-through-unstable.stderr b/src/test/ui/stability-attribute/allowed-through-unstable.stderr index 70b1dc3010a..132c00b89b2 100644 --- a/src/test/ui/stability-attribute/allowed-through-unstable.stderr +++ b/src/test/ui/stability-attribute/allowed-through-unstable.stderr @@ -1,5 +1,5 @@ error[E0658]: use of unstable library feature 'unstable_test_feature' - --> $DIR/allowed-through-unstable.rs:10:5 + --> $DIR/allowed-through-unstable.rs:9:5 | LL | use allowed_through_unstable_core::unstable_module::NewStableTraitNotAllowedThroughUnstable; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/test/ui/stability-attribute/rustc-encodable-stability.rs b/src/test/ui/stability-attribute/rustc-encodable-stability.rs deleted file mode 100644 index 18eff27d9a5..00000000000 --- a/src/test/ui/stability-attribute/rustc-encodable-stability.rs +++ /dev/null @@ -1,8 +0,0 @@ -// edition:2018 -#![allow(deprecated)] -extern crate rustc_serialize; - -#[derive(RustcDecodable, RustcEncodable)] -struct ArbitraryTestType(()); - -fn main() {} diff --git a/src/tools/tidy/src/error_codes_check.rs b/src/tools/tidy/src/error_codes_check.rs index e56ce3329cc..4ffa1fa8b28 100644 --- a/src/tools/tidy/src/error_codes_check.rs +++ b/src/tools/tidy/src/error_codes_check.rs @@ -11,7 +11,7 @@ use regex::Regex; // A few of those error codes can't be tested but all the others can and *should* be tested! const EXEMPTED_FROM_TEST: &[&str] = &[ "E0279", "E0313", "E0377", "E0461", "E0462", "E0465", "E0476", "E0490", "E0514", "E0519", - "E0523", "E0554", "E0640", "E0717", "E0729", + "E0523", "E0554", "E0640", "E0717", "E0729", "E0789", ]; // Some error codes don't have any tests apparently... -- cgit 1.4.1-3-g733a5