Hi,

On Sun, Aug 01, 2021 at 01:29:16PM +0200, Mark Wielaard wrote:
> And you cannot use unions as function arguments or return values.
> This example (sorry for the funny naming, union isn't a keyword, so
> you can call basically anything a union, including a union, a union
> field, function argument, etc.) doesn't pass the type checker:
> 
> union union { union: u32, funion: f32 }
> 
> fn funion (union: &union) -> union
> {
>   let v = unsafe { union.union };
>   union { union: v }
> }
> 
> pub fn main ()
> {
>   let union = union { union: 1 };
>   let u = unsafe { union.union };
>   let f = unsafe { union.funion };
>   let r = funion (&union);
>   let _r3 = unsafe { r.union } + unsafe { union.union } + u;
>   let _f2 = f + unsafe { r.funion };
> }
> 
> $ gcc/gccrs -Bgcc -g fn_union.rs 
> fn_union.rs:5:20: error: expected algebraic data type got: [& 
> union{union:Uint:u32:(Ref: 20 TyRef: 3[3,]), funion:Float:f32:(Ref: 23 TyRef: 
> 12[12,])}]
>     5 |   let v = unsafe { union.union };
>       |                    ^
> fn_union.rs:5:20: error: failed to type resolve expression
> fn_union.rs:6:3: error: expected an ADT type for constructor
>     6 |   union { union: v }
>       |   ^
> fn_union.rs:3:1: error: expected [union{union:Uint:u32:(Ref: 20 TyRef: 
> 3[3,]), funion:Float:f32:(Ref: 23 TyRef: 12[12,])}] got [<tyty::error>]
>     3 | fn funion (union: &union) -> union
>       | ^                            ~

This example might be slightly hard to read because it uses "union"
also as identifier for things that aren't unions (which is allowed
since union is a weak keyword). So here is a simpler hopefully more
clear example to show the issue:

union U
{
  v1: u64,
  v2: i8
}

fn f (u: &U) -> U
{
  let v = unsafe { u.v2 };
  U { v2: v }
}

pub fn main ()
{
  let u = U { v1: 356 };
  let r = f (u);
  let _v100 = unsafe { r.v1 };
}

$ gcc/gccrs -Bgcc -g union_call.rs 
union_call.rs:9:20: error: expected algebraic data type got: [& 
U{v1:Uint:u64:(Ref: 20 TyRef: 4[4,]), v2:Int:i8:(Ref: 23 TyRef: 6[6,])}]
    9 |   let v = unsafe { u.v2 };
      |                    ^
union_call.rs:9:20: error: failed to type resolve expression
union_call.rs:9:3: error: Failed to resolve IdentifierExpr type: ( v ([C: 0 
Nid: 42 Hid: 40]))
    9 |   let v = unsafe { u.v2 };
      |   ^
union_call.rs:10:11: error: failed to type resolve expression
   10 |   U { v2: v }
      |           ^
union_call.rs:10:11: error: expected [i8] got [<tyty::error>]

I am a little lost where the typechecking goes wrong.
Could someone give me a hint?

Thanks,

Mark

-- 
Gcc-rust mailing list
Gcc-rust@gcc.gnu.org
https://gcc.gnu.org/mailman/listinfo/gcc-rust

Reply via email to