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

Support interpolated block for try and async #112953

Merged
merged 1 commit into from
Jul 22, 2023
Merged
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
14 changes: 10 additions & 4 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3003,7 +3003,8 @@ impl<'a> Parser<'a> {
fn is_do_catch_block(&self) -> bool {
self.token.is_keyword(kw::Do)
&& self.is_keyword_ahead(1, &[kw::Catch])
&& self.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace))
&& self
.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
&& !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
}

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

fn is_try_block(&self) -> bool {
self.token.is_keyword(kw::Try)
&& self.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace))
&& self
.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block())
&& self.token.uninterpolated_span().at_least_rust_2018()
}

Expand All @@ -3032,10 +3034,14 @@ impl<'a> Parser<'a> {
&& ((
// `async move {`
self.is_keyword_ahead(1, &[kw::Move])
&& self.look_ahead(2, |t| *t == token::OpenDelim(Delimiter::Brace))
&& self.look_ahead(2, |t| {
*t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need lookahead here in the first place?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea. This could probably be relaxed as long as we make sure not to allow code like async #[attr] {, but that could be done in the code that actually parses the async expr.

})
) || (
// `async {`
self.look_ahead(1, |t| *t == token::OpenDelim(Delimiter::Brace))
self.look_ahead(1, |t| {
*t == token::OpenDelim(Delimiter::Brace) || t.is_whole_block()
})
))
}

Expand Down
16 changes: 16 additions & 0 deletions tests/ui/parser/async-with-nonterminal-block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// check-pass
// edition:2021

macro_rules! create_async {
($body:block) => {
async $body
};
}

async fn other() {}

fn main() {
let y = create_async! {{
other().await;
}};
}
19 changes: 19 additions & 0 deletions tests/ui/parser/try-with-nonterminal-block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// check-pass
// edition:2021

#![feature(try_blocks)]

macro_rules! create_try {
($body:block) => {
try $body
};
}

fn main() {
let x: Option<&str> = create_try! {{
None?;
"Hello world"
}};

println!("{x:?}");
}