From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id A0132398383F for ; Wed, 4 Aug 2021 21:04:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A0132398383F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x02.wildebeest.org [172.31.17.132]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 693FE30291A9; Wed, 4 Aug 2021 23:04:42 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id EDDE42E808D7; Wed, 4 Aug 2021 23:04:41 +0200 (CEST) Date: Wed, 4 Aug 2021 23:04:41 +0200 From: Mark Wielaard To: Philip Herron Cc: gcc-rust@gcc.gnu.org Subject: Re: [PATCH 2/2] WIP union hir-lowering and type support Message-ID: References: <20210722231902.7401-1-mark@klomp.org> <20210722231902.7401-3-mark@klomp.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-rust@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: gcc-rust mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Aug 2021 21:04:47 -0000 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 [] > 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 [] I am a little lost where the typechecking goes wrong. Could someone give me a hint? Thanks, Mark