rust/tests
Matthias Krüger cb5dd1d598
Rollup merge of #111745 - Badel2:emitter-add-overflow, r=compiler-errors
Fix overflow in error emitter

Fix #109854
Close #94171 (was already fixed before but missing test)

This bug happens when a multipart suggestion spans more than one line.

The fix is to update the `acc` variable, which didn't handle the case when the text to remove spans multiple lines but the text to add spans only one line.

Also, use `usize::try_from` instead of  `as usize` to detect overflows earlier in the future, and point to the source of the overflow (the original issue points to a different place where this value is used, not where the overflow had happened).

And finally add an `if start != end` check to avoid doing any extra work in case of empty ranges.

Long explanation:

Given this test case:

```rust
fn generate_setter() {
    String::with_capacity(
    //~^ ERROR this function takes 1 argument but 3 arguments were supplied
    generate_setter,
    r#"
pub(crate) struct Person<T: Clone> {}
"#,
     r#""#,
    );
}
```

The compiler will try to convert that code into the following:

```rust
fn generate_setter() {
    String::with_capacity(
    //~^ ERROR this function takes 1 argument but 3 arguments were supplied
    /* usize */,
    );
}
```

So it creates a suggestion with 3 separate parts:

```
// Replace "generate_setter" with "/* usize */"
SubstitutionPart { span: fuzz_input.rs:4:5: 4:20 (#0), snippet: "/* usize */" }
// Remove second arg (multiline string)
SubstitutionPart { span: fuzz_input.rs:4:20: 7:3 (#0), snippet: "" }
// Remove third arg (r#""#)
SubstitutionPart { span: fuzz_input.rs:7:3: 8:11 (#0), snippet: "" }
```

Each of this parts gets a separate `SubstitutionHighlight` (this marks the relevant text green in a terminal, the values are 0-indexed so `start: 4` means column 5):

```
SubstitutionHighlight { start: 4, end: 15 }
SubstitutionHighlight { start: 15, end: 15 }
SubstitutionHighlight { start: 18446744073709551614, end: 18446744073709551614 }
```

The 2nd and 3rd suggestion are empty (start = end) because they only remove text, so there are no additions to highlight. But the 3rd span has overflowed because the compiler assumes that the 3rd suggestion is on the same line as the first suggestion. The 2nd span starts at column 20 and the highlight starts at column 16 (15+1), so that suggestion is good. But since the 3rd span starts at column 3, the result is `3 - 4`, or column -1, which turns into -2 with 0-indexed, and that's equivalent to `18446744073709551614 as isize`.

With this fix, the resulting `SubstitutionHighlight` are:

```
SubstitutionHighlight { start: 4, end: 15 }
SubstitutionHighlight { start: 15, end: 15 }
SubstitutionHighlight { start: 15, end: 15 }
```

As expected. I guess ideally we shouldn't emit empty highlights when removing text, but I am too scared to change that.
2023-05-21 16:02:59 +02:00
..
assembly
auxiliary
codegen Auto merge of #111697 - rcvalle:rust-cfi-fix-111510, r=bjorn3 2023-05-21 06:42:05 +00:00
codegen-units
debuginfo Add multiple borrow test. 2023-05-13 10:32:32 +00:00
incremental Delay a bug when overwriting fed value. 2023-05-17 20:42:03 +00:00
mir-opt Auto merge of #111568 - scottmcm:undo-opt, r=WaffleLapkin 2023-05-17 18:53:26 +00:00
pretty
run-make Auto merge of #111675 - Urgau:fix-local-libs-for-native-static-libs, r=bjorn3 2023-05-21 01:12:45 +00:00
run-make-fulldeps Merge query property modules into one 2023-05-18 09:45:12 +02:00
run-pass-valgrind
rustdoc Add regression test for #111415 2023-05-16 14:35:46 +02:00
rustdoc-gui Rollup merge of #111765 - GuillaumeGomez:migrate-gui-test-color, r=notriddle 2023-05-20 15:37:24 +02:00
rustdoc-js
rustdoc-js-std
rustdoc-json rustdoc-json: Add tests for visibility of impls 2023-05-15 17:48:48 +00:00
rustdoc-ui Fix backtrace normalization in ice-bug-report-url.rs 2023-05-11 13:59:38 +02:00
ui Rollup merge of #111745 - Badel2:emitter-add-overflow, r=compiler-errors 2023-05-21 16:02:59 +02:00
ui-fulldeps
COMPILER_TESTS.md