Skip to content

Key comparison is broken between 'static and borrowed keys #733

Description

@matteo-zeggiotti-ok

Two Keys wrapping the same string compare as not equal when one is built
from a 'static str and the other from a borrowed str:

let static_key = Key::from_str_static("a key");
let borrowed_key = Key::from_str("a key");

assert_eq!(static_key, borrowed_key); // panics

Cause: Key stores its string in a MaybeStaticStr enum and derives
PartialEq/PartialOrd/Ord. The derived impls compare the enum variant
first, so Static("a key") != Borrowed("a key") even though the contents match.
This breaks the invariant documented on the type ("these impls must only be
based on the as_str() representation").

Impact: key equality/ordering depends on how the key was constructed, so any
code that uses Key in a map/set, dedups or sorts keys can get wrong results
when keys come from a mix of 'static and borrowed sources.

Fix: implement the comparison traits by hand against as_str() so they
depend only on the string contents, not on the MaybeStaticStr variant.

PR: #733

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions