Skip to content

Method SourceFile::normalized_byte_pos is broken #149568

Closed

Description

@cpud36

I have been looking through the source of rustc_span and stumbled upon an inconsistent usage of byte positions.

The problematic code is from SourceFile::normalized_byte_pos:

self
    .normalized_pos
    .binary_search_by(|np| (np.pos.0 + np.diff).cmp(&(self.start_pos.0 + offset)))

Here offset is a byte offset relative to the start of the file. The problem is, that NormalizedPos contains a relative byte pos, but we are comparing it with an absolute one. The current definition of NormalizedPos is the following:

pub struct NormalizedPos {
    /// The relative offset of the character in the `SourceFile`.
    pub pos: RelativeBytePos,
    /// The difference between original and normalized string at position.
    pub diff: u32,
}

When the method normalized_byte_pos was introduced(bba2a1e) the NormalizedPos used an absolute byte pos. But it was later to changed to use relative byte pos in 258ace6

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 2, 2025
  2. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Dec 3, 2025
  3. eggyal commented on Jan 21, 2026

    @eggyal
    Contributor

    @rustbot claim

  4. added 4 commits that reference this issue on Feb 8, 2026
    250de8c
    b65a52c
    ce7139b
    67db528
  5. added a commit that references this issue on Feb 8, 2026
    36ce3b6
  6. added a commit that references this issue on Feb 8, 2026
    329353e
  7. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Feb 8, 2026
  8. added a commit that references this issue on Feb 8, 2026
    a369697
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions