Skip to content

Commit f40a70d

Browse files
committed
Auto merge of #149478 - matthiaskrgr:rollup-0omf56q, r=matthiaskrgr
Rollup of 3 pull requests Successful merges: - #149236 (Clarify edge cases for Barrier::new) - #149444 (collapse `constness` query `match` logic) - #149475 (float::min/max: reference NaN bit pattern rules) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 03ce87d + 1a9f527 commit f40a70d

File tree

7 files changed

+87
-67
lines changed

7 files changed

+87
-67
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
1-
use rustc_hir as hir;
2-
use rustc_hir::def::DefKind;
31
use rustc_hir::def_id::{DefId, LocalDefId};
2+
use rustc_hir::{
3+
Constness, ExprKind, ForeignItemKind, ImplItem, ImplItemImplKind, ImplItemKind, Item, ItemKind,
4+
Node, TraitItem, TraitItemKind, VariantData,
5+
};
46
use rustc_middle::query::Providers;
57
use rustc_middle::ty::TyCtxt;
68

7-
fn parent_impl_or_trait_constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
8-
let parent_id = tcx.local_parent(def_id);
9-
match tcx.def_kind(parent_id) {
10-
DefKind::Impl { of_trait: true } => tcx.impl_trait_header(parent_id).constness,
11-
DefKind::Impl { of_trait: false } => tcx.constness(parent_id),
12-
DefKind::Trait => {
13-
if tcx.is_const_trait(parent_id.into()) {
14-
hir::Constness::Const
15-
} else {
16-
hir::Constness::NotConst
17-
}
18-
}
19-
_ => hir::Constness::NotConst,
20-
}
21-
}
22-
23-
/// Checks whether a function-like definition is considered to be `const`.
24-
fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
9+
/// Checks whether a function-like definition is considered to be `const`. Also stores constness of inherent impls.
10+
fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Constness {
2511
let node = tcx.hir_node_by_def_id(def_id);
2612

2713
match node {
28-
hir::Node::Ctor(hir::VariantData::Tuple(..)) => hir::Constness::Const,
29-
hir::Node::ForeignItem(item) if let hir::ForeignItemKind::Fn(..) = item.kind => {
14+
Node::Ctor(VariantData::Tuple(..)) => Constness::Const,
15+
Node::ForeignItem(item) if let ForeignItemKind::Fn(..) = item.kind => {
3016
// Foreign functions cannot be evaluated at compile-time.
31-
hir::Constness::NotConst
17+
Constness::NotConst
3218
}
33-
hir::Node::Expr(e) if let hir::ExprKind::Closure(c) = e.kind => c.constness,
34-
hir::Node::Item(i) if let hir::ItemKind::Impl(impl_) = i.kind => impl_.constness,
35-
_ => {
36-
if let Some(fn_kind) = node.fn_kind() {
37-
if fn_kind.constness() == hir::Constness::Const {
38-
return hir::Constness::Const;
39-
}
40-
41-
// If the function itself is not annotated with `const`, it may still be a `const fn`
42-
// if it resides in a const trait impl.
43-
parent_impl_or_trait_constness(tcx, def_id)
44-
} else {
45-
tcx.dcx().span_bug(
46-
tcx.def_span(def_id),
47-
format!("should not be requesting the constness of items that can't be const: {node:#?}: {:?}", tcx.def_kind(def_id))
48-
)
19+
Node::Expr(e) if let ExprKind::Closure(c) = e.kind => c.constness,
20+
// FIXME(fee1-dead): extract this one out and rename this query to `fn_constness` so we don't need `is_const_fn` anymore.
21+
Node::Item(i) if let ItemKind::Impl(impl_) = i.kind => impl_.constness,
22+
Node::Item(Item { kind: ItemKind::Fn { sig, .. }, .. }) => sig.header.constness,
23+
Node::ImplItem(ImplItem {
24+
impl_kind: ImplItemImplKind::Trait { .. },
25+
kind: ImplItemKind::Fn(..),
26+
..
27+
}) => tcx.impl_trait_header(tcx.local_parent(def_id)).constness,
28+
Node::ImplItem(ImplItem {
29+
impl_kind: ImplItemImplKind::Inherent { .. },
30+
kind: ImplItemKind::Fn(sig, _),
31+
..
32+
}) => {
33+
match sig.header.constness {
34+
Constness::Const => Constness::Const,
35+
// inherent impl could be const
36+
Constness::NotConst => tcx.constness(tcx.local_parent(def_id)),
4937
}
5038
}
39+
Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. }) => tcx.trait_def(tcx.local_parent(def_id)).constness,
40+
_ => {
41+
tcx.dcx().span_bug(
42+
tcx.def_span(def_id),
43+
format!("should not be requesting the constness of items that can't be const: {node:#?}: {:?}", tcx.def_kind(def_id))
44+
)
45+
}
5146
}
5247
}
5348

library/core/src/num/f128.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,14 @@ impl f128 {
694694

695695
/// Returns the maximum of the two numbers, ignoring NaN.
696696
///
697-
/// If one of the arguments is NaN, then the other argument is returned.
697+
/// If exactly one of the arguments is NaN, then the other argument is returned. If both
698+
/// arguments are NaN, the return value is NaN, with the bit pattern picked using the usual
699+
/// [rules for arithmetic operations](f32#nan-bit-patterns). If the inputs compare equal (such
700+
/// as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
701+
///
698702
/// This follows the IEEE 754-2008 semantics for `maxNum`, except for handling of signaling NaNs;
699703
/// this function handles all NaNs the same way and avoids `maxNum`'s problems with associativity.
700-
/// This also matches the behavior of libm’s fmax. In particular, if the inputs compare equal
701-
/// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
704+
/// This also matches the behavior of libm’s `fmax`.
702705
///
703706
/// ```
704707
/// #![feature(f128)]
@@ -722,11 +725,14 @@ impl f128 {
722725

723726
/// Returns the minimum of the two numbers, ignoring NaN.
724727
///
725-
/// If one of the arguments is NaN, then the other argument is returned.
728+
/// If exactly one of the arguments is NaN, then the other argument is returned. If both
729+
/// arguments are NaN, the return value is NaN, with the bit pattern picked using the usual
730+
/// [rules for arithmetic operations](f32#nan-bit-patterns). If the inputs compare equal (such
731+
/// as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
732+
///
726733
/// This follows the IEEE 754-2008 semantics for `minNum`, except for handling of signaling NaNs;
727734
/// this function handles all NaNs the same way and avoids `minNum`'s problems with associativity.
728-
/// This also matches the behavior of libm’s fmin. In particular, if the inputs compare equal
729-
/// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
735+
/// This also matches the behavior of libm’s `fmin`.
730736
///
731737
/// ```
732738
/// #![feature(f128)]

library/core/src/num/f16.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,14 @@ impl f16 {
687687

688688
/// Returns the maximum of the two numbers, ignoring NaN.
689689
///
690-
/// If one of the arguments is NaN, then the other argument is returned.
690+
/// If exactly one of the arguments is NaN, then the other argument is returned. If both
691+
/// arguments are NaN, the return value is NaN, with the bit pattern picked using the usual
692+
/// [rules for arithmetic operations](f32#nan-bit-patterns). If the inputs compare equal (such
693+
/// as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
694+
///
691695
/// This follows the IEEE 754-2008 semantics for `maxNum`, except for handling of signaling NaNs;
692696
/// this function handles all NaNs the same way and avoids `maxNum`'s problems with associativity.
693-
/// This also matches the behavior of libm’s fmax. In particular, if the inputs compare equal
694-
/// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
697+
/// This also matches the behavior of libm’s `fmax`.
695698
///
696699
/// ```
697700
/// #![feature(f16)]
@@ -714,11 +717,14 @@ impl f16 {
714717

715718
/// Returns the minimum of the two numbers, ignoring NaN.
716719
///
717-
/// If one of the arguments is NaN, then the other argument is returned.
720+
/// If exactly one of the arguments is NaN, then the other argument is returned. If both
721+
/// arguments are NaN, the return value is NaN, with the bit pattern picked using the usual
722+
/// [rules for arithmetic operations](f32#nan-bit-patterns). If the inputs compare equal (such
723+
/// as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
724+
///
718725
/// This follows the IEEE 754-2008 semantics for `minNum`, except for handling of signaling NaNs;
719726
/// this function handles all NaNs the same way and avoids `minNum`'s problems with associativity.
720-
/// This also matches the behavior of libm’s fmin. In particular, if the inputs compare equal
721-
/// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
727+
/// This also matches the behavior of libm’s `fmin`.
722728
///
723729
/// ```
724730
/// #![feature(f16)]

library/core/src/num/f32.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,11 +897,14 @@ impl f32 {
897897

898898
/// Returns the maximum of the two numbers, ignoring NaN.
899899
///
900-
/// If one of the arguments is NaN, then the other argument is returned.
900+
/// If exactly one of the arguments is NaN, then the other argument is returned. If both
901+
/// arguments are NaN, the return value is NaN, with the bit pattern picked using the usual
902+
/// [rules for arithmetic operations](f32#nan-bit-patterns). If the inputs compare equal (such
903+
/// as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
904+
///
901905
/// This follows the IEEE 754-2008 semantics for `maxNum`, except for handling of signaling NaNs;
902906
/// this function handles all NaNs the same way and avoids `maxNum`'s problems with associativity.
903-
/// This also matches the behavior of libm’s fmax. In particular, if the inputs compare equal
904-
/// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
907+
/// This also matches the behavior of libm’s `fmax`.
905908
///
906909
/// ```
907910
/// let x = 1.0f32;
@@ -920,11 +923,14 @@ impl f32 {
920923

921924
/// Returns the minimum of the two numbers, ignoring NaN.
922925
///
923-
/// If one of the arguments is NaN, then the other argument is returned.
926+
/// If exactly one of the arguments is NaN, then the other argument is returned. If both
927+
/// arguments are NaN, the return value is NaN, with the bit pattern picked using the usual
928+
/// [rules for arithmetic operations](f32#nan-bit-patterns). If the inputs compare equal (such
929+
/// as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
930+
///
924931
/// This follows the IEEE 754-2008 semantics for `minNum`, except for handling of signaling NaNs;
925932
/// this function handles all NaNs the same way and avoids `minNum`'s problems with associativity.
926-
/// This also matches the behavior of libm’s fmin. In particular, if the inputs compare equal
927-
/// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
933+
/// This also matches the behavior of libm’s `fmin`.
928934
///
929935
/// ```
930936
/// let x = 1.0f32;

library/core/src/num/f64.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -915,11 +915,14 @@ impl f64 {
915915

916916
/// Returns the maximum of the two numbers, ignoring NaN.
917917
///
918-
/// If one of the arguments is NaN, then the other argument is returned.
918+
/// If exactly one of the arguments is NaN, then the other argument is returned. If both
919+
/// arguments are NaN, the return value is NaN, with the bit pattern picked using the usual
920+
/// [rules for arithmetic operations](f32#nan-bit-patterns). If the inputs compare equal (such
921+
/// as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
922+
///
919923
/// This follows the IEEE 754-2008 semantics for `maxNum`, except for handling of signaling NaNs;
920924
/// this function handles all NaNs the same way and avoids `maxNum`'s problems with associativity.
921-
/// This also matches the behavior of libm’s fmax. In particular, if the inputs compare equal
922-
/// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
925+
/// This also matches the behavior of libm’s `fmax`.
923926
///
924927
/// ```
925928
/// let x = 1.0_f64;
@@ -938,11 +941,14 @@ impl f64 {
938941

939942
/// Returns the minimum of the two numbers, ignoring NaN.
940943
///
941-
/// If one of the arguments is NaN, then the other argument is returned.
944+
/// If exactly one of the arguments is NaN, then the other argument is returned. If both
945+
/// arguments are NaN, the return value is NaN, with the bit pattern picked using the usual
946+
/// [rules for arithmetic operations](f32#nan-bit-patterns). If the inputs compare equal (such
947+
/// as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
948+
///
942949
/// This follows the IEEE 754-2008 semantics for `minNum`, except for handling of signaling NaNs;
943950
/// this function handles all NaNs the same way and avoids `minNum`'s problems with associativity.
944-
/// This also matches the behavior of libm’s fmin. In particular, if the inputs compare equal
945-
/// (such as for the case of `+0.0` and `-0.0`), either input may be returned non-deterministically.
951+
/// This also matches the behavior of libm’s `fmin`.
946952
///
947953
/// ```
948954
/// let x = 1.0_f64;

library/std/src/sync/barrier.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ impl fmt::Debug for Barrier {
6565
impl Barrier {
6666
/// Creates a new barrier that can block a given number of threads.
6767
///
68-
/// A barrier will block `n`-1 threads which call [`wait()`] and then wake
69-
/// up all threads at once when the `n`th thread calls [`wait()`].
68+
/// A barrier will block all threads which call [`wait()`] until the `n`th thread calls [`wait()`],
69+
/// and then wake up all threads at once.
7070
///
7171
/// [`wait()`]: Barrier::wait
7272
///

tests/crashes/137187.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//@ known-bug: #137187
22
use std::ops::Add;
3-
trait A where
3+
4+
const trait A where
45
*const Self: Add,
56
{
6-
const fn b(c: *const Self) -> <*const Self as Add>::Output {
7+
fn b(c: *const Self) -> <*const Self as Add>::Output {
78
c + c
89
}
910
}

0 commit comments

Comments
 (0)