From: Daniel Swarbrick <dswarbrick@debian.org>
Date: Wed, 22 Nov 2023 18:01:43 +0100
Subject: Partially backport PR 12650

Forwarded: not-needed
Last-Update: 2023-11-22

Partially backport upstream commit 86729d4 to fix FTBFS with recent
versions of golang.org/x/exp.
---
 model/labels/labels.go | 9 +++++----
 tsdb/querier.go        | 8 ++++++--
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/model/labels/labels.go b/model/labels/labels.go
index 0c27e15..3dc3049 100644
--- a/model/labels/labels.go
+++ b/model/labels/labels.go
@@ -19,6 +19,7 @@ import (
 	"bytes"
 	"encoding/json"
 	"strconv"
+	"strings"
 
 	"github.com/cespare/xxhash/v2"
 	"github.com/prometheus/common/model"
@@ -362,7 +363,7 @@ func EmptyLabels() Labels {
 func New(ls ...Label) Labels {
 	set := make(Labels, 0, len(ls))
 	set = append(set, ls...)
-	slices.SortFunc(set, func(a, b Label) bool { return a.Name < b.Name })
+	slices.SortFunc(set, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })
 
 	return set
 }
@@ -386,7 +387,7 @@ func FromStrings(ss ...string) Labels {
 		res = append(res, Label{Name: ss[i], Value: ss[i+1]})
 	}
 
-	slices.SortFunc(res, func(a, b Label) bool { return a.Name < b.Name })
+	slices.SortFunc(res, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })
 	return res
 }
 
@@ -591,7 +592,7 @@ func (b *Builder) Labels() Labels {
 	}
 	if len(b.add) > 0 { // Base is already in order, so we only need to sort if we add to it.
 		res = append(res, b.add...)
-		slices.SortFunc(res, func(a, b Label) bool { return a.Name < b.Name })
+		slices.SortFunc(res, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })
 	}
 	return res
 }
@@ -618,7 +619,7 @@ func (b *ScratchBuilder) Add(name, value string) {
 
 // Sort the labels added so far by name.
 func (b *ScratchBuilder) Sort() {
-	slices.SortFunc(b.add, func(a, b Label) bool { return a.Name < b.Name })
+	slices.SortFunc(b.add, func(a, b Label) int { return strings.Compare(a.Name, b.Name) })
 }
 
 // Assign is for when you already have a Labels which you want this ScratchBuilder to return.
diff --git a/tsdb/querier.go b/tsdb/querier.go
index 4342d60..4efa9cd 100644
--- a/tsdb/querier.go
+++ b/tsdb/querier.go
@@ -277,8 +277,12 @@ func PostingsForMatchers(ix IndexReader, ms ...*labels.Matcher) (index.Postings,
 	// there is no chance that the set we subtract from
 	// contains postings of series that didn't exist when
 	// we constructed the set we subtract by.
-	slices.SortStableFunc(ms, func(i, j *labels.Matcher) bool {
-		return !isSubtractingMatcher(i) && isSubtractingMatcher(j)
+	slices.SortStableFunc(ms, func(i, j *labels.Matcher) int {
+		if !isSubtractingMatcher(i) && isSubtractingMatcher(j) {
+			return -1
+		}
+
+		return +1
 	})
 
 	for _, m := range ms {
