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: cannot use x (variable of type X(T)) as X(T) value in assignment #40300

Open
Merovius opened this issue Jul 20, 2020 · 2 comments
Open
Assignees
Milestone

Comments

@Merovius
Copy link

@Merovius Merovius commented Jul 20, 2020

What did you do?

Tried to compile this program (playground link):

type A(type T) struct {
	x X(T)
}

func F(type T)(x X(T)) {
	var a A(T)
	a.x = x
}

type X(type T) func(y Y(T))

type Y(type T) func(a *A(T))

What did you expect to see?

No output, as in this program (playground link):

type A(type T) struct {
	x X(T)
}

func F(type T)(x X(T)) {
	var a A(T)
	a.x = x
}

type X(type T) func(y func(a *A(T)))

type Y(type T) func(a *A(T))

What did you see instead?

Compiler error:

type checking failed for main
prog.go2:12:8: cannot use x (variable of type X(T)) as X(T) value in assignment


I tried to minimize the code as much as possible, that's why the original intent is a bit obscured.

I suspect that this is a bug in the prototype - at least I see no reason why it would work with func(*A(T)), but not with Y(T). I thought it might be an issue with recursive generic types (A(T) mentions X(T) mentions Y(T) mentions A(T)), but it should fulfill the requirement of only mentioning the original parameters in the same order. And I wouldn't understand why the error is reported in the assignment and why it vanishes when you delete that. In any case, the error message is, of course, not super helpful :)

I'd be interested to know if this is WAI or a limitation of the prototype.

@toothrot toothrot added this to the Unreleased milestone Jul 21, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jul 23, 2020

CC @griesemer

Appears to be a problem in the type checker. It seems like this should work; if it doesn't work, it definitely needs a better error message.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Jul 28, 2020

This appears to be an evaluation/type-checking ordering problem caused by the mutually referencing types. Moving the declaration of type Y to the beginning "solves" the problem: https://go2goplay.golang.org/p/h4tcdkyvm2N .

This (and related issues) require some significant re-engineering of the type-checker. Probably won't address in the prototype, in favor of spending engineering effort on a real implementation. Leaving open so we have a test case.

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.