Rollup merge of #151455 - eggyal:normalized-byte-pos, r=cjgillot

Fix `SourceFile::normalized_byte_pos`

This method was broken by 258ace6, which changed `self.normalized_pos` to use relative offsets however this method continued to compare against an absolute offset.

Also adds a regression test for the issue that this method was originally introduced to fix.

Closes rust-lang/rust#149568
Fixes regression of rust-lang/rust#110885

r? cjgillot (as author of the breaking commit)
This commit is contained in:
Jonathan Brouwer
2026-02-08 19:15:24 +01:00
committed by GitHub
7 changed files with 47 additions and 9 deletions
+6 -8
View File
@@ -2402,14 +2402,12 @@ pub fn original_relative_byte_pos(&self, pos: BytePos) -> RelativeBytePos {
/// normalized one. Hence we need to convert those offsets to the normalized
/// form when constructing spans.
pub fn normalized_byte_pos(&self, offset: u32) -> BytePos {
let diff = match self
.normalized_pos
.binary_search_by(|np| (np.pos.0 + np.diff).cmp(&(self.start_pos.0 + offset)))
{
Ok(i) => self.normalized_pos[i].diff,
Err(0) => 0,
Err(i) => self.normalized_pos[i - 1].diff,
};
let diff =
match self.normalized_pos.binary_search_by(|np| (np.pos.0 + np.diff).cmp(&offset)) {
Ok(i) => self.normalized_pos[i].diff,
Err(0) => 0,
Err(i) => self.normalized_pos[i - 1].diff,
};
BytePos::from_u32(self.start_pos.0 + offset - diff)
}
@@ -290,7 +290,7 @@ pub(super) fn handle_needs(
}
// Handled elsewhere.
if name == "needs-llvm-components" {
if name == "needs-llvm-components" || name == "needs-backends" {
return IgnoreDecision::Continue;
}
+1
View File
@@ -219,6 +219,7 @@ fn check_unexpected_extension(check: &mut RunningCheck, file_path: &Path, ext: &
const EXTENSION_EXCEPTION_PATHS: &[&str] = &[
"tests/ui/asm/named-asm-labels.s", // loading an external asm file to test named labels lint
"tests/ui/asm/normalize-offsets-for-crlf.s", // loading an external asm file to test CRLF normalization
"tests/ui/codegen/mismatched-data-layout.json", // testing mismatched data layout w/ custom targets
"tests/ui/check-cfg/my-awesome-platform.json", // testing custom targets with cfgs
"tests/ui/argfile/commandline-argfile-badutf8.args", // passing args via a file
+2
View File
@@ -0,0 +1,2 @@
# Disable EOL normalization, as it is deliberately denormalized
normalize-offsets-for-crlf.s -text
@@ -0,0 +1,14 @@
// Byte positions into inline assembly reported by codegen errors require normalization or else
// they may not identify the appropriate span. Worse still, an ICE can occur if the erroneous
// span begins or ends part-way through a multibyte character.
//
// Regression test for https://github.com/rust-lang/rust/issues/110885
// This test is tied to assembler syntax and errors, which can vary by backend and architecture.
//@only-x86_64
//@needs-backends: llvm
//@build-fail
//~? ERROR instruction mnemonic
std::arch::global_asm!(include_str!("normalize-offsets-for-crlf.s"));
fn main() {}
+13
View File
@@ -0,0 +1,13 @@
// This file contains (some) CRLF line endings. When codegen reports an error, the byte
// offsets into this file that it identifies require normalization or else they will not
// identify the appropriate span. Worse still, an ICE can result if the erroneous span
// begins or ends part-way through a multibyte character such as £.
non_existent_mnemonic
// Without normalization, the three CRLF line endings below cause the diagnostic on the
// `non_existent_mnemonic` above to be spanned three bytes backward, and thus begin
// part-way inside the multibyte character in the preceding comment.
//
// NOTE: The lines of this note DELIBERATELY end with CRLF - DO NOT strip/convert them!
// It may not be obvious if you accidentally do, eg `git diff` may appear to show
// that the lines have been updated to the exact same content.
@@ -0,0 +1,10 @@
error: invalid instruction mnemonic 'non_existent_mnemonic'
|
note: instantiated into assembly here
--> <inline asm>:6:1
|
LL | non_existent_mnemonic
| ^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error