From: Mark Wielaard <mark@klomp.org>
To: Philip Herron <philip.herron@embecosm.com>
Cc: gcc-rust@gcc.gnu.org
Subject: Re: [PATCH 2/2] WIP union hir-lowering and type support
Date: Wed, 4 Aug 2021 23:04:41 +0200 [thread overview]
Message-ID: <YQsA6ddOZtti7ktX@wildebeest.org> (raw)
In-Reply-To: <YQaFjHGnPoojjecb@wildebeest.org>
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
prev parent reply other threads:[~2021-08-04 21:04 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-22 23:19 union support Mark Wielaard
2021-07-22 23:19 ` [PATCH 1/2] Better union support in the parser Mark Wielaard
2021-07-23 11:19 ` Philip Herron
2021-07-22 23:19 ` [PATCH 2/2] WIP union hir-lowering and type support Mark Wielaard
2021-07-23 11:19 ` Philip Herron
2021-08-01 11:29 ` Mark Wielaard
2021-08-01 22:37 ` Mark Wielaard
2021-08-02 12:33 ` Thomas Schwinge
2021-08-04 21:04 ` Mark Wielaard [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YQsA6ddOZtti7ktX@wildebeest.org \
--to=mark@klomp.org \
--cc=gcc-rust@gcc.gnu.org \
--cc=philip.herron@embecosm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).