Skip to content

Commit

Permalink
impl Ord for Diagnostics and Issue so they are sorted correctly (#8805)
Browse files Browse the repository at this point in the history
### Description

impl Ord for Diagnostics and Issue so they are sorted correctly

Collectibles are unordered by default and need to be ordered correctly
to avoid over invalidation

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
sokra authored Jul 22, 2024
1 parent 59b2e90 commit 7677ac8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
37 changes: 34 additions & 3 deletions crates/turbopack-core/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
use std::collections::HashMap;
use std::cmp::Ordering;

use anyhow::Result;
use async_trait::async_trait;
use indexmap::IndexMap;
use turbo_tasks::{emit, CollectiblesSource, RcStr, Upcast, Vc};

#[turbo_tasks::value(serialization = "none")]
#[derive(Clone, Debug)]
pub struct PlainDiagnostic {
pub category: RcStr,
pub name: RcStr,
pub payload: HashMap<RcStr, RcStr>,
pub payload: IndexMap<RcStr, RcStr>,
}

impl Ord for PlainDiagnostic {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.name
.cmp(&other.name)
.then_with(|| self.category.cmp(&other.category))
.then_with(|| self.payload.len().cmp(&other.payload.len()))
.then_with(|| {
for ((a_key, a_value), (b_key, b_value)) in
self.payload.iter().zip(other.payload.iter())
{
match a_key.cmp(b_key) {
Ordering::Equal => {}
other => return other,
}
match a_value.cmp(b_value) {
Ordering::Equal => {}
other => return other,
}
}
Ordering::Equal
})
}
}

impl PartialOrd for PlainDiagnostic {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

#[turbo_tasks::value(transparent)]
pub struct DiagnosticPayload(pub HashMap<RcStr, RcStr>);
pub struct DiagnosticPayload(pub IndexMap<RcStr, RcStr>);

/// An arbitrary payload can be used to analyze, diagnose
/// Turbopack's behavior.
Expand Down
8 changes: 6 additions & 2 deletions crates/turbopack-core/src/issue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,12 @@ impl Ord for PlainIssue {

cmp!(self.severity, other.severity);
cmp!(self.stage, other.stage);

self.title.cmp(&other.title)
cmp!(self.title, other.title);
cmp!(self.file_path, other.file_path);
cmp!(self.description, other.description);
cmp!(self.detail, other.detail);
cmp!(self.documentation_link, other.documentation_link);
Ordering::Equal
}
}

Expand Down

0 comments on commit 7677ac8

Please sign in to comment.