Skip to content
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

compiler/rustc_borrowck/src/borrow_set.rs:250:17: found two uses for 2-phase borrow temporary #128097

Open
iduanyingjie opened this issue Jul 23, 2024 · 6 comments
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. F-explicit_tail_calls `#![feature(explicit_tail_calls)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@iduanyingjie
Copy link

Code

trait Cut {
    fn floor(&mut self) -> (Option<TransDataType>, Bson)
    where
        Self: Serialize,
    {
        match serde_json::to_string(&self) {
            Ok(json) => match json.len() {
                len if len > MAX_VARCHAR_SIZE => {
                    (Some(TransDataType::Varchar(MAX_VARCHAR_SIZE)), self.cut())
                }
                len => (
                    Some(TransDataType::Varchar(varchar_size(len))),
                    Bson::String(json),
                ),
            },
            Err(e) => {
                tracing::error!(?e);
                (None, Bson::Null)
            }
        }
    }
    fn cut(&mut self) -> Bson;
}

impl Cut for Vec<Bson> {
    /// When the size of the JSON array string exceeds 65535, it needs to be trimmed so that the length is less than 65535;<br>
    /// Discard the largest element in the array, and then recursively repeat the judgment
    fn cut(&mut self) -> Bson {
        let remove_index = self
            .iter()
            .enumerate()
            .map(|(idx, value)| {
                (
                    idx,
                    serde_json::to_string(value)
                        .map(|s| s.len())
                        .unwrap_or(usize::MAX),
                )
            })
            .max_by(|a, b| a.1.cmp(&b.1))
            .map(|(idx, _)| idx);

        if let Some(remove_index) = remove_index {
            let remove_value = self.remove(remove_index);
            tracing::warn!(?remove_index, ?remove_value, "Cut the BSON array length");
        }

        match serde_json::to_string(&self) {
            Ok(json) if json.len() > MAX_VARCHAR_SIZE => become self.cut(),
            Ok(json) => Bson::String(json),
            Err(e) => {
                tracing::error!(?e);
                Bson::Null
            }
        }
    }
}

impl Cut for Document {
    /// When the size of the JSON object string exceeds 65535, it needs to be trimmed so that the length is less than 65535;<br>
    /// Discard the largest element in the array, and then recursively repeat the judgment
    fn cut(&mut self) -> Bson {
        let replace_key = self
            .iter()
            .map(|(key, value)| {
                (
                    key,
                    serde_json::to_string(value)
                        .map(|s| s.len())
                        .unwrap_or(usize::MAX),
                )
            })
            .max_by(|a, b| a.1.cmp(&b.1))
            .map(|(k, _)| k.to_string());

        if let Some(replace_key) = replace_key {
            tracing::warn!(
                "Replace the value of the largest field: {} in the BSON object as ...",
                replace_key
            );
            let _ = self.insert(replace_key, Bson::String("...".to_string()));
        }

        match serde_json::to_string(&self) {
            Ok(json) if json.len() > MAX_VARCHAR_SIZE => become self.cut(),
            Ok(json) => Bson::String(json),
            Err(e) => {
                tracing::error!(?e);
                Bson::Null
            }
        }
    }
}

Meta

rustc --version --verbose:

rustc 1.82.0-nightly (cefe1dcef 2024-07-22)
binary: rustc
commit-hash: cefe1dcef0e21f4d0c8ea856ad61c1936dfb7913
commit-date: 2024-07-22
host: x86_64-unknown-linux-gnu
release: 1.82.0-nightly
LLVM version: 18.1.7

Error output

internal compiler error: compiler/rustc_borrowck/src/borrow_set.rs:250:17: found two uses for 2-phase borrow temporary _179: bb204[0] and bb205[0]
Backtrace

warning: the feature `explicit_tail_calls` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/lib.rs:8:12
  |
8 | #![feature(explicit_tail_calls)]
  |            ^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #112788 <https://github.com/rust-lang/rust/issues/112788> for more information
  = note: `#[warn(incomplete_features)]` on by default

error: internal compiler error: compiler/rustc_borrowck/src/borrow_set.rs:250:17: found two uses for 2-phase borrow temporary _234: bb210[0] and bb211[0]
   --> src/trans/bson.rs:142:58
    |
142 |             Ok(json) if json.len() > MAX_VARCHAR_SIZE => become self.cut(),
    |                                                          ^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at compiler/rustc_borrowck/src/borrow_set.rs:250: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_errors::DiagCtxtHandle>::span_bug::<rustc_span::span_encoding::Span, alloc::string::String>
   3: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   4: 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::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}, !>
   6: rustc_middle::util::bug::span_bug_fmt::<rustc_span::span_encoding::Span>
   7: <rustc_borrowck::borrow_set::BorrowSet>::build
   8: rustc_borrowck::do_mir_borrowck
      [... omitted 1 frame ...]
   9: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  10: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, 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 `/home/iduanyingjie/Projects/rust/data-mongodb2redshift/rustc-ice-2024-07-23T13_38_17-80983.txt` to your bug report

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [mir_borrowck] borrow-checking `trans::bson::<impl at src/trans/bson.rs:118:1: 118:23>::cut`
#1 [analysis] running analysis passes on this crate
end of query stack
error: internal compiler error: compiler/rustc_borrowck/src/borrow_set.rs:250:17: found two uses for 2-phase borrow temporary _179: bb204[0] and bb205[0]
   --> src/trans/bson.rs:178:58
    |
178 |             Ok(json) if json.len() > MAX_VARCHAR_SIZE => become self.cut(),
    |                                                          ^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at compiler/rustc_borrowck/src/borrow_set.rs:250: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_errors::DiagCtxtHandle>::span_bug::<rustc_span::span_encoding::Span, alloc::string::String>
   3: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   4: 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::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}, !>
   6: rustc_middle::util::bug::span_bug_fmt::<rustc_span::span_encoding::Span>
   7: <rustc_borrowck::borrow_set::BorrowSet>::build
   8: rustc_borrowck::do_mir_borrowck
      [... omitted 1 frame ...]
   9: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  10: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, 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 `/home/iduanyingjie/Projects/rust/data-mongodb2redshift/rustc-ice-2024-07-23T13_38_17-80983.txt` to your bug report

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [mir_borrowck] borrow-checking `trans::bson::<impl at src/trans/bson.rs:152:1: 152:22>::cut`
#1 [analysis] running analysis passes on this crate
end of query stack
warning: `mongodb2redshift` (lib) generated 1 warning
error: could not compile `mongodb2redshift` (lib); 1 warning emitted

@iduanyingjie iduanyingjie added C-bug Category: This is a bug. 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. labels Jul 23, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 23, 2024
@theemathas
Copy link
Contributor

Could you provide enough code that would be needed to compile and reproduce the issue?

@rustbot labels +S-needs-repro

@rustbot rustbot added the S-needs-repro Status: This issue has no reproduction and needs a reproduction to make progress. label Jul 23, 2024
@tgross35 tgross35 added A-borrow-checker Area: The borrow checker E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 23, 2024
@matthiaskrgr matthiaskrgr added the F-explicit_tail_calls `#![feature(explicit_tail_calls)]` label Jul 24, 2024
@WaffleLapkin
Copy link
Member

@iduanyingjie note that explicit_tail_calls is an incomplete feature and not ready yet for a real use. I'd recommend you do not use it until it is properly implemented.

still, it would be nice to see a reproducible example, as this is not an error I'd expect to see.

@cyrgani
Copy link
Contributor

cyrgani commented Jul 31, 2024

Reduced example:

#![feature(explicit_tail_calls)]
fn cut(x: &mut ()) {
    if let Some(_) = Some(Box::new(())) {
        become cut(x);
    }
}

@lqd
Copy link
Member

lqd commented Jul 31, 2024

The repro ICEs on beta even without a feature gate.

@lqd lqd removed E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example S-needs-repro Status: This issue has no reproduction and needs a reproduction to make progress. labels Jul 31, 2024
@WaffleLapkin
Copy link
Member

@lqd the ICEs without a feature gate is more a general but that we continue complication even if the gate is not enabled, not necessarily specific to explicit tail calls

@WaffleLapkin
Copy link
Member

WaffleLapkin commented Aug 1, 2024

Minimized a little bit more:

#![feature(explicit_tail_calls)]
fn f(x: &mut ()) {
    let _y = String::new();
    become f(x);
}

workingjubilee added a commit to workingjubilee/rustc that referenced this issue Aug 31, 2024
add crashtests for several old unfixed ICEs

Adds several new crashtests for some older ICEs that did not yet have any.
Tests were added for rust-lang#128097, rust-lang#119095, rust-lang#117460 and rust-lang#126443.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 1, 2024
add crashtests for several old unfixed ICEs

Adds several new crashtests for some older ICEs that did not yet have any.
Tests were added for rust-lang#128097, rust-lang#119095, rust-lang#117460 and rust-lang#126443.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Sep 1, 2024
Rollup merge of rust-lang#129780 - cyrgani:master, r=compiler-errors

add crashtests for several old unfixed ICEs

Adds several new crashtests for some older ICEs that did not yet have any.
Tests were added for rust-lang#128097, rust-lang#119095, rust-lang#117460 and rust-lang#126443.
@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. F-explicit_tail_calls `#![feature(explicit_tail_calls)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. 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

8 participants