Skip to content

Commit 4c70d7e

Browse files
committed
Support block expressions within macros
When we parse DelimTokenTree's the delimiter's are synthesised when we ask for the token stream which results in tokens lacking location info. This removes the hack by adding the actual tokens from the lexer into the stream.
1 parent 19c5dde commit 4c70d7e

File tree

4 files changed

+19
-45
lines changed

4 files changed

+19
-45
lines changed

gcc/rust/ast/rust-ast-full-test.cc

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4387,14 +4387,6 @@ std::vector<std::unique_ptr<Token> >
43874387
DelimTokenTree::to_token_stream () const
43884388
{
43894389
std::vector<std::unique_ptr<Token> > tokens;
4390-
4391-
// simulate presence of delimiters
4392-
const_TokenPtr left_paren
4393-
= Rust::Token::make (left_delim_type_tok_token_id (delim_type),
4394-
Linemap::unknown_location ());
4395-
tokens.push_back (
4396-
std::unique_ptr<Token> (new Token (std::move (left_paren))));
4397-
43984390
for (const auto &tree : token_trees)
43994391
{
44004392
std::vector<std::unique_ptr<Token> > stream = tree->to_token_stream ();
@@ -4403,14 +4395,7 @@ DelimTokenTree::to_token_stream () const
44034395
std::make_move_iterator (stream.end ()));
44044396
}
44054397

4406-
const_TokenPtr right_paren
4407-
= Rust::Token::make (right_delim_type_tok_token_id (delim_type),
4408-
Linemap::unknown_location ());
4409-
tokens.push_back (
4410-
std::unique_ptr<Token> (new Token (std::move (right_paren))));
4411-
44124398
tokens.shrink_to_fit ();
4413-
44144399
return tokens;
44154400
}
44164401

gcc/rust/ast/rust-ast.h

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -778,36 +778,6 @@ class DelimTokenTree : public TokenTree, public AttrInput
778778
}
779779

780780
DelimType get_delim_type () const { return delim_type; }
781-
782-
static TokenId left_delim_type_tok_token_id (DelimType delim_type)
783-
{
784-
switch (delim_type)
785-
{
786-
case PARENS:
787-
return LEFT_PAREN;
788-
case SQUARE:
789-
return LEFT_SQUARE;
790-
case CURLY:
791-
return LEFT_CURLY;
792-
default:
793-
gcc_unreachable ();
794-
}
795-
}
796-
797-
static TokenId right_delim_type_tok_token_id (DelimType delim_type)
798-
{
799-
switch (delim_type)
800-
{
801-
case PARENS:
802-
return RIGHT_PAREN;
803-
case SQUARE:
804-
return RIGHT_SQUARE;
805-
case CURLY:
806-
return RIGHT_CURLY;
807-
default:
808-
gcc_unreachable ();
809-
}
810-
}
811781
};
812782

813783
/* Forward decl - definition moved to rust-expr.h as it requires LiteralExpr to

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,9 @@ Parser<ManagedTokenSource>::parse_delim_token_tree ()
905905

906906
// parse actual token tree vector - 0 or more
907907
std::vector<std::unique_ptr<AST::TokenTree>> token_trees_in_tree;
908+
auto delim_open
909+
= std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
910+
token_trees_in_tree.push_back (std::move (delim_open));
908911

909912
// repeat loop until finding the matching delimiter
910913
t = lexer.peek_token ();
@@ -929,6 +932,9 @@ Parser<ManagedTokenSource>::parse_delim_token_tree ()
929932
// lexer.skip_token();
930933
t = lexer.peek_token ();
931934
}
935+
auto delim_close
936+
= std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
937+
token_trees_in_tree.push_back (std::move (delim_close));
932938

933939
AST::DelimTokenTree token_tree (delim_type, std::move (token_trees_in_tree),
934940
initial_loc);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
macro_rules! add {
2+
($a:expr,$b:expr) => {{
3+
$a + $b
4+
}};
5+
}
6+
7+
fn test() -> i32 {
8+
add!(1, 2)
9+
}
10+
11+
fn main() -> i32 {
12+
test() - 3
13+
}

0 commit comments

Comments
 (0)