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
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@JakeMont
Copy link

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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 18, 2020
@dmitshur dmitshur added this to the Unreleased milestone Aug 18, 2020
@dmitshur
Copy link
Contributor

/cc @griesemer @ianlancetaylor

@griesemer
Copy link
Contributor

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

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

@gopherbot
Copy link

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

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

@golang golang locked and limited conversation to collaborators Aug 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants