Skip to content

Commit

Permalink
Handle generic maps and channels (#1224)
Browse files Browse the repository at this point in the history
Handle generic maps and channels
  • Loading branch information
ndtretyak authored Sep 5, 2023
1 parent 6bdfec6 commit b7931cb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 59 deletions.
47 changes: 30 additions & 17 deletions contrib/tools/workflowcheck/determinism/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,23 +346,8 @@ func (c *collector) collectFuncInfo(fn *types.Func, decl *ast.FuncDecl) {
case *ast.RangeStmt:
// Map and chan ranges are non-deterministic
rangeType := c.pass.TypesInfo.TypeOf(n.X)
// Unwrap named type
for {
if namedType, _ := rangeType.(*types.Named); namedType != nil {
rangeType = namedType.Underlying()
} else {
break
}
}
switch rangeType.(type) {
case *types.Map:
c.checker.debugf("Marking %v as non-deterministic because it iterates over a map", fn.FullName())
pos := c.pass.Fset.Position(n.Pos())
info.reasons = append(info.reasons, &ReasonMapRange{SourcePos: &pos})
case *types.Chan:
c.checker.debugf("Marking %v as non-deterministic because it iterates over a channel", fn.FullName())
pos := c.pass.Fset.Position(n.Pos())
info.reasons = append(info.reasons, &ReasonConcurrency{SourcePos: &pos, Kind: ConcurrencyKindRange})
if reason := c.checkRangeType(rangeType, n, fn); reason != nil {
info.reasons = append(info.reasons, reason)
}
case *ast.SendStmt:
// Any send statement is non-deterministic
Expand All @@ -381,6 +366,34 @@ func (c *collector) collectFuncInfo(fn *types.Func, decl *ast.FuncDecl) {
})
}

func (c *collector) checkRangeType(rangeType types.Type, n ast.Node, fn *types.Func) Reason {
switch t := rangeType.(type) {
case *types.Named, *types.TypeParam:
return c.checkRangeType(t.Underlying(), n, fn)
case *types.Map:
c.checker.debugf("Marking %v as non-deterministic because it iterates over a map", fn.FullName())
pos := c.pass.Fset.Position(n.Pos())
return &ReasonMapRange{SourcePos: &pos}
case *types.Chan:
c.checker.debugf("Marking %v as non-deterministic because it iterates over a channel", fn.FullName())
pos := c.pass.Fset.Position(n.Pos())
return &ReasonConcurrency{SourcePos: &pos, Kind: ConcurrencyKindRange}
case *types.Interface:
for i := 0; i < t.NumEmbeddeds(); i++ {
if reason := c.checkRangeType(t.EmbeddedType(i), n, fn); reason != nil {
return reason
}
}
case *types.Union:
for i := 0; i < t.Len(); i++ {
if reason := c.checkRangeType(t.Term(i).Type(), n, fn); reason != nil {
return reason
}
}
}
return nil
}

// Expects to be called as second pass after all func infos collected.
func (c *collector) applyFacts() PackageNonDeterminisms {
p := PackageNonDeterminisms{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ func ChannelRangeWrappedType() { // want ChannelRangeWrappedType:"iterates over
for range ch {
}
}

func ChannelRangeGenericType[C chan T, T any](ch C) { // want ChannelRangeGenericType:"iterates over channel"
for range ch {
}
}
6 changes: 6 additions & 0 deletions contrib/tools/workflowcheck/determinism/testdata/src/a/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ func MapIterateWrappedType() { // want MapIterateWrappedType:"iterates over map"
_ = fmt.Sprint(k, v)
}
}

func MapIterateGenericType[M ~map[K]V, K comparable, V any](m M) { // want MapIterateGenericType:"iterates over map"
for k, v := range m {
_ = fmt.Sprint(k, v)
}
}
11 changes: 8 additions & 3 deletions contrib/tools/workflowcheck/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
module go.temporal.io/sdk/contrib/tools/workflowcheck

go 1.16
go 1.20

require (
github.com/kr/pretty v0.1.0 // indirect
golang.org/x/tools v0.10.0
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/kr/pretty v0.1.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/sys v0.9.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
39 changes: 0 additions & 39 deletions contrib/tools/workflowcheck/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,13 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg=
golang.org/x/tools v0.10.0/go.mod h1:UJwyiVBsOA2uwvK/e5OY3GTpDUJriEd+/YlqAwLPmyM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
Binary file added contrib/tools/workflowcheck/workflowcheck
Binary file not shown.

0 comments on commit b7931cb

Please sign in to comment.