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 1D2FE388B82F for ; Thu, 22 Jul 2021 23:19:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1D2FE388B82F 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 (deer0x18.wildebeest.org [172.31.17.154]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id C27B6300066F for ; Fri, 23 Jul 2021 01:19:25 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id 1FE8C2E80C66; Fri, 23 Jul 2021 01:19:25 +0200 (CEST) From: Mark Wielaard To: gcc-rust@gcc.gnu.org Subject: union support Date: Fri, 23 Jul 2021 01:19:00 +0200 Message-Id: <20210722231902.7401-1-mark@klomp.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: Thu, 22 Jul 2021 23:19:34 -0000 Hi, I have been playing with union support which was mostly missing. union is slightly odd because it is a weak keyword, so it can be used as a "normal" identifier and is only special when defining a union type. Which means you can have programs like: union union { union: u32, funion: f32 } pub fn main () { let union = union { union: 1 }; let _u = unsafe { union.union }; let _f = unsafe { union.funion; }; } Here are two patches to support the above (partially). Also on https://code.wildebeest.org/git/user/mjw/gccrs/log/?h=union The first fixes up the parser and should be good to go. [PATCH 1/2] Better union support in the parser The second provides lowering to HIR and type checking support. This is mostly cargo culting existing code for structs and tuple struct. It seems to work, but I cannot say I fully understand what I am doing. [PATCH 2/2] WIP union hir-lowering and type support Currently this will eventually crash when trying to generate code in Gcc_backend::constructor_expression because we try to emit code for a struct with multiple fields, but the union doesn't have real fields, just multiple variants. Hopefully this is somewhat useful. Feedback on how to proceed and/or how to get a better design (it seems a good idea to treat a union as much as possible as a struct/tuple variant, but maybe there is a better way) is very welcome. Thanks, Mark