aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorZhi Heng <yapzhhg@gmail.com>2025-04-03 20:23:46 +0800
committerPhilip Herron <philip.herron@embecosm.com>2025-04-16 11:03:05 +0000
commit2c109a2e8a97980b293edb7dc3609556473c7f3d (patch)
tree62d1c09ecbc9b8c34a9375f56bf7aa155896e33f /gcc
parent5ac41dce35b947f5d1f904b39958ebf6f1b538cf (diff)
downloadgcc-2c109a2e8a97980b293edb7dc3609556473c7f3d.zip
gcc-2c109a2e8a97980b293edb7dc3609556473c7f3d.tar.gz
gcc-2c109a2e8a97980b293edb7dc3609556473c7f3d.tar.bz2
gccrs: Implement typecheck for zero-variant enums
gcc/rust/ChangeLog: * typecheck/rust-tyty.h: Add new `ReprKind` enum to `ReprOptions`. * typecheck/rust-hir-type-check-base.cc: Handle setting of `repr_kind`. * typecheck/rust-hir-type-check-item.cc: New check for zero-variant enums. Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-base.cc34
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-item.cc12
-rw-r--r--gcc/rust/typecheck/rust-tyty.h17
-rw-r--r--gcc/testsuite/rust/compile/issue-3530-1.rs2
-rw-r--r--gcc/testsuite/rust/compile/issue-3530-2.rs2
5 files changed, 61 insertions, 6 deletions
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index f231021..f066ddc 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -353,13 +353,27 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus)
// manually parsing the string "packed(2)" here.
size_t oparen = inline_option.find ('(', 0);
- bool is_pack = false, is_align = false;
+ bool is_pack = false, is_align = false, is_c = false,
+ is_integer = false;
unsigned char value = 1;
if (oparen == std::string::npos)
{
is_pack = inline_option.compare ("packed") == 0;
is_align = inline_option.compare ("align") == 0;
+ is_c = inline_option.compare ("C") == 0;
+ is_integer = (inline_option.compare ("isize") == 0
+ || inline_option.compare ("i8") == 0
+ || inline_option.compare ("i16") == 0
+ || inline_option.compare ("i32") == 0
+ || inline_option.compare ("i64") == 0
+ || inline_option.compare ("i128") == 0
+ || inline_option.compare ("usize") == 0
+ || inline_option.compare ("u8") == 0
+ || inline_option.compare ("u16") == 0
+ || inline_option.compare ("u32") == 0
+ || inline_option.compare ("u64") == 0
+ || inline_option.compare ("u128") == 0);
}
else
@@ -379,9 +393,23 @@ TypeCheckBase::parse_repr_options (const AST::AttrVec &attrs, location_t locus)
}
if (is_pack)
- repr.pack = value;
+ {
+ repr.repr_kind = TyTy::ADTType::ReprKind::PACKED;
+ repr.pack = value;
+ }
else if (is_align)
- repr.align = value;
+ {
+ repr.repr_kind = TyTy::ADTType::ReprKind::ALIGN;
+ repr.align = value;
+ }
+ else if (is_c)
+ {
+ repr.repr_kind = TyTy::ADTType::ReprKind::C;
+ }
+ else if (is_integer)
+ {
+ repr.repr_kind = TyTy::ADTType::ReprKind::INT;
+ }
delete meta_items;
diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc b/gcc/rust/typecheck/rust-hir-type-check-item.cc
index f13e3a9..7491cb4 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc
@@ -355,6 +355,18 @@ TypeCheckItem::visit (HIR::Enum &enum_decl)
variants.push_back (field_type);
}
+ // Check for zero-variant enum compatibility before processing repr attribute
+ if (enum_decl.is_zero_variant ())
+ {
+ if (repr.repr_kind == TyTy::ADTType::ReprKind::INT
+ || repr.repr_kind == TyTy::ADTType::ReprKind::C)
+ {
+ rust_error_at (enum_decl.get_locus (),
+ "unsupported representation for zero-variant enum");
+ return;
+ }
+ }
+
// get the path
tl::optional<CanonicalPath> canonical_path;
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index dc30d7d..cd7bf24 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -711,12 +711,23 @@ public:
ENUM
};
+ enum ReprKind
+ {
+ RUST,
+ C,
+ INT,
+ ALIGN,
+ PACKED,
+ // TRANSPARENT,
+ // PACKED,
+ // SIMD,
+ // ...
+ };
+
// Representation options, specified via attributes e.g. #[repr(packed)]
struct ReprOptions
{
- // bool is_c;
- // bool is_transparent;
- //...
+ ReprKind repr_kind = ReprKind::RUST;
// For align and pack: 0 = unspecified. Nonzero = byte alignment.
// It is an error for both to be nonzero, this should be caught when
diff --git a/gcc/testsuite/rust/compile/issue-3530-1.rs b/gcc/testsuite/rust/compile/issue-3530-1.rs
new file mode 100644
index 0000000..b38b5cd
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3530-1.rs
@@ -0,0 +1,2 @@
+#[repr(i32)]
+enum NightsWatch {} // { dg-error "unsupported representation for zero-variant enum" }
diff --git a/gcc/testsuite/rust/compile/issue-3530-2.rs b/gcc/testsuite/rust/compile/issue-3530-2.rs
new file mode 100644
index 0000000..7432730
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3530-2.rs
@@ -0,0 +1,2 @@
+#[repr(C)]
+enum NightsWatch {} // { dg-error "unsupported representation for zero-variant enum" }