Skip to content

Commit b65a52c

Browse files
Rollup merge of rust-lang#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#149568 Fixes regression of rust-lang#110885 r? cjgillot (as author of the breaking commit)
2 parents be4794c + 01290cc commit b65a52c

7 files changed

Lines changed: 47 additions & 9 deletions

File tree

compiler/rustc_span/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,14 +2402,12 @@ impl SourceFile {
24022402
/// normalized one. Hence we need to convert those offsets to the normalized
24032403
/// form when constructing spans.
24042404
pub fn normalized_byte_pos(&self, offset: u32) -> BytePos {
2405-
let diff = match self
2406-
.normalized_pos
2407-
.binary_search_by(|np| (np.pos.0 + np.diff).cmp(&(self.start_pos.0 + offset)))
2408-
{
2409-
Ok(i) => self.normalized_pos[i].diff,
2410-
Err(0) => 0,
2411-
Err(i) => self.normalized_pos[i - 1].diff,
2412-
};
2405+
let diff =
2406+
match self.normalized_pos.binary_search_by(|np| (np.pos.0 + np.diff).cmp(&offset)) {
2407+
Ok(i) => self.normalized_pos[i].diff,
2408+
Err(0) => 0,
2409+
Err(i) => self.normalized_pos[i - 1].diff,
2410+
};
24132411

24142412
BytePos::from_u32(self.start_pos.0 + offset - diff)
24152413
}

src/tools/compiletest/src/directives/needs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub(super) fn handle_needs(
290290
}
291291

292292
// Handled elsewhere.
293-
if name == "needs-llvm-components" {
293+
if name == "needs-llvm-components" || name == "needs-backends" {
294294
return IgnoreDecision::Continue;
295295
}
296296

src/tools/tidy/src/ui_tests.rs

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

220220
const EXTENSION_EXCEPTION_PATHS: &[&str] = &[
221221
"tests/ui/asm/named-asm-labels.s", // loading an external asm file to test named labels lint
222+
"tests/ui/asm/normalize-offsets-for-crlf.s", // loading an external asm file to test CRLF normalization
222223
"tests/ui/codegen/mismatched-data-layout.json", // testing mismatched data layout w/ custom targets
223224
"tests/ui/check-cfg/my-awesome-platform.json", // testing custom targets with cfgs
224225
"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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Byte positions into inline assembly reported by codegen errors require normalization or else
2+
// they may not identify the appropriate span. Worse still, an ICE can occur if the erroneous
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+
// This test is tied to assembler syntax and errors, which can vary by backend and architecture.
8+
//@only-x86_64
9+
//@needs-backends: llvm
10+
//@build-fail
11+
12+
//~? ERROR instruction mnemonic
13+
std::arch::global_asm!(include_str!("normalize-offsets-for-crlf.s"));
14+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 erroneous span
4+
// begins or ends part-way through a multibyte character such as £.
5+
non_existent_mnemonic
6+
7+
// Without normalization, the three CRLF line endings below cause the diagnostic on the
8+
// `non_existent_mnemonic` above to be spanned three bytes backward, and thus begin
9+
// part-way inside the multibyte character in the preceding comment.
10+
//
11+
// NOTE: The lines of this note DELIBERATELY end with CRLF - DO NOT strip/convert them!
12+
// It may not be obvious if you accidentally do, eg `git diff` may appear to show
13+
// that the lines have been updated to the exact same content.
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)