checker: disallow comparison between enum and int (#7886)

pull/7971/head
Swastik Baranwal 2021-01-08 22:11:52 +05:30 committed by GitHub
parent 46a5c487c1
commit 9291fb5e0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 154 additions and 165 deletions

View File

@ -7,7 +7,8 @@
- `byte.str()` has been fixed and works like with all other numbers. `byte.ascii_str()` has been added. - `byte.str()` has been fixed and works like with all other numbers. `byte.ascii_str()` has been added.
- Smart cast in for loops: `for mut x is string {}`. - Smart cast in for loops: `for mut x is string {}`.
- `[noinit]` struct attribute to disallow direct struct initialization with `Foo{}`. - `[noinit]` struct attribute to disallow direct struct initialization with `Foo{}`.
- support `[manualfree] fn f1(){}` and `[manualfree] module m1`, for functions doing their own memory management. - Treating `enum` as `int` is removed for strict type checking.
- Support `[manualfree] fn f1(){}` and `[manualfree] module m1`, for functions doing their own memory management.
## V 0.2.1 ## V 0.2.1
*30 Dec 2020* *30 Dec 2020*

View File

@ -797,7 +797,7 @@ fn (mut app App) next_theme() {
[inline] [inline]
fn (mut app App) next_tile_format() { fn (mut app App) next_tile_format() {
app.tile_format = int(app.tile_format) + 1 app.tile_format = TileFormat(int(app.tile_format) + 1)
if app.tile_format == .end_ { if app.tile_format == .end_ {
app.tile_format = .normal app.tile_format = .normal
} }

View File

@ -14,7 +14,7 @@ mut:
fn main() { fn main() {
mut color_action := C.sg_color_attachment_action{ mut color_action := C.sg_color_attachment_action{
action: C.SG_ACTION_CLEAR action: gfx.Action(C.SG_ACTION_CLEAR)
} }
color_action.val[0] = 0.3 color_action.val[0] = 0.3
color_action.val[1] = 0.3 color_action.val[1] = 0.3

View File

@ -64,7 +64,7 @@ mut:
fn main() { fn main() {
mut color_action := C.sg_color_attachment_action{ mut color_action := C.sg_color_attachment_action{
action: C.SG_ACTION_CLEAR action: gfx.Action(C.SG_ACTION_CLEAR)
} }
color_action.val[0] = 1 color_action.val[0] = 1
color_action.val[1] = 1 color_action.val[1] = 1

View File

@ -82,8 +82,8 @@ fn init(user_data voidptr) {
mut pipdesc := C.sg_pipeline_desc{} mut pipdesc := C.sg_pipeline_desc{}
unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))} unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))}
pipdesc.blend.enabled = true pipdesc.blend.enabled = true
pipdesc.blend.src_factor_rgb = C.SG_BLENDFACTOR_SRC_ALPHA pipdesc.blend.src_factor_rgb = gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA)
pipdesc.blend.dst_factor_rgb = C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA pipdesc.blend.dst_factor_rgb = gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA)
app.alpha_pip = sgl.make_pipeline(&pipdesc) app.alpha_pip = sgl.make_pipeline(&pipdesc)
} }

View File

@ -145,8 +145,8 @@ fn gg_init_sokol_window(user_data voidptr) {
mut pipdesc := C.sg_pipeline_desc{} mut pipdesc := C.sg_pipeline_desc{}
unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) } unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) }
pipdesc.blend.enabled = true pipdesc.blend.enabled = true
pipdesc.blend.src_factor_rgb = C.SG_BLENDFACTOR_SRC_ALPHA pipdesc.blend.src_factor_rgb = gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA)
pipdesc.blend.dst_factor_rgb = C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA pipdesc.blend.dst_factor_rgb = gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA)
g.timage_pip = sgl.make_pipeline(&pipdesc) g.timage_pip = sgl.make_pipeline(&pipdesc)
// //
if g.config.init_fn != voidptr(0) { if g.config.init_fn != voidptr(0) {
@ -189,7 +189,7 @@ fn gg_event_fn(ce &C.sapp_event, user_data voidptr) {
.key_down { .key_down {
if g.config.keydown_fn != voidptr(0) { if g.config.keydown_fn != voidptr(0) {
kdfn := g.config.keydown_fn kdfn := g.config.keydown_fn
kdfn(e.key_code, e.modifiers, g.config.user_data) kdfn(e.key_code, sapp.Modifier(e.modifiers), g.config.user_data)
} }
} }
.char { .char {

View File

@ -36,37 +36,36 @@ pub enum FieldType {
pub fn (f FieldType) str() string { pub fn (f FieldType) str() string {
return match f { return match f {
0 { 'decimal' } .type_decimal { 'decimal' }
1 { 'tiny' } .type_tiny { 'tiny' }
2 { 'short' } .type_short { 'short' }
3 { 'long' } .type_long { 'long' }
4 { 'float' } .type_float { 'float' }
5 { 'double' } .type_double { 'double' }
6 { 'null' } .type_null { 'null' }
7 { 'timestamp' } .type_timestamp { 'timestamp' }
8 { 'longlong' } .type_longlong { 'longlong' }
9 { 'int24' } .type_int24 { 'int24' }
10 { 'date' } .type_date { 'date' }
11 { 'time' } .type_time { 'time' }
12 { 'datetime' } .type_datetime { 'datetime' }
13 { 'year' } .type_year { 'year' }
14 { 'newdate' } .type_newdate { 'newdate' }
15 { 'varchar' } .type_varchar { 'varchar' }
16 { 'bit' } .type_bit { 'bit' }
17 { 'timestamp2' } .type_timestamp2 { 'timestamp2' }
18 { 'datetime2' } .type_datetime2 { 'datetime2' }
19 { 'time2' } .type_time2 { 'time2' }
245 { 'json' } .type_json { 'json' }
246 { 'newdecimal' } .type_newdecimal { 'newdecimal' }
247 { 'enum' } .type_enum { 'enum' }
248 { 'set' } .type_set { 'set' }
249 { 'tiny_blob' } .type_tiny_blob { 'tiny_blob' }
250 { 'medium_blob' } .type_medium_blob { 'medium_blob' }
251 { 'long_blob' } .type_long_blob { 'long_blob' }
252 { 'blob' } .type_blob { 'blob' }
253 { 'var_string' } .type_var_string { 'var_string' }
254 { 'string' } .type_string { 'string' }
255 { 'geometry' } .type_geometry { 'geometry' }
else { 'unknown' }
} }
} }

View File

@ -9,6 +9,9 @@ pub:
port int port int
} }
struct C.addrinfo {
}
pub fn (a Addr) str() string { pub fn (a Addr) str() string {
return '${a.saddr}:${a.port}' return '${a.saddr}:${a.port}'
} }
@ -19,7 +22,7 @@ const (
) )
fn new_addr(addr C.sockaddr) ?Addr { fn new_addr(addr C.sockaddr) ?Addr {
addr_len := if addr.sa_family == SocketFamily.inet { addr_len := if addr.sa_family == int(SocketFamily.inet) {
sizeof(C.sockaddr) sizeof(C.sockaddr)
} else { } else {
// TODO NOOOOOOOOOOOO // TODO NOOOOOOOOOOOO
@ -57,8 +60,8 @@ pub fn resolve_addr(addr string, family SocketFamily, typ SocketType) ?Addr {
address, port := split_address(addr)? address, port := split_address(addr)?
mut hints := C.addrinfo{} mut hints := C.addrinfo{}
hints.ai_family = family hints.ai_family = int(family)
hints.ai_socktype = typ hints.ai_socktype = int(typ)
hints.ai_flags = C.AI_PASSIVE hints.ai_flags = C.AI_PASSIVE
hints.ai_protocol = 0 hints.ai_protocol = 0
hints.ai_addrlen = 0 hints.ai_addrlen = 0

View File

@ -20,7 +20,7 @@ pub fn socket_error(potential_code int) ?int {
if potential_code < 0 { if potential_code < 0 {
last_error_int := C.WSAGetLastError() last_error_int := C.WSAGetLastError()
last_error := wsa_error(last_error_int) last_error := wsa_error(last_error_int)
return error_with_code('net: socket error: ($last_error_int) $last_error', last_error) return error_with_code('net: socket error: ($last_error_int) $last_error', int(last_error))
} }
} }
$else { $else {

View File

@ -7,7 +7,7 @@ pub fn ssl_error(ret int, ssl voidptr) ?SSLError {
.ssl_error_syscall { return error_with_code('unrecoverable syscall ($res)', res) } .ssl_error_syscall { return error_with_code('unrecoverable syscall ($res)', res) }
.ssl_error_ssl { return error_with_code('unrecoverable ssl protocol error ($res)', .ssl_error_ssl { return error_with_code('unrecoverable ssl protocol error ($res)',
res) } res) }
else { return res } else { return SSLError(res) }
} }
} }

View File

@ -105,7 +105,7 @@ fn (c Client) expect_reply(expected ReplyCode) ? {
if str.len >= 3 { if str.len >= 3 {
status := str[..3].int() status := str[..3].int()
if status != expected { if ReplyCode(status) != expected {
return error('Received unexpected status code $status, expecting $expected') return error('Received unexpected status code $status, expecting $expected')
} }
} else { return error('Recieved unexpected SMTP data: $str') } } else { return error('Recieved unexpected SMTP data: $str') }

View File

@ -47,14 +47,11 @@ pub fn (c TcpConn) write_ptr(b byteptr, len int) ? {
mut sent := C.send(c.sock.handle, ptr, remaining, msg_nosignal) mut sent := C.send(c.sock.handle, ptr, remaining, msg_nosignal)
if sent < 0 { if sent < 0 {
code := error_code() code := error_code()
match code { if code == int(error_ewouldblock) {
error_ewouldblock { c.wait_for_write()
c.wait_for_write() continue
continue } else {
} wrap_error(code) ?
else {
wrap_error(code) ?
}
} }
} }
total_sent += sent total_sent += sent
@ -82,18 +79,15 @@ pub fn (c TcpConn) read_ptr(buf_ptr byteptr, len int) ?int {
return res return res
} }
code := error_code() code := error_code()
match code { if code == int(error_ewouldblock) {
error_ewouldblock { c.wait_for_read() ?
c.wait_for_read() ? res = wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
res = wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ? $if trace_tcp ? {
$if trace_tcp ? { eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
}
return socket_error(res)
}
else {
wrap_error(code) ?
} }
return socket_error(res)
} else {
wrap_error(code) ?
} }
} }
@ -160,7 +154,7 @@ pub fn (c TcpConn) peer_ip() ?string {
buf := [44]byte{} buf := [44]byte{}
peeraddr := C.sockaddr_in{} peeraddr := C.sockaddr_in{}
speeraddr := sizeof(peeraddr) speeraddr := sizeof(peeraddr)
socket_error(C.getpeername(c.sock.handle, unsafe {&C.sockaddr(&peeraddr)}, &speeraddr)) ? socket_error(C.getpeername(c.sock.handle, unsafe { &C.sockaddr(&peeraddr) }, &speeraddr)) ?
cstr := C.inet_ntop(C.AF_INET, &peeraddr.sin_addr, buf, sizeof(buf)) cstr := C.inet_ntop(C.AF_INET, &peeraddr.sin_addr, buf, sizeof(buf))
if cstr == 0 { if cstr == 0 {
return error('net.peer_ip: inet_ntop failed') return error('net.peer_ip: inet_ntop failed')
@ -185,12 +179,12 @@ pub fn listen_tcp(port int) ?TcpListener {
s := new_tcp_socket() ? s := new_tcp_socket() ?
validate_port(port) ? validate_port(port) ?
mut addr := C.sockaddr_in{} mut addr := C.sockaddr_in{}
addr.sin_family = SocketFamily.inet addr.sin_family = int(SocketFamily.inet)
addr.sin_port = C.htons(port) addr.sin_port = C.htons(port)
addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY) addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
size := sizeof(C.sockaddr_in) size := sizeof(C.sockaddr_in)
// cast to the correct type // cast to the correct type
sockaddr := unsafe {&C.sockaddr(&addr)} sockaddr := unsafe { &C.sockaddr(&addr) }
socket_error(C.bind(s.handle, sockaddr, size)) ? socket_error(C.bind(s.handle, sockaddr, size)) ?
socket_error(C.listen(s.handle, 128)) ? socket_error(C.listen(s.handle, 128)) ?
return TcpListener{ return TcpListener{
@ -205,7 +199,7 @@ pub fn (l TcpListener) accept() ?TcpConn {
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_storage)) } unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_storage)) }
size := sizeof(C.sockaddr_storage) size := sizeof(C.sockaddr_storage)
// cast to correct type // cast to correct type
sock_addr := unsafe {&C.sockaddr(&addr)} sock_addr := unsafe { &C.sockaddr(&addr) }
mut new_handle := C.accept(l.sock.handle, sock_addr, &size) mut new_handle := C.accept(l.sock.handle, sock_addr, &size)
if new_handle <= 0 { if new_handle <= 0 {
l.wait_for_accept() ? l.wait_for_accept() ?
@ -344,7 +338,7 @@ pub fn (s TcpSocket) address() ?Addr {
mut addr := C.sockaddr_in{} mut addr := C.sockaddr_in{}
size := sizeof(C.sockaddr_in) size := sizeof(C.sockaddr_in)
// cast to the correct type // cast to the correct type
sockaddr := unsafe {&C.sockaddr(&addr)} sockaddr := unsafe { &C.sockaddr(&addr) }
C.getsockname(s.handle, sockaddr, &size) C.getsockname(s.handle, sockaddr, &size)
return new_addr(sockaddr) return new_addr(sockaddr)
} }

View File

@ -8,35 +8,29 @@ const (
) )
pub struct UdpConn { pub struct UdpConn {
sock UdpSocket sock UdpSocket
mut: mut:
write_deadline time.Time write_deadline time.Time
read_deadline time.Time read_deadline time.Time
read_timeout time.Duration
read_timeout time.Duration write_timeout time.Duration
write_timeout time.Duration
} }
pub fn dial_udp(laddr string, raddr string) ?UdpConn { pub fn dial_udp(laddr string, raddr string) ?UdpConn {
// Dont have to do this when its fixed // Dont have to do this when its fixed
// this just allows us to store this `none` optional in a struct // this just allows us to store this `none` optional in a struct
resolve_wrapper := fn(raddr string) ?Addr { resolve_wrapper := fn (raddr string) ?Addr {
x := resolve_addr(raddr, .inet, .udp) or { return none } x := resolve_addr(raddr, .inet, .udp) or { return none }
return x return x
} }
local := resolve_addr(laddr, .inet, .udp) ?
local := resolve_addr(laddr, .inet, .udp)? sbase := new_udp_socket(local.port) ?
sbase := new_udp_socket(local.port)? sock := UdpSocket{
sock := UdpSocket {
handle: sbase.handle handle: sbase.handle
l: local l: local
r: resolve_wrapper(raddr) r: resolve_wrapper(raddr)
} }
return UdpConn{
return UdpConn {
sock: sock sock: sock
read_timeout: udp_default_read_timeout read_timeout: udp_default_read_timeout
write_timeout: udp_default_write_timeout write_timeout: udp_default_write_timeout
@ -44,10 +38,7 @@ pub fn dial_udp(laddr string, raddr string) ?UdpConn {
} }
pub fn (c UdpConn) write_ptr(b byteptr, len int) ? { pub fn (c UdpConn) write_ptr(b byteptr, len int) ? {
remote := c.sock.remote() or { remote := c.sock.remote() or { return err_no_udp_remote }
return err_no_udp_remote
}
return c.write_to_ptr(remote, b, len) return c.write_to_ptr(remote, b, len)
} }
@ -61,22 +52,16 @@ pub fn (c UdpConn) write_str(s string) ? {
pub fn (c UdpConn) write_to_ptr(addr Addr, b byteptr, len int) ? { pub fn (c UdpConn) write_to_ptr(addr Addr, b byteptr, len int) ? {
res := C.sendto(c.sock.handle, b, len, 0, &addr.addr, addr.len) res := C.sendto(c.sock.handle, b, len, 0, &addr.addr, addr.len)
if res >= 0 { if res >= 0 {
return none return none
} }
code := error_code() code := error_code()
match code { if code == int(error_ewouldblock) {
error_ewouldblock { c.wait_for_write() ?
c.wait_for_write()? socket_error(C.sendto(c.sock.handle, b, len, 0, &addr.addr, addr.len)) ?
socket_error(C.sendto(c.sock.handle, b, len, 0, &addr.addr, addr.len))? } else {
} wrap_error(code) ?
else {
wrap_error(code)?
}
} }
return none return none
} }
@ -94,30 +79,24 @@ pub fn (c UdpConn) write_to_string(addr Addr, s string) ? {
pub fn (c UdpConn) read(mut buf []byte) ?(int, Addr) { pub fn (c UdpConn) read(mut buf []byte) ?(int, Addr) {
mut addr_from := C.sockaddr{} mut addr_from := C.sockaddr{}
len := sizeof(C.sockaddr) len := sizeof(C.sockaddr)
mut res := wrap_read_result(C.recvfrom(c.sock.handle, buf.data, buf.len, 0, &addr_from,
mut res := wrap_read_result(C.recvfrom(c.sock.handle, buf.data, buf.len, 0, &addr_from, &len))? &len)) ?
if res > 0 { if res > 0 {
addr := new_addr(addr_from)? addr := new_addr(addr_from) ?
return res, addr return res, addr
} }
code := error_code() code := error_code()
match code { if code == int(error_ewouldblock) {
error_ewouldblock { c.wait_for_read() ?
c.wait_for_read()? // same setup as in tcp
// same setup as in tcp res = wrap_read_result(C.recvfrom(c.sock.handle, buf.data, buf.len, 0, &addr_from,
res = wrap_read_result(C.recvfrom(c.sock.handle, buf.data, buf.len, 0, &addr_from, &len))? &len)) ?
res2 := socket_error(res)? res2 := socket_error(res) ?
addr := new_addr(addr_from) ?
addr := new_addr(addr_from)? return res2, addr
return res2, addr } else {
} wrap_error(code) ?
else {
wrap_error(code)?
}
} }
return none return none
} }
@ -147,7 +126,7 @@ pub fn (c UdpConn) read_timeout() time.Duration {
return c.read_timeout return c.read_timeout
} }
pub fn(mut c UdpConn) set_read_timeout(t time.Duration) { pub fn (mut c UdpConn) set_read_timeout(t time.Duration) {
c.read_timeout = t c.read_timeout = t
} }
@ -179,9 +158,8 @@ pub fn (c UdpConn) close() ? {
} }
pub fn listen_udp(port int) ?UdpConn { pub fn listen_udp(port int) ?UdpConn {
s := new_udp_socket(port)? s := new_udp_socket(port) ?
return UdpConn{
return UdpConn {
sock: s sock: s
read_timeout: udp_default_read_timeout read_timeout: udp_default_read_timeout
write_timeout: udp_default_write_timeout write_timeout: udp_default_write_timeout
@ -190,38 +168,32 @@ pub fn listen_udp(port int) ?UdpConn {
struct UdpSocket { struct UdpSocket {
handle int handle int
l Addr
l Addr r ?Addr
r ?Addr
} }
fn new_udp_socket(local_port int) ?UdpSocket { fn new_udp_socket(local_port int) ?UdpSocket {
sockfd := socket_error(C.socket(SocketFamily.inet, SocketType.udp, 0))? sockfd := socket_error(C.socket(SocketFamily.inet, SocketType.udp, 0)) ?
s := UdpSocket { s := UdpSocket{
handle: sockfd handle: sockfd
} }
s.set_option_bool(.reuse_addr, true)? s.set_option_bool(.reuse_addr, true) ?
$if windows { $if windows {
t := true t := true
socket_error(C.ioctlsocket(sockfd, fionbio, &t))? socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
} $else { } $else {
socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK)) socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK))
} }
// In UDP we always have to bind to a port // In UDP we always have to bind to a port
validate_port(local_port)? validate_port(local_port) ?
mut addr := C.sockaddr_in{} mut addr := C.sockaddr_in{}
addr.sin_family = SocketFamily.inet addr.sin_family = int(SocketFamily.inet)
addr.sin_port = C.htons(local_port) addr.sin_port = C.htons(local_port)
addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY) addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
size := sizeof(C.sockaddr_in) size := sizeof(C.sockaddr_in)
// cast to the correct type // cast to the correct type
sockaddr := unsafe {&C.sockaddr(&addr)} sockaddr := unsafe { &C.sockaddr(&addr) }
socket_error(C.bind(s.handle, sockaddr, size)) ?
socket_error(C.bind(s.handle, sockaddr, size))?
return s return s
} }
@ -237,7 +209,7 @@ pub fn (s UdpSocket) set_option_bool(opt SocketOption, value bool) ? {
// if opt !in opts_bool { // if opt !in opts_bool {
// return err_option_wrong_type // return err_option_wrong_type
// } // }
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool)))? socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool))) ?
return none return none
} }

View File

@ -2,7 +2,7 @@ module gfx
pub fn create_clear_pass(r f32, g f32, b f32, a f32) C.sg_pass_action { pub fn create_clear_pass(r f32, g f32, b f32, a f32) C.sg_pass_action {
mut color_action := C.sg_color_attachment_action{ mut color_action := C.sg_color_attachment_action{
action: C.SG_ACTION_CLEAR action: gfx.Action(C.SG_ACTION_CLEAR)
} }
// color_action.set_color_values(r, g, b, a) // color_action.set_color_values(r, g, b, a)
color_action.val[0] = r color_action.val[0] = r

View File

@ -88,11 +88,6 @@ pub fn (mut c Checker) check_basic(got table.Type, expected table.Type) bool {
// fn == 0 // fn == 0
return true return true
} }
// allow enum value to be used as int
if (got_type_sym.is_int() && exp_type_sym.kind == .enum_) ||
(exp_type_sym.is_int() && got_type_sym.kind == .enum_) {
return true
}
// array fn // array fn
if got_type_sym.kind == .array && exp_type_sym.kind == .array { if got_type_sym.kind == .array && exp_type_sym.kind == .array {
if c.table.type_to_str(got) == c.table.type_to_str(expected) { if c.table.type_to_str(got) == c.table.type_to_str(expected) {

View File

@ -0,0 +1,13 @@
vlib/v/checker/tests/enum_as_int_err.vv:9:10: error: cannot assign to `color`: expected `Color`, not `int literal`
7 | mut color := Color.red
8 | mut foo := 1
9 | color = 1
| ^
10 | foo = Color.red
11 | println(color == 0)
vlib/v/checker/tests/enum_as_int_err.vv:11:2: error: cannot assign to `foo`: expected `int`, not `Color`
9 | color = 1
10 | foo = Color.red
11 | println(color == 0)
| ~~~~~~~
12 | }

View File

@ -0,0 +1,12 @@
enum Color {
red
blue
}
fn main() {
mut color := Color.red
mut foo := 1
color = 1
foo = Color.red
println(color == 0)
}

View File

@ -372,10 +372,10 @@ pub fn (mut g Gen) ret() {
pub fn (mut g Gen) push(reg Register) { pub fn (mut g Gen) push(reg Register) {
if reg < .r8 { if reg < .r8 {
g.write8(0x50 + reg) g.write8(0x50 + int(reg))
} else { } else {
g.write8(0x41) g.write8(0x41)
g.write8(0x50 + reg - 8) g.write8(0x50 + int(reg) - 8)
} }
/* /*
match reg { match reg {
@ -387,7 +387,7 @@ pub fn (mut g Gen) push(reg Register) {
} }
pub fn (mut g Gen) pop(reg Register) { pub fn (mut g Gen) pop(reg Register) {
g.write8(0x58 + reg) g.write8(0x58 + int(reg))
// TODO r8... // TODO r8...
g.println('pop $reg') g.println('pop $reg')
} }
@ -395,7 +395,7 @@ pub fn (mut g Gen) pop(reg Register) {
pub fn (mut g Gen) sub32(reg Register, val int) { pub fn (mut g Gen) sub32(reg Register, val int) {
g.write8(0x48) g.write8(0x48)
g.write8(0x81) g.write8(0x81)
g.write8(0xe8 + reg) // TODO rax is different? g.write8(0xe8 + int(reg)) // TODO rax is different?
g.write32(val) g.write32(val)
g.println('sub32 $reg,$val.hex2()') g.println('sub32 $reg,$val.hex2()')
} }
@ -403,7 +403,7 @@ pub fn (mut g Gen) sub32(reg Register, val int) {
pub fn (mut g Gen) sub8(reg Register, val int) { pub fn (mut g Gen) sub8(reg Register, val int) {
g.write8(0x48) g.write8(0x48)
g.write8(0x83) g.write8(0x83)
g.write8(0xe8 + reg) // TODO rax is different? g.write8(0xe8 + int(reg)) // TODO rax is different?
g.write8(val) g.write8(val)
g.println('sub8 $reg,$val.hex2()') g.println('sub8 $reg,$val.hex2()')
} }
@ -411,7 +411,7 @@ pub fn (mut g Gen) sub8(reg Register, val int) {
pub fn (mut g Gen) add(reg Register, val int) { pub fn (mut g Gen) add(reg Register, val int) {
g.write8(0x48) g.write8(0x48)
g.write8(0x81) g.write8(0x81)
g.write8(0xe8 + reg) // TODO rax is different? g.write8(0xe8 + int(reg)) // TODO rax is different?
g.write32(val) g.write32(val)
g.println('add $reg,$val.hex2()') g.println('add $reg,$val.hex2()')
} }

View File

@ -2065,9 +2065,9 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
p.scanner.codegen(' p.scanner.codegen('
// //
$pubfn ( e &$enum_name) has(flag $enum_name) bool { return (int(*e) & (int(flag))) != 0 } $pubfn ( e &$enum_name) has(flag $enum_name) bool { return (int(*e) & (int(flag))) != 0 }
$pubfn (mut e $enum_name) set(flag $enum_name) { unsafe{ *e = int(*e) | (int(flag)) } } $pubfn (mut e $enum_name) set(flag $enum_name) { unsafe{ *e = ${enum_name}(int(*e) | (int(flag))) } }
$pubfn (mut e $enum_name) clear(flag $enum_name) { unsafe{ *e = int(*e) & ~(int(flag)) } } $pubfn (mut e $enum_name) clear(flag $enum_name) { unsafe{ *e = ${enum_name}(int(*e) & ~(int(flag))) } }
$pubfn (mut e $enum_name) toggle(flag $enum_name) { unsafe{ *e = int(*e) ^ (int(flag)) } } $pubfn (mut e $enum_name) toggle(flag $enum_name) { unsafe{ *e = ${enum_name}(int(*e) ^ (int(flag))) } }
// //
') ')
} }

View File

@ -442,9 +442,9 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr {
// } // }
p.next() p.next()
mut right := if op == .minus { mut right := if op == .minus {
p.expr(token.Precedence.call) p.expr(int(token.Precedence.call))
} else { } else {
p.expr(token.Precedence.prefix) p.expr(int(token.Precedence.prefix))
} }
p.is_amp = false p.is_amp = false
if mut right is ast.CastExpr { if mut right is ast.CastExpr {

View File

@ -10,7 +10,7 @@ mut:
} }
fn test_enum_first_value() { fn test_enum_first_value() {
assert MyEnum.first == 20 assert MyEnum.first == MyEnum(20)
} }
fn test_enum_default_value() { fn test_enum_default_value() {

View File

@ -68,10 +68,10 @@ enum Foo {
fn test_nums() { fn test_nums() {
foo := Foo.a foo := Foo.a
assert foo == 1 assert foo == Foo(1)
assert Foo.c == 3 assert Foo.c == Foo(3)
d := Foo.d d := Foo.d
assert d == -10 assert d == Foo(-10)
} }
/* /*

View File

@ -172,7 +172,7 @@ fn build_keys() map[string]Kind {
mut res := map[string]Kind{} mut res := map[string]Kind{}
for t in int(Kind.keyword_beg) + 1 .. int(Kind.keyword_end) { for t in int(Kind.keyword_beg) + 1 .. int(Kind.keyword_end) {
key := token_str[t] key := token_str[t]
res[key] = t res[key] = Kind(t)
} }
return res return res
} }

View File

@ -214,7 +214,7 @@ pub fn (mut s SSLConn) write(bytes []byte) ? {
} else if err_res == .ssl_error_zero_return { } else if err_res == .ssl_error_zero_return {
return error('ssl write on closed connection') // Todo error_with_code close return error('ssl write on closed connection') // Todo error_with_code close
} }
return error_with_code('Could not write SSL. ($err_res),err', err_res) return error_with_code('Could not write SSL. ($err_res),err', int(err_res))
} }
total_sent += sent total_sent += sent
} }