[ICE]: `field should be captured by immutable ref if we have an Fn instance, but it was`

未关闭
#166,476 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
18/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
rust
领域
compilers

调研方向

The panic is raised by a delayed bug in compiler/rustc_mir_transform/src/shim.rs around line 1282, inside make_shim, when building the closure-body shim for a coroutine captured by a move closure. Start by reading the code that decides how upvars are captured for the Fn-style shim and compare it with the generator_capture_2 UI test the reporter mutated. Done means the reproducer compiles without an internal compiler error and a regression test is added under tests/ui/iterators/.

由索引模型根据 Issue 内容生成。

描述

C-bug I-ICE needs-triage S-found-by-fuzzing T-compiler
Code
#![feature(iter_macro, yield_expr)]

use std::iter::iter;

fn main() {
    let f = {
        let s = Box::new("foo");
        iter! { move || {
            for c in s.chars() {
                yield c;
            }
        }}
    };
    f();
}

A mutant of tests/ui/iterators/generator_capture_2.rs.

Meta

rustc --version --verbose:

rustc 1.102.0-nightly (20fe935d5 2026-11-23)
binary: rustc
commit-hash: 20fe935d50367c0ff8872c2ebfa050c5c99d9201
commit-date: 2026-11-23
host: x86_64-unknown-linux-gnu
release: 1.102.0-nightly
LLVM version: 23.1.6
Error output

Command: rustc --edition 2024

warning: unused coroutine that must be used
  --> asdf.rs:14:5
   |
14 |     f();
   |     ^^^
   |
   = note: coroutines are lazy and do nothing unless resumed
   = note: `#[warn(unused_must_use)]` (part of `#[warn(unused)]`) on by default

warning: 1 warning emitted

note: no errors encountered even though delayed bugs were created

note: those delayed bugs will now be shown as internal compiler errors

error: internal compiler error: field should be captured by immutable ref if we have an `Fn` instance, but it was: Box<&str>
 |
 = note: delayed at /rustc-dev/20fe935d50367c0ff8872c2ebfa050c5c99d9201/compiler/rustc_mir_transform/src/shim.rs:1282:27
Backtrace

error: internal compiler error: field should be captured by immutable ref if we have an `Fn` instance, but it was: Box<&str>
 |
 = note: delayed at /rustc-dev/20fe935d50367c0ff8872c2ebfa050c5c99d9201/compiler/rustc_mir_transform/src/shim.rs:1282:27
            0: <rustc_errors::DiagCtxtInner>::emit_diagnostic
            1: <rustc_errors::DiagCtxtHandle>::emit_diagnostic
            2: <rustc_errors::diagnostic::Diag>::emit_err
            3: <rustc_errors::DiagCtxtHandle>::delayed_bug::<alloc::string::String>
            4: rustc_mir_transform::shim::make_shim
            5: <rustc_mir_transform::shim::provide::{closure#0} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_middle::ty::instance::ShimKind)>>::call_once
            6: rustc_query_impl::execution::try_execute_query::<rustc_middle::query::caches::DefaultCache<rustc_middle::ty::instance::ShimKind, rustc_middle::query::erase::ErasedData<[u8; 8]>>, false>
            7: rustc_query_impl::query_vtables::mir_shims::execute_query_non_incr::__rust_end_short_backtrace
            8: <rustc_middle::ty::context::TyCtxt>::instance_mir
            9: rustc_monomorphize::collector::items_of_instance
           10: rustc_query_impl::query_vtables::items_of_instance::invoke_provider_fn::__rust_begin_short_backtrace
           11: rustc_query_impl::execution::try_execute_query::<rustc_middle::query::caches::DefaultCache<(rustc_middle::ty::instance::Instance, rustc_middle::mono::CollectionMode), rustc_middle::query::erase::ErasedData<[u8; 32]>>, false>
           12: rustc_query_impl::query_vtables::items_of_instance::execute_query_non_incr::__rust_end_short_backtrace
           13: rustc_monomorphize::collector::collect_items_rec
           14: rustc_monomorphize::collector::collect_items_rec
           15: rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}
           16: rustc_thread_pool::scope::scope::<rustc_data_structures::sync::parallel::par_range_internal<rustc_data_structures::sync::parallel::par_for_each_in<rustc_middle::mono::MonoItem, [rustc_middle::mono::MonoItem], rustc_monomorphize::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>::{closure#1}, ()>::{closure#0}
           17: rustc_monomorphize::partitioning::collect_and_partition_mono_items
           18: rustc_query_impl::query_vtables::collect_and_partition_mono_items::invoke_provider_fn::__rust_begin_short_backtrace
           19: rustc_query_impl::execution::try_execute_query::<rustc_middle::query::caches::SingleCache<rustc_middle::query::erase::ErasedData<[u8; 24]>>, false>
           20: rustc_query_impl::query_vtables::collect_and_partition_mono_items::execute_query_non_incr::__rust_end_short_backtrace
           21: rustc_codegen_ssa::base::codegen_crate::<rustc_codegen_llvm::back::llvm_backend::LlvmCodegenBackend, rustc_codegen_llvm::back::llvm_backend::ModuleLlvm>
           22: <rustc_codegen_llvm::back::llvm_backend::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
           23: <rustc_interface::queries::Linker>::codegen_and_build_linker
           24: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::compiler_entrypoint::{closure#0}>::{closure#2}
           25: <rustc_thread_pool::job::StackJob<rustc_thread_pool::latch::LatchRef<rustc_thread_pool::latch::LockLatch>, <rustc_thread_pool::registry::Registry>::in_worker_cold<<rustc_thread_pool::thread_pool::ThreadPool>::install<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::compiler_entrypoint::{closure#0}>::{closure#2}, ()>::{closure#6}::{closure#0}::{closure#1}::{closure#0}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()> as rustc_thread_pool::job::Job>::execute
           26: <rustc_thread_pool::registry::WorkerThread>::wait_or_steal_until_cold
           27: <rustc_thread_pool::registry::ThreadBuilder>::run
           28: std::sys::backtrace::__rust_begin_short_backtrace::<<rustc_thread_pool::ThreadPoolBuilder>::build_scoped<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::compiler_entrypoint::{closure#0}>::{closure#2}, ()>::{closure#6}::{closure#0}::{closure#0}, rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::compiler_entrypoint::{closure#0}>::{closure#2}, ()>::{closure#6}::{closure#0}::{closure#1}, ()>::{closure#0}::{closure#0}::{closure#0}, ()>
           29: <std::thread::lifecycle::spawn_unchecked<<rustc_thread_pool::ThreadPoolBuilder>::build_scoped<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::compiler_entrypoint::{closure#0}>::{closure#2}, ()>::{closure#6}::{closure#0}::{closure#0}, rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::compiler_entrypoint::{closure#0}>::{closure#2}, ()>::{closure#6}::{closure#0}::{closure#1}, ()>::{closure#0}::{closure#0}::{closure#0}, ()>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
           30: <std::sys::thread::unix::Thread>::new::thread_start
           31: start_thread
                      at ./nptl/pthread_create.c:447:8
           32: clone3
                      at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:78:0
         

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: please attach the file at `/home/jb/workspace/261124-ice-report-check/tmp/rustc-ice-2026-11-24T08_53_13-296194.txt` to your bug report

note: rustc 1.102.0-nightly (20fe935d5 2026-11-23) running on x86_64-unknown-linux-gnu

query stack during panic:
end of query stack

Notes
Duplication Check
  • No open issue searched with ICE message query "field should be captured by immutable ref". github search page

@rustbot label +S-found-by-fuzzing +F-iter_macro +F-yield_expr

主要语言
Rust
星标
123k
派生
16.6k
平均合并
2 天 17 小时
30 天内合并 PR
532

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/rust 的其他 Issue

查看 rust-lang/rust 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。