Skip to content

parse attrs if a loop's parent hir node is a stmt#158170

Open
usamoi wants to merge 1 commit into
rust-lang:mainfrom
usamoi:loop-unroll-stmt
Open

parse attrs if a loop's parent hir node is a stmt#158170
usamoi wants to merge 1 commit into
rust-lang:mainfrom
usamoi:loop-unroll-stmt

Conversation

@usamoi

@usamoi usamoi commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

fix this case:

// unroll doesn't work
#[unsafe(no_mangle)]
fn f(n: usize) -> usize {
    let mut sum = 0;
    let mut i = 0;
    #[unroll(4)]
    loop { if i < n { sum += i; i += 1; } else { break; } }
    sum
}

// unroll works
#[unsafe(no_mangle)]
fn g(n: usize) {
    let mut i = 0;
    #[unroll(4)]
    loop { if i < n { i += 1; } else { break; } }
}

cc #156874

r? saethlin

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 20, 2026
@@ -26,5 +26,24 @@ pub fn unroll_hint() {
}
}

@saethlin saethlin Jun 20, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// HIR for a `loop` statement is a bit different, make sure we still apply the metadata in that case.

View changes since the review

Comment on lines -77 to +79
if let hir::Node::Block(_) = self.tcx.parent_hir_node(hir_expr.hir_id) {
if let hir::Node::Block(_) | hir::Node::Stmt(_) =
self.tcx.parent_hir_node(hir_expr.hir_id)
{

@saethlin saethlin Jun 20, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@JonathanBrouwer Is this the right way to fix this? I really don't know enough about HIR to be confident that this is the right way to handle things or if this is hack on a hack.

View changes since the review

@saethlin saethlin added the F-loop-hints `#![feature(loop_hints)]` label Jun 20, 2026
@JonathanBrouwer JonathanBrouwer self-assigned this Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-loop-hints `#![feature(loop_hints)]` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants