From 56190de988d7bf53b88f17dadbc67788538fec4e Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Mon, 23 Sep 2024 15:32:26 -0400 Subject: [PATCH] Improve handline of SelfParam with missing type gcc/rust/ChangeLog: * ast/rust-ast-visitor.h (DefaultASTVisitor::visit): Add overload for tl::optional. * ast/rust-item.h (SelfParam::get_type): Return tl::optional. * ast/rust-ast-collector.cc (TokenCollector::visit): Handle changed return type of SelfParam::get_type. * expand/rust-cfg-strip.cc (CfgStrip::visit): Likewise. * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_self): Likewise. * resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit): Likewise. (ResolveItem::visit): Likewise. * resolve/rust-ast-resolve-stmt.h (ResolveStmt::visit): Likewise. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Likewise, and properly handle empty optional. gcc/testsuite/ChangeLog: * rust/compile/nr2/exclude: Remove entries. Signed-off-by: Owen Avery --- gcc/rust/ast/rust-ast-collector.cc | 2 +- gcc/rust/ast/rust-ast-visitor.h | 6 ++++++ gcc/rust/ast/rust-item.h | 5 ++--- gcc/rust/expand/rust-cfg-strip.cc | 2 +- gcc/rust/hir/rust-ast-lower-base.cc | 2 +- gcc/rust/resolve/rust-ast-resolve-item.cc | 6 +++--- gcc/rust/resolve/rust-ast-resolve-stmt.h | 2 +- gcc/rust/resolve/rust-default-resolver.cc | 2 +- gcc/testsuite/rust/compile/nr2/exclude | 3 --- 9 files changed, 16 insertions(+), 14 deletions(-) diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index a1306463e250..56949e4657cd 100644 --- a/gcc/rust/ast/rust-ast-collector.cc +++ b/gcc/rust/ast/rust-ast-collector.cc @@ -2014,7 +2014,7 @@ TokenCollector::visit (SelfParam ¶m) if (param.has_type ()) { push (Rust::Token::make (COLON, UNDEF_LOCATION)); - visit (param.get_type ()); + visit (param.get_type ().value ()); } } diff --git a/gcc/rust/ast/rust-ast-visitor.h b/gcc/rust/ast/rust-ast-visitor.h index 80214fa7fcec..cac8e577abef 100644 --- a/gcc/rust/ast/rust-ast-visitor.h +++ b/gcc/rust/ast/rust-ast-visitor.h @@ -410,6 +410,12 @@ class DefaultASTVisitor : public ASTVisitor node->accept_vis (*this); } + template void visit (tl::optional opt) + { + if (opt.has_value ()) + visit (opt.value ()); + } + virtual void visit (AST::GenericArgsBinding &binding); virtual void visit (AST::PathExprSegment &segment); virtual void visit (AST::GenericArgs &args); diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 8a156663fa72..5c53bfc2ac26 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -528,10 +528,9 @@ class SelfParam : public Param NodeId get_node_id () const { return node_id; } // TODO: is this better? Or is a "vis_block" better? - Type &get_type () + tl::optional get_type () { - rust_assert (has_type ()); - return *type; + return has_type () ? tl::optional (*type) : tl::optional (); } std::unique_ptr &get_type_ptr () diff --git a/gcc/rust/expand/rust-cfg-strip.cc b/gcc/rust/expand/rust-cfg-strip.cc index 33fb64f0ff2d..640a1f0dcad6 100644 --- a/gcc/rust/expand/rust-cfg-strip.cc +++ b/gcc/rust/expand/rust-cfg-strip.cc @@ -2697,7 +2697,7 @@ CfgStrip::visit (AST::SelfParam ¶m) if (param.has_type ()) { - auto &type = param.get_type (); + auto &type = param.get_type ().value (); if (type.is_marked_for_strip ()) rust_error_at (type.get_locus (), "cannot strip type in this position"); } diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index 272de0f87172..8fd6f15f53a0 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -674,7 +674,7 @@ ASTLoweringBase::lower_self (AST::Param ¶m) if (self.has_type ()) { - HIR::Type *type = ASTLoweringType::translate (self.get_type ()); + HIR::Type *type = ASTLoweringType::translate (self.get_type ().value ()); return HIR::SelfParam (mapping, std::unique_ptr (type), self.get_is_mut (), self.get_locus ()); } diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc index bf47c73495c8..5bb0798e9920 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.cc +++ b/gcc/rust/resolve/rust-ast-resolve-item.cc @@ -99,7 +99,7 @@ ResolveTraitItems::visit (AST::Function &function) { // This shouldn't happen the parser should already error for this rust_assert (!param.get_has_ref ()); - ResolveType::go (param.get_type ()); + ResolveType::go (param.get_type ().value ()); } else { @@ -503,7 +503,7 @@ ResolveItem::visit (AST::Function &function) { // This shouldn't happen the parser should already error for this rust_assert (!self_param.get_has_ref ()); - ResolveType::go (self_param.get_type ()); + ResolveType::go (self_param.get_type ().value ()); } else { @@ -536,7 +536,7 @@ ResolveItem::visit (AST::Function &function) { auto ¶m = static_cast (*p); if (param.has_type ()) - ResolveType::go (param.get_type ()); + ResolveType::go (param.get_type ().value ()); } else { diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.h b/gcc/rust/resolve/rust-ast-resolve-stmt.h index 7118b70d7058..c2012fab1f09 100644 --- a/gcc/rust/resolve/rust-ast-resolve-stmt.h +++ b/gcc/rust/resolve/rust-ast-resolve-stmt.h @@ -364,7 +364,7 @@ class ResolveStmt : public ResolverBase else if (p->is_self ()) { auto ¶m = static_cast (*p); - ResolveType::go (param.get_type ()); + ResolveType::go (param.get_type ().value ()); } else { diff --git a/gcc/rust/resolve/rust-default-resolver.cc b/gcc/rust/resolve/rust-default-resolver.cc index 8138a8949004..801b5dcb624e 100644 --- a/gcc/rust/resolve/rust-default-resolver.cc +++ b/gcc/rust/resolve/rust-default-resolver.cc @@ -68,7 +68,7 @@ DefaultResolver::visit (AST::Function &function) else if (p->is_self ()) { auto ¶m = static_cast (*p); - param.get_type ().accept_vis (*this); + visit (param.get_type ()); param.get_lifetime ().accept_vis (*this); } else diff --git a/gcc/testsuite/rust/compile/nr2/exclude b/gcc/testsuite/rust/compile/nr2/exclude index 3251921acd45..fe873e90c4c4 100644 --- a/gcc/testsuite/rust/compile/nr2/exclude +++ b/gcc/testsuite/rust/compile/nr2/exclude @@ -57,7 +57,6 @@ derive_macro3.rs derive_macro4.rs derive_macro6.rs diagnostic_underline.rs -expand_macro_qual_path_in_type.rs expected_type_args2.rs expected_type_args3.rs feature_rust_attri0.rs @@ -105,7 +104,6 @@ issue-1289.rs issue-1383.rs issue-1447.rs issue-1483.rs -issue-1524.rs issue-1589.rs issue-1725-1.rs issue-1725-2.rs @@ -152,7 +150,6 @@ issue-2772-2.rs issue-2775.rs issue-2747.rs issue-2782.rs -issue-2788.rs issue-2812.rs issue-2847-b.rs issue-850.rs