do not allow casting a type to itself

pull/3006/head
Alexander Medvednikov 2019-12-07 15:31:56 +03:00
parent a854d396ff
commit d7ccbba2c9
5 changed files with 17 additions and 12 deletions

View File

@ -41,7 +41,7 @@ jobs:
run: |
brew services start postgresql
sleep 3
psql -c 'select rolname from pg_roles'
psql -c -d postgres 'select rolname from pg_roles'
psql -U postgres -c 'create database customerdb;'
psql -d customerdb -f examples/database/pg/mydb.sql
- name: Test v->c

View File

@ -53,7 +53,8 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
nr_ptrs := C.backtrace(*voidptr(buffer), 100)
nr_actual_frames := nr_ptrs-skipframes
mut sframes := []string
csymbols := *byteptr(C.backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames))
csymbols := C.backtrace_symbols(*voidptr(&buffer[skipframes]),
nr_actual_frames)
for i in 0..nr_actual_frames { sframes << tos2(csymbols[i]) }
for sframe in sframes {
executable := sframe.all_before('(')
@ -72,7 +73,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
output += tos(buf, vstrlen(buf))
}
output = output.trim_space()+':'
if 0 != int(C.pclose(f)) {
if 0 != C.pclose(f) {
println(sframe) continue
}
if output in ['??:0:','??:?:'] { output = '' }

View File

@ -155,7 +155,7 @@ fn utf8_len(c byte) int {
// Reads an utf8 character from standard input
pub fn utf8_getchar() int {
c := int(C.getchar())
c := C.getchar()
len := utf8_len(~c)
if c < 0 {
return 0
@ -164,12 +164,12 @@ pub fn utf8_getchar() int {
} else if len == 1 {
return -1
} else {
mut uc := int(c & ((1 << (7 - len)) - 1))
mut uc := c & ((1 << (7 - len)) - 1)
for i := 0; i + 1 < len; i++ {
c2 := int(C.getchar())
c2 := C.getchar()
if c2 != -1 && (c2 >> 6) == 2 {
uc <<= 6
uc |= int((c2 & 63))
uc |= (c2 & 63)
} else if c2 == -1 {
return 0
} else {

View File

@ -527,6 +527,10 @@ fn (p mut Parser) cast(typ string) {
p.check(.lpar)
p.expected_type = typ
expr_typ := p.bool_expression()
// Do not allow `int(my_int)`
if expr_typ == typ {
p.warn('casting `$typ` to `$expr_typ` is not needed')
}
// `face := FT_Face(cobj)` => `FT_Face face = *((FT_Face*)cobj);`
casting_voidptr_to_value := expr_typ == 'void*' && typ != 'int' &&
typ != 'byteptr' && !typ.ends_with('*')

View File

@ -61,7 +61,7 @@ pub fn common_parse_uint(s string, _base int, _bit_size int, error_on_non_digit
}
if bit_size == 0 {
bit_size = int(int_size)
bit_size = int_size
} else if bit_size < 0 || bit_size > 64 {
// return error('parse_uint: bitsize error $s - $bit_size')
return u64(0)
@ -69,11 +69,11 @@ pub fn common_parse_uint(s string, _base int, _bit_size int, error_on_non_digit
// Cutoff is the smallest number such that cutoff*base > maxUint64.
// Use compile-time constants for common cases.
cutoff := u64(max_u64/u64(base)) + u64(1)
cutoff := max_u64/u64(base) + u64(1)
max_val := if bit_size == 64 {
max_u64
} else {
u64(u64(1)<<u64(bit_size))-u64(1)
(u64(1)<<u64(bit_size))-u64(1)
}
mut underscores := false
@ -160,11 +160,11 @@ pub fn common_parse_int(_s string, base int, _bit_size int, error_on_non_digit b
}
if bit_size == 0 {
bit_size = int(int_size)
bit_size = int_size
}
// TODO: check should u64(bit_size-1) be size of int (32)?
cutoff := u64(u64(1) << u64(bit_size-1))
cutoff := u64(1) << u64(bit_size-1)
if !neg && un >= cutoff {
// return error('parse_int: range error $s0')
return i64(cutoff - u64(1))