From 01f2a1542006631d6644ac767926e3bd3324f3c0 Mon Sep 17 00:00:00 2001 From: Mahmoud Mazouz Date: Sat, 19 Nov 2022 18:53:36 +0100 Subject: [PATCH 1/2] Fix substraction with overflow in `wrong_number_of_generic_args.rs` Rarranging the substration and equality check into an addition and an equality check is sufficient. Algebra is cool, isn't it? Co-authored-by: Michael Goulet --- .../src/structured_errors/wrong_number_of_generic_args.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs b/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs index 4359124646df1..9c77387c238fe 100644 --- a/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs +++ b/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs @@ -728,7 +728,8 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> { && let Some(trait_path_segment) = path.segments.get(0) { let num_generic_args_supplied_to_trait = trait_path_segment.args().num_generic_params(); - if num_assoc_fn_excess_args == num_trait_generics_except_self - num_generic_args_supplied_to_trait { + if num_generic_args_supplied_to_trait + num_assoc_fn_excess_args == num_trait_generics_except_self + { if let Some(span) = self.gen_args.span_ext() && let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) { let sugg = vec![ From 3046af0cf652a4ccecd823d7b5cf81fbf026ab38 Mon Sep 17 00:00:00 2001 From: Mahmoud Mazouz Date: Sat, 19 Nov 2022 18:54:19 +0100 Subject: [PATCH 2/2] Add a UI test to ensure rustc doesn't do arithmetic overflows This relies on the CI testing a rustc that's compiled with overflow-checks = true --- src/test/ui/suggestions/issue-104287.rs | 9 ++++++ src/test/ui/suggestions/issue-104287.stderr | 36 +++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/test/ui/suggestions/issue-104287.rs create mode 100644 src/test/ui/suggestions/issue-104287.stderr diff --git a/src/test/ui/suggestions/issue-104287.rs b/src/test/ui/suggestions/issue-104287.rs new file mode 100644 index 0000000000000..b7601a548b998 --- /dev/null +++ b/src/test/ui/suggestions/issue-104287.rs @@ -0,0 +1,9 @@ +// The purpose of this test is not to validate the output of the compiler. +// Instead, it ensures the suggestion is generated without performing an arithmetic overflow. + +fn main() { + let x = not_found; //~ ERROR cannot find value `not_found` in this scope + simd_gt::<()>(x); + //~^ ERROR this associated function takes 0 generic arguments but 1 generic argument was supplied + //~| ERROR cannot find function `simd_gt` in this scope +} diff --git a/src/test/ui/suggestions/issue-104287.stderr b/src/test/ui/suggestions/issue-104287.stderr new file mode 100644 index 0000000000000..4b302dd650919 --- /dev/null +++ b/src/test/ui/suggestions/issue-104287.stderr @@ -0,0 +1,36 @@ +error[E0425]: cannot find value `not_found` in this scope + --> $DIR/issue-104287.rs:5:13 + | +LL | let x = not_found; + | ^^^^^^^^^ not found in this scope + +error[E0107]: this associated function takes 0 generic arguments but 1 generic argument was supplied + --> $DIR/issue-104287.rs:6:5 + | +LL | simd_gt::<()>(x); + | ^^^^^^^------ help: remove these generics + | | + | expected 0 generic arguments + | +note: associated function defined here, with 0 generic parameters + --> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/ord.rs:LL:COL + | +LL | fn simd_gt(self, other: Self) -> Self::Mask; + | ^^^^^^^ + +error[E0425]: cannot find function `simd_gt` in this scope + --> $DIR/issue-104287.rs:6:5 + | +LL | simd_gt::<()>(x); + | ^^^^^^^ not found in this scope + | +help: use the `.` operator to call the method `SimdPartialOrd::simd_gt` on `[type error]` + | +LL - simd_gt::<()>(x); +LL + x.simd_gt(); + | + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0107, E0425. +For more information about an error, try `rustc --explain E0107`.