Skip to content

Permit . after macro invocations at the starts of statements #17759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2810,8 +2810,12 @@ impl<'a> Parser<'a> {
/// actually, this seems to be the main entry point for
/// parsing an arbitrary expression.
pub fn parse_assign_expr(&mut self) -> P<Expr> {
let lo = self.span.lo;
let lhs = self.parse_binops();
self.parse_more_assign_expr(lhs)
}

pub fn parse_more_assign_expr(&mut self, lhs: P<Expr>) -> P<Expr> {
let lo = self.span.lo;
let restrictions = self.restrictions & RestrictionNoStructLiteral;
match self.token {
token::EQ => {
Expand Down Expand Up @@ -3557,8 +3561,17 @@ impl<'a> Parser<'a> {
let hi = self.span.hi;

if id.name == token::special_idents::invalid.name {
P(spanned(lo, hi, StmtMac(
spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT)), false)))
if self.token == token::DOT {
let b = self.mk_mac_expr(lo,
hi,
MacInvocTT(pth, tts, EMPTY_CTXT));
let d = self.parse_dot_or_call_expr_with(b);
let e = self.parse_more_assign_expr(d);
P(spanned(lo, e.span.hi, StmtExpr(e, ast::DUMMY_NODE_ID)))
} else {
P(spanned(lo, hi, StmtMac(
spanned(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT)), false)))
}
} else {
// if it has a special ident, it's definitely an item
P(spanned(lo, hi, StmtDecl(
Expand Down
40 changes: 40 additions & 0 deletions src/test/run-pass/macro-followed-by-dot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(macro_rules, tuple_indexing)]

struct Foo(bool);

impl Foo {
fn frob(&mut self) {
self.0 = true;
}
}

impl Drop for Foo {
fn drop(&mut self) {
if !self.0 {
fail!("frob() was not run!")
}
}
}

macro_rules! foo {
($x: ident) => {{ $x = Foo(false); $x }}
}

fn main() {
let mut x;
// This both tests that it is parsed properly AND that it actually runs the method
foo!(x).frob();
// Test that other operators can follow the macro invocation
let mut y;
foo!(y).0 = true;
}