Skip to content

Commit 14cd5b1

Browse files
committed
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.
1 parent 88ad3d4 commit 14cd5b1

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
lines changed

compiler/rustc_span/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,14 +2426,12 @@ impl SourceFile {
24262426
/// normalized one. Hence we need to convert those offsets to the normalized
24272427
/// form when constructing spans.
24282428
pub fn normalized_byte_pos(&self, offset: u32) -> BytePos {
2429-
let diff = match self
2430-
.normalized_pos
2431-
.binary_search_by(|np| (np.pos.0 + np.diff).cmp(&(self.start_pos.0 + offset)))
2432-
{
2433-
Ok(i) => self.normalized_pos[i].diff,
2434-
Err(0) => 0,
2435-
Err(i) => self.normalized_pos[i - 1].diff,
2436-
};
2429+
let diff =
2430+
match self.normalized_pos.binary_search_by(|np| (np.pos.0 + np.diff).cmp(&offset)) {
2431+
Ok(i) => self.normalized_pos[i].diff,
2432+
Err(0) => 0,
2433+
Err(i) => self.normalized_pos[i - 1].diff,
2434+
};
24372435

24382436
BytePos::from_u32(self.start_pos.0 + offset - diff)
24392437
}

src/tools/tidy/src/ui_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ fn check_unexpected_extension(check: &mut RunningCheck, file_path: &Path, ext: &
150150

151151
const EXTENSION_EXCEPTION_PATHS: &[&str] = &[
152152
"tests/ui/asm/named-asm-labels.s", // loading an external asm file to test named labels lint
153+
"tests/ui/asm/normalize-offsets-for-crlf.s", // loading an external asm file to test CRLF normalization
153154
"tests/ui/codegen/mismatched-data-layout.json", // testing mismatched data layout w/ custom targets
154155
"tests/ui/check-cfg/my-awesome-platform.json", // testing custom targets with cfgs
155156
"tests/ui/argfile/commandline-argfile-badutf8.args", // passing args via a file

tests/ui/asm/.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Disable EOL normalization, as it is deliberately denormalized
2+
normalize-offsets-for-crlf.s -text
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Byte positions into inline assembly reported by codegen errors require normalization or else
2+
// they will not identify the appropriate span. Worse still, an ICE can result if the resulting
3+
// span begins or ends part-way through a multibyte character.
4+
//
5+
// Regression test for https://github.com/rust-lang/rust/issues/110885
6+
7+
//@build-fail
8+
//~? ERROR mnemonic
9+
10+
std::arch::global_asm!(include_str!("normalize-offsets-for-crlf.s"));
11+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This file contains (some) CRLF line endings. When codegen reports an error, the byte
2+
// offsets into this file that it identifies require normalization or else they will not
3+
// identify the appropriate span. Worse still, an ICE can result if the resulting span
4+
// begins or ends part-way through a multibyte character such as £.
5+
non_existent_mnemonic
6+
7+
// NOTE: The following lines DELIBERATELY end with CRLF - DO NOT strip/convert them!
8+
// It may not be obvious if you accidentally do, eg `git diff` may appear to show
9+
// that the lines have been updated to the exact same content.
10+
//
11+
//
12+
//
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: invalid instruction mnemonic 'non_existent_mnemonic'
2+
|
3+
note: instantiated into assembly here
4+
--> <inline asm>:6:1
5+
|
6+
LL | non_existent_mnemonic
7+
| ^^^^^^^^^^^^^^^^^^^^^
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)