]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: fix compilation of inferred type arguments
authorMatthew Dempsky <mdempsky@google.com>
Wed, 3 May 2023 21:53:03 +0000 (14:53 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 3 May 2023 22:12:27 +0000 (22:12 +0000)
Previously, type arguments could only be inferred for generic
functions in call expressions, whereas with the reverse type inference
proposal they can now be inferred in assignment contexts too. As a
consequence, we now need to check Info.Instances to find the inferred
type for more cases now.

Updates #59338.
Fixes #59955.

Change-Id: I9b6465395869459c2387d0424febe7337b28b90e
Reviewed-on: https://go-review.googlesource.com/c/go/+/492455
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>

src/cmd/compile/internal/noder/writer.go
test/fixedbugs/issue59338.go

index 72c7a1fc8649cbfc12ba539ecf40d97995696560..178c3eb1a9c022d8d33da598a35d7c87f4e0a36e 100644 (file)
@@ -123,14 +123,25 @@ func (pw *pkgWriter) unexpected(what string, p poser) {
 }
 
 func (pw *pkgWriter) typeAndValue(x syntax.Expr) syntax.TypeAndValue {
-       tv := x.GetTypeInfo()
-       if tv.Type == nil {
+       tv, ok := pw.maybeTypeAndValue(x)
+       if !ok {
                pw.fatalf(x, "missing Types entry: %v", syntax.String(x))
        }
        return tv
 }
+
 func (pw *pkgWriter) maybeTypeAndValue(x syntax.Expr) (syntax.TypeAndValue, bool) {
        tv := x.GetTypeInfo()
+
+       // If x is a generic function whose type arguments are inferred
+       // from assignment context, then we need to find its inferred type
+       // in Info.Instances instead.
+       if name, ok := x.(*syntax.Name); ok {
+               if inst, ok := pw.info.Instances[name]; ok {
+                       tv.Type = inst.Type
+               }
+       }
+
        return tv, tv.Type != nil
 }
 
index dc8604f31967a956552fc77862dc0516df9e4b9a..8ba3fd2b3b911dadb87d5375e060bbfb7c487645 100644 (file)
@@ -21,15 +21,13 @@ func main() {
                panic(2)
        }
 
-       // Disabled for now - requires some noder work
-       // TODO fix this
-       // if g3(g1, 3) != g1(3) {
-       //      panic(3)
-       // }
-
-       // if g4(g2, 4) != "" {
-       //      panic(4)
-       // }
+       if g3(g1, 3) != g1(3) {
+               panic(3)
+       }
+
+       if g4(g2, 4) != "" {
+               panic(4)
+       }
 }
 
 func g1[P any](x P) P    { return x }