Skip to content

Commit 660b5c3

Browse files
committed
Use byte offsets instead of char offsets in debuginfo
cc #807
1 parent 092cae1 commit 660b5c3

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/debuginfo/line_info.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::{Component, Path};
33

44
use crate::prelude::*;
55

6-
use rustc_span::FileName;
6+
use rustc_span::{FileName, SourceFileAndLine};
77

88
use cranelift_codegen::binemit::CodeOffset;
99

@@ -139,25 +139,32 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
139139
rustc_span::hygiene::walk_chain(span, function_span.ctxt())
140140
};
141141

142-
let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
142+
let (file, line, col) = match tcx.sess.source_map().lookup_line(span.lo()) {
143+
Ok(SourceFileAndLine { sf: file, line }) => {
144+
let line_pos = file.line_begin_pos(span.lo());
145+
146+
(file, u64::try_from(line).unwrap() + 1, u64::from((span.lo() - line_pos).to_u32()) + 1)
147+
}
148+
Err(file) => (file, 0, 0)
149+
};
143150

144151
// line_program_add_file is very slow.
145152
// Optimize for the common case of the current file not being changed.
146153
let current_file_changed = if let Some(last_file) = &mut last_file {
147154
// If the allocations are not equal, then the files may still be equal, but that
148155
// is not a problem, as this is just an optimization.
149-
!Lrc::ptr_eq(last_file, &loc.file)
156+
!Lrc::ptr_eq(last_file, &file)
150157
} else {
151158
true
152159
};
153160
if current_file_changed {
154-
let file_id = line_program_add_file(line_program, line_strings, &loc.file.name);
161+
let file_id = line_program_add_file(line_program, line_strings, &file.name);
155162
line_program.row().file = file_id;
156-
last_file = Some(loc.file.clone());
163+
last_file = Some(file.clone());
157164
}
158165

159-
line_program.row().line = loc.line as u64;
160-
line_program.row().column = loc.col.to_u32() as u64 + 1;
166+
line_program.row().line = line;
167+
line_program.row().column = col;
161168
line_program.generate_row();
162169
};
163170

0 commit comments

Comments
 (0)