Skip to content

Commit b8f87dc

Browse files
committed
fix: Preserve error order when deduplicating errors
Previously when deduplicating errors, the errors were first sorted and then deduplicated. This lost the ordering of errors which can be useful for understanding why a value is not compatible with a declaration. Signed-off-by: Graham Dennis <[email protected]>
1 parent 721ca5d commit b8f87dc

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Diff for: cue/errors/errors.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,7 @@ func (p list) sanitize() list {
396396
if p == nil {
397397
return p
398398
}
399-
a := slices.Clone(p)
400-
a.RemoveMultiples()
401-
return a
399+
return p.RemoveMultiples()
402400
}
403401

404402
// Sort sorts an List. *posError entries are sorted by position,
@@ -417,19 +415,22 @@ func (p list) Sort() {
417415
})
418416
}
419417

420-
// RemoveMultiples sorts an List and removes all but the first error per line.
421-
func (p *list) RemoveMultiples() {
422-
p.Sort()
423-
var last Error
424-
i := 0
425-
for _, e := range *p {
426-
if last == nil || !approximateEqual(last, e) {
427-
last = e
428-
(*p)[i] = e
429-
i++
418+
// RemoveMultiples removes all but the first instance of an error message per line.
419+
func (p list) RemoveMultiples() list {
420+
deduplicated := slices.Clone(p)
421+
422+
deduplicatedIdx := 0
423+
OUTER:
424+
for _, element := range p {
425+
for _, deduplicatedElement := range deduplicated[0:deduplicatedIdx] {
426+
if approximateEqual(deduplicatedElement, element) {
427+
continue OUTER
428+
}
430429
}
430+
deduplicated[deduplicatedIdx] = element
431+
deduplicatedIdx++
431432
}
432-
(*p) = (*p)[0:i]
433+
return deduplicated[0:deduplicatedIdx]
433434
}
434435

435436
func approximateEqual(a, b Error) bool {

0 commit comments

Comments
 (0)