Skip to content
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

Creating a trait function with a macro using block designator: error: expected ; or {, found { ... } #54707

Closed
birktj opened this issue Sep 30, 2018 · 1 comment · Fixed by #54969
Labels
A-parser Area: The parsing of Rust source code to an AST. C-bug Category: This is a bug.

Comments

@birktj
Copy link

birktj commented Sep 30, 2018

Given a simple macro:

macro_rules! def_fn{
    ($body:block) => {
        fn test() $body
    };
}

I can use it without problems to define a function:

def_fn!({println!("test"); });

I can also use it to define a struct method without problems:

struct Test {}

impl Test {
    def_fn!({println!("test"); });
}

However when I try to use it to define a default method for a trait it doesn't work:

trait Test {
    def_fn!({println!("test"); });
}

It gives an error message:

error: expected `;` or `{`, found `{ println!("test"); }`
  --> src/main.rs:9:19
   |
9  |         fn test() $body
   |                   ^^^^^ expected `;` or `{`
...
16 |     def_fn!({println!("test"); });
   |     ------------------------------ in this macro invocation
error: aborting due to previous error

If I change the macro to include braces around $body it works:

macro_rules! def_fn{
    ($body:block) => {
        fn test() {$body}
    };
}

I have no idea why this happens but I guess it is some sort of bug in how default trait methods are parsed.

@csmoe csmoe added the A-parser Area: The parsing of Rust source code to an AST. label Oct 1, 2018
@dtolnay dtolnay added the C-bug Category: This is a bug. label Oct 1, 2018
@matthew-russo
Copy link
Contributor

If no one is working on this, I'll take it.

pietroalbini added a commit to pietroalbini/rust that referenced this issue Oct 9, 2018
…m-macro, r=nikomatsakis

Fix rust-lang#54707 - parse_trait_item_ now handles interpolated blocks as function body decls

Fix rust-lang#54707 - parse_trait_item_ now handles interpolated blocks as function body decls

Previously parsing trait items only handled opening brace token and semicolon, I added a branch to the match statement that will also handle interpolated blocks.
pietroalbini added a commit to pietroalbini/rust that referenced this issue Oct 10, 2018
…m-macro, r=nikomatsakis

Fix rust-lang#54707 - parse_trait_item_ now handles interpolated blocks as function body decls

Fix rust-lang#54707 - parse_trait_item_ now handles interpolated blocks as function body decls

Previously parsing trait items only handled opening brace token and semicolon, I added a branch to the match statement that will also handle interpolated blocks.
bors added a commit that referenced this issue Oct 11, 2018
Rollup of 9 pull requests

Successful merges:

 - #54747 (codegen_llvm: verify that inline assembly operands are scalars)
 - #54848 (Better Diagnostic for Trait Object Capture)
 - #54850 (Fix #54707 - parse_trait_item_ now handles interpolated blocks as function body decls)
 - #54858 (second round of refactorings for universes)
 - #54862 (Implement RFC 2539: cfg_attr with multiple attributes)
 - #54869 (Fix mobile docs)
 - #54870 (Stabilize tool lints)
 - #54893 (Fix internal compiler error on malformed match arm pattern.)
 - #54904 (Stabilize the `Option::replace` method)

Failed merges:

 - #54909 ( Add chalk rules related to associated type defs)

r? @ghost
bors added a commit that referenced this issue Oct 11, 2018
Rollup of 9 pull requests

Successful merges:

 - #54747 (codegen_llvm: verify that inline assembly operands are scalars)
 - #54848 (Better Diagnostic for Trait Object Capture)
 - #54850 (Fix #54707 - parse_trait_item_ now handles interpolated blocks as function body decls)
 - #54858 (second round of refactorings for universes)
 - #54862 (Implement RFC 2539: cfg_attr with multiple attributes)
 - #54869 (Fix mobile docs)
 - #54870 (Stabilize tool lints)
 - #54893 (Fix internal compiler error on malformed match arm pattern.)
 - #54904 (Stabilize the `Option::replace` method)

Failed merges:

 - #54909 ( Add chalk rules related to associated type defs)

r? @ghost
bors added a commit that referenced this issue Oct 11, 2018
…nikomatsakis

Fix #54707 - parse_trait_item_ now handles interpolated blocks as function body decls

Fix #54707 - parse_trait_item_ now handles interpolated blocks as function body decls

Previously parsing trait items only handled opening brace token and semicolon, I added a branch to the match statement that will also handle interpolated blocks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-parser Area: The parsing of Rust source code to an AST. C-bug Category: This is a bug.
Projects
None yet
4 participants