-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix panic in annotated_snippet dependency (GitHub issue #4968). #5629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ use std::path::PathBuf; | |
|
|
||
| use super::read_config; | ||
|
|
||
| use crate::FormatReportFormatterBuilder; | ||
| use crate::modules::{ModuleResolutionError, ModuleResolutionErrorKind}; | ||
| use crate::{ErrorKind, Input, Session}; | ||
|
|
||
|
|
@@ -69,3 +70,21 @@ fn crate_parsing_stashed_diag2() { | |
| let filename = "tests/parser/stashed-diag2.rs"; | ||
| assert_parser_error(filename); | ||
| } | ||
|
|
||
| #[test] | ||
| fn indexing_mismatch_with_annotated_snippet() { | ||
| // See also https://github.com/rust-lang/rustfmt/issues/4968 | ||
| let filename = "tests/parser/issue_4968.rs"; | ||
| let file = PathBuf::from(filename); | ||
| let config = read_config(&file); | ||
| let mut session = Session::<io::Stdout>::new(config, None); | ||
| let report = session.format(Input::File(filename.into())).unwrap(); | ||
| let report = FormatReportFormatterBuilder::new(&report).build(); | ||
| { | ||
| // Panic can only be triggered if we actually try to write the report | ||
| // and call into the annotated_snippet dependency. | ||
| use std::io::Write; | ||
|
|
||
| write!(&mut Vec::new(), "{report}").unwrap(); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively: if you're happy just testing things run ok: you could move this to be an integration test: move The alternative being: testing the diagnostics are displayed ok, but trying to do that would require basically testing behaviour of the |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // rustfmt-hard_tabs: true | ||
| // rustfmt-max_width: 40 | ||
| // rustfmt-error_on_unformatted: true | ||
| // rustfmt-error_on_line_overflow: true | ||
|
|
||
| fn foo(x: u32) { | ||
| if x > 10 { | ||
| if x > 20 { | ||
| println!("0123456789abcdefghijklmnopqrstuvwxyz"); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might produce some slightly odd diagnostics if the line overflows on a tab character, specifically, the diagnostic will point at a single space and not cover a full tab, e.g.
I think the best way to address this would be to update
annotate-snippets, which handles rendering with tabs more nicely (I had a play around #6635 (comment)). But I don't think that should block/stop merging this change, since that will be a chunk of extra work and fixing a panic is worth the edge case above.