Skip to content

Method SourceFile::normalized_byte_pos is broken #149568

@cpud36

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

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