-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add preliminary support for generic code
Note that we don't yet support reporting unused parameters on generic functions, as showed by the TODO in typeparams.txt, but at least we don't panic as quickly as before. Also note that running unparam on std currently panics as well, presumably due to a bug in go/ssa that I will report shortly. Thanks to Ainar Garipov for reporting and Ludovic Fernandez for suggesting a fix. Note that we need to use x/exp/typeparams to keep support for Go 1.17.x. Fixes #62.
- Loading branch information
Showing
5 changed files
with
126 additions
and
16 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
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
[!go1.18] skip | ||
|
||
# TODO: catch unusedGeneric. | ||
unparam . | ||
|
||
-- stdout.golden -- | ||
-- go.mod -- | ||
module testdata.tld/foo | ||
|
||
go 1.18 | ||
-- foo.go -- | ||
package foo | ||
|
||
var DoWork func() | ||
|
||
func GenericFunc[GenericParamA, B any](x GenericParamA, y B) {} | ||
|
||
type GenericVector[GenericParamT any] []GenericParamT | ||
|
||
type GenericGraph[T any] struct { | ||
Content T | ||
Edges []GenericGraph[T] | ||
} | ||
|
||
type PredeclaredSignedInteger interface { | ||
int | int8 | int16 | int32 | int64 | ||
} | ||
|
||
type StringableSignedInteger interface { | ||
~int | ~int8 | ~int16 | ~int32 | ~int64 | ||
|
||
String() string | ||
} | ||
|
||
type CombineEmbeds interface { | ||
string | int | ||
|
||
interface { EmbeddedMethod() } | ||
RegularMethod() | ||
} | ||
|
||
type Tuple[T1 any, T2 any] struct { | ||
left T1 | ||
right T2 | ||
} | ||
|
||
func (t Tuple[T1, T2]) Left() T1 { return t.left } | ||
func (t Tuple[T1, T2]) Right() T2 { return t.right } | ||
|
||
func (t Tuple[T1, T2]) unusedGeneric(t1 T1, t2 T2) T2 { | ||
DoWork() | ||
return t2 | ||
} | ||
|
||
// otherwise Tuple isn't reachable. | ||
var Sink interface{} = new(Tuple[int, string]) | ||
|
||
// The different number of type parameters result in different receiver AST | ||
// nodes on methods: IndexExpr and IndexListExpr. | ||
type GenericType1[T1 any] []T1 | ||
type GenericType2[T1 any, T2 any] struct { | ||
t1 T1 | ||
t2 T2 | ||
} | ||
|
||
-- foo_main.go -- | ||
//+build !other | ||
|
||
package foo | ||
|
||
func (g *GenericType1[T1]) multImplsMethodGeneric(f1 T1) { | ||
DoWork() | ||
} | ||
|
||
func (g GenericType2[T1, T2]) multImplsMethodGeneric(f1 T1, f2 T2) T2 { | ||
DoWork() | ||
return f2 | ||
} | ||
-- foo_other.go -- | ||
//+build other | ||
|
||
package foo | ||
|
||
func (g *GenericType1[T1]) multImplsMethodGeneric(f1 T1) { | ||
DoWork() | ||
} | ||
|
||
func (g GenericType2[T1, T2]) multImplsMethodGeneric(f1 T1, f2 T2) T2 { | ||
DoWork() | ||
return f2 | ||
} |