GizmoLineStyle: derived PartialEq with manual Hash breaks the Eq/Hash contract for -0.0

Aperta Adatta ai principianti
#26,635 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
game-dev

Direzione di ricerca

Il test di riproduzione si trova in crates/bevy_gizmos/src/config.rs, accanto all'impl Hash manuale di GizmoLineStyle, che calcola l'hash dei campi Dashed con to_bits(). Inizia eseguendo il test con cargo test -p bevy_gizmos --lib --no-default-features. È completato quando 0.0 e -0.0 producono lo stesso valore di hash, il test passa e il comportamento con NaN è stabilito, rifiutando NaN oppure confrontando i bit in PartialEq. Verifica che le struct chiave in crates/bevy_gizmos_render/src/pipeline_2d.rs e pipeline_3d.rs continuino a comportarsi come previsto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Bevy version and features

Bevy main (8c3f96155d), crate bevy_gizmos, built with --no-default-features.

[Optional] Relevant system information

Linux x86_64, rustc 1.100.0.

What you did

Add to crates/bevy_gizmos/src/config.rs and run
cargo test -p bevy_gizmos --lib --no-default-features gizmo_line_style_eq_hash_minus_zero:

#[cfg(test)]
mod kern_eq_hash_test {
    use super::*;
    use std::collections::HashSet;

    #[test]
    fn gizmo_line_style_eq_hash_minus_zero() {
        let a = GizmoLineStyle::Dashed { gap_scale: 0.0, line_scale: 1.0 };
        let b = GizmoLineStyle::Dashed { gap_scale: -0.0, line_scale: 1.0 };
        assert_eq!(a, b, "derived PartialEq compares f32 numerically");
        let mut set = HashSet::new();
        assert!(set.insert(a), "fresh key");
        assert!(set.contains(&b), "Eq/Hash contract broken on -0.0");
    }
}

What went wrong

thread 'config::kern_eq_hash_test::gizmo_line_style_eq_hash_minus_zero' panicked at
crates/bevy_gizmos/src/config.rs:326:9:
Eq/Hash contract broken on -0.0
test result: FAILED. 0 passed; 1 failed

GizmoLineStyle derives PartialEq, declares impl Eq, and hashes the Dashed
variant as gap_scale.to_bits() / line_scale.to_bits(). 0.0 and -0.0 are
equal under the derived PartialEq but produce different bits, so equal values
hash differently and a key inserted into a HashSet/HashMap is not found again.

This is not a theoretical key. LineGizmoPipelineKey contains
line_style: GizmoLineStyle and is used as the Specializer::Key, i.e. as a key
of the SpecializedRenderPipelines cache:

  • crates/bevy_gizmos_render/src/pipeline_2d.rs:99 — struct LineGizmoPipelineKey { …, line_style: GizmoLineStyle }
  • crates/bevy_gizmos_render/src/pipeline_3d.rs:123 — same struct for the 3D path

So two gizmos that differ only in the sign of zero specialize two separate render
pipelines for a visually identical line.

Secondary: gap_scale and line_scale are f32 while impl Eq is declared, so
Dashed { gap_scale: f32::NAN, .. } is not equal to itself, which contradicts
reflexivity.

Additional information

Suggested fix — canonicalise the sign of zero before hashing, and decide
explicitly what to do with NaN (either reject it or compare bits in PartialEq):

fn canonical(v: f32) -> f32 {
    if v == 0.0 { 0.0 } else { v }
}

impl Hash for GizmoLineStyle {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Self::Solid => {
                0u64.hash(state);
            }
            Self::Dotted => 1u64.hash(state),
            Self::Dashed { gap_scale, line_scale } => {
                2u64.hash(state);
                canonical(*gap_scale).to_bits().hash(state);
                canonical(*line_scale).to_bits().hash(state);
            }
        }
    }
}

Happy to open a PR.

Lingua principale
Rust
Stelle
49.8k
Fork
5.1k
Merge medio
3g 4h
PR unite (30g)
136

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bevyengine/bevy

Tutte le issue di bevyengine/bevy

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.