Skip to content

Commit f9c1a14

Browse files
Merge #1069
1069: Handle macro invocations in type contexts r=CohenArthur a=CohenArthur Closes #1067 This highlighted two issues where parsing types is not entirely correct, which I'll raise. The code necessary to handle macro invocations in these two places should already be implemented. Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
2 parents bd1f435 + 6bf4283 commit f9c1a14

File tree

6 files changed

+344
-12
lines changed

6 files changed

+344
-12
lines changed

gcc/rust/ast/rust-ast.h

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,7 @@ class SingleASTNode
15151515
TRAIT,
15161516
IMPL,
15171517
TRAIT_IMPL,
1518+
TYPE,
15181519
};
15191520

15201521
private:
@@ -1528,6 +1529,7 @@ class SingleASTNode
15281529
std::unique_ptr<TraitItem> trait_item;
15291530
std::unique_ptr<InherentImplItem> impl_item;
15301531
std::unique_ptr<TraitImplItem> trait_impl_item;
1532+
std::unique_ptr<Type> type;
15311533

15321534
public:
15331535
SingleASTNode (std::unique_ptr<Expr> expr)
@@ -1558,6 +1560,10 @@ class SingleASTNode
15581560
: kind (TRAIT_IMPL), trait_impl_item (std::move (trait_impl_item))
15591561
{}
15601562

1563+
SingleASTNode (std::unique_ptr<Type> type)
1564+
: kind (TYPE), type (std::move (type))
1565+
{}
1566+
15611567
SingleASTNode (SingleASTNode const &other)
15621568
{
15631569
kind = other.kind;
@@ -1590,6 +1596,10 @@ class SingleASTNode
15901596
case TRAIT_IMPL:
15911597
trait_impl_item = other.trait_impl_item->clone_trait_impl_item ();
15921598
break;
1599+
1600+
case TYPE:
1601+
type = other.type->clone_type ();
1602+
break;
15931603
}
15941604
}
15951605

@@ -1625,6 +1635,10 @@ class SingleASTNode
16251635
case TRAIT_IMPL:
16261636
trait_impl_item = other.trait_impl_item->clone_trait_impl_item ();
16271637
break;
1638+
1639+
case TYPE:
1640+
type = other.type->clone_type ();
1641+
break;
16281642
}
16291643
return *this;
16301644
}
@@ -1699,6 +1713,12 @@ class SingleASTNode
16991713
return std::move (trait_impl_item);
17001714
}
17011715

1716+
std::unique_ptr<Type> take_type ()
1717+
{
1718+
rust_assert (!is_error ());
1719+
return std::move (type);
1720+
}
1721+
17021722
void accept_vis (ASTVisitor &vis)
17031723
{
17041724
switch (kind)
@@ -1730,6 +1750,10 @@ class SingleASTNode
17301750
case TRAIT_IMPL:
17311751
trait_impl_item->accept_vis (vis);
17321752
break;
1753+
1754+
case TYPE:
1755+
type->accept_vis (vis);
1756+
break;
17331757
}
17341758
}
17351759

@@ -1751,6 +1775,8 @@ class SingleASTNode
17511775
return impl_item == nullptr;
17521776
case TRAIT_IMPL:
17531777
return trait_impl_item == nullptr;
1778+
case TYPE:
1779+
return type == nullptr;
17541780
}
17551781

17561782
gcc_unreachable ();
@@ -1774,7 +1800,9 @@ class SingleASTNode
17741800
case IMPL:
17751801
return "Impl Item: " + impl_item->as_string ();
17761802
case TRAIT_IMPL:
1777-
return "Trait Impl Item: " + impl_item->as_string ();
1803+
return "Trait Impl Item: " + trait_impl_item->as_string ();
1804+
case TYPE:
1805+
return "Type: " + type->as_string ();
17781806
}
17791807

17801808
gcc_unreachable ();
@@ -1799,6 +1827,18 @@ class ASTFragment
17991827
std::vector<SingleASTNode> nodes;
18001828
bool fragment_is_error;
18011829

1830+
/**
1831+
* We need to make a special case for Expression and Type fragments as only
1832+
* one Node will be extracted from the `nodes` vector
1833+
*/
1834+
1835+
bool is_single_fragment () const { return nodes.size () == 1; }
1836+
1837+
bool is_single_fragment_kind (SingleASTNode::NodeType kind) const
1838+
{
1839+
return is_single_fragment () && nodes[0].get_kind () == kind;
1840+
}
1841+
18021842
public:
18031843
ASTFragment (std::vector<SingleASTNode> nodes, bool fragment_is_error = false)
18041844
: nodes (std::move (nodes)), fragment_is_error (fragment_is_error)
@@ -1839,21 +1879,16 @@ class ASTFragment
18391879

18401880
bool should_expand () const { return !is_error () && !nodes.empty (); }
18411881

1842-
/**
1843-
* We need to make a special case for Expression fragments as only one
1844-
* Node will be extracted from the `nodes` vector
1845-
*/
1846-
1847-
bool is_expression_fragment () const
1882+
std::unique_ptr<Expr> take_expression_fragment ()
18481883
{
1849-
return nodes.size () == 1
1850-
&& nodes[0].get_kind () == SingleASTNode::NodeType::EXPRESSION;
1884+
rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::EXPRESSION));
1885+
return nodes[0].take_expr ();
18511886
}
18521887

1853-
std::unique_ptr<Expr> take_expression_fragment ()
1888+
std::unique_ptr<Type> take_type_fragment ()
18541889
{
1855-
rust_assert (is_expression_fragment ());
1856-
return nodes[0].take_expr ();
1890+
rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::TYPE));
1891+
return nodes[0].take_type ();
18571892
}
18581893

18591894
void accept_vis (ASTVisitor &vis)

0 commit comments

Comments
 (0)