Skip to content

Commit 3e937d0

Browse files
author
Yuki Okushi
authored
Rollup merge of #104467 - fuzzypixelz:fix/attempt-to-substract-with-overflow, r=compiler-errors
Fix substraction with overflow in `wrong_number_of_generic_args.rs` Fixes #104287 This issue happens in the `suggest_moving_args_from_assoc_fn_to_trait_for_qualified_path` function, which seems to run before the error checking facilities can catch an invalid use of generic arguments. Thus we get a subtraction with overflow because the code implicitly assumes that the source program makes sense (or is this assumption not true even if the program is correct?).
2 parents 785237d + 3046af0 commit 3e937d0

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
728728
&& let Some(trait_path_segment) = path.segments.get(0) {
729729
let num_generic_args_supplied_to_trait = trait_path_segment.args().num_generic_params();
730730

731-
if num_assoc_fn_excess_args == num_trait_generics_except_self - num_generic_args_supplied_to_trait {
731+
if num_generic_args_supplied_to_trait + num_assoc_fn_excess_args == num_trait_generics_except_self
732+
{
732733
if let Some(span) = self.gen_args.span_ext()
733734
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
734735
let sugg = vec![
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// The purpose of this test is not to validate the output of the compiler.
2+
// Instead, it ensures the suggestion is generated without performing an arithmetic overflow.
3+
4+
fn main() {
5+
let x = not_found; //~ ERROR cannot find value `not_found` in this scope
6+
simd_gt::<()>(x);
7+
//~^ ERROR this associated function takes 0 generic arguments but 1 generic argument was supplied
8+
//~| ERROR cannot find function `simd_gt` in this scope
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error[E0425]: cannot find value `not_found` in this scope
2+
--> $DIR/issue-104287.rs:5:13
3+
|
4+
LL | let x = not_found;
5+
| ^^^^^^^^^ not found in this scope
6+
7+
error[E0107]: this associated function takes 0 generic arguments but 1 generic argument was supplied
8+
--> $DIR/issue-104287.rs:6:5
9+
|
10+
LL | simd_gt::<()>(x);
11+
| ^^^^^^^------ help: remove these generics
12+
| |
13+
| expected 0 generic arguments
14+
|
15+
note: associated function defined here, with 0 generic parameters
16+
--> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/ord.rs:LL:COL
17+
|
18+
LL | fn simd_gt(self, other: Self) -> Self::Mask;
19+
| ^^^^^^^
20+
21+
error[E0425]: cannot find function `simd_gt` in this scope
22+
--> $DIR/issue-104287.rs:6:5
23+
|
24+
LL | simd_gt::<()>(x);
25+
| ^^^^^^^ not found in this scope
26+
|
27+
help: use the `.` operator to call the method `SimdPartialOrd::simd_gt` on `[type error]`
28+
|
29+
LL - simd_gt::<()>(x);
30+
LL + x.simd_gt();
31+
|
32+
33+
error: aborting due to 3 previous errors
34+
35+
Some errors have detailed explanations: E0107, E0425.
36+
For more information about an error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)