Skip to content

Commit

Permalink
Merge pull request #1100 from hacspec/fix-spans-unicode
Browse files Browse the repository at this point in the history
Fix spans in error reporting with unicode characters.
  • Loading branch information
franziskuskiefer authored Nov 5, 2024
2 parents a80e986 + ccff2d9 commit acb0c3d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
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

0 comments on commit acb0c3d

Please sign in to comment.