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

Check fails if interface is extended by another interface #3

Open
atombender opened this issue Mar 24, 2017 · 0 comments · May be fixed by #12
Open

Check fails if interface is extended by another interface #3

atombender opened this issue Mar 24, 2017 · 0 comments · May be fixed by #12

Comments

@atombender
Copy link

Test case:

package main

//go-sumtype:decl Animal

type Animal interface {
	MakeSound()
	sealed()
}

type Dog struct{}

func (*Dog) sealed() {}

func (*Dog) MakeSound() {
	println("WOOF")
}

type Canine interface {
	Animal
	Breed() string
}

func test(animal Animal) {
	switch animal.(type) {
	case *Dog:
		println("is dog")
	}
}

func main() {
	test(&Dog{})
}

Fails:

$ go-sumtype main.go
main.go:24:2: exhaustiveness check failed for sum type 'Animal': missing cases for Canine

I did not expect this to fail, as the switch is exhaustive on all concrete types of the interface and there aren't even any structs implementing the second one. The checks fail even there are structs which implement both or one of them.

lantw44 added a commit to lantw44/go-sumtype that referenced this issue Apr 17, 2019
Sometimes it is possible for a sum type interface to be implemented by
another interface. For example, we have a sum type called T including
3 variants, A, B, C. We also have an U interface embedding T and thus
implementing T. Assuming that B and C implement U, a type switch
statement can be considered exhaustive if all of A, B, C are listed.
It should also be considered exhaustive if only A and U are listed.

However, go-sumtype does not distinguish between concrete and interface
types. It fails in both cases, requiring all of A, B, C, U to be listed.
It is unlikely to code to be written in this way. U already covers B and
C, and it is unnecessary to list them in a type switch statement. This
commit fixes the problem by handling interfaces specially.

Closes: BurntSushi#1
Closes: BurntSushi#3
diogo464 pushed a commit to diogo464/go-sumtype that referenced this issue May 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant