public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] unsafe: Add checks for union field accesses Date: Thu, 28 Jul 2022 18:02:57 +0000 (GMT) [thread overview] Message-ID: <20220728180257.159933858D33@sourceware.org> (raw) https://gcc.gnu.org/g:f532ae5aa37c8555dd6a99d8502436509db7930f commit f532ae5aa37c8555dd6a99d8502436509db7930f Author: Arthur Cohen <arthur.cohen@embecosm.com> Date: Tue Jul 26 15:44:18 2022 +0200 unsafe: Add checks for union field accesses Co-authored-by: philberty <philip.herron@embecosm.com> Diff: --- gcc/rust/checks/errors/rust-unsafe-checker.cc | 20 +++++++++++++++++--- gcc/testsuite/rust/compile/unsafe9.rs | 10 ++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index def3cc11b7a..99f59ac1aaf 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -262,10 +262,24 @@ UnsafeChecker::visit (MethodCallExpr &expr) void UnsafeChecker::visit (FieldAccessExpr &expr) { - // FIXME: If the receiver is an union, we need to be in an unsafe context to - // access it. Make sure to check. - expr.get_receiver_expr ()->accept_vis (*this); + + if (is_unsafe_context ()) + return; + + TyTy::BaseType *receiver_ty; + auto ok = context.lookup_type ( + expr.get_receiver_expr ()->get_mappings ().get_hirid (), &receiver_ty); + rust_assert (ok); + + if (receiver_ty->get_kind () == TyTy::TypeKind::ADT) + { + auto maybe_union = static_cast<TyTy::ADTType *> (receiver_ty); + if (maybe_union->is_union ()) + rust_error_at ( + expr.get_locus (), + "access to union field requires unsafe function or block"); + } } void diff --git a/gcc/testsuite/rust/compile/unsafe9.rs b/gcc/testsuite/rust/compile/unsafe9.rs new file mode 100644 index 00000000000..fb46c8ffde3 --- /dev/null +++ b/gcc/testsuite/rust/compile/unsafe9.rs @@ -0,0 +1,10 @@ +union U { + a: i32, + b: f32, + c: u8, +} + +fn main() { + let u = U { a: 14 }; + let _ = u.a; // { dg-error "access to union" } +}
reply other threads:[~2022-07-28 18:02 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20220728180257.159933858D33@sourceware.org \ --to=tschwinge@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /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: linkBe 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).