Skip to content

Commit 3df8506

Browse files
committed
Permit . after macro invocations at the starts of statements
This fixes the most commonly-encountered case of #5941.
1 parent ae81c89 commit 3df8506

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,8 +2810,12 @@ impl<'a> Parser<'a> {
28102810
/// actually, this seems to be the main entry point for
28112811
/// parsing an arbitrary expression.
28122812
pub fn parse_assign_expr(&mut self) -> P<Expr> {
2813-
let lo = self.span.lo;
28142813
let lhs = self.parse_binops();
2814+
self.parse_more_assign_expr(lhs)
2815+
}
2816+
2817+
pub fn parse_more_assign_expr(&mut self, lhs: P<Expr>) -> P<Expr> {
2818+
let lo = self.span.lo;
28152819
let restrictions = self.restrictions & RestrictionNoStructLiteral;
28162820
match self.token {
28172821
token::EQ => {
@@ -3557,8 +3561,17 @@ impl<'a> Parser<'a> {
35573561
let hi = self.span.hi;
35583562

35593563
if id.name == token::special_idents::invalid.name {
3560-
P(spanned(lo, hi, StmtMac(
3561-
spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT)), false)))
3564+
if self.token == token::DOT {
3565+
let b = self.mk_mac_expr(lo,
3566+
hi,
3567+
MacInvocTT(pth, tts, EMPTY_CTXT));
3568+
let d = self.parse_dot_or_call_expr_with(b);
3569+
let e = self.parse_more_assign_expr(d);
3570+
P(spanned(lo, e.span.hi, StmtExpr(e, ast::DUMMY_NODE_ID)))
3571+
} else {
3572+
P(spanned(lo, hi, StmtMac(
3573+
spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT)), false)))
3574+
}
35623575
} else {
35633576
// if it has a special ident, it's definitely an item
35643577
P(spanned(lo, hi, StmtDecl(
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(macro_rules, tuple_indexing)]
12+
13+
struct Foo(bool);
14+
15+
impl Foo {
16+
fn frob(&mut self) {
17+
self.0 = true;
18+
}
19+
}
20+
21+
impl Drop for Foo {
22+
fn drop(&mut self) {
23+
if !self.0 {
24+
fail!("frob() was not run!")
25+
}
26+
}
27+
}
28+
29+
macro_rules! foo {
30+
($x: ident) => {{ $x = Foo(false); $x }}
31+
}
32+
33+
fn main() {
34+
let mut x;
35+
// This both tests that it is parsed properly AND that it actually runs the method
36+
foo!(x).frob();
37+
// Test that other operators can follow the macro invocation
38+
let mut y;
39+
foo!(y).0 = true;
40+
}

0 commit comments

Comments
 (0)