gc: Better typechecks and errors in switches.
Allow any type in switch on interface value.
Statically check typeswitch early.
Fixes issue 2423.
Fixes issue 2424.
On 2011/11/07 08:18:22, dsymonds wrote: > Isn't this a language change? I understood (value) switches ...
13 years, 4 months ago
(2011-11-07 08:22:40 UTC)
#4
On 2011/11/07 08:18:22, dsymonds wrote:
> Isn't this a language change? I understood (value) switches to be glorified
> if chains with ==, but interface{} is not comparable to string.
if x == "hello" {... just compiles and passes here
(hm don't know why code. doesn't send my reply) On Mon, Nov 7, 2011 at ...
13 years, 4 months ago
(2011-11-07 08:31:16 UTC)
#5
(hm don't know why code. doesn't send my reply)
On Mon, Nov 7, 2011 at 09:18, David Symonds <dsymonds@golang.org> wrote:
> Isn't this a language change? I understood (value) switches to be
> glorified if chains with ==, but interface{} is not comparable to string.
>
if x == "hello" {... compiles and works fine here.
i added in a fix for the next issue, but that trips
fixedbugs/bug270.go:19: impossible type switch case: I(T literal) (type I)
cannot have dynamic type interface {} (missing F method)
bug 270 reads
package main
type I interface { F() }
type T struct{}
func (T) F() {}
func main() {
switch I(T{}).(type) {
case interface{}: // new error is here
}
}
i think the new behaviour is correct, since if i add
var x interface{}
var y I = x
i get
cannot use x (type interface {}) as type I in assignment:
interface {} does not implement I (missing F method)
On Mon, Nov 7, 2011 at 09:31, Luuk van Dijk <lvd@google.com> wrote: > (hm don't ...
13 years, 4 months ago
(2011-11-07 08:34:30 UTC)
#6
On Mon, Nov 7, 2011 at 09:31, Luuk van Dijk <lvd@google.com> wrote:
> (hm don't know why code. doesn't send my reply)
>
>
> On Mon, Nov 7, 2011 at 09:18, David Symonds <dsymonds@golang.org> wrote:
>
>> Isn't this a language change? I understood (value) switches to be
>> glorified if chains with ==, but interface{} is not comparable to string.
>>
>
> if x == "hello" {... compiles and works fine here.
>
> i added in a fix for the next issue, but that trips
>
> fixedbugs/bug270.go:19: impossible type switch case: I(T literal) (type
> I) cannot have dynamic type interface {} (missing F method)
>
> bug 270 reads
>
> package main
>
> type I interface { F() }
>
> type T struct{}
>
> func (T) F() {}
>
> func main() {
> switch I(T{}).(type) {
> case interface{}: // new error is here
> }
> }
>
> i think the new behaviour is correct, since if i add
>
> var x interface{}
> var y I = x
>
> i get
> cannot use x (type interface {}) as type I in assignment:
> interface {} does not implement I (missing F method)
>
>
>
I looked at the original issue
http://code.google.com/p/go/issues/detail?id=746 and i think replacing
interface{} with default keeps the gist of the test (for missing typecheck
on the switch test)
/L
On Mon, Nov 7, 2011 at 7:31 PM, Luuk van Dijk <lvd@google.com> wrote: > On ...
13 years, 4 months ago
(2011-11-07 11:09:10 UTC)
#7
On Mon, Nov 7, 2011 at 7:31 PM, Luuk van Dijk <lvd@google.com> wrote:
> On Mon, Nov 7, 2011 at 09:18, David Symonds <dsymonds@golang.org> wrote:
>>
>> Isn't this a language change? I understood (value) switches to be
>> glorified if chains with ==, but interface{} is not comparable to string.
>
> if x == "hello" {... compiles and works fine here.
Aah, duh. "hello" is assignable to x, so they are comparable.
Never mind me...
http://codereview.appspot.com/5339045/diff/1002/src/cmd/gc/swt.c File src/cmd/gc/swt.c (right): http://codereview.appspot.com/5339045/diff/1002/src/cmd/gc/swt.c#newcode863 src/cmd/gc/swt.c:863: ; btw i just realized I could do an ...
13 years, 4 months ago
(2011-11-07 14:45:00 UTC)
#8
http://codereview.appspot.com/5339045/diff/1002/src/cmd/gc/swt.c File src/cmd/gc/swt.c (right): http://codereview.appspot.com/5339045/diff/1002/src/cmd/gc/swt.c#newcode863 src/cmd/gc/swt.c:863: ; I think this new code is only necessary ...
13 years, 4 months ago
(2011-11-07 17:36:58 UTC)
#9
http://codereview.appspot.com/5339045/diff/17002/test/fixedbugs/bug270.go File test/fixedbugs/bug270.go (right): http://codereview.appspot.com/5339045/diff/17002/test/fixedbugs/bug270.go#newcode19 test/fixedbugs/bug270.go:19: default: I think you can remove this test. It ...
13 years, 4 months ago
(2011-11-07 19:06:06 UTC)
#11
13 years, 4 months ago
(2011-11-07 19:14:36 UTC)
#13
http://codereview.appspot.com/5339045/diff/17002/src/cmd/gc/swt.c
File src/cmd/gc/swt.c (right):
http://codereview.appspot.com/5339045/diff/17002/src/cmd/gc/swt.c#newcode864
src/cmd/gc/swt.c:864: else if(ll->n->type != T && assignop(ll->n->type, t, &why)
== 0)
I believe you need both assignop checks, which means that
keeping the why is a little tricky. I would settle for using the
same syntax as the error in this program:
package main
type I interface {m()}
func main() {
var i I
_ = i == "hello"
}
It prints
x.go:5: invalid operation: i == "hello" (mismatched types I and string)
Please add tests for both cases:
type I interface { M() }
var i I
var s string
switch i {
case s: // ERROR "mismatched types I and string"
}
switch s {
case i: // ERROR "mismatched types string and I"
}
but also add test cases that if you have
type I interface{}
then both those switches are valid.
http://codereview.appspot.com/5339045/diff/17002/src/cmd/gc/swt.c#newcode875
src/cmd/gc/swt.c:875: if(have && !missing->broke && !have->broke)
Presumably the !missing->broke && !have->broke is needed
in typecheck.c's copy of this code too. Maybe lift out into a
bool checkimplements(Type*, Type*)
or whatever the signature needs to be to reproduce the errors.
On Mon, Nov 7, 2011 at 20:14, <rsc@golang.org> wrote: > > http://codereview.appspot.com/**5339045/diff/17002/src/cmd/gc/**swt.c<http://codereview.appspot.com/5339045/diff/17002/src/cmd/gc/swt.c> > File src/cmd/gc/swt.c ...
13 years, 4 months ago
(2011-11-07 20:21:53 UTC)
#14
On Mon, Nov 7, 2011 at 20:14, <rsc@golang.org> wrote:
>
>
http://codereview.appspot.com/**5339045/diff/17002/src/cmd/gc/**swt.c<http://...
> File src/cmd/gc/swt.c (right):
>
> http://codereview.appspot.com/**5339045/diff/17002/src/cmd/gc/**
>
swt.c#newcode864<http://codereview.appspot.com/5339045/diff/17002/src/cmd/gc/swt.c#newcode864>
> src/cmd/gc/swt.c:864: else if(ll->n->type != T && assignop(ll->n->type,
> t, &why) == 0)
> I believe you need both assignop checks, which means that
> keeping the why is a little tricky. I would settle for using the
> same syntax as the error in this program:
>
> package main
> type I interface {m()}
> func main() {
> var i I
> _ = i == "hello"
> }
>
> It prints
>
> x.go:5: invalid operation: i == "hello" (mismatched types I and string)
>
> Please add tests for both cases:
>
> type I interface { M() }
> var i I
> var s string
>
> switch i {
> case s: // ERROR "mismatched types I and string"
> }
> switch s {
> case i: // ERROR "mismatched types string and I"
> }
>
> but also add test cases that if you have
>
> type I interface{}
>
> then both those switches are valid.
>
added those to typeswitch2.go, and surprise, they aren't valid, because of
the symmetric assign check. i think. let me think that over.
>
> http://codereview.appspot.com/**5339045/diff/17002/src/cmd/gc/**
>
swt.c#newcode875<http://codereview.appspot.com/5339045/diff/17002/src/cmd/gc/swt.c#newcode875>
> src/cmd/gc/swt.c:875: if(have && !missing->broke && !have->broke)
> Presumably the !missing->broke && !have->broke is needed
> in typecheck.c's copy of this code too. Maybe lift out into a
> bool checkimplements(Type*, Type*)
> or whatever the signature needs to be to reproduce the errors.
>
>
http://codereview.appspot.com/**5339045/<http://codereview.appspot.com/5339045/>
>
On Mon, Nov 7, 2011 at 21:21, Luuk van Dijk <lvd@google.com> wrote: > > > ...
13 years, 4 months ago
(2011-11-07 20:40:53 UTC)
#15
On Mon, Nov 7, 2011 at 21:21, Luuk van Dijk <lvd@google.com> wrote:
>
>
> On Mon, Nov 7, 2011 at 20:14, <rsc@golang.org> wrote:
>
>>
>>
http://codereview.appspot.com/**5339045/diff/17002/src/cmd/gc/**swt.c<http://...
>> File src/cmd/gc/swt.c (right):
>>
>> http://codereview.appspot.com/**5339045/diff/17002/src/cmd/gc/**
>>
swt.c#newcode864<http://codereview.appspot.com/5339045/diff/17002/src/cmd/gc/swt.c#newcode864>
>> src/cmd/gc/swt.c:864: else if(ll->n->type != T && assignop(ll->n->type,
>> t, &why) == 0)
>> I believe you need both assignop checks, which means that
>> keeping the why is a little tricky. I would settle for using the
>> same syntax as the error in this program:
>>
>> package main
>> type I interface {m()}
>> func main() {
>> var i I
>> _ = i == "hello"
>> }
>>
>> It prints
>>
>> x.go:5: invalid operation: i == "hello" (mismatched types I and string)
>>
>> Please add tests for both cases:
>>
>> type I interface { M() }
>> var i I
>> var s string
>>
>> switch i {
>> case s: // ERROR "mismatched types I and string"
>> }
>> switch s {
>> case i: // ERROR "mismatched types string and I"
>> }
>>
>> but also add test cases that if you have
>>
>> type I interface{}
>>
>> then both those switches are valid.
>>
>
> added those to typeswitch2.go, and surprise, they aren't valid, because of
> the symmetric assign check. i think. let me think that over.
>
it is because i have no brain at all.
>
>
>>
>> http://codereview.appspot.com/**5339045/diff/17002/src/cmd/gc/**
>>
swt.c#newcode875<http://codereview.appspot.com/5339045/diff/17002/src/cmd/gc/swt.c#newcode875>
>> src/cmd/gc/swt.c:875: if(have && !missing->broke && !have->broke)
>> Presumably the !missing->broke && !have->broke is needed
>> in typecheck.c's copy of this code too. Maybe lift out into a
>> bool checkimplements(Type*, Type*)
>> or whatever the signature needs to be to reproduce the errors.
>>
>>
http://codereview.appspot.com/**5339045/<http://codereview.appspot.com/5339045/>
>>
>
>
On Mon, Nov 7, 2011 at 21:40, Luuk van Dijk <lvd@google.com> wrote: > > > ...
13 years, 4 months ago
(2011-11-07 21:41:06 UTC)
#16
On Mon, Nov 7, 2011 at 21:40, Luuk van Dijk <lvd@google.com> wrote:
>
>
> On Mon, Nov 7, 2011 at 21:21, Luuk van Dijk <lvd@google.com> wrote:
>
>>
>>
>> On Mon, Nov 7, 2011 at 20:14, <rsc@golang.org> wrote:
>>
>>>
>>>
http://codereview.appspot.com/**5339045/diff/17002/src/cmd/gc/**swt.c<http://...
>>> File src/cmd/gc/swt.c (right):
>>>
>>> http://codereview.appspot.com/**5339045/diff/17002/src/cmd/gc/**
>>>
swt.c#newcode864<http://codereview.appspot.com/5339045/diff/17002/src/cmd/gc/swt.c#newcode864>
>>> src/cmd/gc/swt.c:864: else if(ll->n->type != T && assignop(ll->n->type,
>>> t, &why) == 0)
>>> I believe you need both assignop checks, which means that
>>> keeping the why is a little tricky. I would settle for using the
>>> same syntax as the error in this program:
>>>
>>> package main
>>> type I interface {m()}
>>> func main() {
>>> var i I
>>> _ = i == "hello"
>>> }
>>>
>>> It prints
>>>
>>> x.go:5: invalid operation: i == "hello" (mismatched types I and string)
>>>
>>> Please add tests for both cases:
>>>
>>> type I interface { M() }
>>> var i I
>>> var s string
>>>
>>> switch i {
>>> case s: // ERROR "mismatched types I and string"
>>> }
>>> switch s {
>>> case i: // ERROR "mismatched types string and I"
>>> }
>>>
>>> but also add test cases that if you have
>>>
>>> type I interface{}
>>>
>>> then both those switches are valid.
>>>
>>
>> added those to typeswitch2.go, and surprise, they aren't valid, because
>> of the symmetric assign check. i think. let me think that over.
>>
>
> it is because i have no brain at all.
>
had to fiddle a bit with the linenumbers, and made it typecheck3.go,
because those errors will prevent walk from happening, which generates the
existing messages in typecheck2.go
>
>
>>
>>
>>>
>>> http://codereview.appspot.com/**5339045/diff/17002/src/cmd/gc/**
>>>
swt.c#newcode875<http://codereview.appspot.com/5339045/diff/17002/src/cmd/gc/swt.c#newcode875>
>>> src/cmd/gc/swt.c:875: if(have && !missing->broke && !have->broke)
>>> Presumably the !missing->broke && !have->broke is needed
>>> in typecheck.c's copy of this code too. Maybe lift out into a
>>> bool checkimplements(Type*, Type*)
>>> or whatever the signature needs to be to reproduce the errors.
>>>
>>>
http://codereview.appspot.com/**5339045/<http://codereview.appspot.com/5339045/>
>>>
>>
>>
>
Issue 5339045: code review 5339045: gc: Allow any type in switch on interface value. Static...
(Closed)
Created 13 years, 4 months ago by lvd
Modified 13 years, 4 months ago
Reviewers:
Base URL:
Comments: 11