Skip to content

bug(syntax): Ord on ast::Position is inconsistent with Eq #1393

Description

@langston-barrett

What version of regex are you using?

0.8.11

Describe the bug at a high level.

Ord for Position is keyed off of offset only, but its Eq and PartialEq are derived, so they can disagree.

self.offset.cmp(&other.offset)

This affects Span as well, since it stores Positions.

The methods of this trait must be consistent with each other and with those of PartialEq.

https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html

What are the steps to reproduce the behavior?

use std::collections::BTreeSet;
use regex_syntax::ast::{Position, Span};

fn main() {
    let a = Position { offset: 0, line: 1, column: 1 };
    let b = Position { offset: 0, line: 9, column: 9 };

    assert_ne!(a, b);
    assert_eq!(a.cmp(&b), std::cmp::Ordering::Equal);

    let mut set = BTreeSet::new();
    set.insert(a);
    set.insert(b);
    assert_eq!(set.len(), 1);
}

What is the actual behavior?

BTreeSet is confused, all assertions pass.

What is the expected behavior?

Both a and b are inserted.

Found with a fuzzer I'm developing.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions