Skip to content

Commit f4df330

Browse files
authored
Rollup merge of rust-lang#121155 - tspiteri:strict-doc-overflow, r=Nilstrieb
doc: add note about panicking examples for strict_overflow_ops The first commit adds a note before the panicking examples for strict_overflow_ops to make it clearer that the following examples should panic and why, without needing the reader to hover the mouse over the information icon. The second commit adds panicking examples for division by zero operations for strict division operations on unsigned numbers. The signed numbers already have two panicking examples each: one for division by zero and one for overflowing division (`MIN/-1`); this commit includes the division by zero examples for the unsigned numbers.
2 parents 7902d33 + 675d092 commit f4df330

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

library/core/src/num/int_macros.rs

+36
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ macro_rules! int_impl {
472472
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).strict_add(1), ", stringify!($SelfT), "::MAX - 1);")]
473473
/// ```
474474
///
475+
/// The following panics because of overflow:
476+
///
475477
/// ```should_panic
476478
/// #![feature(strict_overflow_ops)]
477479
#[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add(3);")]
@@ -552,6 +554,8 @@ macro_rules! int_impl {
552554
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_add_unsigned(2), 3);")]
553555
/// ```
554556
///
557+
/// The following panics because of overflow:
558+
///
555559
/// ```should_panic
556560
/// #![feature(strict_overflow_ops)]
557561
#[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add_unsigned(3);")]
@@ -606,6 +610,8 @@ macro_rules! int_impl {
606610
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 2).strict_sub(1), ", stringify!($SelfT), "::MIN + 1);")]
607611
/// ```
608612
///
613+
/// The following panics because of overflow:
614+
///
609615
/// ```should_panic
610616
/// #![feature(strict_overflow_ops)]
611617
#[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub(3);")]
@@ -686,6 +692,8 @@ macro_rules! int_impl {
686692
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_sub_unsigned(2), -1);")]
687693
/// ```
688694
///
695+
/// The following panics because of overflow:
696+
///
689697
/// ```should_panic
690698
/// #![feature(strict_overflow_ops)]
691699
#[doc = concat!("let _ = (", stringify!($SelfT), "::MIN + 2).strict_sub_unsigned(3);")]
@@ -740,6 +748,8 @@ macro_rules! int_impl {
740748
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.strict_mul(1), ", stringify!($SelfT), "::MAX);")]
741749
/// ```
742750
///
751+
/// The following panics because of overflow:
752+
///
743753
/// ``` should_panic
744754
/// #![feature(strict_overflow_ops)]
745755
#[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_mul(2);")]
@@ -831,11 +841,15 @@ macro_rules! int_impl {
831841
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div(-1), ", stringify!($Max), ");")]
832842
/// ```
833843
///
844+
/// The following panics because of overflow:
845+
///
834846
/// ```should_panic
835847
/// #![feature(strict_overflow_ops)]
836848
#[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div(-1);")]
837849
/// ```
838850
///
851+
/// The following panics because of division by zero:
852+
///
839853
/// ```should_panic
840854
/// #![feature(strict_overflow_ops)]
841855
#[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div(0);")]
@@ -901,11 +915,15 @@ macro_rules! int_impl {
901915
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MIN + 1).strict_div_euclid(-1), ", stringify!($Max), ");")]
902916
/// ```
903917
///
918+
/// The following panics because of overflow:
919+
///
904920
/// ```should_panic
905921
/// #![feature(strict_overflow_ops)]
906922
#[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_div_euclid(-1);")]
907923
/// ```
908924
///
925+
/// The following panics because of division by zero:
926+
///
909927
/// ```should_panic
910928
/// #![feature(strict_overflow_ops)]
911929
#[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div_euclid(0);")]
@@ -970,11 +988,15 @@ macro_rules! int_impl {
970988
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem(2), 1);")]
971989
/// ```
972990
///
991+
/// The following panics because of division by zero:
992+
///
973993
/// ```should_panic
974994
/// #![feature(strict_overflow_ops)]
975995
#[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem(0);")]
976996
/// ```
977997
///
998+
/// The following panics because of overflow:
999+
///
9781000
/// ```should_panic
9791001
/// #![feature(strict_overflow_ops)]
9801002
#[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem(-1);")]
@@ -1039,11 +1061,15 @@ macro_rules! int_impl {
10391061
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_rem_euclid(2), 1);")]
10401062
/// ```
10411063
///
1064+
/// The following panics because of division by zero:
1065+
///
10421066
/// ```should_panic
10431067
/// #![feature(strict_overflow_ops)]
10441068
#[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem_euclid(0);")]
10451069
/// ```
10461070
///
1071+
/// The following panics because of overflow:
1072+
///
10471073
/// ```should_panic
10481074
/// #![feature(strict_overflow_ops)]
10491075
#[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_rem_euclid(-1);")]
@@ -1121,6 +1147,8 @@ macro_rules! int_impl {
11211147
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_neg(), -5);")]
11221148
/// ```
11231149
///
1150+
/// The following panics because of overflow:
1151+
///
11241152
/// ```should_panic
11251153
/// #![feature(strict_overflow_ops)]
11261154
#[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_neg();")]
@@ -1175,6 +1203,8 @@ macro_rules! int_impl {
11751203
#[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".strict_shl(4), 0x10);")]
11761204
/// ```
11771205
///
1206+
/// The following panics because of overflow:
1207+
///
11781208
/// ```should_panic
11791209
/// #![feature(strict_overflow_ops)]
11801210
#[doc = concat!("let _ = 0x1", stringify!($SelfT), ".strict_shl(129);")]
@@ -1256,6 +1286,8 @@ macro_rules! int_impl {
12561286
#[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".strict_shr(4), 0x1);")]
12571287
/// ```
12581288
///
1289+
/// The following panics because of overflow:
1290+
///
12591291
/// ```should_panic
12601292
/// #![feature(strict_overflow_ops)]
12611293
#[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shr(128);")]
@@ -1340,6 +1372,8 @@ macro_rules! int_impl {
13401372
#[doc = concat!("assert_eq!((-5", stringify!($SelfT), ").strict_abs(), 5);")]
13411373
/// ```
13421374
///
1375+
/// The following panics because of overflow:
1376+
///
13431377
/// ```should_panic
13441378
/// #![feature(strict_overflow_ops)]
13451379
#[doc = concat!("let _ = ", stringify!($SelfT), "::MIN.strict_abs();")]
@@ -1414,6 +1448,8 @@ macro_rules! int_impl {
14141448
#[doc = concat!("assert_eq!(8", stringify!($SelfT), ".strict_pow(2), 64);")]
14151449
/// ```
14161450
///
1451+
/// The following panics because of overflow:
1452+
///
14171453
/// ```should_panic
14181454
/// #![feature(strict_overflow_ops)]
14191455
#[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_pow(2);")]

library/core/src/num/uint_macros.rs

+43
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ macro_rules! uint_impl {
480480
#[doc = concat!("assert_eq!((", stringify!($SelfT), "::MAX - 2).strict_add(1), ", stringify!($SelfT), "::MAX - 1);")]
481481
/// ```
482482
///
483+
/// The following panics because of overflow:
484+
///
483485
/// ```should_panic
484486
/// #![feature(strict_overflow_ops)]
485487
#[doc = concat!("let _ = (", stringify!($SelfT), "::MAX - 2).strict_add(3);")]
@@ -561,6 +563,8 @@ macro_rules! uint_impl {
561563
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_add_signed(2), 3);")]
562564
/// ```
563565
///
566+
/// The following panic because of overflow:
567+
///
564568
/// ```should_panic
565569
/// #![feature(strict_overflow_ops)]
566570
#[doc = concat!("let _ = 1", stringify!($SelfT), ".strict_add_signed(-2);")]
@@ -620,6 +624,8 @@ macro_rules! uint_impl {
620624
#[doc = concat!("assert_eq!(1", stringify!($SelfT), ".strict_sub(1), 0);")]
621625
/// ```
622626
///
627+
/// The following panics because of overflow:
628+
///
623629
/// ```should_panic
624630
/// #![feature(strict_overflow_ops)]
625631
#[doc = concat!("let _ = 0", stringify!($SelfT), ".strict_sub(1);")]
@@ -700,6 +706,8 @@ macro_rules! uint_impl {
700706
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".strict_mul(1), 5);")]
701707
/// ```
702708
///
709+
/// The following panics because of overflow:
710+
///
703711
/// ``` should_panic
704712
/// #![feature(strict_overflow_ops)]
705713
#[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_mul(2);")]
@@ -785,6 +793,13 @@ macro_rules! uint_impl {
785793
/// #![feature(strict_overflow_ops)]
786794
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".strict_div(10), 10);")]
787795
/// ```
796+
///
797+
/// The following panics because of division by zero:
798+
///
799+
/// ```should_panic
800+
/// #![feature(strict_overflow_ops)]
801+
#[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div(0);")]
802+
/// ```
788803
#[unstable(feature = "strict_overflow_ops", issue = "118260")]
789804
#[rustc_const_unstable(feature = "const_strict_overflow_ops", issue = "118260")]
790805
#[must_use = "this returns the result of the operation, \
@@ -840,6 +855,12 @@ macro_rules! uint_impl {
840855
/// #![feature(strict_overflow_ops)]
841856
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".strict_div_euclid(10), 10);")]
842857
/// ```
858+
/// The following panics because of division by zero:
859+
///
860+
/// ```should_panic
861+
/// #![feature(strict_overflow_ops)]
862+
#[doc = concat!("let _ = (1", stringify!($SelfT), ").strict_div_euclid(0);")]
863+
/// ```
843864
#[unstable(feature = "strict_overflow_ops", issue = "118260")]
844865
#[rustc_const_unstable(feature = "const_strict_overflow_ops", issue = "118260")]
845866
#[must_use = "this returns the result of the operation, \
@@ -895,6 +916,13 @@ macro_rules! uint_impl {
895916
/// #![feature(strict_overflow_ops)]
896917
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".strict_rem(10), 0);")]
897918
/// ```
919+
///
920+
/// The following panics because of division by zero:
921+
///
922+
/// ```should_panic
923+
/// #![feature(strict_overflow_ops)]
924+
#[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem(0);")]
925+
/// ```
898926
#[unstable(feature = "strict_overflow_ops", issue = "118260")]
899927
#[rustc_const_unstable(feature = "const_strict_overflow_ops", issue = "118260")]
900928
#[must_use = "this returns the result of the operation, \
@@ -951,6 +979,13 @@ macro_rules! uint_impl {
951979
/// #![feature(strict_overflow_ops)]
952980
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".strict_rem_euclid(10), 0);")]
953981
/// ```
982+
///
983+
/// The following panics because of division by zero:
984+
///
985+
/// ```should_panic
986+
/// #![feature(strict_overflow_ops)]
987+
#[doc = concat!("let _ = 5", stringify!($SelfT), ".strict_rem_euclid(0);")]
988+
/// ```
954989
#[unstable(feature = "strict_overflow_ops", issue = "118260")]
955990
#[rustc_const_unstable(feature = "const_strict_overflow_ops", issue = "118260")]
956991
#[must_use = "this returns the result of the operation, \
@@ -1172,6 +1207,8 @@ macro_rules! uint_impl {
11721207
#[doc = concat!("assert_eq!(0", stringify!($SelfT), ".strict_neg(), 0);")]
11731208
/// ```
11741209
///
1210+
/// The following panics because of overflow:
1211+
///
11751212
/// ```should_panic
11761213
/// #![feature(strict_overflow_ops)]
11771214
#[doc = concat!("let _ = 1", stringify!($SelfT), ".strict_neg();")]
@@ -1226,6 +1263,8 @@ macro_rules! uint_impl {
12261263
#[doc = concat!("assert_eq!(0x1", stringify!($SelfT), ".strict_shl(4), 0x10);")]
12271264
/// ```
12281265
///
1266+
/// The following panics because of overflow:
1267+
///
12291268
/// ```should_panic
12301269
/// #![feature(strict_overflow_ops)]
12311270
#[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shl(129);")]
@@ -1307,6 +1346,8 @@ macro_rules! uint_impl {
13071346
#[doc = concat!("assert_eq!(0x10", stringify!($SelfT), ".strict_shr(4), 0x1);")]
13081347
/// ```
13091348
///
1349+
/// The following panics because of overflow:
1350+
///
13101351
/// ```should_panic
13111352
/// #![feature(strict_overflow_ops)]
13121353
#[doc = concat!("let _ = 0x10", stringify!($SelfT), ".strict_shr(129);")]
@@ -1406,6 +1447,8 @@ macro_rules! uint_impl {
14061447
#[doc = concat!("assert_eq!(2", stringify!($SelfT), ".strict_pow(5), 32);")]
14071448
/// ```
14081449
///
1450+
/// The following panics because of overflow:
1451+
///
14091452
/// ```should_panic
14101453
/// #![feature(strict_overflow_ops)]
14111454
#[doc = concat!("let _ = ", stringify!($SelfT), "::MAX.strict_pow(2);")]

0 commit comments

Comments
 (0)