Skip to content

Commit 1d56e3a

Browse files
committedJul 22, 2023
Auto merge of rust-lang#112953 - compiler-errors:interpolated-block-exprs, r=WaffleLapkin
Support interpolated block for `try` and `async` I'm putting this up for T-lang discussion, to decide whether or not they feel like this should be supported. This was raised in rust-lang#112952, which surprised me. There doesn't seem to be a *technical* reason why we don't support this. ### Precedent: This is supported: ```rust macro_rules! always { ($block:block) => { if true $block } } fn main() { always!({}); } ``` ### Counterpoint: However, for context, this is *not* supported: ```rust macro_rules! unsafe_block { ($block:block) => { unsafe $block } } fn main() { unsafe_block!({}); } ``` If this support for `async` and `try` with interpolated blocks is *not* desirable, then I can convert them to instead the same diagnostic as `unsafe $block` and make this situation a lot less ambiguous. ---- I'll try to write up more before T-lang triage on Tuesday. I couldn't find anything other than rust-lang#69760 for why something like `unsafe $block` is not supported, and even that PR doesn't have much information. Fixes rust-lang#112952
2 parents a6fbd1c + 7b962d7 commit 1d56e3a

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed
 

‎compiler/rustc_parse/src/parser/expr.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -3003,7 +3003,8 @@ impl<'a> Parser<'a> {
30033003
fn is_do_catch_block(&self) -> bool {
30043004
self.token.is_keyword(kw::Do)
30053005
&& self.is_keyword_ahead(1, &[kw::Catch])
3006-
&& self.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace))
3006+
&& self
3007+
.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
30073008
&& !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
30083009
}
30093010

@@ -3013,7 +3014,8 @@ impl<'a> Parser<'a> {
30133014

30143015
fn is_try_block(&self) -> bool {
30153016
self.token.is_keyword(kw::Try)
3016-
&& self.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace))
3017+
&& self
3018+
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
30173019
&& self.token.uninterpolated_span().at_least_rust_2018()
30183020
}
30193021

@@ -3032,10 +3034,14 @@ impl<'a> Parser<'a> {
30323034
&& ((
30333035
// `async move {`
30343036
self.is_keyword_ahead(1, &[kw::Move])
3035-
&& self.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace))
3037+
&& self.look_ahead(2, |t| {
3038+
*t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block()
3039+
})
30363040
) || (
30373041
// `async {`
3038-
self.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace))
3042+
self.look_ahead(1, |t| {
3043+
*t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block()
3044+
})
30393045
))
30403046
}
30413047

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
// edition:2021
3+
4+
macro_rules! create_async {
5+
($body:block) => {
6+
async $body
7+
};
8+
}
9+
10+
async fn other() {}
11+
12+
fn main() {
13+
let y = create_async! {{
14+
other().await;
15+
}};
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// check-pass
2+
// edition:2021
3+
4+
#![feature(try_blocks)]
5+
6+
macro_rules! create_try {
7+
($body:block) => {
8+
try $body
9+
};
10+
}
11+
12+
fn main() {
13+
let x: Option<&str> = create_try! {{
14+
None?;
15+
"Hello world"
16+
}};
17+
18+
println!("{x:?}");
19+
}

0 commit comments

Comments
 (0)