Skip to content

Commit dae69e8

Browse files
powerboat9CohenArthur
authored andcommitted
Ensure TupleStructPattern and TuplePattern have items
Note that instances of both classes which have been moved from will have (items == nullptr). gcc/rust/ChangeLog: * ast/rust-pattern.h (class TupleStructPattern): Assert that items != nullptr. (class TuplePattern): Likewise. (TupleStructPattern::has_items): Remove. (TuplePattern::has_tuple_pattern_items): Likewise. * parse/rust-parse-impl.h (Parser::parse_ident_leading_pattern): Prevent construction of TupleStructPattern with (items == nullptr). (Parser::parse_pattern_no_alt): Likewise. * ast/rust-ast-collector.cc (TokenCollector::visit): Remove usage of TupleStructPattern::has_items. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Likewise. gcc/testsuite/ChangeLog: * rust/compile/pattern-struct.rs: Fix test. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
1 parent ede65c2 commit dae69e8

File tree

6 files changed

+26
-54
lines changed

6 files changed

+26
-54
lines changed

gcc/rust/ast/rust-ast-collector.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -2503,8 +2503,7 @@ TokenCollector::visit (TupleStructPattern &pattern)
25032503
{
25042504
visit (pattern.get_path ());
25052505
push (Rust::Token::make (LEFT_PAREN, pattern.get_locus ()));
2506-
if (pattern.has_items ())
2507-
visit (pattern.get_items ());
2506+
visit (pattern.get_items ());
25082507
push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
25092508
}
25102509

gcc/rust/ast/rust-ast-visitor.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -1225,8 +1225,7 @@ void
12251225
DefaultASTVisitor::visit (AST::TupleStructPattern &pattern)
12261226
{
12271227
visit (pattern.get_path ());
1228-
if (pattern.has_items ())
1229-
visit (pattern.get_items ());
1228+
visit (pattern.get_items ());
12301229
}
12311230

12321231
void

gcc/rust/ast/rust-pattern.h

+23-23
Original file line numberDiff line numberDiff line change
@@ -1123,22 +1123,22 @@ class TupleStructPattern : public Pattern
11231123
public:
11241124
std::string as_string () const override;
11251125

1126-
// Returns whether the pattern has tuple struct items.
1127-
bool has_items () const { return items != nullptr; }
1128-
11291126
TupleStructPattern (PathInExpression tuple_struct_path,
11301127
std::unique_ptr<TupleStructItems> items)
11311128
: path (std::move (tuple_struct_path)), items (std::move (items)),
11321129
node_id (Analysis::Mappings::get ()->get_next_node_id ())
1133-
{}
1130+
{
1131+
rust_assert (this->items != nullptr);
1132+
}
11341133

11351134
// Copy constructor required to clone
11361135
TupleStructPattern (TupleStructPattern const &other) : path (other.path)
11371136
{
11381137
// guard to protect from null dereference
1138+
rust_assert (other.items != nullptr);
1139+
11391140
node_id = other.node_id;
1140-
if (other.items != nullptr)
1141-
items = other.items->clone_tuple_struct_items ();
1141+
items = other.items->clone_tuple_struct_items ();
11421142
}
11431143

11441144
// Operator overload assignment operator to clone
@@ -1148,10 +1148,9 @@ class TupleStructPattern : public Pattern
11481148
node_id = other.node_id;
11491149

11501150
// guard to protect from null dereference
1151-
if (other.items != nullptr)
1152-
items = other.items->clone_tuple_struct_items ();
1153-
else
1154-
items = nullptr;
1151+
rust_assert (other.items != nullptr);
1152+
1153+
items = other.items->clone_tuple_struct_items ();
11551154

11561155
return *this;
11571156
}
@@ -1164,7 +1163,11 @@ class TupleStructPattern : public Pattern
11641163

11651164
void accept_vis (ASTVisitor &vis) override;
11661165

1167-
std::unique_ptr<TupleStructItems> &get_items () { return items; }
1166+
std::unique_ptr<TupleStructItems> &get_items ()
1167+
{
1168+
rust_assert (items != nullptr);
1169+
return items;
1170+
}
11681171

11691172
PathInExpression &get_path () { return path; }
11701173
const PathInExpression &get_path () const { return path; }
@@ -1358,29 +1361,28 @@ class TuplePatternItemsRanged : public TuplePatternItems
13581361
// AST node representing a tuple pattern
13591362
class TuplePattern : public Pattern
13601363
{
1361-
// bool has_tuple_pattern_items;
13621364
std::unique_ptr<TuplePatternItems> items;
13631365
location_t locus;
13641366
NodeId node_id;
13651367

13661368
public:
13671369
std::string as_string () const override;
13681370

1369-
// Returns true if the tuple pattern has items
1370-
bool has_tuple_pattern_items () const { return items != nullptr; }
1371-
13721371
TuplePattern (std::unique_ptr<TuplePatternItems> items, location_t locus)
13731372
: items (std::move (items)), locus (locus),
13741373
node_id (Analysis::Mappings::get ()->get_next_node_id ())
1375-
{}
1374+
{
1375+
rust_assert (this->items != nullptr);
1376+
}
13761377

13771378
// Copy constructor requires clone
13781379
TuplePattern (TuplePattern const &other) : locus (other.locus)
13791380
{
13801381
// guard to prevent null dereference
1382+
rust_assert (other.items != nullptr);
1383+
13811384
node_id = other.node_id;
1382-
if (other.items != nullptr)
1383-
items = other.items->clone_tuple_pattern_items ();
1385+
items = other.items->clone_tuple_pattern_items ();
13841386
}
13851387

13861388
// Overload assignment operator to clone
@@ -1390,11 +1392,9 @@ class TuplePattern : public Pattern
13901392
node_id = other.node_id;
13911393

13921394
// guard to prevent null dereference
1393-
if (other.items != nullptr)
1394-
items = other.items->clone_tuple_pattern_items ();
1395-
else
1396-
items = nullptr;
1395+
rust_assert (other.items != nullptr);
13971396

1397+
items = other.items->clone_tuple_pattern_items ();
13981398
return *this;
13991399
}
14001400

@@ -1405,7 +1405,7 @@ class TuplePattern : public Pattern
14051405
// TODO: seems kinda dodgy. Think of better way.
14061406
std::unique_ptr<TuplePatternItems> &get_items ()
14071407
{
1408-
rust_assert (has_tuple_pattern_items ());
1408+
rust_assert (items != nullptr);
14091409
return items;
14101410
}
14111411

gcc/rust/parse/rust-parse-impl.h

-16
Original file line numberDiff line numberDiff line change
@@ -10631,14 +10631,6 @@ Parser<ManagedTokenSource>::parse_pattern_no_alt ()
1063110631
// tuple struct
1063210632
lexer.skip_token ();
1063310633

10634-
// check if empty tuple
10635-
if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
10636-
{
10637-
lexer.skip_token ();
10638-
return std::unique_ptr<AST::TupleStructPattern> (
10639-
new AST::TupleStructPattern (std::move (path), nullptr));
10640-
}
10641-
1064210634
// parse items
1064310635
std::unique_ptr<AST::TupleStructItems> items
1064410636
= parse_tuple_struct_items ();
@@ -11094,14 +11086,6 @@ Parser<ManagedTokenSource>::parse_ident_leading_pattern ()
1109411086
// DEBUG
1109511087
rust_debug ("parsing tuple struct pattern");
1109611088

11097-
// check if empty tuple
11098-
if (lexer.peek_token ()->get_id () == RIGHT_PAREN)
11099-
{
11100-
lexer.skip_token ();
11101-
return std::unique_ptr<AST::TupleStructPattern> (
11102-
new AST::TupleStructPattern (std::move (path), nullptr));
11103-
}
11104-
1110511089
// parse items
1110611090
std::unique_ptr<AST::TupleStructItems> items
1110711091
= parse_tuple_struct_items ();

gcc/rust/resolve/rust-early-name-resolver.cc

-10
Original file line numberDiff line numberDiff line change
@@ -558,16 +558,6 @@ EarlyNameResolver::visit (AST::StructPattern &)
558558
void
559559
EarlyNameResolver::visit (AST::TupleStructPattern &pattern)
560560
{
561-
if (!pattern.has_items ())
562-
{
563-
rich_location rich_locus (line_table, pattern.get_locus ());
564-
rich_locus.add_fixit_replace (
565-
"function calls are not allowed in patterns");
566-
rust_error_at (
567-
rich_locus, ErrorCode::E0164,
568-
"expected tuple struct or tuple variant, found associated function");
569-
return;
570-
}
571561
pattern.get_items ()->accept_vis (*this);
572562
}
573563

gcc/testsuite/rust/compile/pattern-struct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
fn bar(foo: A) {
1212
match foo {
1313
A::new() => (),
14-
// { dg-error "expected tuple struct or tuple variant, found associated function" "" { target *-*-* } .-1 }
14+
// { dg-error "expected tuple struct or tuple variant, found function" "" { target *-*-* } .-1 }
1515
_ => {}
1616
}
1717
}

0 commit comments

Comments
 (0)