Skip to content

Commit 24699bc

Browse files
committed
Auto merge of rust-lang#95956 - yaahc:stable-in-unstable, r=cjgillot
Support unstable moves via stable in unstable items part of https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/moving.20items.20to.20core.20unstably and a blocker of rust-lang#90328. The libs-api team needs the ability to move an already stable item to a new location unstably, in this case for Error in core. Otherwise these changes are insta-stable making them much harder to merge. This PR attempts to solve the problem by checking the stability of path segments as well as the last item in the path itself, which is currently the only thing checked.
2 parents f1a8854 + 3e2c5b5 commit 24699bc

34 files changed

+304
-33
lines changed

compiler/rustc_attr/src/builtin.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl ConstStability {
137137
pub enum StabilityLevel {
138138
// Reason for the current stability level and the relevant rust-lang issue
139139
Unstable { reason: Option<Symbol>, issue: Option<NonZeroU32>, is_soft: bool },
140-
Stable { since: Symbol },
140+
Stable { since: Symbol, allowed_through_unstable_modules: bool },
141141
}
142142

143143
impl StabilityLevel {
@@ -172,6 +172,7 @@ where
172172
let mut stab: Option<(Stability, Span)> = None;
173173
let mut const_stab: Option<(ConstStability, Span)> = None;
174174
let mut promotable = false;
175+
let mut allowed_through_unstable_modules = false;
175176

176177
let diagnostic = &sess.parse_sess.span_diagnostic;
177178

@@ -182,6 +183,7 @@ where
182183
sym::unstable,
183184
sym::stable,
184185
sym::rustc_promotable,
186+
sym::rustc_allowed_through_unstable_modules,
185187
]
186188
.iter()
187189
.any(|&s| attr.has_name(s))
@@ -193,6 +195,8 @@ where
193195

194196
if attr.has_name(sym::rustc_promotable) {
195197
promotable = true;
198+
} else if attr.has_name(sym::rustc_allowed_through_unstable_modules) {
199+
allowed_through_unstable_modules = true;
196200
}
197201
// attributes with data
198202
else if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta {
@@ -406,7 +410,7 @@ where
406410

407411
match (feature, since) {
408412
(Some(feature), Some(since)) => {
409-
let level = Stable { since };
413+
let level = Stable { since, allowed_through_unstable_modules: false };
410414
if sym::stable == meta_name {
411415
stab = Some((Stability { level, feature }, attr.span));
412416
} else {
@@ -447,6 +451,27 @@ where
447451
}
448452
}
449453

454+
if allowed_through_unstable_modules {
455+
if let Some((
456+
Stability {
457+
level: StabilityLevel::Stable { ref mut allowed_through_unstable_modules, .. },
458+
..
459+
},
460+
_,
461+
)) = stab
462+
{
463+
*allowed_through_unstable_modules = true;
464+
} else {
465+
struct_span_err!(
466+
diagnostic,
467+
item_sp,
468+
E0789,
469+
"`rustc_allowed_through_unstable_modules` attribute must be paired with a `stable` attribute"
470+
)
471+
.emit();
472+
}
473+
}
474+
450475
(stab, const_stab)
451476
}
452477

compiler/rustc_error_codes/src/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -644,4 +644,5 @@ E0788: include_str!("./error_codes/E0788.md"),
644644
// E0721, // `await` keyword
645645
// E0723, // unstable feature in `const` context
646646
// E0738, // Removed; errored on `#[track_caller] fn`s in `extern "Rust" { ... }`.
647+
E0789, // rustc_allowed_through_unstable_modules without stability attribute
647648
}

compiler/rustc_feature/src/builtin_attrs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
512512
allow_internal_unsafe, Normal, template!(Word), WarnFollowing,
513513
"allow_internal_unsafe side-steps the unsafe_code lint",
514514
),
515+
rustc_attr!(rustc_allowed_through_unstable_modules, Normal, template!(Word), WarnFollowing,
516+
"rustc_allowed_through_unstable_modules special cases accidental stabilizations of stable items \
517+
through unstable paths"),
515518

516519
// ==========================================================================
517520
// Internal attributes: Type system related:

compiler/rustc_middle/src/middle/stability.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,15 @@ impl<'tcx> TyCtxt<'tcx> {
471471
///
472472
/// This function will also check if the item is deprecated.
473473
/// If so, and `id` is not `None`, a deprecated lint attached to `id` will be emitted.
474+
///
475+
/// Returns `true` if item is allowed aka, stable or unstable under an enabled feature.
474476
pub fn check_stability(
475477
self,
476478
def_id: DefId,
477479
id: Option<HirId>,
478480
span: Span,
479481
method_span: Option<Span>,
480-
) {
482+
) -> bool {
481483
self.check_stability_allow_unstable(def_id, id, span, method_span, AllowUnstable::No)
482484
}
483485

@@ -490,14 +492,16 @@ impl<'tcx> TyCtxt<'tcx> {
490492
/// If so, and `id` is not `None`, a deprecated lint attached to `id` will be emitted.
491493
///
492494
/// Pass `AllowUnstable::Yes` to `allow_unstable` to force an unstable item to be allowed. Deprecation warnings will be emitted normally.
495+
///
496+
/// Returns `true` if item is allowed aka, stable or unstable under an enabled feature.
493497
pub fn check_stability_allow_unstable(
494498
self,
495499
def_id: DefId,
496500
id: Option<HirId>,
497501
span: Span,
498502
method_span: Option<Span>,
499503
allow_unstable: AllowUnstable,
500-
) {
504+
) -> bool {
501505
self.check_optional_stability(
502506
def_id,
503507
id,
@@ -516,6 +520,8 @@ impl<'tcx> TyCtxt<'tcx> {
516520
/// missing stability attributes (not necessarily just emit a `bug!`). This is necessary
517521
/// for default generic parameters, which only have stability attributes if they were
518522
/// added after the type on which they're defined.
523+
///
524+
/// Returns `true` if item is allowed aka, stable or unstable under an enabled feature.
519525
pub fn check_optional_stability(
520526
self,
521527
def_id: DefId,
@@ -524,13 +530,16 @@ impl<'tcx> TyCtxt<'tcx> {
524530
method_span: Option<Span>,
525531
allow_unstable: AllowUnstable,
526532
unmarked: impl FnOnce(Span, DefId),
527-
) {
533+
) -> bool {
528534
let soft_handler = |lint, span, msg: &_| {
529535
self.struct_span_lint_hir(lint, id.unwrap_or(hir::CRATE_HIR_ID), span, |lint| {
530536
lint.build(msg).emit();
531537
})
532538
};
533-
match self.eval_stability_allow_unstable(def_id, id, span, method_span, allow_unstable) {
539+
let eval_result =
540+
self.eval_stability_allow_unstable(def_id, id, span, method_span, allow_unstable);
541+
let is_allowed = matches!(eval_result, EvalResult::Allow);
542+
match eval_result {
534543
EvalResult::Allow => {}
535544
EvalResult::Deny { feature, reason, issue, suggestion, is_soft } => report_unstable(
536545
self.sess,
@@ -544,6 +553,8 @@ impl<'tcx> TyCtxt<'tcx> {
544553
),
545554
EvalResult::Unmarked => unmarked(span, def_id),
546555
}
556+
557+
is_allowed
547558
}
548559

549560
pub fn lookup_deprecation(self, id: DefId) -> Option<Deprecation> {

compiler/rustc_passes/src/check_attr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ impl CheckAttrVisitor<'_> {
139139
| sym::rustc_const_stable
140140
| sym::unstable
141141
| sym::stable
142+
| sym::rustc_allowed_through_unstable_modules
142143
| sym::rustc_promotable => self.check_stability_promotable(&attr, span, target),
143144
_ => true,
144145
};

compiler/rustc_passes/src/stability.rs

+48-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A pass that annotates every item and method with its stability level,
22
//! propagating default levels lexically from parent to children ast nodes.
33
4+
use attr::StabilityLevel;
45
use rustc_attr::{self as attr, ConstStability, Stability};
56
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
67
use rustc_errors::struct_span_err;
@@ -223,7 +224,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
223224

224225
// Check if deprecated_since < stable_since. If it is,
225226
// this is *almost surely* an accident.
226-
if let (&Some(dep_since), &attr::Stable { since: stab_since }) =
227+
if let (&Some(dep_since), &attr::Stable { since: stab_since, .. }) =
227228
(&depr.as_ref().and_then(|(d, _)| d.since), &stab.level)
228229
{
229230
// Explicit version of iter::order::lt to handle parse errors properly
@@ -773,7 +774,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
773774
fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, id: hir::HirId) {
774775
if let Some(def_id) = path.res.opt_def_id() {
775776
let method_span = path.segments.last().map(|s| s.ident.span);
776-
self.tcx.check_stability_allow_unstable(
777+
let item_is_allowed = self.tcx.check_stability_allow_unstable(
777778
def_id,
778779
Some(id),
779780
path.span,
@@ -783,8 +784,52 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
783784
} else {
784785
AllowUnstable::No
785786
},
786-
)
787+
);
788+
789+
let is_allowed_through_unstable_modules = |def_id| {
790+
self.tcx
791+
.lookup_stability(def_id)
792+
.map(|stab| match stab.level {
793+
StabilityLevel::Stable { allowed_through_unstable_modules, .. } => {
794+
allowed_through_unstable_modules
795+
}
796+
_ => false,
797+
})
798+
.unwrap_or(false)
799+
};
800+
801+
if item_is_allowed && !is_allowed_through_unstable_modules(def_id) {
802+
// Check parent modules stability as well if the item the path refers to is itself
803+
// stable. We only emit warnings for unstable path segments if the item is stable
804+
// or allowed because stability is often inherited, so the most common case is that
805+
// both the segments and the item are unstable behind the same feature flag.
806+
//
807+
// We check here rather than in `visit_path_segment` to prevent visiting the last
808+
// path segment twice
809+
//
810+
// We include special cases via #[rustc_allowed_through_unstable_modules] for items
811+
// that were accidentally stabilized through unstable paths before this check was
812+
// added, such as `core::intrinsics::transmute`
813+
let parents = path.segments.iter().rev().skip(1);
814+
for path_segment in parents {
815+
if let Some(def_id) = path_segment.res.as_ref().and_then(Res::opt_def_id) {
816+
// use `None` for id to prevent deprecation check
817+
self.tcx.check_stability_allow_unstable(
818+
def_id,
819+
None,
820+
path.span,
821+
None,
822+
if is_unstable_reexport(self.tcx, id) {
823+
AllowUnstable::Yes
824+
} else {
825+
AllowUnstable::No
826+
},
827+
);
828+
}
829+
}
830+
}
787831
}
832+
788833
intravisit::walk_path(self, path)
789834
}
790835
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,7 @@ symbols! {
11911191
rustc_allocator_nounwind,
11921192
rustc_allow_const_fn_unstable,
11931193
rustc_allow_incoherent_impl,
1194+
rustc_allowed_through_unstable_modules,
11941195
rustc_attrs,
11951196
rustc_box,
11961197
rustc_builtin_macro,

compiler/rustc_typeck/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
439439
// as the rest of the type. As such, we ignore missing
440440
// stability attributes.
441441
},
442-
)
442+
);
443443
}
444444
if let (hir::TyKind::Infer, false) = (&ty.kind, self.astconv.allow_ty_infer()) {
445445
self.inferred_params.push(ty.span);

library/core/src/intrinsics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,7 @@ extern "rust-intrinsic" {
14571457
/// }
14581458
/// ```
14591459
#[stable(feature = "rust1", since = "1.0.0")]
1460+
#[cfg_attr(not(bootstrap), rustc_allowed_through_unstable_modules)]
14601461
#[rustc_const_stable(feature = "const_transmute", since = "1.56.0")]
14611462
#[rustc_diagnostic_item = "transmute"]
14621463
pub fn transmute<T, U>(e: T) -> U;
@@ -2649,6 +2650,7 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
26492650
/// Here is an example of how this could cause a problem:
26502651
/// ```no_run
26512652
/// #![feature(const_eval_select)]
2653+
/// #![feature(core_intrinsics)]
26522654
/// use std::hint::unreachable_unchecked;
26532655
/// use std::intrinsics::const_eval_select;
26542656
///

library/core/src/macros/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ pub(crate) mod builtin {
15371537
/// Unstable implementation detail of the `rustc` compiler, do not use.
15381538
#[rustc_builtin_macro]
15391539
#[stable(feature = "rust1", since = "1.0.0")]
1540-
#[allow_internal_unstable(core_intrinsics, libstd_sys_internals)]
1540+
#[allow_internal_unstable(core_intrinsics, libstd_sys_internals, rt)]
15411541
#[deprecated(since = "1.52.0", note = "rustc-serialize is deprecated and no longer supported")]
15421542
#[doc(hidden)] // While technically stable, using it is unstable, and deprecated. Hide it.
15431543
pub macro RustcDecodable($item:item) {
@@ -1547,7 +1547,7 @@ pub(crate) mod builtin {
15471547
/// Unstable implementation detail of the `rustc` compiler, do not use.
15481548
#[rustc_builtin_macro]
15491549
#[stable(feature = "rust1", since = "1.0.0")]
1550-
#[allow_internal_unstable(core_intrinsics)]
1550+
#[allow_internal_unstable(core_intrinsics, rt)]
15511551
#[deprecated(since = "1.52.0", note = "rustc-serialize is deprecated and no longer supported")]
15521552
#[doc(hidden)] // While technically stable, using it is unstable, and deprecated. Hide it.
15531553
pub macro RustcEncodable($item:item) {

library/core/tests/unicode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[test]
22
pub fn version() {
3-
let (major, _minor, _update) = core::unicode::UNICODE_VERSION;
3+
let (major, _minor, _update) = core::char::UNICODE_VERSION;
44
assert!(major >= 10);
55
}

library/std/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@
214214
#![cfg_attr(not(bootstrap), deny(ffi_unwind_calls))]
215215
// std may use features in a platform-specific way
216216
#![allow(unused_features)]
217-
#![cfg_attr(test, feature(internal_output_capture, print_internals, update_panic_count))]
217+
#![cfg_attr(test, feature(internal_output_capture, print_internals, update_panic_count, rt))]
218218
#![cfg_attr(
219219
all(target_vendor = "fortanix", target_env = "sgx"),
220220
feature(slice_index_methods, coerce_unsized, sgx_platform)
@@ -297,6 +297,7 @@
297297
// Library features (alloc):
298298
#![feature(alloc_layout_extra)]
299299
#![feature(alloc_c_string)]
300+
#![feature(alloc_ffi)]
300301
#![feature(allocator_api)]
301302
#![feature(get_mut_unchecked)]
302303
#![feature(map_try_insert)]

library/std/src/panic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::thread::Result;
1111

1212
#[doc(hidden)]
1313
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
14-
#[allow_internal_unstable(libstd_sys_internals, const_format_args, core_panic)]
14+
#[allow_internal_unstable(libstd_sys_internals, const_format_args, core_panic, rt)]
1515
#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
1616
#[rustc_macro_transparency = "semitransparent"]
1717
pub macro panic_2015 {

src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ fn render_stability_since_raw(
854854
}
855855

856856
let const_title_and_stability = match const_stability {
857-
Some(ConstStability { level: StabilityLevel::Stable { since }, .. })
857+
Some(ConstStability { level: StabilityLevel::Stable { since, .. }, .. })
858858
if Some(since) != containing_const_ver =>
859859
{
860860
Some((format!("const since {}", since), format!("const: {}", since)))

src/test/codegen/intrinsics/const_eval_select.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#![crate_type = "lib"]
44
#![feature(const_eval_select)]
5+
#![feature(core_intrinsics)]
56

67
use std::intrinsics::const_eval_select;
78

src/test/ui/intrinsics/const-eval-select-bad.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(const_eval_select)]
2+
#![feature(core_intrinsics)]
23

34
use std::intrinsics::const_eval_select;
45

0 commit comments

Comments
 (0)