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/compile: suppress typecheck errors in a type switch case with broken type #28926

Open
josharian opened this Issue Nov 23, 2018 · 10 comments

Comments

Projects
None yet
3 participants
@josharian
Contributor

josharian commented Nov 23, 2018

package p

func f(e interface{}) {
	switch e := e.(type) {
	case T:
		e.M()
	}
}
$ go tool compile x.go
x.go:5:7: undefined: T
x.go:6:4: e.M undefined (type interface {} is interface with no methods)

The first error message is accurate and helpful. We should suppress the second error message, which is confusing.

@josharian josharian added the NeedsFix label Nov 23, 2018

@josharian josharian added this to the Go1.13 milestone Nov 23, 2018

@Skarlso

This comment has been minimized.

Contributor

Skarlso commented Nov 23, 2018

I call dibs. 🙂

@Skarlso

This comment has been minimized.

Contributor

Skarlso commented Nov 24, 2018

@josharian Hi. Just to be clear. This happens in select when the selected type upon does not exist. And the compiler still continues even though it should in the case of that case just stop there since the type doesn't exist. Is this assessment right?

@josharian

This comment has been minimized.

Contributor

josharian commented Nov 24, 2018

This happens in select when the selected type upon does not exist.

Yes.

And the compiler still continues even though it should in the case of that case just stop there since the type doesn't exist.

The compiler continues (for a while) past many errors, since it is often helpful to print out multiple errors instead of the first one. What is needed here is to either mark the body of the case as typechecked without actually typechecking it, or mark the original type of e with Broke() so no further errors involving e get reported. Neither is perfect. With the former, if there are multiple missing type cases they will all get errors reported; with the latter, unrelated typechecking errors in the body will get reported. But this isn’t mission critical, so probably do whatever is cleanest in the code. And don’t forget to add a test—see “errorcheck” style tests in GOROOT/test.

@Skarlso

This comment has been minimized.

Contributor

Skarlso commented Nov 24, 2018

Thanks a lot for the information and the reminder on the test! Much appreciated! 👍

@Skarlso

This comment has been minimized.

Contributor

Skarlso commented Nov 25, 2018

@josharian Hi. I have an idea. A vague one though and I would like to ask if that is an option at all.

So I was thinking that this error is happening because the type is non existent in the type switch. So the back assignment fails to happen to the value. What if I could somehow mark that happening an once the compiler continues it's thing it can detect that the type assert back then actually failed and that it should tell you so when it's inside the case statement. Something like:

$ go tool compile x.go
x.go:5:7: undefined: T
x.go:6:4: type assert failed for this case leg. this type is of type interface thus has no method called `M`.

Or... something in this regard. What say you?

That way, compilation could continue and the error wouldn't be meaningless.

@josharian

This comment has been minimized.

Contributor

josharian commented Nov 25, 2018

I think it’d be simpler to just suppress the second error and sufficiently informative.

@Skarlso

This comment has been minimized.

Contributor

Skarlso commented Nov 25, 2018

Ack

@Skarlso

This comment has been minimized.

Contributor

Skarlso commented Nov 26, 2018

@josharian so... if I do this:

			yyerrorl(lineno, "undefined: %v", n.Sym)
			n.Type.SetBroke(true)

It will stop the compiler dead in its track. Nothing further will be reported. This fixes the current issue, but like you said, it's not very ideal.

~/Temp/go/typecheck
❯ go tool compile main.go
main.go:6:7: undefined: T

I have to read the code a bit further to see if anything else is actually possible. Like, I can't just mark the case as errored because nothing is actually returned. And because of switching on interface types is allowed, I can't even use that.

Also, am I even in the right place siwthc swt.go under compile/gc? I think that's the right place to poke at things. But it's actually not that easy to debug.

@Skarlso

This comment has been minimized.

Contributor

Skarlso commented Nov 26, 2018

Oh! This might help..

		typecheckdef(n)
		if n.Op == ONONAME {
			n.Type = nil
			return n
		}

So if ONONAME is detected, it will return nil. That's interesting. I think I can work out something with that.

@gopherbot

This comment has been minimized.

gopherbot commented Nov 27, 2018

Change https://golang.org/cl/151323 mentions this issue: cmd/compile: suppress typecheck errors in a type switch case with broken type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment