Skip to content

Commit

Permalink
[dev.go2go] go/go2go, go/types: enable constraint type inference
Browse files Browse the repository at this point in the history
Constraint type inference was not enabled for go/go2go and gotype.

Fixes #40859.

Change-Id: Idcab4d0a2eae6f42f938604fb05aabe4ad94c934
Reviewed-on: https://go-review.googlesource.com/c/go/+/249737
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
griesemer committed Aug 21, 2020
1 parent ec50ad4 commit 12c2128
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/go/go2go/go2go.go
Expand Up @@ -83,8 +83,9 @@ func rewriteFilesInPath(importer *Importer, importPath, dir string, go2files []s

var merr multiErr
conf := types.Config{
Importer: importer,
Error: merr.add,
InferFromConstraints: true,
Importer: importer,
Error: merr.add,
}
path := importPath
if path == "" {
Expand Down Expand Up @@ -131,8 +132,9 @@ func RewriteBuffer(importer *Importer, filename string, file []byte) ([]byte, er
}
var merr multiErr
conf := types.Config{
Importer: importer,
Error: merr.add,
InferFromConstraints: true,
Importer: importer,
Error: merr.add,
}
tpkg, err := conf.Check(pf.Name.Name, fset, []*ast.File{pf}, importer.info)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion src/go/types/gotype.go
Expand Up @@ -290,7 +290,8 @@ func checkPkgFiles(files []*ast.File) {

// if checkPkgFiles is called multiple times, set up conf only once
conf := types.Config{
FakeImportC: true,
InferFromConstraints: true,
FakeImportC: true,
Error: func(err error) {
if !*allErrors && errorCount >= 10 {
panic(bailout{})
Expand Down
38 changes: 38 additions & 0 deletions test/gen/g043.go2
@@ -0,0 +1,38 @@
// compile

// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Issue 40859.
package main

import (
"fmt"
)

type Number interface {
type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64
}

type MySlice []int

type SC[type E] interface {
type []E
}

func DoubleDefined[type S SC[E], E Number](s S) S {
r := make(S, len(s))
for i, v := range s {
r[i] = v + v
}
return r
}

func main() {
// Expicit types work fine
fmt.Println(DoubleDefined[MySlice, int](MySlice{1}))

// Constraint type inference does not work?
fmt.Println(DoubleDefined(MySlice{1}))
}

0 comments on commit 12c2128

Please sign in to comment.