Debian Codegraph @

internal/flow/lattice.go

raw symbols SCIP
1 // Copyright 2026 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package flowscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/
6
7 import (
8 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
9 "mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/"
10 )
11
12 // A Semilattice describes a bounded semilattice over Elem.
13 // That is, a partial order over values of type Elem, with a binary
14 // Merge operator and an identity element.
15 //
16 // This is typically implemented by a stateless type, and acts as a factory for
17 // lattice elements.
18 type Semilatticescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Semilattice#[Elem any] interface {
19 // Ident returns the identity element of this lattice, that is the unit of
20 // the Merge operation.
21 Identscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Semilattice#Ident.() Elem
22
23 // Equals returns whether a and b are the same element.
24 Equalsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Semilattice#Equals.(a, b Elem) bool
25
26 // Merge combines two lattice values, such as the two possible values of a
27 // variable at the end of an if/else statement.
28 //
29 // Merge must satisfy the following identities, where we use ∧ for Merge, =
30 // for Equals, and 𝟏 for Ident:
31 //
32 // - Associativity: x ∧ (y ∧ z) = (x ∧ y) ∧ z
33 // - Commutativity: x ∧ y = y ∧ x
34 // - Idempotency: x ∧ x = x
35 // - Identity: x ∧ 𝟏 = x
36 Mergescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Semilattice#Merge.(a, b Elem) Elem
37 }
38
39 // A MapLattice implements [Semilattice][map[Key]Elem]. The values in the map
40 // are themselves defined by [Semilattice] L.
41 //
42 // Any elements missing from the map are implicitly L's identity element, and
43 // L's identity element never appears as a value in the map.
44 type MapLatticescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#[Key comparable, Elem any, L Semilatticescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Semilattice#[Elem]] struct {
45 lscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#l. L
46 }
47
48 func (m MapLatticescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#[Key, Elem, L]) Identscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#Ident().() map[Key]Elem {
49 return nil
50 }
51
52 func (m MapLatticescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#[Key, Elem, L]) Equalsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#Equals().(a, b map[Key]Elem) bool {
53 return mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/.EqualFuncscip-go gomod github.com/golang/go/src go1.25.0 maps/EqualFunc().(a, b, m.lscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#l..Equalsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Semilattice#Equals.)
54 }
55
56 func (m MapLatticescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#[Key, Elem, L]) Mergescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#Merge().(a, b map[Key]Elem) map[Key]Elem {
57 if len(a) == 0 {
58 return b
59 } else if len(b) == 0 {
60 return a
61 }
62
63 // We need to consider the union of keys in a and b.
64 out := make(map[Key]Elem)
65 id := m.lscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#l..Identscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Semilattice#Ident.()
66 for k, av := range a {
67 bv, ok := b[k]
68 if !ok {
69 // Because Merge(x, Ident()) == x, we can skip calling L.Merge.
70 out[k] = av
71 continue
72 }
73
74 w := m.lscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#l..Mergescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Semilattice#Merge.(av, bv)
75 if m.lscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#l..Equalsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/Semilattice#Equals.(w, id) {
76 // In a semilattice, Merge(x, y) = Ident is only possible when x ==
77 // Ident and y == Ident.
78 panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%T is not a semilattice: Merge returned Ident for non-Ident arguments", m.lscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/flow`/MapLattice#l.))
79 }
80 out[k] = w
81 }
82 // We considered keys that are only in a, and in both a and b. Now we just
83 // need to handle keys that are only in b.
84 for k, v2 := range b {
85 if _, ok := a[k]; !ok {
86 out[k] = v2
87 }
88 }
89
90 return out
91 }