Skip to content

Commit 7b43cfc

Browse files
committed
Auto merge of #93695 - matthiaskrgr:rollup-zslgooo, r=matthiaskrgr
Rollup of 2 pull requests Successful merges: - #90998 (Require const stability attribute on all stable functions that are `const`) - #93489 (Mark the panic_no_unwind lang item as nounwind) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents f624427 + 4695c21 commit 7b43cfc

File tree

10 files changed

+62
-26
lines changed

10 files changed

+62
-26
lines changed

compiler/rustc_passes/src/stability.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -577,17 +577,21 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
577577
}
578578

579579
fn check_missing_const_stability(&self, def_id: LocalDefId, span: Span) {
580-
let stab_map = self.tcx.stability();
581-
let stab = stab_map.local_stability(def_id);
582-
if stab.map_or(false, |stab| stab.level.is_stable()) {
583-
let const_stab = stab_map.local_const_stability(def_id);
584-
if const_stab.is_none() {
585-
self.tcx.sess.span_err(
586-
span,
587-
"`#[stable]` const functions must also be either \
588-
`#[rustc_const_stable]` or `#[rustc_const_unstable]`",
589-
);
590-
}
580+
if !self.tcx.features().staged_api {
581+
return;
582+
}
583+
584+
let is_const = self.tcx.is_const_fn(def_id.to_def_id());
585+
let is_stable = self
586+
.tcx
587+
.lookup_stability(def_id)
588+
.map_or(false, |stability| stability.level.is_stable());
589+
let missing_const_stability_attribute = self.tcx.lookup_const_stability(def_id).is_none();
590+
let is_reachable = self.access_levels.is_reachable(def_id);
591+
592+
if is_const && is_stable && missing_const_stability_attribute && is_reachable {
593+
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
594+
self.tcx.sess.span_err(span, &format!("{descr} has missing const stability attribute"));
591595
}
592596
}
593597
}
@@ -612,13 +616,8 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
612616
self.check_missing_stability(i.def_id, i.span);
613617
}
614618

615-
// Ensure `const fn` that are `stable` have one of `rustc_const_unstable` or
616-
// `rustc_const_stable`.
617-
if self.tcx.features().staged_api
618-
&& matches!(&i.kind, hir::ItemKind::Fn(sig, ..) if sig.header.is_const())
619-
{
620-
self.check_missing_const_stability(i.def_id, i.span);
621-
}
619+
// Ensure stable `const fn` have a const stability attribute.
620+
self.check_missing_const_stability(i.def_id, i.span);
622621

623622
intravisit::walk_item(self, i)
624623
}
@@ -632,6 +631,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
632631
let impl_def_id = self.tcx.hir().get_parent_item(ii.hir_id());
633632
if self.tcx.impl_trait_ref(impl_def_id).is_none() {
634633
self.check_missing_stability(ii.def_id, ii.span);
634+
self.check_missing_const_stability(ii.def_id, ii.span);
635635
}
636636
intravisit::walk_impl_item(self, ii);
637637
}

compiler/rustc_typeck/src/collect.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2778,6 +2778,13 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
27782778
}
27792779
}
27802780

2781+
// The panic_no_unwind function called by TerminatorKind::Abort will never
2782+
// unwind. If the panic handler that it invokes unwind then it will simply
2783+
// call the panic handler again.
2784+
if Some(id) == tcx.lang_items().panic_no_unwind() {
2785+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
2786+
}
2787+
27812788
let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);
27822789

27832790
let mut inline_span = None;

library/core/src/array/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ impl<T, const N: usize> [T; N] {
512512

513513
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
514514
#[stable(feature = "array_as_slice", since = "1.57.0")]
515+
#[rustc_const_stable(feature = "array_as_slice", since = "1.57.0")]
515516
pub const fn as_slice(&self) -> &[T] {
516517
self
517518
}

library/core/src/cell.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,7 @@ impl<T: ?Sized> UnsafeCell<T> {
19591959
/// ```
19601960
#[inline(always)]
19611961
#[stable(feature = "unsafe_cell_raw_get", since = "1.56.0")]
1962+
#[rustc_const_stable(feature = "unsafe_cell_raw_get", since = "1.56.0")]
19621963
pub const fn raw_get(this: *const Self) -> *mut T {
19631964
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of
19641965
// #[repr(transparent)]. This exploits libstd's special status, there is

library/core/src/num/int_macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,7 @@ macro_rules! int_impl {
10641064
///
10651065
/// ```
10661066
#[stable(feature = "saturating_div", since = "1.58.0")]
1067+
#[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
10671068
#[must_use = "this returns the result of the operation, \
10681069
without modifying the original"]
10691070
#[inline]

library/core/src/num/nonzero.rs

+1
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,7 @@ macro_rules! nonzero_unsigned_is_power_of_two {
972972
/// ```
973973
#[must_use]
974974
#[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
975+
#[rustc_const_stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
975976
#[inline]
976977
pub const fn is_power_of_two(self) -> bool {
977978
// LLVM 11 normalizes `unchecked_sub(x, 1) & x == 0` to the implementation seen here.

library/core/src/num/uint_macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,7 @@ macro_rules! uint_impl {
11321132
///
11331133
/// ```
11341134
#[stable(feature = "saturating_div", since = "1.58.0")]
1135+
#[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
11351136
#[must_use = "this returns the result of the operation, \
11361137
without modifying the original"]
11371138
#[inline]

library/core/src/panic/panic_info.rs

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ impl<'a> PanicInfo<'a> {
136136
/// This is true for most kinds of panics with the exception of panics
137137
/// caused by trying to unwind out of a `Drop` implementation or a function
138138
/// whose ABI does not support unwinding.
139+
///
140+
/// It is safe for a panic handler to unwind even when this function returns
141+
/// true, however this will simply cause the panic handler to be called
142+
/// again.
139143
#[must_use]
140144
#[unstable(feature = "panic_can_unwind", issue = "92988")]
141145
pub fn can_unwind(&self) -> bool {
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
#![feature(staged_api)]
2+
#![feature(const_trait_impl)]
3+
#![stable(feature = "stable", since = "1.0.0")]
24

3-
#![stable(feature = "rust1", since = "1.0.0")]
5+
#[stable(feature = "stable", since = "1.0.0")]
6+
pub const fn foo() {} //~ ERROR function has missing const stability attribute
47

5-
#[stable(feature = "foo", since = "1.0.0")]
6-
pub const fn foo() {}
7-
//~^ ERROR rustc_const_stable
8+
#[unstable(feature = "unstable", issue = "none")]
9+
pub const fn bar() {} // ok because function is unstable
810

9-
#[unstable(feature = "bar", issue = "none")]
10-
pub const fn bar() {} // ok
11+
#[stable(feature = "stable", since = "1.0.0")]
12+
pub struct Foo;
13+
impl Foo {
14+
#[stable(feature = "stable", since = "1.0.0")]
15+
pub const fn foo() {} //~ ERROR associated function has missing const stability attribute
16+
17+
#[unstable(feature = "unstable", issue = "none")]
18+
pub const fn bar() {} // ok because function is unstable
19+
}
20+
21+
// FIXME Once #![feature(const_trait_impl)] is allowed to be stable, add a test
22+
// for const trait impls. Right now, a "trait methods cannot be stable const fn"
23+
// error is emitted. This occurs prior to the lint being tested here, such that
24+
// the lint cannot currently be tested on this use case.
1125

1226
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
error: `#[stable]` const functions must also be either `#[rustc_const_stable]` or `#[rustc_const_unstable]`
1+
error: function has missing const stability attribute
22
--> $DIR/missing-const-stability.rs:6:1
33
|
44
LL | pub const fn foo() {}
55
| ^^^^^^^^^^^^^^^^^^^^^
66

7-
error: aborting due to previous error
7+
error: associated function has missing const stability attribute
8+
--> $DIR/missing-const-stability.rs:15:5
9+
|
10+
LL | pub const fn foo() {}
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
814

0 commit comments

Comments
 (0)