Skip to content

Commit 0d2ab67

Browse files
authored
Rollup merge of #107389 - zvavybir:master, r=estebank
Fixing confusion between mod and remainder Like many programming languages, rust too confuses remainder and modulus. The `%` operator and the associated `Rem` trait is (as the trait name suggests) the remainder, but since most people are linguistically more familiar with the modulus the documentation sometimes claims otherwise. This PR tries to fix this problem in rustc.
2 parents ad8e1dc + af9671f commit 0d2ab67

File tree

7 files changed

+10
-8
lines changed

7 files changed

+10
-8
lines changed

compiler/rustc_hir_typeck/src/op.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
335335
format!("cannot divide `{lhs_ty}` by `{rhs_ty}`")
336336
}
337337
hir::BinOpKind::Rem => {
338-
format!("cannot mod `{lhs_ty}` by `{rhs_ty}`")
338+
format!(
339+
"cannot calculate the remainder of `{lhs_ty}` divided by `{rhs_ty}`"
340+
)
339341
}
340342
hir::BinOpKind::BitAnd => {
341343
format!("no implementation for `{lhs_ty} & {rhs_ty}`")

library/core/src/ops/arith.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ div_impl_float! { f32 f64 }
545545
#[lang = "rem"]
546546
#[stable(feature = "rust1", since = "1.0.0")]
547547
#[rustc_on_unimplemented(
548-
message = "cannot mod `{Self}` by `{Rhs}`",
548+
message = "cannot calculate the remainder of `{Self}` divided by `{Rhs}`",
549549
label = "no implementation for `{Self} % {Rhs}`"
550550
)]
551551
#[doc(alias = "%")]
@@ -981,7 +981,7 @@ div_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
981981
#[lang = "rem_assign"]
982982
#[stable(feature = "op_assign_traits", since = "1.8.0")]
983983
#[rustc_on_unimplemented(
984-
message = "cannot mod-assign `{Self}` by `{Rhs}``",
984+
message = "cannot calculate and assign the remainder of `{Self}` divided by `{Rhs}`",
985985
label = "no implementation for `{Self} %= {Rhs}`"
986986
)]
987987
#[doc(alias = "%")]

tests/ui/binop/binary-op-on-double-ref.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn main() {
33
let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
44
let vr = v.iter().filter(|x| {
55
*x % 2 == 0
6-
//~^ ERROR cannot mod `&&{integer}` by `{integer}`
6+
//~^ ERROR cannot calculate the remainder of `&&{integer}` divided by `{integer}`
77
});
88
println!("{:?}", vr);
99
}

tests/ui/binop/binary-op-on-double-ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn main() {
33
let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
44
let vr = v.iter().filter(|x| {
55
x % 2 == 0
6-
//~^ ERROR cannot mod `&&{integer}` by `{integer}`
6+
//~^ ERROR cannot calculate the remainder of `&&{integer}` divided by `{integer}`
77
});
88
println!("{:?}", vr);
99
}

tests/ui/binop/binary-op-on-double-ref.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0369]: cannot mod `&&{integer}` by `{integer}`
1+
error[E0369]: cannot calculate the remainder of `&&{integer}` divided by `{integer}`
22
--> $DIR/binary-op-on-double-ref.rs:5:11
33
|
44
LL | x % 2 == 0

tests/ui/binop/issue-28837.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111

1212
a / a; //~ ERROR cannot divide `A` by `A`
1313

14-
a % a; //~ ERROR cannot mod `A` by `A`
14+
a % a; //~ ERROR cannot calculate the remainder of `A` divided by `A`
1515

1616
a & a; //~ ERROR no implementation for `A & A`
1717

tests/ui/binop/issue-28837.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ LL | struct A;
6262
note: the trait `Div` must be implemented
6363
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
6464

65-
error[E0369]: cannot mod `A` by `A`
65+
error[E0369]: cannot calculate the remainder of `A` divided by `A`
6666
--> $DIR/issue-28837.rs:14:7
6767
|
6868
LL | a % a;

0 commit comments

Comments
 (0)