You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I suspect something in the compiler is getting mixed up between expression switches (which this is) and type switches.
Is this derived from some real code? It looks bizarre to me. The reason I ask is because it affects priority - if this happens in the real world we should probably get a fix in for 1.13; if not it can wait for 1.14.
Go gets this right up to and including 1.4. The bug starts in 1.5.
gccgo gets this right.
I suspect something in the compiler is getting mixed up between expression switches (which this is) and type switches.
Is this derived from some real code? It looks bizarre to me. The reason I ask is because it affects priority - if this happens in the real world we should probably get a fix in for 1.13; if not it can wait for 1.14.
Go gets this right up to and including 1.4. The bug starts in 1.5.
gccgo gets this right.
I got something similar while reviewing a pull request where the code works fine.
And i started wondering why this doesn't panic. I dont think we are going to use this "trick" in production, but it might happen to someone else.
It just seems weird to compare an interface against a concrete type that's type-casted from an interface. You might as well compare them directly. In other words, if x and y are interfaces, then x == y.(T) is the same as x == y, except that the former would panic if y was not a T.
changed the title [-]Tricky type cast inside switch/case block [/-][+]cmd/compile: invalid type assertion in switch case fails to panic[/+]on May 22, 2019
package main
func main() {
var x interface{}
println(x == x.(*int)) // should panic, but prints false
var p *int
println(x == *p) // should panic, but prints false
var s []string
println(x == s[10]) // should panic, but prints false
}
The problem is for interface-to-concrete comparisons, we're short circuiting on the interface value's dynamic type before evaluating the concrete expression for side effects.
The problem doesn't happen for x.(SomeType) because order.go calls copyExpr on non-direct interface types (i.e., non-pointer types):
changed the title [-]cmd/compile: invalid type assertion in switch case fails to panic[/-][+]cmd/compile: short-circuiting interface-to-concrete comparisons misses panics[/+]on May 23, 2019
Activity
skinass commentedon May 22, 2019
UPD:
if i cast to
x.(SomeType)in place ofx.(*SomeType)i get the panicrandall77 commentedon May 22, 2019
Definitely weird. That should panic.
I suspect something in the compiler is getting mixed up between expression switches (which this is) and type switches.
Is this derived from some real code? It looks bizarre to me. The reason I ask is because it affects priority - if this happens in the real world we should probably get a fix in for 1.13; if not it can wait for 1.14.
Go gets this right up to and including 1.4. The bug starts in 1.5.
gccgo gets this right.
skinass commentedon May 22, 2019
I got something similar while reviewing a pull request where the code works fine.
And i started wondering why this doesn't panic. I dont think we are going to use this "trick" in production, but it might happen to someone else.
randall77 commentedon May 22, 2019
It just seems weird to compare an interface against a concrete type that's type-casted from an interface. You might as well compare them directly. In other words, if x and y are interfaces, then
x == y.(T)is the same asx == y, except that the former would panic ifywas not aT.[-]Tricky type cast inside switch/case block [/-][+]cmd/compile: invalid type assertion in switch case fails to panic[/+]mdempsky commentedon May 23, 2019
Simpler repros:
The problem is for interface-to-concrete comparisons, we're short circuiting on the interface value's dynamic type before evaluating the concrete expression for side effects.
The problem doesn't happen for
x.(SomeType)because order.go calls copyExpr on non-direct interface types (i.e., non-pointer types):go/src/cmd/compile/internal/gc/order.go
Lines 1206 to 1213 in b84e0bc
[-]cmd/compile: invalid type assertion in switch case fails to panic[/-][+]cmd/compile: short-circuiting interface-to-concrete comparisons misses panics[/+]gopherbot commentedon May 24, 2019
Change https://golang.org/cl/178817 mentions this issue:
cmd/compile: ensure interface-to-concrete comparison panics when it shouldcmd/compile: ensure interface-to-concrete comparison panics when it s…
cmd/compile: ensure interface-to-concrete comparison panics when it s…