Skip to content

Commit e86b241

Browse files
authored
Merge pull request #251 from oli-obk/cleanups
Fully switch to byte based offsets
2 parents 7ac1d05 + 54843d6 commit e86b241

File tree

27 files changed

+441
-269
lines changed

27 files changed

+441
-269
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
### Removed
1717

18+
## [0.25.0] - 2024-07-24
19+
20+
### Added
21+
22+
### Fixed
23+
24+
* panics when ui_test tried to show diagnostics on multi-byte chars
25+
26+
### Changed
27+
28+
### Removed
29+
1830

1931
## [0.24.0] - 2024-07-11
2032

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ui_test"
3-
version = "0.24.0"
3+
version = "0.25.0"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "A test framework for testing rustc diagnostics output"
@@ -28,7 +28,7 @@ indicatif = "0.17.6"
2828
prettydiff = { version = "0.7", default-features = false }
2929
annotate-snippets = { version = "0.11.2" }
3030
levenshtein = "1.0.5"
31-
spanned = "0.2.1"
31+
spanned = "0.3.0"
3232

3333
[dependencies.regex]
3434
version = "1.5.5"

src/aux_builds.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ impl Flag for AuxBuilder {
2929
) -> Result<(), Errored> {
3030
let aux = &self.aux_file;
3131
let aux_dir = config.aux_dir;
32-
let line = aux.line();
3332
let aux_file = if aux.starts_with("..") {
3433
aux_dir.parent().unwrap().join(&aux.content)
3534
} else {
@@ -60,9 +59,8 @@ impl Flag for AuxBuilder {
6059
}| Errored {
6160
command,
6261
errors: vec![Error::Aux {
63-
path: aux_file.to_path_buf(),
62+
path: Spanned::new(aux_file.to_path_buf(), aux.span()),
6463
errors,
65-
line,
6664
}],
6765
stderr,
6866
stdout,
@@ -84,20 +82,21 @@ pub struct AuxBuilder {
8482
impl Build for AuxBuilder {
8583
fn build(&self, build_manager: &BuildManager<'_>) -> Result<Vec<OsString>, Errored> {
8684
let mut config = build_manager.config().clone();
87-
let file_contents = std::fs::read(&self.aux_file.content).map_err(|err| Errored {
88-
command: format!("reading aux file `{}`", display(&self.aux_file)),
89-
errors: vec![],
90-
stderr: err.to_string().into_bytes(),
91-
stdout: vec![],
92-
})?;
93-
let comments = Comments::parse(&file_contents, &config, &self.aux_file)
85+
let file_contents =
86+
Spanned::read_from_file(&self.aux_file.content).map_err(|err| Errored {
87+
command: format!("reading aux file `{}`", display(&self.aux_file)),
88+
errors: vec![],
89+
stderr: err.to_string().into_bytes(),
90+
stdout: vec![],
91+
})?;
92+
let comments = Comments::parse(file_contents.as_ref(), &config)
9493
.map_err(|errors| Errored::new(errors, "parse aux comments"))?;
9594
assert_eq!(
9695
comments.revisions, None,
9796
"aux builds cannot specify revisions"
9897
);
9998

100-
default_per_file_config(&mut config, &self.aux_file, &file_contents);
99+
default_per_file_config(&mut config, &file_contents);
101100

102101
match CrateType::from_file_contents(&file_contents) {
103102
// Proc macros must be run on the host

src/custom_flags/run.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
//! Types used for running tests after they pass compilation
22
33
use bstr::ByteSlice;
4-
use spanned::{Span, Spanned};
5-
use std::{
6-
path::PathBuf,
7-
process::{Command, Output},
8-
};
4+
use spanned::Spanned;
5+
use std::process::{Command, Output};
96

107
use crate::{
118
build_manager::BuildManager, display, per_test_config::TestConfig, Error, Errored, TestOk,
@@ -116,19 +113,25 @@ fn get_panic_span(stderr: &[u8]) -> Spanned<String> {
116113
let Ok(filename) = filename.to_str() else {
117114
continue;
118115
};
119-
let Ok(line) = line.parse() else {
116+
let Ok(line) = line.parse::<usize>() else {
117+
continue;
118+
};
119+
let Ok(col) = col.parse::<usize>() else {
120+
continue;
121+
};
122+
let Ok(file) = Spanned::read_from_file(filename) else {
123+
continue;
124+
};
125+
let Some(line) = line.checked_sub(1) else {
120126
continue;
121127
};
122-
let Ok(col) = col.parse() else {
128+
let Some(line) = file.lines().nth(line) else {
123129
continue;
124130
};
125-
let span = Span {
126-
file: PathBuf::from(filename),
127-
line_start: line,
128-
line_end: line,
129-
col_start: col,
130-
col_end: col,
131+
let Some(col) = col.checked_sub(1) else {
132+
continue;
131133
};
134+
let span = line.span.inc_col_start(col);
132135
return Spanned::new(message.into(), span);
133136
}
134137
}

src/custom_flags/rustfix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ fn compile_fixed(
234234
.iter()
235235
.flatten()
236236
.chain(diagnostics.messages_from_unknown_file_or_line.iter())
237-
.find_map(|message| message.line_col.clone())
237+
.find_map(|message| message.span.clone())
238238
.unwrap_or_default(),
239239
),
240240
}],

src/diagnostics.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ pub struct Message {
5656
/// The main message of the diagnostic (what will be matched for with `//~`)
5757
pub message: String,
5858
/// Information about where in the file the message was emitted
59-
pub line_col: Option<spanned::Span>,
59+
pub line: Option<usize>,
60+
/// Exact span information of the message
61+
pub span: Option<spanned::Span>,
6062
/// Identifier of the message (E0XXX for rustc errors, or lint names)
6163
pub code: Option<String>,
6264
}

src/error.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub enum Error {
5656
/// The main message of the error.
5757
msgs: Vec<Message>,
5858
/// File and line information of the error.
59-
path: Option<Spanned<PathBuf>>,
59+
path: Option<(PathBuf, NonZeroUsize)>,
6060
},
6161
/// A comment failed to parse.
6262
InvalidComment {
@@ -72,7 +72,7 @@ pub enum Error {
7272
/// The comment being looked for
7373
kind: String,
7474
/// The lines where conflicts happened
75-
lines: Vec<NonZeroUsize>,
75+
lines: Vec<Span>,
7676
},
7777
/// A subcommand (e.g. rustfix) of a test failed.
7878
Command {
@@ -86,11 +86,9 @@ pub enum Error {
8686
/// An auxiliary build failed with its own set of errors.
8787
Aux {
8888
/// Path to the aux file.
89-
path: PathBuf,
89+
path: Spanned<PathBuf>,
9090
/// The errors that occurred during the build of the aux file.
9191
errors: Vec<Error>,
92-
/// The line in which the aux file was requested to be built.
93-
line: NonZeroUsize,
9492
},
9593
/// An error occured applying [`rustfix`] suggestions
9694
Rustfix(anyhow::Error),

src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub use core::run_and_collect;
1818
pub use core::CrateType;
1919
pub use filter::Match;
2020
use per_test_config::TestConfig;
21+
use spanned::Spanned;
2122
use status_emitter::{StatusEmitter, TestStatus};
2223
use std::collections::VecDeque;
2324
use std::path::Path;
@@ -117,7 +118,7 @@ pub fn default_any_file_filter(path: &Path, config: &Config) -> bool {
117118
}
118119

119120
/// The default per-file config used by `run_tests`.
120-
pub fn default_per_file_config(config: &mut Config, _path: &Path, file_contents: &[u8]) {
121+
pub fn default_per_file_config(config: &mut Config, file_contents: &Spanned<Vec<u8>>) {
121122
config.program.args.push(
122123
match CrateType::from_file_contents(file_contents) {
123124
CrateType::ProcMacro => "--crate-type=proc-macro",
@@ -137,9 +138,9 @@ pub fn test_command(mut config: Config, path: &Path) -> Result<Command> {
137138

138139
config.fill_host_and_target()?;
139140

140-
let content =
141-
std::fs::read(path).wrap_err_with(|| format!("failed to read {}", display(path)))?;
142-
let comments = Comments::parse(&content, &config, path)
141+
let content = Spanned::read_from_file(path)
142+
.wrap_err_with(|| format!("failed to read {}", display(path)))?;
143+
let comments = Comments::parse(content.as_ref(), &config)
143144
.map_err(|errors| color_eyre::eyre::eyre!("{errors:#?}"))?;
144145
let config = TestConfig {
145146
config,
@@ -166,7 +167,7 @@ pub fn test_command(mut config: Config, path: &Path) -> Result<Command> {
166167
pub fn run_tests_generic(
167168
mut configs: Vec<Config>,
168169
file_filter: impl Fn(&Path, &Config) -> Option<bool> + Sync,
169-
per_file_config: impl Fn(&mut Config, &Path, &[u8]) + Sync,
170+
per_file_config: impl Fn(&mut Config, &Spanned<Vec<u8>>) + Sync,
170171
status_emitter: impl StatusEmitter + Send,
171172
) -> Result<()> {
172173
if nextest::emulate(&mut configs) {
@@ -232,9 +233,9 @@ pub fn run_tests_generic(
232233
|receive, finished_files_sender| -> Result<()> {
233234
for (status, build_manager) in receive {
234235
let path = status.path();
235-
let file_contents = std::fs::read(path).unwrap();
236+
let file_contents = Spanned::read_from_file(path).unwrap();
236237
let mut config = build_manager.config().clone();
237-
per_file_config(&mut config, path, &file_contents);
238+
per_file_config(&mut config, &file_contents);
238239
let result = match std::panic::catch_unwind(|| {
239240
parse_and_test_file(build_manager, &status, config, file_contents)
240241
}) {
@@ -317,9 +318,9 @@ fn parse_and_test_file(
317318
build_manager: &BuildManager<'_>,
318319
status: &dyn TestStatus,
319320
config: Config,
320-
file_contents: Vec<u8>,
321+
file_contents: Spanned<Vec<u8>>,
321322
) -> Result<Vec<TestRun>, Errored> {
322-
let comments = Comments::parse(&file_contents, &config, status.path())
323+
let comments = Comments::parse(file_contents.as_ref(), &config)
323324
.map_err(|errors| Errored::new(errors, "parse comments"))?;
324325
const EMPTY: &[String] = &[String::new()];
325326
// Run the test for all revisions

0 commit comments

Comments
 (0)