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

Closed
Merovius opened this issue Jul 20, 2020 · 3 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Merovius
Copy link
Contributor

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

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

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.

@Merovius
Copy link
Contributor Author

Not only does the prototype no longer matter, this also build now, so closing.

@golang golang locked and limited conversation to collaborators Jun 23, 2023
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

5 participants