Skip to content

Fix ICE in peekable with unsized types #136451

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

Closed
wants to merge 1 commit into from

Conversation

charmitro
Copy link
Contributor

This commit resolves an Internal Compiler Error (ICE) caused by calling peekable on iterators with unsized item types. The issue arises due to MIR optimizations (Inline and JumpThreading) encountering invalid states when handling unsized types.

Changes:

  • Added a Self::Item: Sized bound to the peekable method in Iterator trait to prevent usage with unsized types.
  • Introduced a new test case (tests/ui/iterators/unsized-peekable.rs) to verify that attempting to use peekable with unsized types results in a proper compile-time error instead of an ICE.

This fix ensures that the compiler provides a clear and actionable error message (E0277) when peekable is used with unsized types, avoiding invalid intermediate representations during MIR optimization.

Closes #136442

This commit resolves an Internal Compiler Error (ICE) caused by calling
`peekable` on iterators with unsized item types. The issue arises due to
MIR optimizations (`Inline` and `JumpThreading`) encountering invalid
states when handling unsized types.

Changes:
  - Added a `Self::Item: Sized` bound to the `peekable` method in
    `Iterator` trait to prevent usage with unsized types.
  - Introduced a new test
    case (`tests/ui/iterators/unsized-peekable.rs`) to verify that
    attempting to use `peekable` with unsized types results in a proper
    compile-time error instead of an ICE.

This fix ensures that the compiler provides a clear and actionable error
message (`E0277`) when `peekable` is used with unsized types, avoiding
invalid intermediate representations during MIR optimization.

Signed-off-by: Charalampos Mitrodimas <[email protected]>
@rustbot
Copy link
Collaborator

rustbot commented Feb 2, 2025

r? @SparrowLii

rustbot has assigned @SparrowLii.
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

@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 2, 2025
Copy link
Contributor

@cyrgani cyrgani left a comment

Choose a reason for hiding this comment

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

This is not the right fix for this issue: for one, it would break some existing uses of peekable, and secondly, the error is not specific to Peekable and could be caused with other types too.
The actual fix would need to update the jump threading or inlining optimization logic to prevent any such errors.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#22 exporting to docker image format
#22 sending tarball 27.9s done
#22 DONE 34.2s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---
---- [ui] tests/ui/layout/unexpected-unsized-field-issue-135020.rs stdout ----

error: test compilation failed although it shouldn't!
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/layout/unexpected-unsized-field-issue-135020.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/layout/unexpected-unsized-field-issue-135020" "-A" "unused" "-A" "internal_features" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"
--- stderr -------------------------------
error[E0277]: the size for values of type `str` cannot be known at compilation time
##[error]  --> /checkout/tests/ui/layout/unexpected-unsized-field-issue-135020.rs:4:11
   |

@charmitro charmitro closed this Feb 2, 2025
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
5 participants