Skip to content

Rollup of 2 pull requests #93695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,17 +577,21 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
}

fn check_missing_const_stability(&self, def_id: LocalDefId, span: Span) {
let stab_map = self.tcx.stability();
let stab = stab_map.local_stability(def_id);
if stab.map_or(false, |stab| stab.level.is_stable()) {
let const_stab = stab_map.local_const_stability(def_id);
if const_stab.is_none() {
self.tcx.sess.span_err(
span,
"`#[stable]` const functions must also be either \
`#[rustc_const_stable]` or `#[rustc_const_unstable]`",
);
}
if !self.tcx.features().staged_api {
return;
}

let is_const = self.tcx.is_const_fn(def_id.to_def_id());
let is_stable = self
.tcx
.lookup_stability(def_id)
.map_or(false, |stability| stability.level.is_stable());
let missing_const_stability_attribute = self.tcx.lookup_const_stability(def_id).is_none();
let is_reachable = self.access_levels.is_reachable(def_id);

if is_const && is_stable && missing_const_stability_attribute && is_reachable {
let descr = self.tcx.def_kind(def_id).descr(def_id.to_def_id());
self.tcx.sess.span_err(span, &format!("{descr} has missing const stability attribute"));
}
}
}
Expand All @@ -612,13 +616,8 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
self.check_missing_stability(i.def_id, i.span);
}

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

intravisit::walk_item(self, i)
}
Expand All @@ -632,6 +631,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
let impl_def_id = self.tcx.hir().get_parent_item(ii.hir_id());
if self.tcx.impl_trait_ref(impl_def_id).is_none() {
self.check_missing_stability(ii.def_id, ii.span);
self.check_missing_const_stability(ii.def_id, ii.span);
}
intravisit::walk_impl_item(self, ii);
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2778,6 +2778,13 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
}
}

// The panic_no_unwind function called by TerminatorKind::Abort will never
// unwind. If the panic handler that it invokes unwind then it will simply
// call the panic handler again.
if Some(id) == tcx.lang_items().panic_no_unwind() {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
}

let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);

let mut inline_span = None;
Expand Down
1 change: 1 addition & 0 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ impl<T, const N: usize> [T; N] {

/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
#[stable(feature = "array_as_slice", since = "1.57.0")]
#[rustc_const_stable(feature = "array_as_slice", since = "1.57.0")]
pub const fn as_slice(&self) -> &[T] {
self
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,7 @@ impl<T: ?Sized> UnsafeCell<T> {
/// ```
#[inline(always)]
#[stable(feature = "unsafe_cell_raw_get", since = "1.56.0")]
#[rustc_const_stable(feature = "unsafe_cell_raw_get", since = "1.56.0")]
pub const fn raw_get(this: *const Self) -> *mut T {
// We can just cast the pointer from `UnsafeCell<T>` to `T` because of
// #[repr(transparent)]. This exploits libstd's special status, there is
Expand Down
1 change: 1 addition & 0 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ macro_rules! int_impl {
///
/// ```
#[stable(feature = "saturating_div", since = "1.58.0")]
#[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
Expand Down
1 change: 1 addition & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ macro_rules! nonzero_unsigned_is_power_of_two {
/// ```
#[must_use]
#[stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
#[rustc_const_stable(feature = "nonzero_is_power_of_two", since = "1.59.0")]
#[inline]
pub const fn is_power_of_two(self) -> bool {
// LLVM 11 normalizes `unchecked_sub(x, 1) & x == 0` to the implementation seen here.
Expand Down
1 change: 1 addition & 0 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@ macro_rules! uint_impl {
///
/// ```
#[stable(feature = "saturating_div", since = "1.58.0")]
#[rustc_const_stable(feature = "saturating_div", since = "1.58.0")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
Expand Down
4 changes: 4 additions & 0 deletions library/core/src/panic/panic_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ impl<'a> PanicInfo<'a> {
/// This is true for most kinds of panics with the exception of panics
/// caused by trying to unwind out of a `Drop` implementation or a function
/// whose ABI does not support unwinding.
///
/// It is safe for a panic handler to unwind even when this function returns
/// true, however this will simply cause the panic handler to be called
/// again.
#[must_use]
#[unstable(feature = "panic_can_unwind", issue = "92988")]
pub fn can_unwind(&self) -> bool {
Expand Down
26 changes: 20 additions & 6 deletions src/test/ui/stability-attribute/missing-const-stability.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
#![feature(staged_api)]
#![feature(const_trait_impl)]
#![stable(feature = "stable", since = "1.0.0")]

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

#[stable(feature = "foo", since = "1.0.0")]
pub const fn foo() {}
//~^ ERROR rustc_const_stable
#[unstable(feature = "unstable", issue = "none")]
pub const fn bar() {} // ok because function is unstable

#[unstable(feature = "bar", issue = "none")]
pub const fn bar() {} // ok
#[stable(feature = "stable", since = "1.0.0")]
pub struct Foo;
impl Foo {
#[stable(feature = "stable", since = "1.0.0")]
pub const fn foo() {} //~ ERROR associated function has missing const stability attribute

#[unstable(feature = "unstable", issue = "none")]
pub const fn bar() {} // ok because function is unstable
}

// FIXME Once #![feature(const_trait_impl)] is allowed to be stable, add a test
// for const trait impls. Right now, a "trait methods cannot be stable const fn"
// error is emitted. This occurs prior to the lint being tested here, such that
// the lint cannot currently be tested on this use case.

fn main() {}
10 changes: 8 additions & 2 deletions src/test/ui/stability-attribute/missing-const-stability.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
error: `#[stable]` const functions must also be either `#[rustc_const_stable]` or `#[rustc_const_unstable]`
error: function has missing const stability attribute
--> $DIR/missing-const-stability.rs:6:1
|
LL | pub const fn foo() {}
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
error: associated function has missing const stability attribute
--> $DIR/missing-const-stability.rs:15:5
|
LL | pub const fn foo() {}
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors