diff options
author | Antonio Gomes <antoniospg100@gmail.com> | 2024-07-18 22:50:54 -0300 |
---|---|---|
committer | P-E-P <32375388+P-E-P@users.noreply.github.com> | 2024-07-25 08:52:31 +0000 |
commit | 1eb42b0e3444423f458d684d14a0698053ecea53 (patch) | |
tree | e8c927fa5f34870d6195b8b1f9bb324effae4811 | |
parent | 8c357fd0072a29b24e10a8a1fe287f86f8c9b553 (diff) | |
download | gcc-1eb42b0e3444423f458d684d14a0698053ecea53.zip gcc-1eb42b0e3444423f458d684d14a0698053ecea53.tar.gz gcc-1eb42b0e3444423f458d684d14a0698053ecea53.tar.bz2 |
Properly striping struct fields when using attrs
gcc/rust/ChangeLog:
* expand/rust-cfg-strip.cc:
Strip struct expr fields and strip fields in struct definition
* expand/rust-cfg-strip.h:
Signatures for new function maybe_strip_struct_expr_fields
gcc/testsuite/ChangeLog:
* rust/compile/macro-issue2983_2984.rs:
Add test to check for correct stripped fields
Signed-off-by: Antonio Gomes <antoniospg100@gmail.com>
-rw-r--r-- | gcc/rust/expand/rust-cfg-strip.cc | 26 | ||||
-rw-r--r-- | gcc/rust/expand/rust-cfg-strip.h | 2 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/macro-issue2983_2984.rs | 27 |
3 files changed, 55 insertions, 0 deletions
diff --git a/gcc/rust/expand/rust-cfg-strip.cc b/gcc/rust/expand/rust-cfg-strip.cc index 19f377e..33fb64f 100644 --- a/gcc/rust/expand/rust-cfg-strip.cc +++ b/gcc/rust/expand/rust-cfg-strip.cc @@ -195,6 +195,26 @@ CfgStrip::maybe_strip_struct_fields (std::vector<AST::StructField> &fields) } void +CfgStrip::maybe_strip_struct_expr_fields ( + std::vector<std::unique_ptr<AST::StructExprField>> &fields) +{ + for (auto it = fields.begin (); it != fields.end ();) + { + auto &field = *it; + + auto &field_attrs = field->get_outer_attrs (); + expand_cfg_attrs (field_attrs); + if (fails_cfg_with_expand (field_attrs)) + { + it = fields.erase (it); + continue; + } + + ++it; + } +} + +void CfgStrip::maybe_strip_tuple_fields (std::vector<AST::TupleField> &fields) { for (auto it = fields.begin (); it != fields.end ();) @@ -962,6 +982,8 @@ CfgStrip::visit (AST::StructExprStructFields &expr) "cannot strip expression in this position - outer " "attributes not allowed"); } + + maybe_strip_struct_expr_fields (expr.get_fields ()); } void @@ -1852,6 +1874,10 @@ CfgStrip::visit (AST::StructStruct &struct_item) } AST::DefaultASTVisitor::visit (struct_item); + + /* strip struct fields if required - this is presumably + * allowed by spec */ + maybe_strip_struct_fields (struct_item.get_fields ()); } void CfgStrip::visit (AST::TupleStruct &tuple_struct) diff --git a/gcc/rust/expand/rust-cfg-strip.h b/gcc/rust/expand/rust-cfg-strip.h index 048cebd..9f1f211 100644 --- a/gcc/rust/expand/rust-cfg-strip.h +++ b/gcc/rust/expand/rust-cfg-strip.h @@ -36,6 +36,8 @@ public: void go (AST::Crate &crate); void maybe_strip_struct_fields (std::vector<AST::StructField> &fields); + void maybe_strip_struct_expr_fields ( + std::vector<std::unique_ptr<AST::StructExprField>> &fields); void maybe_strip_tuple_fields (std::vector<AST::TupleField> &fields); void maybe_strip_function_params ( std::vector<std::unique_ptr<AST::Param>> ¶ms); diff --git a/gcc/testsuite/rust/compile/macro-issue2983_2984.rs b/gcc/testsuite/rust/compile/macro-issue2983_2984.rs new file mode 100644 index 0000000..637d572 --- /dev/null +++ b/gcc/testsuite/rust/compile/macro-issue2983_2984.rs @@ -0,0 +1,27 @@ +pub struct ReadDir { + pub inner: i32, + #[cfg(not(A))] + pub end_of_stream: bool, + #[cfg(A)] + pub end_of_stream_but_different: bool, +} + +fn main() { + // Success + let _ = ReadDir { + inner: 14, + #[cfg(not(A))] + end_of_stream: false, + #[cfg(A)] + end_of_stream_but_different: false, + }; + + // Error + let _ = ReadDir { + inner: 14, + end_of_stream: false, + end_of_stream_but_different: false, // { dg-error "failed to resolve type for field" } + // { dg-error "unknown field" "" { target *-*-* } .-1 } + // { dg-prune-output "compilation terminated" } + }; +} |