Skip to content

Commit a18f635

Browse files
committed
Auto merge of #114841 - bvanjoi:fix-114814, r=<try>
fix: add track_caller attr for div and mod Fixes #114814 `#[track_caller]` is works, r? `@scottmcm`
2 parents fd80c02 + 28b88c9 commit a18f635

25 files changed

+121
-4
lines changed

library/core/src/ops/arith.rs

+10
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub trait Add<Rhs = Self> {
8686
/// assert_eq!(12 + 1, 13);
8787
/// ```
8888
#[must_use = "this returns the result of the operation, without modifying the original"]
89+
#[track_caller]
8990
#[rustc_diagnostic_item = "add"]
9091
#[stable(feature = "rust1", since = "1.0.0")]
9192
fn add(self, rhs: Rhs) -> Self::Output;
@@ -194,6 +195,7 @@ pub trait Sub<Rhs = Self> {
194195
/// assert_eq!(12 - 1, 11);
195196
/// ```
196197
#[must_use = "this returns the result of the operation, without modifying the original"]
198+
#[track_caller]
197199
#[rustc_diagnostic_item = "sub"]
198200
#[stable(feature = "rust1", since = "1.0.0")]
199201
fn sub(self, rhs: Rhs) -> Self::Output;
@@ -323,6 +325,7 @@ pub trait Mul<Rhs = Self> {
323325
/// assert_eq!(12 * 2, 24);
324326
/// ```
325327
#[must_use = "this returns the result of the operation, without modifying the original"]
328+
#[track_caller]
326329
#[rustc_diagnostic_item = "mul"]
327330
#[stable(feature = "rust1", since = "1.0.0")]
328331
fn mul(self, rhs: Rhs) -> Self::Output;
@@ -456,6 +459,7 @@ pub trait Div<Rhs = Self> {
456459
/// assert_eq!(12 / 2, 6);
457460
/// ```
458461
#[must_use = "this returns the result of the operation, without modifying the original"]
462+
#[track_caller]
459463
#[rustc_diagnostic_item = "div"]
460464
#[stable(feature = "rust1", since = "1.0.0")]
461465
fn div(self, rhs: Rhs) -> Self::Output;
@@ -557,6 +561,7 @@ pub trait Rem<Rhs = Self> {
557561
/// assert_eq!(12 % 10, 2);
558562
/// ```
559563
#[must_use = "this returns the result of the operation, without modifying the original"]
564+
#[track_caller]
560565
#[rustc_diagnostic_item = "rem"]
561566
#[stable(feature = "rust1", since = "1.0.0")]
562567
fn rem(self, rhs: Rhs) -> Self::Output;
@@ -740,6 +745,7 @@ pub trait AddAssign<Rhs = Self> {
740745
/// x += 1;
741746
/// assert_eq!(x, 13);
742747
/// ```
748+
#[track_caller]
743749
#[stable(feature = "op_assign_traits", since = "1.8.0")]
744750
fn add_assign(&mut self, rhs: Rhs);
745751
}
@@ -806,6 +812,7 @@ pub trait SubAssign<Rhs = Self> {
806812
/// x -= 1;
807813
/// assert_eq!(x, 11);
808814
/// ```
815+
#[track_caller]
809816
#[stable(feature = "op_assign_traits", since = "1.8.0")]
810817
fn sub_assign(&mut self, rhs: Rhs);
811818
}
@@ -863,6 +870,7 @@ pub trait MulAssign<Rhs = Self> {
863870
/// x *= 2;
864871
/// assert_eq!(x, 24);
865872
/// ```
873+
#[track_caller]
866874
#[stable(feature = "op_assign_traits", since = "1.8.0")]
867875
fn mul_assign(&mut self, rhs: Rhs);
868876
}
@@ -920,6 +928,7 @@ pub trait DivAssign<Rhs = Self> {
920928
/// x /= 2;
921929
/// assert_eq!(x, 6);
922930
/// ```
931+
#[track_caller]
923932
#[stable(feature = "op_assign_traits", since = "1.8.0")]
924933
fn div_assign(&mut self, rhs: Rhs);
925934
}
@@ -980,6 +989,7 @@ pub trait RemAssign<Rhs = Self> {
980989
/// x %= 10;
981990
/// assert_eq!(x, 2);
982991
/// ```
992+
#[track_caller]
983993
#[stable(feature = "op_assign_traits", since = "1.8.0")]
984994
fn rem_assign(&mut self, rhs: Rhs);
985995
}

tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
let mut _3: u8;
99
scope 1 {
1010
}
11-
scope 2 (inlined <u8 as Add>::add) {
11+
scope 2 (inlined #[track_caller] <u8 as Add>::add) {
1212
debug self => _2;
1313
debug other => _3;
1414
let mut _4: (u8, bool);

tests/mir-opt/const_prop/inherit_overflow.main.ConstProp.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
let mut _3: u8;
99
scope 1 {
1010
}
11-
scope 2 (inlined <u8 as Add>::add) {
11+
scope 2 (inlined #[track_caller] <u8 as Add>::add) {
1212
debug self => _2;
1313
debug other => _3;
1414
let mut _4: (u8, bool);

tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
let mut _3: u8;
99
scope 1 {
1010
}
11-
scope 2 (inlined <u8 as Add>::add) {
11+
scope 2 (inlined #[track_caller] <u8 as Add>::add) {
1212
debug self => _2;
1313
debug other => _3;
1414
let mut _4: (u8, bool);

tests/mir-opt/dataflow-const-prop/inherit_overflow.main.DataflowConstProp.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
let mut _3: u8;
99
scope 1 {
1010
}
11-
scope 2 (inlined <u8 as Add>::add) {
11+
scope 2 (inlined #[track_caller] <u8 as Add>::add) {
1212
debug self => _2;
1313
debug other => _3;
1414
let mut _4: (u8, bool);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-fail
2+
// check-run-results
3+
// error-pattern:location-add-assign-overflow.rs
4+
5+
fn main() {
6+
let mut a: u8 = 255;
7+
a += &1;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thread 'main' panicked at $DIR/location-add-assign-overflow.rs:7:5:
2+
attempt to add with overflow
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-fail
2+
// check-run-results
3+
// error-pattern:location-add-overflow.rs
4+
5+
fn main() {
6+
let _: u8 = 255 + &1;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thread 'main' panicked at $DIR/location-add-overflow.rs:6:17:
2+
attempt to add with overflow
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-fail
2+
// check-run-results
3+
// error-pattern:location-divide-assign-by-zero.rs
4+
5+
fn main() {
6+
let mut a = 1;
7+
a /= &0;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thread 'main' panicked at $DIR/location-divide-assign-by-zero.rs:7:5:
2+
attempt to divide by zero
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-fail
2+
// check-run-results
3+
// error-pattern:location-divide-by-zero.rs
4+
5+
// https://github.com/rust-lang/rust/issues/114814
6+
7+
fn main() {
8+
let _ = 1 / &0;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thread 'main' panicked at $DIR/location-divide-by-zero.rs:8:13:
2+
attempt to divide by zero
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-fail
2+
// check-run-results
3+
// error-pattern:location-mod-assign-by-zero.rs
4+
5+
fn main() {
6+
let mut a = 1;
7+
a %= &0;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thread 'main' panicked at $DIR/location-mod-assign-by-zero.rs:7:5:
2+
attempt to calculate the remainder with a divisor of zero
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-fail
2+
// check-run-results
3+
// error-pattern:location-mod-by-zero.rs
4+
5+
fn main() {
6+
let _ = 1 % &0;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thread 'main' panicked at $DIR/location-mod-by-zero.rs:6:13:
2+
attempt to calculate the remainder with a divisor of zero
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-fail
2+
// check-run-results
3+
// error-pattern:location-mul-assign-overflow.rs
4+
5+
fn main() {
6+
let mut a: u8 = 255;
7+
a *= &2;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thread 'main' panicked at $DIR/location-mul-assign-overflow.rs:7:5:
2+
attempt to multiply with overflow
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-fail
2+
// check-run-results
3+
// error-pattern:location-mul-overflow.rs
4+
5+
fn main() {
6+
let _: u8 = 255 * &2;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thread 'main' panicked at $DIR/location-mul-overflow.rs:6:17:
2+
attempt to multiply with overflow
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-fail
2+
// check-run-results
3+
// error-pattern:location-sub-assign-overflow.rs
4+
5+
fn main() {
6+
let mut a: u8 = 0;
7+
a -= &1;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thread 'main' panicked at $DIR/location-sub-assign-overflow.rs:7:5:
2+
attempt to subtract with overflow
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-fail
2+
// check-run-results
3+
// error-pattern:location-sub-overflow.rs
4+
5+
fn main() {
6+
let _: u8 = 0 - &1;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
thread 'main' panicked at $DIR/location-sub-overflow.rs:6:17:
2+
attempt to subtract with overflow
3+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0 commit comments

Comments
 (0)