From fc87d6e23d23ee267269106b9566ad17e1dc4d3e Mon Sep 17 00:00:00 2001 From: bohan Date: Fri, 24 Nov 2023 00:54:06 +0800 Subject: [PATCH] add track_caller for arith ops --- library/core/src/internal_macros.rs | 4 ++++ library/core/src/ops/arith.rs | 10 ++++++++++ .../inherit_overflow.main.ConstProp.panic-abort.diff | 2 +- .../inherit_overflow.main.ConstProp.panic-unwind.diff | 2 +- ...it_overflow.main.DataflowConstProp.panic-abort.diff | 2 +- ...t_overflow.main.DataflowConstProp.panic-unwind.diff | 2 +- .../numbers-arithmetic/location-add-assign-overflow.rs | 8 ++++++++ tests/ui/numbers-arithmetic/location-add-overflow.rs | 7 +++++++ .../location-divide-assign-by-zero.rs | 8 ++++++++ tests/ui/numbers-arithmetic/location-divide-by-zero.rs | 9 +++++++++ .../numbers-arithmetic/location-mod-assign-by-zero.rs | 8 ++++++++ tests/ui/numbers-arithmetic/location-mod-by-zero.rs | 7 +++++++ .../numbers-arithmetic/location-mul-assign-overflow.rs | 8 ++++++++ tests/ui/numbers-arithmetic/location-mul-overflow.rs | 7 +++++++ .../numbers-arithmetic/location-sub-assign-overflow.rs | 8 ++++++++ tests/ui/numbers-arithmetic/location-sub-overflow.rs | 7 +++++++ 16 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 tests/ui/numbers-arithmetic/location-add-assign-overflow.rs create mode 100644 tests/ui/numbers-arithmetic/location-add-overflow.rs create mode 100644 tests/ui/numbers-arithmetic/location-divide-assign-by-zero.rs create mode 100644 tests/ui/numbers-arithmetic/location-divide-by-zero.rs create mode 100644 tests/ui/numbers-arithmetic/location-mod-assign-by-zero.rs create mode 100644 tests/ui/numbers-arithmetic/location-mod-by-zero.rs create mode 100644 tests/ui/numbers-arithmetic/location-mul-assign-overflow.rs create mode 100644 tests/ui/numbers-arithmetic/location-mul-overflow.rs create mode 100644 tests/ui/numbers-arithmetic/location-sub-assign-overflow.rs create mode 100644 tests/ui/numbers-arithmetic/location-sub-overflow.rs diff --git a/library/core/src/internal_macros.rs b/library/core/src/internal_macros.rs index 5774107f520..bf53b2245ac 100644 --- a/library/core/src/internal_macros.rs +++ b/library/core/src/internal_macros.rs @@ -31,6 +31,7 @@ impl<'a> $imp<$u> for &'a $t { type Output = <$t as $imp<$u>>::Output; #[inline] + #[track_caller] fn $method(self, other: $u) -> <$t as $imp<$u>>::Output { $imp::$method(*self, other) } @@ -41,6 +42,7 @@ impl $imp<&$u> for $t { type Output = <$t as $imp<$u>>::Output; #[inline] + #[track_caller] fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output { $imp::$method(self, *other) } @@ -51,6 +53,7 @@ impl $imp<&$u> for &$t { type Output = <$t as $imp<$u>>::Output; #[inline] + #[track_caller] fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output { $imp::$method(*self, *other) } @@ -69,6 +72,7 @@ macro_rules! forward_ref_op_assign { #[$attr] impl $imp<&$u> for $t { #[inline] + #[track_caller] fn $method(&mut self, other: &$u) { $imp::$method(self, *other); } diff --git a/library/core/src/ops/arith.rs b/library/core/src/ops/arith.rs index 840c8cd2fe8..1773fdbf37c 100644 --- a/library/core/src/ops/arith.rs +++ b/library/core/src/ops/arith.rs @@ -98,6 +98,7 @@ impl Add for $t { type Output = $t; #[inline] + #[track_caller] #[rustc_inherit_overflow_checks] fn add(self, other: $t) -> $t { self + other } } @@ -206,6 +207,7 @@ impl Sub for $t { type Output = $t; #[inline] + #[track_caller] #[rustc_inherit_overflow_checks] fn sub(self, other: $t) -> $t { self - other } } @@ -335,6 +337,7 @@ impl Mul for $t { type Output = $t; #[inline] + #[track_caller] #[rustc_inherit_overflow_checks] fn mul(self, other: $t) -> $t { self * other } } @@ -474,6 +477,7 @@ impl Div for $t { type Output = $t; #[inline] + #[track_caller] fn div(self, other: $t) -> $t { self / other } } @@ -575,6 +579,7 @@ impl Rem for $t { type Output = $t; #[inline] + #[track_caller] fn rem(self, other: $t) -> $t { self % other } } @@ -749,6 +754,7 @@ macro_rules! add_assign_impl { #[stable(feature = "op_assign_traits", since = "1.8.0")] impl AddAssign for $t { #[inline] + #[track_caller] #[rustc_inherit_overflow_checks] fn add_assign(&mut self, other: $t) { *self += other } } @@ -815,6 +821,7 @@ macro_rules! sub_assign_impl { #[stable(feature = "op_assign_traits", since = "1.8.0")] impl SubAssign for $t { #[inline] + #[track_caller] #[rustc_inherit_overflow_checks] fn sub_assign(&mut self, other: $t) { *self -= other } } @@ -872,6 +879,7 @@ macro_rules! mul_assign_impl { #[stable(feature = "op_assign_traits", since = "1.8.0")] impl MulAssign for $t { #[inline] + #[track_caller] #[rustc_inherit_overflow_checks] fn mul_assign(&mut self, other: $t) { *self *= other } } @@ -929,6 +937,7 @@ macro_rules! div_assign_impl { #[stable(feature = "op_assign_traits", since = "1.8.0")] impl DivAssign for $t { #[inline] + #[track_caller] fn div_assign(&mut self, other: $t) { *self /= other } } @@ -989,6 +998,7 @@ macro_rules! rem_assign_impl { #[stable(feature = "op_assign_traits", since = "1.8.0")] impl RemAssign for $t { #[inline] + #[track_caller] fn rem_assign(&mut self, other: $t) { *self %= other } } diff --git a/tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.panic-abort.diff b/tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.panic-abort.diff index b30deb2a4d4..11cdf9e09db 100644 --- a/tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.panic-abort.diff +++ b/tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.panic-abort.diff @@ -8,7 +8,7 @@ let mut _3: u8; scope 1 { } - scope 2 (inlined ::add) { + scope 2 (inlined #[track_caller] ::add) { debug self => _2; debug other => _3; let mut _4: (u8, bool); diff --git a/tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.panic-unwind.diff b/tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.panic-unwind.diff index 47c51196c02..181a2f287d6 100644 --- a/tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.panic-unwind.diff +++ b/tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.panic-unwind.diff @@ -8,7 +8,7 @@ let mut _3: u8; scope 1 { } - scope 2 (inlined ::add) { + scope 2 (inlined #[track_caller] ::add) { debug self => _2; debug other => _3; let mut _4: (u8, bool); diff --git a/tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-abort.diff b/tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-abort.diff index 2d4591ea2d3..09fc48043b9 100644 --- a/tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-abort.diff +++ b/tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-abort.diff @@ -8,7 +8,7 @@ let mut _3: u8; scope 1 { } - scope 2 (inlined ::add) { + scope 2 (inlined #[track_caller] ::add) { debug self => _2; debug other => _3; let mut _4: (u8, bool); diff --git a/tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-unwind.diff b/tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-unwind.diff index e99ac782a2f..c0b26080f56 100644 --- a/tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-unwind.diff +++ b/tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-unwind.diff @@ -8,7 +8,7 @@ let mut _3: u8; scope 1 { } - scope 2 (inlined ::add) { + scope 2 (inlined #[track_caller] ::add) { debug self => _2; debug other => _3; let mut _4: (u8, bool); diff --git a/tests/ui/numbers-arithmetic/location-add-assign-overflow.rs b/tests/ui/numbers-arithmetic/location-add-assign-overflow.rs new file mode 100644 index 00000000000..2c4bdad3e91 --- /dev/null +++ b/tests/ui/numbers-arithmetic/location-add-assign-overflow.rs @@ -0,0 +1,8 @@ +// run-fail +// ignore-wasm32 +// error-pattern:location-add-assign-overflow.rs + +fn main() { + let mut a: u8 = 255; + a += &1; +} diff --git a/tests/ui/numbers-arithmetic/location-add-overflow.rs b/tests/ui/numbers-arithmetic/location-add-overflow.rs new file mode 100644 index 00000000000..085623c9bf7 --- /dev/null +++ b/tests/ui/numbers-arithmetic/location-add-overflow.rs @@ -0,0 +1,7 @@ +// run-fail +// ignore-wasm32 +// error-pattern:location-add-overflow.rs + +fn main() { + let _: u8 = 255 + &1; +} diff --git a/tests/ui/numbers-arithmetic/location-divide-assign-by-zero.rs b/tests/ui/numbers-arithmetic/location-divide-assign-by-zero.rs new file mode 100644 index 00000000000..21b5e7a8110 --- /dev/null +++ b/tests/ui/numbers-arithmetic/location-divide-assign-by-zero.rs @@ -0,0 +1,8 @@ +// run-fail +// ignore-wasm32 +// error-pattern:location-divide-assign-by-zero.rs + +fn main() { + let mut a = 1; + a /= &0; +} diff --git a/tests/ui/numbers-arithmetic/location-divide-by-zero.rs b/tests/ui/numbers-arithmetic/location-divide-by-zero.rs new file mode 100644 index 00000000000..7d045fc5602 --- /dev/null +++ b/tests/ui/numbers-arithmetic/location-divide-by-zero.rs @@ -0,0 +1,9 @@ +// run-fail +// ignore-wasm32 +// error-pattern:location-divide-by-zero.rs + +// https://github.com/rust-lang/rust/issues/114814 + +fn main() { + let _ = 1 / &0; +} diff --git a/tests/ui/numbers-arithmetic/location-mod-assign-by-zero.rs b/tests/ui/numbers-arithmetic/location-mod-assign-by-zero.rs new file mode 100644 index 00000000000..88d602e4b6d --- /dev/null +++ b/tests/ui/numbers-arithmetic/location-mod-assign-by-zero.rs @@ -0,0 +1,8 @@ +// run-fail +// ignore-wasm32 +// error-pattern:location-mod-assign-by-zero.rs + +fn main() { + let mut a = 1; + a %= &0; +} diff --git a/tests/ui/numbers-arithmetic/location-mod-by-zero.rs b/tests/ui/numbers-arithmetic/location-mod-by-zero.rs new file mode 100644 index 00000000000..4397adb75d1 --- /dev/null +++ b/tests/ui/numbers-arithmetic/location-mod-by-zero.rs @@ -0,0 +1,7 @@ +// run-fail +// ignore-wasm32 +// error-pattern:location-mod-by-zero.rs + +fn main() { + let _ = 1 % &0; +} diff --git a/tests/ui/numbers-arithmetic/location-mul-assign-overflow.rs b/tests/ui/numbers-arithmetic/location-mul-assign-overflow.rs new file mode 100644 index 00000000000..b042751ded9 --- /dev/null +++ b/tests/ui/numbers-arithmetic/location-mul-assign-overflow.rs @@ -0,0 +1,8 @@ +// run-fail +// ignore-wasm32 +// error-pattern:location-mul-assign-overflow.rs + +fn main() { + let mut a: u8 = 255; + a *= &2; +} diff --git a/tests/ui/numbers-arithmetic/location-mul-overflow.rs b/tests/ui/numbers-arithmetic/location-mul-overflow.rs new file mode 100644 index 00000000000..6dd58874874 --- /dev/null +++ b/tests/ui/numbers-arithmetic/location-mul-overflow.rs @@ -0,0 +1,7 @@ +// run-fail +// ignore-wasm32 +// error-pattern:location-mul-overflow.rs + +fn main() { + let _: u8 = 255 * &2; +} diff --git a/tests/ui/numbers-arithmetic/location-sub-assign-overflow.rs b/tests/ui/numbers-arithmetic/location-sub-assign-overflow.rs new file mode 100644 index 00000000000..5b92ada2e0b --- /dev/null +++ b/tests/ui/numbers-arithmetic/location-sub-assign-overflow.rs @@ -0,0 +1,8 @@ +// run-fail +// ignore-wasm32 +// error-pattern:location-sub-assign-overflow.rs + +fn main() { + let mut a: u8 = 0; + a -= &1; +} diff --git a/tests/ui/numbers-arithmetic/location-sub-overflow.rs b/tests/ui/numbers-arithmetic/location-sub-overflow.rs new file mode 100644 index 00000000000..2d77cb8f55e --- /dev/null +++ b/tests/ui/numbers-arithmetic/location-sub-overflow.rs @@ -0,0 +1,7 @@ +// run-fail +// ignore-wasm32 +// error-pattern:location-sub-overflow.rs + +fn main() { + let _: u8 = 0 - &1; +}