checker: warn when accessing `union` fields outside `unsafe` (#7869)

pull/7871/head
Nick Treleaven 2021-01-04 19:44:37 +00:00 committed by GitHub
parent 7c9fb73b3f
commit 040b923665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 9 deletions

View File

@ -522,6 +522,7 @@ pub fn atof64(s string) f64 {
res_parsing,pn = parser(s + ' ') // TODO: need an extra char for now res_parsing,pn = parser(s + ' ') // TODO: need an extra char for now
// println(pn) // println(pn)
unsafe {
match res_parsing { match res_parsing {
parser_ok { parser_ok {
res.u = converter(mut pn) res.u = converter(mut pn)
@ -539,6 +540,8 @@ pub fn atof64(s string) f64 {
res.u = double_minus_infinity res.u = double_minus_infinity
} }
else { else {
}} }
}
return res.f return res.f
}
} }

View File

@ -37,6 +37,7 @@ pub fn atof_quick(s string) f64 {
i++ i++
} }
} }
unsafe {
// infinite // infinite
if s[i] == `i` && i + 2 < s.len && s[i + 1] == `n` && s[i + 2] == `f` { if s[i] == `i` && i + 2 < s.len && s[i + 1] == `n` && s[i + 2] == `f` {
if sign > 0.0 { if sign > 0.0 {
@ -133,6 +134,7 @@ pub fn atof_quick(s string) f64 {
} }
f.f = f.f * sign f.f = f.f * sign
return f.f return f.f
}
} }
const ( const (

View File

@ -321,8 +321,8 @@ pub fn f32_to_decimal(mant u32, exp u32) Dec32 {
// f32_to_str return a string in scientific notation with max n_digit after the dot // f32_to_str return a string in scientific notation with max n_digit after the dot
pub fn f32_to_str(f f32, n_digit int) string { pub fn f32_to_str(f f32, n_digit int) string {
mut u1 := Uf32{} mut u1 := Uf32{}
u1.f = f unsafe { u1.f = f }
u := u1.u u := unsafe {u1.u}
neg := (u>>(mantbits32+expbits32)) != 0 neg := (u>>(mantbits32+expbits32)) != 0
mant := u & ((u32(1)<<mantbits32) - u32(1)) mant := u & ((u32(1)<<mantbits32) - u32(1))
@ -348,8 +348,8 @@ pub fn f32_to_str(f f32, n_digit int) string {
// f32_to_str return a string in scientific notation with max n_digit after the dot // f32_to_str return a string in scientific notation with max n_digit after the dot
pub fn f32_to_str_pad(f f32, n_digit int) string { pub fn f32_to_str_pad(f f32, n_digit int) string {
mut u1 := Uf32{} mut u1 := Uf32{}
u1.f = f unsafe { u1.f = f }
u := u1.u u := unsafe {u1.u}
neg := (u>>(mantbits32+expbits32)) != 0 neg := (u>>(mantbits32+expbits32)) != 0
mant := u & ((u32(1)<<mantbits32) - u32(1)) mant := u & ((u32(1)<<mantbits32) - u32(1))

View File

@ -370,8 +370,8 @@ fn f64_to_decimal(mant u64, exp u64) Dec64 {
// f64_to_str return a string in scientific notation with max n_digit after the dot // f64_to_str return a string in scientific notation with max n_digit after the dot
pub fn f64_to_str(f f64, n_digit int) string { pub fn f64_to_str(f f64, n_digit int) string {
mut u1 := Uf64{} mut u1 := Uf64{}
u1.f = f unsafe { u1.f = f }
u := u1.u u := unsafe {u1.u}
neg := (u>>(mantbits64+expbits64)) != 0 neg := (u>>(mantbits64+expbits64)) != 0
mant := u & ((u64(1)<<mantbits64) - u64(1)) mant := u & ((u64(1)<<mantbits64) - u64(1))
@ -395,8 +395,8 @@ pub fn f64_to_str(f f64, n_digit int) string {
// f64_to_str return a string in scientific notation with max n_digit after the dot // f64_to_str return a string in scientific notation with max n_digit after the dot
pub fn f64_to_str_pad(f f64, n_digit int) string { pub fn f64_to_str_pad(f f64, n_digit int) string {
mut u1 := Uf64{} mut u1 := Uf64{}
u1.f = f unsafe { u1.f = f }
u := u1.u u := unsafe {u1.u}
neg := (u>>(mantbits64+expbits64)) != 0 neg := (u>>(mantbits64+expbits64)) != 0
mant := u & ((u64(1)<<mantbits64) - u64(1)) mant := u & ((u64(1)<<mantbits64) - u64(1))

View File

@ -1050,6 +1050,9 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
// No automatic lock for struct access // No automatic lock for struct access
explicit_lock_needed = true explicit_lock_needed = true
} }
if struct_info.is_union && !c.inside_unsafe {
c.warn('accessing union fields requires `unsafe`', expr.pos)
}
} }
.array, .string { .array, .string {
// This should only happen in `builtin` // This should only happen in `builtin`