Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
1cdcfda1
Commit
1cdcfda1
authored
Mar 18, 2009
by
Ken Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove assignment cases from switch
R=r OCL=26480 CL=26480
parent
bd3c478f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
105 deletions
+51
-105
swt.c
src/cmd/gc/swt.c
+3
-57
typeswitch.go
test/typeswitch.go
+48
-48
No files found.
src/cmd/gc/swt.c
View file @
1cdcfda1
...
...
@@ -44,49 +44,15 @@ sw0(Node *c, Type *place, int arg)
return
T
;
}
walktype
(
c
,
Erv
);
return
T
;
break
;
case
OTYPESW
:
if
(
arg
!=
Stype
)
yyerror
(
"inappropriate type case"
);
return
T
;
case
OAS
:
break
;
}
walktype
(
c
->
left
,
Elv
);
r
=
c
->
right
;
if
(
c
==
N
)
return
T
;
switch
(
r
->
op
)
{
default:
goto
bad
;
case
ORECV
:
// <-chan
walktype
(
r
->
left
,
Erv
);
if
(
!
istype
(
r
->
left
->
type
,
TCHAN
))
goto
bad
;
break
;
case
OINDEX
:
// map[e]
walktype
(
r
->
left
,
Erv
);
if
(
!
istype
(
r
->
left
->
type
,
TMAP
))
goto
bad
;
break
;
case
ODOTTYPE
:
// interface.(type)
walktype
(
r
->
left
,
Erv
);
if
(
!
istype
(
r
->
left
->
type
,
TINTER
))
goto
bad
;
case
OAS
:
yyerror
(
"inappropriate assignment in a case statement"
);
break
;
}
c
->
type
=
types
[
TBOOL
];
if
(
arg
!=
Strue
)
goto
bad
;
return
T
;
bad:
yyerror
(
"inappropriate assignment in a case statement"
);
return
T
;
}
...
...
@@ -311,26 +277,6 @@ loop:
t
->
ninit
=
N
;
}
if
(
t
->
left
->
op
==
OAS
)
{
if
(
bool
==
N
)
{
bool
=
nod
(
OXXX
,
N
,
N
);
tempname
(
bool
,
types
[
TBOOL
]);
}
t
->
left
->
left
=
nod
(
OLIST
,
t
->
left
->
left
,
bool
);
cas
=
list
(
cas
,
t
->
left
);
// v,bool = rhs
a
=
nod
(
OIF
,
N
,
N
);
a
->
nbody
=
t
->
right
;
// then goto l
a
->
ntest
=
bool
;
if
(
arg
!=
Strue
)
a
->
ntest
=
nod
(
ONOT
,
bool
,
N
);
cas
=
list
(
cas
,
a
);
// if bool goto l
t
=
listnext
(
&
save
);
goto
loop
;
}
switch
(
arg
)
{
default:
// not bool const
...
...
test/typeswitch.go
View file @
1cdcfda1
...
...
@@ -62,30 +62,30 @@ func f(i int) interface{} {
func
main
()
{
// type guard style
for
i
:=
Bool
;
i
<
Last
;
i
++
{
switch
v
:=
f
(
i
);
true
{
case
x
:=
v
.
(
bool
)
:
assert
(
x
==
true
&&
i
==
Bool
,
"switch 1 bool"
);
case
x
:=
v
.
(
int
)
:
assert
(
x
==
7
&&
i
==
Int
,
"switch 1 int"
);
case
x
:=
v
.
(
float
)
:
assert
(
x
==
7.4
&&
i
==
Float
,
"switch 1 float"
);
case
x
:=
v
.
(
string
)
:
assert
(
x
==
"hello"
&&
i
==
String
,
"switch 1 string"
);
case
x
:=
v
.
(
S
)
:
assert
(
x
.
a
==
1234
&&
i
==
Struct
,
"switch 1 struct"
);
case
x
:=
v
.
(
chan
int
)
:
assert
(
x
==
c
&&
i
==
Chan
,
"switch 1 chan"
);
case
x
:=
v
.
([]
int
)
:
assert
(
x
[
3
]
==
3
&&
i
==
Array
,
"switch 1 array"
);
case
x
:=
v
.
(
map
[
string
]
int
)
:
assert
(
x
==
m
&&
i
==
Map
,
"switch 1 map"
);
case
x
:=
v
.
(
func
(
i
int
)
interface
{})
:
assert
(
x
==
f
&&
i
==
Func
,
"switch 1 fun"
);
default
:
assert
(
false
,
"switch 1 unknown"
);
}
}
//
for i := Bool; i < Last; i++ {
//
switch v := f(i); true {
//
case x := v.(bool):
//
assert(x == true && i == Bool, "switch 1 bool");
//
case x := v.(int):
//
assert(x == 7 && i == Int, "switch 1 int");
//
case x := v.(float):
//
assert(x == 7.4 && i == Float, "switch 1 float");
//
case x := v.(string):
//
assert(x == "hello" && i == String, "switch 1 string");
//
case x := v.(S):
//
assert(x.a == 1234 && i == Struct, "switch 1 struct");
//
case x := v.(chan int):
//
assert(x == c && i == Chan, "switch 1 chan");
//
case x := v.([]int):
//
assert(x[3] == 3 && i == Array, "switch 1 array");
//
case x := v.(map[string]int):
//
assert(x == m && i == Map, "switch 1 map");
//
case x := v.(func(i int) interface{}):
//
assert(x == f && i == Func, "switch 1 fun");
//
default:
//
assert(false, "switch 1 unknown");
//
}
//
}
// type switch style
for
i
:=
Bool
;
i
<
Last
;
i
++
{
...
...
@@ -135,24 +135,24 @@ func main() {
assert
(
false
,
"switch 4 unknown"
);
}
switch
true
{
case
x
:=
f
(
Int
)
.
(
float
)
:
assert
(
false
,
"switch 5 type guard wrong type"
);
case
x
:=
f
(
Int
)
.
(
int
)
:
assert
(
x
==
7
,
"switch 5 type guard"
);
default
:
assert
(
false
,
"switch 5 unknown"
);
}
//
switch true {
//
case x := f(Int).(float):
//
assert(false, "switch 5 type guard wrong type");
//
case x := f(Int).(int):
//
assert(x == 7, "switch 5 type guard");
//
default:
//
assert(false, "switch 5 unknown");
//
}
m
[
"7"
]
=
7
;
switch
true
{
case
x
:=
m
[
"6"
]
:
assert
(
false
,
"switch 6 map reference wrong"
);
case
x
:=
m
[
"7"
]
:
assert
(
x
==
7
,
"switch 6 map reference"
);
default
:
assert
(
false
,
"switch 6 unknown"
);
}
//
switch true {
//
case x := m["6"]:
//
assert(false, "switch 6 map reference wrong");
//
case x := m["7"]:
//
assert(x == 7, "switch 6 map reference");
//
default:
//
assert(false, "switch 6 unknown");
//
}
go
func
()
{
<-
c
;
c
<-
77
;
}
();
// guarantee the channel is ready
...
...
@@ -161,13 +161,13 @@ func main() {
sys
.
Gosched
();
}
dummyc
:=
make
(
chan
int
);
switch
true
{
case
x
:=
<-
dummyc
:
assert
(
false
,
"switch 7 chan wrong"
);
case
x
:=
<-
c
:
assert
(
x
==
77
,
"switch 7 chan"
);
default
:
assert
(
false
,
"switch 7 unknown"
);
}
//
switch true {
//
case x := <-dummyc:
//
assert(false, "switch 7 chan wrong");
//
case x := <-c:
//
assert(x == 77, "switch 7 chan");
//
default:
//
assert(false, "switch 7 unknown");
//
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment