From f2a4a3e88bc0dcbb13ea2d8a3e92b09fe9619616 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 31 Jan 2024 08:48:53 -0600 Subject: [PATCH 1/2] test(diagnostic): Show panic --- tests/testsuite/diagnostics.rs | 23 +++++++++++++++++++++++ tests/testsuite/main.rs | 1 + 2 files changed, 24 insertions(+) create mode 100644 tests/testsuite/diagnostics.rs diff --git a/tests/testsuite/diagnostics.rs b/tests/testsuite/diagnostics.rs new file mode 100644 index 00000000000..32632a64802 --- /dev/null +++ b/tests/testsuite/diagnostics.rs @@ -0,0 +1,23 @@ +use cargo_test_support::project; + +#[cargo_test] +fn dont_panic_on_render() { + let p = project() + .file( + "Cargo.toml", + r#" +[package] +name = "foo" +version = "0.1.0" +edition = "2021" +[[bench.foo]] +"#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("check") + .with_status(101) + .with_stderr_contains("attempt to subtract with overflow") + .run(); +} diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index 4643f25f834..1ec140d7618 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -79,6 +79,7 @@ mod cross_publish; mod custom_target; mod death; mod dep_info; +mod diagnostics; mod direct_minimal_versions; mod directory; mod doc; From 1c05d412af53d554f1f64470e5939bcfe953dd4f Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 31 Jan 2024 09:11:12 -0600 Subject: [PATCH 2/2] fix(diagnostic): Don't panic on empty spans There is another level to this bug where we better point to where the error occurs. --- src/cargo/util/toml/mod.rs | 2 +- tests/testsuite/diagnostics.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 0ffeda2ada8..f4c3b9034c7 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -121,7 +121,7 @@ fn read_manifest_from_str( .rfind('\n') .map(|s| s + 1) .unwrap_or(0); - let source_end = contents[span.end - 1..] + let source_end = contents[span.end.saturating_sub(1)..] .find('\n') .map(|s| s + span.end) .unwrap_or(contents.len()); diff --git a/tests/testsuite/diagnostics.rs b/tests/testsuite/diagnostics.rs index 32632a64802..2de841825f2 100644 --- a/tests/testsuite/diagnostics.rs +++ b/tests/testsuite/diagnostics.rs @@ -18,6 +18,13 @@ edition = "2021" p.cargo("check") .with_status(101) - .with_stderr_contains("attempt to subtract with overflow") + .with_stderr( + "\ +error: invalid type: map, expected a sequence +--> Cargo.toml:1:1 + | + | +", + ) .run(); }