1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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.
- Smart cast in for loops: `for mut x is string {}`.
- `[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
*30 Dec 2020*

View File

@ -797,7 +797,7 @@ fn (mut app App) next_theme() {
[inline]
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_ {
app.tile_format = .normal
}

View File

@ -14,7 +14,7 @@ mut:
fn main() {
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[1] = 0.3

View File

@ -64,7 +64,7 @@ mut:
fn main() {
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[1] = 1

View File

@ -82,8 +82,8 @@ fn init(user_data voidptr) {
mut pipdesc := C.sg_pipeline_desc{}
unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))}
pipdesc.blend.enabled = true
pipdesc.blend.src_factor_rgb = C.SG_BLENDFACTOR_SRC_ALPHA
pipdesc.blend.dst_factor_rgb = C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA
pipdesc.blend.src_factor_rgb = gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA)
pipdesc.blend.dst_factor_rgb = gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA)
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{}
unsafe { C.memset(&pipdesc, 0, sizeof(pipdesc)) }
pipdesc.blend.enabled = true
pipdesc.blend.src_factor_rgb = C.SG_BLENDFACTOR_SRC_ALPHA
pipdesc.blend.dst_factor_rgb = C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA
pipdesc.blend.src_factor_rgb = gfx.BlendFactor(C.SG_BLENDFACTOR_SRC_ALPHA)
pipdesc.blend.dst_factor_rgb = gfx.BlendFactor(C.SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA)
g.timage_pip = sgl.make_pipeline(&pipdesc)
//
if g.config.init_fn != voidptr(0) {
@ -189,7 +189,7 @@ fn gg_event_fn(ce &C.sapp_event, user_data voidptr) {
.key_down {
if g.config.keydown_fn != voidptr(0) {
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 {

View File

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

View File

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

View File

@ -20,7 +20,7 @@ pub fn socket_error(potential_code int) ?int {
if potential_code < 0 {
last_error_int := C.WSAGetLastError()
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 {

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_ssl { return error_with_code('unrecoverable ssl protocol error ($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 {
status := str[..3].int()
if status != expected {
if ReplyCode(status) != expected {
return error('Received unexpected status code $status, expecting $expected')
}
} 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)
if sent < 0 {
code := error_code()
match code {
error_ewouldblock {
c.wait_for_write()
continue
}
else {
wrap_error(code) ?
}
if code == int(error_ewouldblock) {
c.wait_for_write()
continue
} else {
wrap_error(code) ?
}
}
total_sent += sent
@ -82,18 +79,15 @@ pub fn (c TcpConn) read_ptr(buf_ptr byteptr, len int) ?int {
return res
}
code := error_code()
match code {
error_ewouldblock {
c.wait_for_read() ?
res = wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
$if trace_tcp ? {
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) ?
if code == int(error_ewouldblock) {
c.wait_for_read() ?
res = wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
$if trace_tcp ? {
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) ?
}
}
@ -160,7 +154,7 @@ pub fn (c TcpConn) peer_ip() ?string {
buf := [44]byte{}
peeraddr := C.sockaddr_in{}
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))
if cstr == 0 {
return error('net.peer_ip: inet_ntop failed')
@ -185,12 +179,12 @@ pub fn listen_tcp(port int) ?TcpListener {
s := new_tcp_socket() ?
validate_port(port) ?
mut addr := C.sockaddr_in{}
addr.sin_family = SocketFamily.inet
addr.sin_family = int(SocketFamily.inet)
addr.sin_port = C.htons(port)
addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
size := sizeof(C.sockaddr_in)
// 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.listen(s.handle, 128)) ?
return TcpListener{
@ -205,7 +199,7 @@ pub fn (l TcpListener) accept() ?TcpConn {
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_storage)) }
size := sizeof(C.sockaddr_storage)
// 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)
if new_handle <= 0 {
l.wait_for_accept() ?
@ -344,7 +338,7 @@ pub fn (s TcpSocket) address() ?Addr {
mut addr := C.sockaddr_in{}
size := sizeof(C.sockaddr_in)
// cast to the correct type
sockaddr := unsafe {&C.sockaddr(&addr)}
sockaddr := unsafe { &C.sockaddr(&addr) }
C.getsockname(s.handle, sockaddr, &size)
return new_addr(sockaddr)
}

View File

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

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 {
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.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
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
if got_type_sym.kind == .array && exp_type_sym.kind == .array {
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) {
if reg < .r8 {
g.write8(0x50 + reg)
g.write8(0x50 + int(reg))
} else {
g.write8(0x41)
g.write8(0x50 + reg - 8)
g.write8(0x50 + int(reg) - 8)
}
/*
match reg {
@ -387,7 +387,7 @@ pub fn (mut g Gen) push(reg Register) {
}
pub fn (mut g Gen) pop(reg Register) {
g.write8(0x58 + reg)
g.write8(0x58 + int(reg))
// TODO r8...
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) {
g.write8(0x48)
g.write8(0x81)
g.write8(0xe8 + reg) // TODO rax is different?
g.write8(0xe8 + int(reg)) // TODO rax is different?
g.write32(val)
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) {
g.write8(0x48)
g.write8(0x83)
g.write8(0xe8 + reg) // TODO rax is different?
g.write8(0xe8 + int(reg)) // TODO rax is different?
g.write8(val)
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) {
g.write8(0x48)
g.write8(0x81)
g.write8(0xe8 + reg) // TODO rax is different?
g.write8(0xe8 + int(reg)) // TODO rax is different?
g.write32(val)
g.println('add $reg,$val.hex2()')
}

View File

@ -2065,9 +2065,9 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
p.scanner.codegen('
//
$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) clear(flag $enum_name) { unsafe{ *e = int(*e) & ~(int(flag)) } }
$pubfn (mut e $enum_name) toggle(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 = ${enum_name}(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()
mut right := if op == .minus {
p.expr(token.Precedence.call)
p.expr(int(token.Precedence.call))
} else {
p.expr(token.Precedence.prefix)
p.expr(int(token.Precedence.prefix))
}
p.is_amp = false
if mut right is ast.CastExpr {

View File

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

View File

@ -68,10 +68,10 @@ enum Foo {
fn test_nums() {
foo := Foo.a
assert foo == 1
assert Foo.c == 3
assert foo == Foo(1)
assert Foo.c == Foo(3)
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{}
for t in int(Kind.keyword_beg) + 1 .. int(Kind.keyword_end) {
key := token_str[t]
res[key] = t
res[key] = Kind(t)
}
return res
}

View File

@ -214,7 +214,7 @@ pub fn (mut s SSLConn) write(bytes []byte) ? {
} else if err_res == .ssl_error_zero_return {
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
}