Skip to content

Commit

Permalink
internal/xtypes: handle new Union and TypeParam types (#399)
Browse files Browse the repository at this point in the history
Fixes #398
  • Loading branch information
quasilyte authored Aug 28, 2022
1 parent e0687de commit 4bff5cb
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"runs-on": "ubuntu-latest",
steps: [
{
name: "Set up Go 1.16",
name: "Set up Go 1.18",
uses: "actions/setup-go@v1",
"with": {"go-version": 1.16},
"with": {"go-version": 1.18},
id: "go",
},
{name: "Check out code into the Go module directory", uses: "actions/checkout@v1"},
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test-release:
@echo "everything is OK"

lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH_DIR)/bin v1.43.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH_DIR)/bin v1.49.0
$(GOPATH_DIR)/bin/golangci-lint run ./...
go build -o go-ruleguard ./cmd/ruleguard
./go-ruleguard -debug-imports -rules rules.go ./...
Expand Down
1 change: 1 addition & 0 deletions analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var tests = []struct {
{name: "localfunc"},
{name: "goversion", flags: map[string]string{"go": "1.16"}},
{name: "imports"},
{name: "generics"},

{name: "quickfix", quickfixes: true},
}
Expand Down
12 changes: 12 additions & 0 deletions analyzer/testdata/src/generics/rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build ignore
// +build ignore

package gorules

import "github.com/quasilyte/go-ruleguard/dsl"

func externalErrorReassign(m dsl.Matcher) {
m.Match(`$pkg.$err = $x`).
Where(m["err"].Type.Is(`error`) && m["pkg"].Object.Is(`PkgName`)).
Report(`suspicious reassigment of error from another package`)
}
9 changes: 9 additions & 0 deletions analyzer/testdata/src/generics/target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

type myType[T any] struct {
value T
}

func (m *myType[T]) set(v T) {
m.value = v
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71
github.com/quasilyte/gogrep v0.0.0-20220120141003-628d8b3623b5
github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567
golang.org/x/exp/typeparams v0.0.0-20220827204233-334a2380cb91
golang.org/x/tools v0.1.12
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp/typeparams v0.0.0-20220827204233-334a2380cb91 h1:Ic/qN6TEifvObMGQy72k0n1LlJr7DjWWEi+MOsDOiSk=
golang.org/x/exp/typeparams v0.0.0-20220827204233-334a2380cb91/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand Down
16 changes: 13 additions & 3 deletions internal/xtypes/xtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package xtypes

import (
"go/types"

"golang.org/x/exp/typeparams"
)

// Implements reports whether type v implements iface.
Expand Down Expand Up @@ -63,9 +65,6 @@ func typeIdentical(x, y types.Type, p *ifacePair) bool {
}

switch x := x.(type) {
case nil:
return false

case *types.Basic:
// Basic types are singletons except for the rune and byte
// aliases, thus we cannot solely rely on the x == y check
Expand Down Expand Up @@ -142,6 +141,11 @@ func typeIdentical(x, y types.Type, p *ifacePair) bool {
typeIdentical(x.Results(), y.Results(), p)
}

case *typeparams.Union:
// TODO(quasilyte): do we want to match generic union types too?
// It would require copying a lot of code from the go/types.
return false

case *types.Interface:
// Two interface types are identical if they have the same set of methods with
// the same names and identical function types. Lower-case method names from
Expand Down Expand Up @@ -214,6 +218,12 @@ func typeIdentical(x, y types.Type, p *ifacePair) bool {
}
return sameID(x.Obj(), y.Obj().Pkg(), y.Obj().Name())

case *typeparams.TypeParam:
// nothing to do (x and y being equal is caught in the very beginning of this function)

case nil:
// avoid a crash in case of nil type

default:
panic("unreachable")
}
Expand Down

0 comments on commit 4bff5cb

Please sign in to comment.