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.
This commit is contained in:
Matthias Krüger 2023-02-01 05:54:37 +01:00 committed by GitHub
commit 0d2ab67742
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 10 additions and 8 deletions

View File

@ -335,7 +335,9 @@ fn check_overloaded_binop(
format!("cannot divide `{lhs_ty}` by `{rhs_ty}`")
}
hir::BinOpKind::Rem => {
format!("cannot mod `{lhs_ty}` by `{rhs_ty}`")
format!(
"cannot calculate the remainder of `{lhs_ty}` divided by `{rhs_ty}`"
)
}
hir::BinOpKind::BitAnd => {
format!("no implementation for `{lhs_ty} & {rhs_ty}`")

View File

@ -545,7 +545,7 @@ fn div(self, other: $t) -> $t { self / other }
#[lang = "rem"]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented(
message = "cannot mod `{Self}` by `{Rhs}`",
message = "cannot calculate the remainder of `{Self}` divided by `{Rhs}`",
label = "no implementation for `{Self} % {Rhs}`"
)]
#[doc(alias = "%")]
@ -981,7 +981,7 @@ fn div_assign(&mut self, other: $t) { *self /= other }
#[lang = "rem_assign"]
#[stable(feature = "op_assign_traits", since = "1.8.0")]
#[rustc_on_unimplemented(
message = "cannot mod-assign `{Self}` by `{Rhs}``",
message = "cannot calculate and assign the remainder of `{Self}` divided by `{Rhs}`",
label = "no implementation for `{Self} %= {Rhs}`"
)]
#[doc(alias = "%")]

View File

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

View File

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

View File

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

View File

@ -11,7 +11,7 @@ fn main() {
a / a; //~ ERROR cannot divide `A` by `A`
a % a; //~ ERROR cannot mod `A` by `A`
a % a; //~ ERROR cannot calculate the remainder of `A` divided by `A`
a & a; //~ ERROR no implementation for `A & A`

View File

@ -62,7 +62,7 @@ LL | struct A;
note: the trait `Div` must be implemented
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
error[E0369]: cannot mod `A` by `A`
error[E0369]: cannot calculate the remainder of `A` divided by `A`
--> $DIR/issue-28837.rs:14:7
|
LL | a % a;