Skip to content
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

Fix spans in error reporting with unicode characters. #1100

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hax-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ hax-adt-into.workspace = true
tracing.workspace = true
serde-brief ={ version = "*", features = ["std", "alloc"]}
zstd = "0.13.1"
miette = "7.2.0"

[features]
rustc = ["hax-frontend-exporter/rustc"]
26 changes: 3 additions & 23 deletions hax-types/src/diagnostics/report.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Diagnostics;
use annotate_snippets::*;
use miette::SourceOffset;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;
Expand All @@ -11,29 +12,8 @@ pub struct ReportCtx {
}

/// Translates a line and column position into an absolute offset
fn compute_offset(src: &str, mut line: usize, col: usize) -> usize {
let mut chars = src.chars().enumerate();
while line > 1 {
while let Some((_offset, ch)) = chars.next() {
if ch == '\n' {
break;
}
}
line -= 1;
}
let offset = chars
.clone()
.next()
.map(|(offset, _ch)| offset)
.unwrap_or(0);
let are_col_first_chars_blank = chars
.take(col)
.all(|(_offset, ch)| matches!(ch, ' ' | '\t'));
if are_col_first_chars_blank {
offset
} else {
offset + col
}
fn compute_offset(src: &str, line: usize, col: usize) -> usize {
SourceOffset::from_location(src, line, col).offset() + 1
}

impl ReportCtx {
Expand Down
Loading