From 1891acf2bdd0cdce74e53c7c90ddedae47742624 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Wed, 21 Jun 2023 15:56:26 +0200 Subject: [PATCH] gccrs: expand: Change names and document behavior Change some argument name, as well as some documentation. gcc/rust/ChangeLog: * ast/rust-ast.cc (BlockExpr::normalize_tail_expr): Refactor code and warn about dangling reference. * expand/rust-expand-visitor.cc (expand_stmt_attribute): Document function and change argument names to make those clearer. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/ast/rust-ast.cc | 4 +++- gcc/rust/expand/rust-expand-visitor.cc | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 2eac09ba5d37..14ad3a05620e 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -4228,8 +4228,10 @@ BlockExpr::normalize_tail_expr () if (!expr) { // HACK: try to turn the last statement into a tail expression - if (statements.size () && statements.back ()->is_expr ()) + if (!statements.empty () && statements.back ()->is_expr ()) { + // Watch out: This reference become invalid when the vector is + // modified. auto &stmt = static_cast (*statements.back ()); if (!stmt.is_semicolon_followed ()) diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc index 28ff3df31487..055f723fc75f 100644 --- a/gcc/rust/expand/rust-expand-visitor.cc +++ b/gcc/rust/expand/rust-expand-visitor.cc @@ -149,12 +149,18 @@ expand_item_attribute (AST::Item &item, AST::SimplePath &name, return result; } +/* Helper function to expand a given attribute on a statement and collect back + * statements. + * T should be anything that can be used as a statement accepting outer + * attributes. + */ template static std::vector> -expand_stmt_attribute (T &item, AST::SimplePath &name, MacroExpander &expander) +expand_stmt_attribute (T &statement, AST::SimplePath &attribute, + MacroExpander &expander) { std::vector> result; - auto frag = expander.expand_attribute_proc_macro (item, name); + auto frag = expander.expand_attribute_proc_macro (statement, attribute); if (!frag.is_error ()) { for (auto &node : frag.get_nodes ()) -- 2.43.5