maps: make Clone preserve nil map

The x/exp/maps.Clone function doesn't preserve nil
the way x/exp/slices.Clone does, but it should be.

Fixes golang/go#53817

Change-Id: Ib1867fb6e31d990d8b8a846e9dba8a5b6ba83c48
Reviewed-on: https://go-review.googlesource.com/c/exp/+/417274
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
diff --git a/maps/maps.go b/maps/maps.go
index f49768b..ecc0dab 100644
--- a/maps/maps.go
+++ b/maps/maps.go
@@ -63,6 +63,10 @@
 // Clone returns a copy of m.  This is a shallow clone:
 // the new keys and values are set using ordinary assignment.
 func Clone[M ~map[K]V, K comparable, V any](m M) M {
+	// Preserve nil in case it matters.
+	if m == nil {
+		return nil
+	}
 	r := make(M, len(m))
 	for k, v := range m {
 		r[k] = v
diff --git a/maps/maps_test.go b/maps/maps_test.go
index c21ef30..bf7c6f4 100644
--- a/maps/maps_test.go
+++ b/maps/maps_test.go
@@ -142,6 +142,14 @@
 	}
 }
 
+func TestCloneNil(t *testing.T) {
+	var m1 map[string]int
+	mc := Clone(m1)
+	if mc != nil {
+		t.Errorf("Clone(%v) = %v, want %v", m1, mc, m1)
+	}
+}
+
 func TestCopy(t *testing.T) {
 	mc := Clone(m1)
 	Copy(mc, mc)