aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-07-28 15:37:41 +0000
committerGitHub <noreply@github.com>2022-07-28 15:37:41 +0000
commit6d5eb739f069e41a8789c15b199893acf06915a8 (patch)
tree7755f2c23856c20a9ebd60c7a796d4cd3efed242 /gcc/rust
parentadd0846629c918618a0a9196ba62ccb466d05beb (diff)
parentf532ae5aa37c8555dd6a99d8502436509db7930f (diff)
downloadgcc-6d5eb739f069e41a8789c15b199893acf06915a8.zip
gcc-6d5eb739f069e41a8789c15b199893acf06915a8.tar.gz
gcc-6d5eb739f069e41a8789c15b199893acf06915a8.tar.bz2
Merge #1416
1416: unsafe: Add checks for union field accesses r=CohenArthur a=CohenArthur Addresses #1411 Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/checks/errors/rust-unsafe-checker.cc20
1 files changed, 17 insertions, 3 deletions
diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc
index d234179..683a803 100644
--- a/gcc/rust/checks/errors/rust-unsafe-checker.cc
+++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc
@@ -332,10 +332,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