Skip to content

ICE: Same capture can't be ByUse and ByValue at the same time #141916

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

Open
cushionbadak opened this issue Jun 2, 2025 · 1 comment
Open

ICE: Same capture can't be ByUse and ByValue at the same time #141916

cushionbadak opened this issue Jun 2, 2025 · 1 comment
Labels
C-bug Category: This is a bug. F-ergonomic_clones `#![feature(ergonomic_clones)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cushionbadak
Copy link

cushionbadak commented Jun 2, 2025

Code

#![allow(incomplete_features)]
#![feature(ergonomic_clones)]

use std::clone::UseCloned;

#[derive(Clone)]
struct Foo;

impl UseCloned for Foo {}

fn do_not_move_test(x: Foo) -> Foo { async {
    let s = x.use;
    x
} }

fn main() {}

A mutant of tests/ui/ergonomic-clones/dotuse/basic.rs

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (99e7c15e8 2025-06-01)
binary: rustc
commit-hash: 99e7c15e81385b38a8186b51edc4577d5d7b5bdd
commit-date: 2025-06-01
host: aarch64-apple-darwin
release: 1.89.0-nightly
LLVM version: 20.1.5

Error output

command: rustc --edition=2024

--edition=2021 also ICEs

error[E0308]: mismatched types
  --> afidt_3.rs:11:38
   |
11 |   fn do_not_move_test(x: Foo) -> Foo { async {
   |  ________________________________---___^
   | |                                |
   | |                                expected `Foo` because of return type
12 | |     let s = x.use;
13 | |     x
14 | | } }
   | |_^ expected `Foo`, found `async` block
   |
   = note:     expected struct `Foo`
           found `async` block `{async block@afidt_3.rs:11:38: 11:43}`

error: internal compiler error: compiler/rustc_hir_typeck/src/upvar.rs:2431:17: Same capture can't be ByUse and ByValue at the same time


thread 'rustc' panicked at compiler/rustc_hir_typeck/src/upvar.rs:2431:17:
Box<dyn Any>
Backtrace

thread 'rustc' panicked at compiler/rustc_hir_typeck/src/upvar.rs:2431:17:
Box<dyn Any>
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::diagnostic::BugAbort as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   2: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   3: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   4: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
   5: rustc_middle::util::bug::bug_fmt
   6: rustc_hir_typeck::upvar::determine_capture_info
   7: <rustc_hir_typeck::fn_ctxt::FnCtxt>::compute_min_captures
   8: <rustc_hir_typeck::fn_ctxt::FnCtxt>::analyze_closure
   9: <rustc_hir_typeck::upvar::InferBorrowKindVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  10: rustc_hir_typeck::typeck_with_inspect::{closure#0}
  11: rustc_hir_typeck::typeck
      [... omitted 1 frame ...]
  12: <rustc_middle::ty::context::TyCtxt>::par_hir_body_owners::<rustc_hir_analysis::check_crate::{closure#1}>::{closure#0}
  13: rustc_hir_analysis::check_crate
  14: rustc_interface::passes::run_required_analyses
  15: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  16: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>
  17: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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 `/Users/jisukbyun/workspace/250203 scratch/rustc-ice-2025-06-02T14_11_03-89049.txt` to your bug report

query stack during panic:
#0 [typeck] type-checking `do_not_move_test`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

Bisects to

searched nightlies: from nightly-2025-01-01 to nightly-2025-06-01
regressed nightly: nightly-2025-03-08
searched commit range: b74da96...f5a1ef7
regressed commit: f5a1ef7

bisected with cargo-bisect-rustc v0.6.9

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --end=2025-06-01 --regress ice --preserve --script rustc -- afidt_3.rs --edition=2024
********************************************************************************
Regression in f5a1ef7121ad661b5a21a1d02941c8064d54ee0b
********************************************************************************

Attempting to search unrolled perf builds
Found commits ["5cf66023", "16414e51", "0b6c069b", "aa349152", "275aad33", "eaf94402"]
installing 5cf660233fcc08fce21f8845d36982b25cb0535c
rust-std-nightly-x86_64-unknown-linux-gnu: 30.69 MB / 30.69 MB [==================================] 100.00 % 12.52 MB/s testing...
RESULT: 5cf660233fcc08fce21f8845d36982b25cb0535c, ===> Script found ICE

Regression in https://github.com/rust-lang-ci/rust/commit/5cf660233fcc08fce21f8845d36982b25cb0535c
The PR introducing the regression in this rollup is #134797: Ergonomic ref counting

Notes

  • ICE location: compiler/rustc_hir_typeck/src/upvar.rs Line-2431
    } else {
    // We select the CaptureKind which ranks higher based the following priority order:
    // (ByUse | ByValue) > MutBorrow > UniqueImmBorrow > ImmBorrow
    match (capture_info_a.capture_kind, capture_info_b.capture_kind) {
    (ty::UpvarCapture::ByUse, ty::UpvarCapture::ByValue)
    | (ty::UpvarCapture::ByValue, ty::UpvarCapture::ByUse) => {
    bug!("Same capture can't be ByUse and ByValue at the same time")
    }
    (ty::UpvarCapture::ByValue, ty::UpvarCapture::ByValue)
    | (ty::UpvarCapture::ByUse, ty::UpvarCapture::ByUse)
    | (ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse, ty::UpvarCapture::ByRef(_)) => {
    capture_info_a
    }

@rustbot label F-ergonomic_clones

@cushionbadak cushionbadak added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Jun 2, 2025
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. F-ergonomic_clones `#![feature(ergonomic_clones)]` labels Jun 2, 2025
@cushionbadak
Copy link
Author

Another ICE code without other errors:

#![allow(incomplete_features)]
#![feature(ergonomic_clones)]

use std::clone::UseCloned;

impl UseCloned for U {}

#[derive(Clone)]
struct S;

#[derive(Clone)]
struct T(S, S);

#[derive(Clone)]
struct U(T, T);

fn foo() {
    let u = U(T(S, S), T(S, S));
    || {
        let _x = u.0.0;
        let _x = ((u).use).0.1;
    };
}

fn main() {}

A mutant of tests/ui/closures/2229_closure_analysis/migrations/precise.rs

Error Output

error: internal compiler error: compiler/rustc_hir_typeck/src/upvar.rs:2431:17: Same capture can't be ByUse and ByValue at the same time


thread 'rustc' panicked at compiler/rustc_hir_typeck/src/upvar.rs:2431:17:
Box<dyn Any>
Backtrace

thread 'rustc' panicked at compiler/rustc_hir_typeck/src/upvar.rs:2431:17:
Box<dyn Any>
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::diagnostic::BugAbort as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   2: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   3: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   4: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
   5: rustc_middle::util::bug::bug_fmt
   6: rustc_hir_typeck::upvar::determine_capture_info
   7: <alloc::vec::Vec<rustc_middle::ty::closure::CapturedPlace>>::retain::<<rustc_hir_typeck::fn_ctxt::FnCtxt>::compute_min_captures::{closure#0}>::{closure#0}
   8: <rustc_hir_typeck::fn_ctxt::FnCtxt>::compute_min_captures
   9: <rustc_hir_typeck::fn_ctxt::FnCtxt>::analyze_closure
  10: <rustc_hir_typeck::upvar::InferBorrowKindVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  11: <rustc_hir_typeck::upvar::InferBorrowKindVisitor as rustc_hir::intravisit::Visitor>::visit_expr
  12: rustc_hir_typeck::typeck_with_inspect::{closure#0}
  13: rustc_hir_typeck::typeck
      [... omitted 1 frame ...]
  14: <rustc_middle::ty::context::TyCtxt>::par_hir_body_owners::<rustc_hir_analysis::check_crate::{closure#1}>::{closure#0}
  15: rustc_hir_analysis::check_crate
  16: rustc_interface::passes::run_required_analyses
  17: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  18: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>
  19: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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 `/Users/jisukbyun/workspace/250203 scratch/rustc-ice-2025-06-03T10_40_01-97032.txt` to your bug report

query stack during panic:
#0 [typeck] type-checking `foo`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 1 previous error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-ergonomic_clones `#![feature(ergonomic_clones)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants