Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit 3ddbccd

Browse files
committed
Do rustc's byte indices work?
cf. rust-lang/rust#35164
1 parent 421be71 commit 3ddbccd

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

examples/do-byte-indices-work.rs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
extern crate serde_json;
2+
extern crate rustfix;
3+
4+
use std::process::Command;
5+
6+
use rustfix::diagnostics::Diagnostic;
7+
8+
fn main() {
9+
let sorry_for_the_churn = Command::new("touch").arg("src/diagnostics.rs").output();
10+
let output = Command::new("cargo")
11+
.args(&["rustc",
12+
"--lib",
13+
"--quiet",
14+
"--color", "never",
15+
"--",
16+
"-Z", "unstable-options",
17+
"--error-format", "json"])
18+
.output()
19+
.unwrap();
20+
let content = String::from_utf8(output.stderr).unwrap();
21+
22+
let suggestions = content.lines()
23+
.filter(|s| !s.trim().is_empty())
24+
.flat_map(|line| serde_json::from_str::<Diagnostic>(line))
25+
.flat_map(|diag| diag.spans.into_iter())
26+
.take(5);
27+
28+
for s in suggestions {
29+
let file = read_file_to_string(&s.file_name).unwrap();
30+
let content_by_byte_index = file[s.byte_start..s.byte_end].to_string();
31+
let content_by_line_col_index: String = file.lines()
32+
.skip(s.line_start - 1)
33+
.take(s.line_end - s.line_start + 1)
34+
.enumerate()
35+
.map(|(i, line)| {
36+
let start = 0;
37+
let end = s.line_end - s.line_start;
38+
if i == start && i == end {
39+
line.chars().skip(s.column_start - 1).take(s.column_end - s.column_start).collect()
40+
} else if i == start {
41+
line.chars().skip(s.column_start - 1).collect()
42+
} else if i == end {
43+
line.chars().take(s.column_end).collect()
44+
} else {
45+
line.to_string()
46+
}
47+
})
48+
.collect();
49+
assert_eq!(content_by_byte_index, content_by_line_col_index);
50+
println!("Worked.");
51+
}
52+
}
53+
54+
fn read_file_to_string(file_name: &str) -> Result<String, std::io::Error> {
55+
use std::fs::File;
56+
use std::io::Read;
57+
58+
let mut file = File::open(file_name)?;
59+
let mut buffer = String::new();
60+
file.read_to_string(&mut buffer)?;
61+
Ok(buffer)
62+
}

src/diagnostics.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Rustc Diagnostic JSON Output
2-
//!
2+
//!
33
//! The following data types are copied from [rust-lang/rust](https://github.com/rust-lang/rust/blob/de78655bca47cac8e783dbb563e7e5c25c1fae40/src/libsyntax/json.rs)
44
5+
use std::fmt::Write; // <- unused on purpose!
6+
57
#[derive(Serialize, Deserialize, Debug)]
68
pub struct Diagnostic {
79
/// The primary error message.

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ extern crate serde_json;
55
pub mod diagnostics;
66
use diagnostics::{Diagnostic, DiagnosticSpan};
77

8+
use std::fmt::Write; // <- unused on purpose!
9+
810
#[derive(Debug, Copy, Clone, Hash, PartialEq)]
911
pub struct LinePosition {
1012
pub line: usize,

0 commit comments

Comments
 (0)