checker: make the compiler stricter when checking pointers

pull/9608/head
Delyan Angelov 2021-04-05 10:02:47 +03:00
parent d82a0c1637
commit 4cde74f120
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
8 changed files with 38 additions and 25 deletions

View File

@ -7,7 +7,7 @@ fn C.wyhash64(u64, u64) u64
[inline] [inline]
pub fn wyhash_c(key byteptr, len u64, seed u64) u64 { pub fn wyhash_c(key byteptr, len u64, seed u64) u64 {
return C.wyhash(key, len, seed, C._wyp) return C.wyhash(key, len, seed, &u64(C._wyp))
} }
[inline] [inline]

View File

@ -11,10 +11,10 @@ pub mut:
[inline] [inline]
pub fn (mut r Request) parse_request(s string, max_headers int) int { pub fn (mut r Request) parse_request(s string, max_headers int) int {
method_len := u64(0) method_len := size_t(0)
path_len := u64(0) path_len := size_t(0)
minor_version := 0 minor_version := 0
num_headers := u64(max_headers) num_headers := size_t(max_headers)
pret := C.phr_parse_request(s.str, s.len, PPchar(&r.method.str), &method_len, PPchar(&r.path.str), pret := C.phr_parse_request(s.str, s.len, PPchar(&r.method.str), &method_len, PPchar(&r.path.str),
&path_len, &minor_version, &r.headers[0], &num_headers, 0) &path_len, &minor_version, &r.headers[0], &num_headers, 0)
@ -23,15 +23,15 @@ pub fn (mut r Request) parse_request(s string, max_headers int) int {
r.method = tos(r.method.str, int(method_len)) r.method = tos(r.method.str, int(method_len))
r.path = tos(r.path.str, int(path_len)) r.path = tos(r.path.str, int(path_len))
} }
r.num_headers = num_headers r.num_headers = u64(num_headers)
} }
return pret return pret
} }
[inline] [inline]
pub fn (mut r Request) parse_request_path(s string) int { pub fn (mut r Request) parse_request_path(s string) int {
method_len := u64(0) method_len := size_t(0)
path_len := u64(0) path_len := size_t(0)
pret := C.phr_parse_request_path(s.str, s.len, PPchar(&r.method.str), &method_len, pret := C.phr_parse_request_path(s.str, s.len, PPchar(&r.method.str), &method_len,
PPchar(&r.path.str), &path_len) PPchar(&r.path.str), &path_len)
@ -46,8 +46,8 @@ pub fn (mut r Request) parse_request_path(s string) int {
[inline] [inline]
pub fn (mut r Request) parse_request_path_pipeline(s string) int { pub fn (mut r Request) parse_request_path_pipeline(s string) int {
method_len := u64(0) method_len := size_t(0)
path_len := u64(0) path_len := size_t(0)
pret := C.phr_parse_request_path_pipeline(s.str, s.len, PPchar(&r.method.str), &method_len, pret := C.phr_parse_request_path_pipeline(s.str, s.len, PPchar(&r.method.str), &method_len,
PPchar(&r.path.str), &path_len) PPchar(&r.path.str), &path_len)

View File

@ -11,15 +11,15 @@ struct C.zip_t {
type Zip = C.zip_t type Zip = C.zip_t
fn C.zip_open(byteptr, int, byte) &Zip fn C.zip_open(&char, int, char) &Zip
fn C.zip_close(&Zip) fn C.zip_close(&Zip)
fn C.zip_entry_open(&Zip, byteptr) int fn C.zip_entry_open(&Zip, &byte) int
fn C.zip_entry_close(&Zip) int fn C.zip_entry_close(&Zip) int
fn C.zip_entry_name(&Zip) byteptr fn C.zip_entry_name(&Zip) &byte
fn C.zip_entry_index(&Zip) int fn C.zip_entry_index(&Zip) int
@ -29,17 +29,17 @@ fn C.zip_entry_size(&Zip) u64
fn C.zip_entry_crc32(&Zip) u32 fn C.zip_entry_crc32(&Zip) u32
fn C.zip_entry_write(&Zip, voidptr, int) int fn C.zip_entry_write(&Zip, voidptr, size_t) int
fn C.zip_entry_fwrite(&Zip, byteptr) int fn C.zip_entry_fwrite(&Zip, &char) int
fn C.zip_entry_read(&Zip, byteptr, int) int fn C.zip_entry_read(&Zip, &&byte, &size_t) int
fn C.zip_entry_fread(&Zip, byteptr) int fn C.zip_entry_fread(&Zip, &char) int
fn C.zip_total_entries(&Zip) int fn C.zip_total_entries(&Zip) int
fn C.zip_extract_without_callback(charptr, charptr) int fn C.zip_extract_without_callback(&char, &char) int
// CompressionLevel lists compression levels, see in "thirdparty/zip/miniz.h" // CompressionLevel lists compression levels, see in "thirdparty/zip/miniz.h"
pub enum CompressionLevel { pub enum CompressionLevel {
@ -84,7 +84,7 @@ pub fn open(name string, level CompressionLevel, mode OpenMode) ?&Zip {
if name.len == 0 { if name.len == 0 {
return error('szip: name of file empty') return error('szip: name of file empty')
} }
p_zip := &Zip(C.zip_open(name.str, int(level), mode.to_byte())) p_zip := &Zip(C.zip_open(name.str, int(level), char(mode.to_byte())))
if isnil(p_zip) { if isnil(p_zip) {
return error('szip: cannot open/create/append new zip archive') return error('szip: cannot open/create/append new zip archive')
} }
@ -183,8 +183,8 @@ pub fn (mut zentry Zip) create_entry(name string) ? {
// NOTE: remember to release the memory allocated for an output buffer. // NOTE: remember to release the memory allocated for an output buffer.
// for large entries, please take a look at zip_entry_extract function. // for large entries, please take a look at zip_entry_extract function.
pub fn (mut zentry Zip) read_entry() ?voidptr { pub fn (mut zentry Zip) read_entry() ?voidptr {
mut buf := voidptr(0) mut buf := &byte(0)
mut bsize := i64(0) mut bsize := size_t(0)
res := C.zip_entry_read(zentry, &buf, &bsize) res := C.zip_entry_read(zentry, &buf, &bsize)
if res == -1 { if res == -1 {
return error('szip: cannot read properly data from entry') return error('szip: cannot read properly data from entry')

View File

@ -79,7 +79,7 @@ fn linux_now() Time {
mut ts := C.timespec{} mut ts := C.timespec{}
C.clock_gettime(C.CLOCK_REALTIME, &ts) C.clock_gettime(C.CLOCK_REALTIME, &ts)
loc_tm := C.tm{} loc_tm := C.tm{}
C.localtime_r(&ts.tv_sec, &loc_tm) C.localtime_r(&time.time_t(&ts.tv_sec), &loc_tm)
return convert_ctime(loc_tm, int(ts.tv_nsec / 1000)) return convert_ctime(loc_tm, int(ts.tv_nsec / 1000))
} }

View File

@ -283,7 +283,7 @@ pub fn (typ Type) is_float() bool {
[inline] [inline]
pub fn (typ Type) is_int() bool { pub fn (typ Type) is_int() bool {
return typ.idx() in ast.integer_type_idxs return int(typ) in ast.integer_type_idxs
} }
[inline] [inline]
@ -298,12 +298,12 @@ pub fn (typ Type) is_unsigned() bool {
[inline] [inline]
pub fn (typ Type) is_int_literal() bool { pub fn (typ Type) is_int_literal() bool {
return typ.idx() == ast.int_literal_type_idx return int(typ) == ast.int_literal_type_idx
} }
[inline] [inline]
pub fn (typ Type) is_number() bool { pub fn (typ Type) is_number() bool {
return typ.idx() in ast.number_type_idxs return int(typ) in ast.number_type_idxs
} }
[inline] [inline]

View File

@ -139,6 +139,15 @@ pub fn (mut c Checker) check_basic(got ast.Type, expected ast.Type) bool {
if got == ast.error_type_idx && expected == ast.string_type_idx { if got == ast.error_type_idx && expected == ast.string_type_idx {
return true return true
} }
// allow `return 0` in a function with `?int` return type
expected_nonopt := expected.clear_flag(.optional)
if got == ast.int_literal_type && expected_nonopt.is_int() {
return true
}
// allow `return 0` in a function with `?f32` return type
if got == ast.float_literal_type && expected_nonopt.is_float() {
return true
}
return false return false
} }

View File

@ -1096,6 +1096,10 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) ast.Type {
if left_type == ast.void_type || right_type == ast.void_type { if left_type == ast.void_type || right_type == ast.void_type {
return ast.void_type return ast.void_type
} }
if left_type.nr_muls() > 0 && right_type.is_int() {
// pointer arithmetic is fine, it is checked in other places
return return_type
}
c.error('infix expr: cannot use `$right.name` (right expression) as `$left.name`', c.error('infix expr: cannot use `$right.name` (right expression) as `$left.name`',
left_right_pos) left_right_pos)
} }

View File

@ -3180,7 +3180,7 @@ fn (mut g Gen) typeof_expr(node ast.TypeOf) {
varg_elem_type_sym := g.table.get_type_symbol(g.table.value_type(node.expr_type)) varg_elem_type_sym := g.table.get_type_symbol(g.table.value_type(node.expr_type))
g.write('_SLIT("...${util.strip_main_name(varg_elem_type_sym.name)}")') g.write('_SLIT("...${util.strip_main_name(varg_elem_type_sym.name)}")')
} else { } else {
g.write('_SLIT("${util.strip_main_name(sym.name)}")') g.write('_SLIT("${util.strip_main_name(g.table.type_to_str(node.expr_type))}")')
} }
} }