-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for checking values whose type is a type parameter
Type parameters This commit implements support for checking exhaustiveness when the value's type is a type parameter. The defined behavior is (copied from package documentation): A switch statement that switches on a value whose type is a type parameter is checked for exhaustiveness iff each type element in the type constraint is an enum type and shares the same underlying basic kind. For example, the following switch statement will be checked, assuming M, N, and O are enum types with the same underlying basic kind. To satisfy exhaustiveness, all enum members for each of the types M, N, and O must be listed in the switch statement's cases. func bar[T M | I](v T) { switch v { } } type I interface{ N | J } type J interface{ O } Exhaustive supports go1.14 and later, but type parameters are only supported from go1.18 or later. Symbols in e.g. package go/types related to type parameters are not available in go version earlier than go1.18. So source files, when necessary, are split into *_pre_go118.go and *_go118.go portions. The majority of the implementation is in common_go118.go. Spurious diagnostic for type conversion (issue 42) This commit also fixes spurious diagnostics that were produced when the case clause expressions involved type conversions. See the relevant func stripTypeConversions, and tests in testdata/general/x/typeconv.go. Diagnostic format changes The output diagnostics now always include the package name for the enum type and the enum members. (Previously the package name was omitted if the symbols were in the same package as the offending source code.) This change was motivated by consistency: With type parameters, a value's type in a switch statement tag can be composed of multiple enum types, possibly from different packages. In such scenarios, consistently using the package name for all of them makes for more comprehensible diagnostics. Many testdata lines are touched due to the diagnostic format change. Internal changes * Flag parsing: Parsing of the -check flag is now handled by a new stringsFlag type that implements flag.Value. * Shared code: Code that was shared (or should have been shared between) switch.go and maps.go now lives in common.go. * The enumMembers fact type has an additional NameToPos field. * The checklist type is updated to account for the fact that enum types can possibly be from multiple packages. * The Makefile now includes a "cover" target (HTML test coverage report) and an "all" target (runs build, vet, test). * The new "member" type represents the concept of an enum member in switch.go and map.go API, replacing the prev. largely string-based API. GitHub workflows The workflow now runs tests for go versions < 1.16. Fixes #31, #42
- Loading branch information
Showing
60 changed files
with
1,973 additions
and
1,266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
on: [push, pull_request] | ||
name: all | ||
jobs: | ||
main: | ||
strategy: | ||
matrix: | ||
go-version: [1.14, 1.16, 1.18, 1.x] | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- name: install go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: ${{ matrix.go-version }} | ||
|
||
- name: checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: build | ||
run: make build | ||
|
||
# for earlier go versions, staticcheck build fails due to | ||
# ../../../go/pkg/mod/honnef.co/go/[email protected]/go/ir/builder.go:36:2: //go:build comment without // +build comment | ||
- name: install vet tools (>= go1.17) | ||
if: ${{ matrix.go-version >= '1.17' }} | ||
run: | | ||
go install github.com/nishanths/exhaustive/cmd/exhaustive@latest | ||
go install github.com/gordonklaus/ineffassign@latest | ||
go install github.com/kisielk/errcheck@latest | ||
go install honnef.co/go/tools/cmd/staticcheck@latest | ||
- name: vet (>= go1.17) | ||
if: ${{ matrix.go-version >= '1.17' }} | ||
run: make vet | ||
|
||
- name: test | ||
run: make test |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.