checker: warn when accessing `union` fields outside `unsafe` (#7869)
							parent
							
								
									7c9fb73b3f
								
							
						
					
					
						commit
						040b923665
					
				| 
						 | 
				
			
			@ -522,6 +522,7 @@ pub fn atof64(s string) f64 {
 | 
			
		|||
 | 
			
		||||
	res_parsing,pn = parser(s + ' ') // TODO: need an extra char for now
 | 
			
		||||
	// println(pn)
 | 
			
		||||
	unsafe {
 | 
			
		||||
	match res_parsing {
 | 
			
		||||
		parser_ok {
 | 
			
		||||
			res.u = converter(mut pn)
 | 
			
		||||
| 
						 | 
				
			
			@ -539,6 +540,8 @@ pub fn atof64(s string) f64 {
 | 
			
		|||
			res.u = double_minus_infinity
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
		}}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return res.f
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -37,6 +37,7 @@ pub fn atof_quick(s string) f64 {
 | 
			
		|||
			i++
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	unsafe {
 | 
			
		||||
	// infinite
 | 
			
		||||
	if s[i] == `i` && i + 2 < s.len && s[i + 1] == `n` && s[i + 2] == `f` {
 | 
			
		||||
		if sign > 0.0 {
 | 
			
		||||
| 
						 | 
				
			
			@ -134,6 +135,7 @@ pub fn atof_quick(s string) f64 {
 | 
			
		|||
	f.f = f.f * sign
 | 
			
		||||
	return f.f
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
// positive exp of 10 binary form
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
pub fn f32_to_str(f f32, n_digit int) string {
 | 
			
		||||
	mut u1 := Uf32{}
 | 
			
		||||
	u1.f = f
 | 
			
		||||
	u := u1.u
 | 
			
		||||
	unsafe { u1.f = f }
 | 
			
		||||
	u := unsafe {u1.u}
 | 
			
		||||
 | 
			
		||||
	neg   := (u>>(mantbits32+expbits32)) != 0
 | 
			
		||||
	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
 | 
			
		||||
pub fn f32_to_str_pad(f f32, n_digit int) string {
 | 
			
		||||
	mut u1 := Uf32{}
 | 
			
		||||
	u1.f = f
 | 
			
		||||
	u := u1.u
 | 
			
		||||
	unsafe { u1.f = f }
 | 
			
		||||
	u := unsafe {u1.u}
 | 
			
		||||
 | 
			
		||||
	neg   := (u>>(mantbits32+expbits32)) != 0
 | 
			
		||||
	mant  := u & ((u32(1)<<mantbits32) - u32(1))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
pub fn f64_to_str(f f64, n_digit int) string {
 | 
			
		||||
	mut u1 := Uf64{}
 | 
			
		||||
	u1.f = f
 | 
			
		||||
	u := u1.u
 | 
			
		||||
	unsafe { u1.f = f }
 | 
			
		||||
	u := unsafe {u1.u}
 | 
			
		||||
 | 
			
		||||
	neg   := (u>>(mantbits64+expbits64)) != 0
 | 
			
		||||
	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
 | 
			
		||||
pub fn f64_to_str_pad(f f64, n_digit int) string {
 | 
			
		||||
	mut u1 := Uf64{}
 | 
			
		||||
	u1.f = f
 | 
			
		||||
	u := u1.u
 | 
			
		||||
	unsafe { u1.f = f }
 | 
			
		||||
	u := unsafe {u1.u}
 | 
			
		||||
 | 
			
		||||
	neg   := (u>>(mantbits64+expbits64)) != 0
 | 
			
		||||
	mant  := u & ((u64(1)<<mantbits64) - u64(1))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1050,6 +1050,9 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
 | 
			
		|||
						// No automatic lock for struct access
 | 
			
		||||
						explicit_lock_needed = true
 | 
			
		||||
					}
 | 
			
		||||
					if struct_info.is_union && !c.inside_unsafe {
 | 
			
		||||
						c.warn('accessing union fields requires `unsafe`', expr.pos)
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				.array, .string {
 | 
			
		||||
					// This should only happen in `builtin`
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue