aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Jasper <mjjasper1@gmail.com>2023-06-24 12:21:44 +0100
committerPhilip Herron <philip.herron@embecosm.com>2023-06-25 20:40:29 +0000
commit210557d8a4129e2b240cbbcc84c0edb18b81a397 (patch)
tree5d46aa121a8610acec79f3cdb13ffa18c41bcb57
parent3ccf008b148455c4800f2a7c70be03e1dd708c59 (diff)
downloadgcc-210557d8a4129e2b240cbbcc84c0edb18b81a397.zip
gcc-210557d8a4129e2b240cbbcc84c0edb18b81a397.tar.gz
gcc-210557d8a4129e2b240cbbcc84c0edb18b81a397.tar.bz2
gccrs: Expand expressions/types correctly in more contexts.
Expressions are expanded in constant/static items, match expressions and in parentheses now. Types are expanded in enum variants. gcc/rust/ChangeLog: * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Expand expressions in more contexts. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Visit enum variants. gcc/testsuite/ChangeLog: * rust/compile/macro54.rs: New test. * rust/compile/macro55.rs: New test. Signed-off-by: Matthew Jasper <mjjasper1@gmail.com>
-rw-r--r--gcc/rust/expand/rust-expand-visitor.cc16
-rw-r--r--gcc/rust/resolve/rust-early-name-resolver.cc30
-rw-r--r--gcc/testsuite/rust/compile/macro54.rs38
-rw-r--r--gcc/testsuite/rust/compile/macro55.rs10
4 files changed, 78 insertions, 16 deletions
diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc
index 055f723..cbd0e50 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -724,7 +724,7 @@ ExpandVisitor::visit (AST::CompoundAssignmentExpr &expr)
void
ExpandVisitor::visit (AST::GroupedExpr &expr)
{
- visit (expr.get_expr_in_parens ());
+ maybe_expand_expr (expr.get_expr_in_parens ());
}
void
@@ -995,9 +995,9 @@ ExpandVisitor::visit (AST::MatchExpr &expr)
visit (pattern);
if (arm.has_match_arm_guard ())
- visit (arm.get_guard_expr ());
+ maybe_expand_expr (arm.get_guard_expr ());
- visit (match_case.get_expr ());
+ maybe_expand_expr (match_case.get_expr ());
}
}
@@ -1153,7 +1153,7 @@ ExpandVisitor::visit (AST::EnumItemStruct &item)
void
ExpandVisitor::visit (AST::EnumItemDiscriminant &item)
{
- visit (item.get_expr ());
+ maybe_expand_expr (item.get_expr ());
}
void
@@ -1163,7 +1163,7 @@ ExpandVisitor::visit (AST::Enum &enum_item)
visit (generic);
for (auto &variant : enum_item.get_variants ())
- visit (variant);
+ variant->accept_vis (*this);
}
void
@@ -1180,7 +1180,7 @@ ExpandVisitor::visit (AST::ConstantItem &const_item)
{
maybe_expand_type (const_item.get_type ());
- visit (const_item.get_expr ());
+ maybe_expand_expr (const_item.get_expr ());
}
void
@@ -1188,7 +1188,7 @@ ExpandVisitor::visit (AST::StaticItem &static_item)
{
maybe_expand_type (static_item.get_type ());
- visit (static_item.get_expr ());
+ maybe_expand_expr (static_item.get_expr ());
}
void
@@ -1215,7 +1215,7 @@ ExpandVisitor::visit (AST::TraitItemConst &const_item)
maybe_expand_type (const_item.get_type ());
if (const_item.has_expr ())
- visit (const_item.get_expr ());
+ maybe_expand_expr (const_item.get_expr ());
}
void
diff --git a/gcc/rust/resolve/rust-early-name-resolver.cc b/gcc/rust/resolve/rust-early-name-resolver.cc
index f6ec8c7..3a2d6a5 100644
--- a/gcc/rust/resolve/rust-early-name-resolver.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver.cc
@@ -714,20 +714,34 @@ EarlyNameResolver::visit (AST::EnumItem &)
{}
void
-EarlyNameResolver::visit (AST::EnumItemTuple &)
-{}
+EarlyNameResolver::visit (AST::EnumItemTuple &item)
+{
+ for (auto &field : item.get_tuple_fields ())
+ field.get_field_type ()->accept_vis (*this);
+}
void
-EarlyNameResolver::visit (AST::EnumItemStruct &)
-{}
+EarlyNameResolver::visit (AST::EnumItemStruct &item)
+{
+ for (auto &field : item.get_struct_fields ())
+ field.get_field_type ()->accept_vis (*this);
+}
void
-EarlyNameResolver::visit (AST::EnumItemDiscriminant &)
-{}
+EarlyNameResolver::visit (AST::EnumItemDiscriminant &item)
+{
+ item.get_expr ()->accept_vis (*this);
+}
void
-EarlyNameResolver::visit (AST::Enum &)
-{}
+EarlyNameResolver::visit (AST::Enum &enum_item)
+{
+ for (auto &generic : enum_item.get_generic_params ())
+ generic->accept_vis (*this);
+
+ for (auto &variant : enum_item.get_variants ())
+ variant->accept_vis (*this);
+}
void
EarlyNameResolver::visit (AST::Union &)
diff --git a/gcc/testsuite/rust/compile/macro54.rs b/gcc/testsuite/rust/compile/macro54.rs
new file mode 100644
index 0000000..afb20263
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macro54.rs
@@ -0,0 +1,38 @@
+macro_rules! foo {
+ () => {"foo"};
+ (number) => { 12 };
+ (false) => { false };
+}
+
+pub const A: &'static str = foo!();
+pub static B: &'static str = foo!();
+
+pub trait Number {
+ const VALUE: u32;
+}
+
+impl Number for u32 {
+ const VALUE: u32 = foo!(number);
+}
+
+impl u32 {
+ pub const TWELVE: u32 = foo!(number);
+}
+
+pub enum E {
+ Variant = foo!(number),
+}
+
+pub fn f(c: bool) -> &'static str {
+ match c {
+ false => foo!(),
+ true if foo!(false) => "abc",
+ _ => "xyz"
+ }
+}
+
+
+fn main() {
+ let _ = A;
+ let _ = u32::VALUE - u32::TWELVE;
+}
diff --git a/gcc/testsuite/rust/compile/macro55.rs b/gcc/testsuite/rust/compile/macro55.rs
new file mode 100644
index 0000000..808718c
--- /dev/null
+++ b/gcc/testsuite/rust/compile/macro55.rs
@@ -0,0 +1,10 @@
+macro_rules! id {
+ ($i:ident) => { $i }
+}
+
+pub enum F {
+ Tuple(id!(u32)),
+ Struct { field: id!(u64) },
+}
+
+fn main() {}