Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go2go: constraint type inference fails #40859

Closed
JakeMont opened this issue Aug 18, 2020 · 5 comments
Closed

cmd/go2go: constraint type inference fails #40859

JakeMont opened this issue Aug 18, 2020 · 5 comments
Assignees
Milestone

Comments

@JakeMont
Copy link

@JakeMont JakeMont commented Aug 18, 2020

The "Element constraint example" in the "Constraint type inference" section of the Type Parameters Draft Design (here) fails when run in the go2go playground:
https://go2goplay.golang.org/p/VBEVxYVbF62

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}))
}

Output:

type checking failed for main
prog.go2:31:38: cannot infer E (prog.go2:17:34) ([main.MySlice <nil>])

The failure is in the second call to DoubleDefined, which is supposed to work according to the design. This is the point of the design doc example.

@dmitshur dmitshur changed the title cmd/go2go: Constraint type inference fails cmd/go2go: constraint type inference fails Aug 18, 2020
@dmitshur dmitshur added this to the Unreleased milestone Aug 18, 2020
@dmitshur
Copy link
Member

@dmitshur dmitshur commented Aug 18, 2020

@griesemer
Copy link
Contributor

@griesemer griesemer commented Aug 20, 2020

Interestingly this works fine when running this example in the type checker test harness, but not when running it outside. Investigating.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Aug 20, 2020

@iant: The go2go tool needs to set types.Config.InferFromConstraints to true, otherwise constraint type inference is not enabled.

@gopherbot
Copy link

@gopherbot gopherbot commented Aug 20, 2020

Change https://golang.org/cl/249737 mentions this issue: [dev.go2go] go/go2go, go/types: enable constraint type inference

gopherbot pushed a commit that referenced this issue Aug 21, 2020
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>
@griesemer
Copy link
Contributor

@griesemer griesemer commented Aug 21, 2020

Fixed in the dev.go2go branch. Will come to the playground sometime soon.

@griesemer griesemer closed this Aug 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.