Skip to content

Commit a534216

Browse files
committed
Auto merge of #54850 - mcr431:fix-54707-trait-function-from-macro, r=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.
2 parents cb6eedd + 3447473 commit a534216

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/libsyntax/parse/parser.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,23 @@ impl<'a> Parser<'a> {
14301430
attrs.extend(inner_attrs.iter().cloned());
14311431
Some(body)
14321432
}
1433+
token::Interpolated(ref nt) => {
1434+
match &nt.0 {
1435+
token::NtBlock(..) => {
1436+
*at_end = true;
1437+
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
1438+
attrs.extend(inner_attrs.iter().cloned());
1439+
Some(body)
1440+
}
1441+
_ => {
1442+
let token_str = self.this_token_to_string();
1443+
let mut err = self.fatal(&format!("expected `;` or `{{`, found `{}`",
1444+
token_str));
1445+
err.span_label(self.span, "expected `;` or `{`");
1446+
return Err(err);
1447+
}
1448+
}
1449+
}
14331450
_ => {
14341451
let token_str = self.this_token_to_string();
14351452
let mut err = self.fatal(&format!("expected `;` or `{{`, found `{}`",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2012 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+
// run-pass
12+
//
13+
// Description - ensure Interpolated blocks can act as valid function bodies
14+
// Covered cases: free functions, struct methods, and default trait functions
15+
16+
macro_rules! def_fn {
17+
($body:block) => {
18+
fn bar() $body
19+
}
20+
}
21+
22+
trait Foo {
23+
def_fn!({ println!("foo"); });
24+
}
25+
26+
struct Baz {}
27+
28+
impl Foo for Baz {}
29+
30+
struct Qux {}
31+
32+
impl Qux {
33+
def_fn!({ println!("qux"); });
34+
}
35+
36+
def_fn!({ println!("quux"); });
37+
38+
pub fn main() {
39+
Baz::bar();
40+
Qux::bar();
41+
bar();
42+
}

0 commit comments

Comments
 (0)