Skip to content

Fix ICE on for await loops with separated keyword tokens - #7063

Open
AsthaMishra wants to merge 2 commits into
rust-lang:mainfrom
AsthaMishra:fix-issue-7054
Open

Fix ICE on for await loops with separated keyword tokens#7063
AsthaMishra wants to merge 2 commits into
rust-lang:mainfrom
AsthaMishra:fix-issue-7054

Conversation

@AsthaMishra

@AsthaMishra AsthaMishra commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Issue : we are searching source for single string literal for await, but source have more than one space between these tokens. Any source that separated tokens with more than one space, new line, or a comment failed the search because span_after panicked

Fix: locate for and await as independent token then combine them with combine_strs_with_missing_comments by preserving any comment between them, like how mut and ref handled in patterns.rs. the lookup uses opt_span_after so a miss results in unformatted output instead of an ICE

Fixes #7054

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Aug 24, 2026
Comment thread tests/source/issue-7054.rs Outdated
Comment on lines +1 to +2
// rustfmt-edition: 2024

@ytmimi ytmimi Aug 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is for await only available in the 2024 edition?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

no

@ytmimi ytmimi Aug 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@AsthaMishra Might be best to set the lowest edition where for await is available.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

will remove this

Comment thread src/expr.rs Outdated
Comment on lines +994 to +1036
let offset = self.keyword.len() + label_string.len() + 1;
let offset = keyword.len() + label_string.len() + 1;

@ytmimi ytmimi Aug 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This doesn't seem correct to me in if there's a multi line comment. For example:

`for /* some
 * multi-line
 * comment
*/ await`

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

multi line comment working fine

async fn for_await_comment_after_for(iter: Iter) {
    for /* some
 * multi-line
 * comment
*/ await
    i in iter {}
}

this formatted to

async fn for_await_comment_after_for(iter: Iter) {
    for /* some
     * multi-line
     * comment
    */
    await i in iter {}
}

and no other errors

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@AsthaMishra what I'm saying is that it doesn't make sense to calculate the offset based on the entire length of the comment + the keywords. Especially if the comment is on multiple lines.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

okay. Understood.

Comment on lines +22 to +32
async fn for_await_comment_between_keywords(iter: Iter) {
for /* between for and await */ await i in iter {}
}

async fn for_await_comment_after_keyword(iter: Iter) {
for await /* between await and pat */ i in iter {}
}

async fn for_await_comment_both_gaps(iter: Iter) {
for /* first gap */ await /* second gap */ i in iter {}
}

@ytmimi ytmimi Aug 24, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's add test cases where the user uses a // line comment instead of an /* inline comment */. Let's also expand on what we've got here and add some multi-line comment test cases too.

View changes since the review

@ytmimi

ytmimi commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Aug 24, 2026
@rustbot

rustbot commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

… last new line. Test cases added for multi-line and line comment
@AsthaMishra

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. labels Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: bad span: for await: with extra space in between

3 participants