Skip to content

fix: Guard SizeSkeleton::compute against stack overflow#156235

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
rajgandhi1:compiler_stack_overflow_fix
May 9, 2026
Merged

fix: Guard SizeSkeleton::compute against stack overflow#156235
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
rajgandhi1:compiler_stack_overflow_fix

Conversation

@rajgandhi1
Copy link
Copy Markdown
Contributor

Fixes #156137

Fix: extract the recursion into a private compute_inner that carries a depth counter. When depth exceeds the crate's recursion limit, return LayoutError::Unknown and let the existing transmute size-check produce a normal error instead of crashing.

A regression test is included in tests/ui/transmute/.

@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 May 6, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 6, 2026

r? @adwinwhite

rustbot has assigned @adwinwhite.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 73 candidates
  • Random selection from 20 candidates

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_middle/src/ty/layout.rs Outdated
// Bail out if we've recursed too deeply (issue #156137); a cyclic type
// alias can otherwise blow the stack here.
if !tcx.recursion_limit().value_within_limit(depth) {
return Err(tcx.arena.alloc(LayoutError::Unknown(ty)));
Copy link
Copy Markdown
Contributor

@oli-obk oli-obk May 6, 2026

Choose a reason for hiding this comment

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

We should just raise a recursion limit error here. There's a function for doing that, several other recursion limit checks should be doing that, too.

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.

you mean the emit_err(RecursionLimitReached{...}) pattern used in struct_tail_raw?

but SizeSkeleton::compute has no span. should I add it and create RecursionLimitReachedSizeSkeleton diagnostic in error.rs?

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.

You can pass span in from SizeSkeleton::compute's call sites. If there's no span outside, then fallback to DUMMY_SP.

There're multiple RecursionLimitReached errors in different crates. If reusing doesn't make sense, you can create your own one to tailor the message. But @oli-obk might know a generic function for that?

@rajgandhi1 rajgandhi1 requested a review from oli-obk May 8, 2026 04:37
@rajgandhi1
Copy link
Copy Markdown
Contributor Author

@adwinwhite @oli-obk
for now added a new RecursionLimitReachedSizeSkeleton diagnostic rather than reusing the existing RecursionLimitReached ("finding the struct tail for…") since the message wouldn't fit.

Implemented this for now, Is there a more generic utility you had in mind, or does this approach look good?

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_lint/src/types.rs Outdated
Comment thread compiler/rustc_middle/src/ty/layout.rs Outdated
Comment thread compiler/rustc_middle/src/ty/layout.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@rajgandhi1 rajgandhi1 requested a review from adwinwhite May 9, 2026 04:12
Comment thread compiler/rustc_hir_typeck/src/intrinsicck.rs Outdated
@rajgandhi1 rajgandhi1 force-pushed the compiler_stack_overflow_fix branch from a8c7309 to 7a900df Compare May 9, 2026 10:32
@rajgandhi1 rajgandhi1 requested a review from adwinwhite May 9, 2026 11:49
@adwinwhite
Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 9, 2026

📌 Commit 7a900df has been approved by adwinwhite

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 9, 2026
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request May 9, 2026
…_fix, r=adwinwhite

fix: Guard SizeSkeleton::compute against stack overflow

Fixes rust-lang#156137

Fix: extract the recursion into a private `compute_inner` that carries a depth counter. When depth exceeds the crate's recursion limit, return `LayoutError::Unknown` and let the existing transmute size-check produce a normal error instead of crashing.

A regression test is included in `tests/ui/transmute/`.
rust-bors Bot pushed a commit that referenced this pull request May 9, 2026
Rollup of 5 pull requests

Successful merges:

 - #156354 (Update example code of `std::array::from_fn`)
 - #156359 (use `cfg_select!` in integer `{to, from}_{le, be}`)
 - #156124 (Make stable hashing names consistent (part 2))
 - #156235 (fix: Guard SizeSkeleton::compute against stack overflow)
 - #156353 (resolve: Set correct parent and expansion for `self` declarations)
@rust-bors rust-bors Bot merged commit 80263e1 into rust-lang:main May 9, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone May 9, 2026
rust-timer added a commit that referenced this pull request May 9, 2026
Rollup merge of #156235 - rajgandhi1:compiler_stack_overflow_fix, r=adwinwhite

fix: Guard SizeSkeleton::compute against stack overflow

Fixes #156137

Fix: extract the recursion into a private `compute_inner` that carries a depth counter. When depth exceeds the crate's recursion limit, return `LayoutError::Unknown` and let the existing transmute size-check produce a normal error instead of crashing.

A regression test is included in `tests/ui/transmute/`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

Transmuting struct that contains itself causes compiler stack overflow

5 participants