rust-lang / rust-lang/rust

overflow evaluating the requirement `&mut Type: Trait` or ICE with -Znext-solver

Open
#145,184 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-trait-system C-bug T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code (project to build with cargo build): overflow-bug.zip

The important parts are:

struct ElementSerializer<W: Write> {
    writer: W, // <<< NOT ref
}

impl<W: Write> serde::ser::SerializeMap for ElementSerializer<W> {
    type Ok = ();
    type Error = SeError;

    fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: ?Sized + Serialize,
    {
        value.serialize(ElementSerializer {
            writer: &mut self.writer, // <<< taking ref
        })
    }
    ...
}

fn main() {
    let mut state = ElementSerializer {
        writer: String::new(),
    };

    // all lines is important, error does not happen if use another serialization approach
    let ser = serde::__private::ser::FlatMapSerializer(&mut state);// using FlatMapSerializer is important
    let state = ser.serialize_struct_variant("Enum", 0u32, "Variant", 0).unwrap();
    state.end().unwrap()// if remove, error didn't happen
}

Note, if replace

  • writer: W by writer: &'w mut W
  • writer: &mut self.writer by writer: self.writer

the error is gone.

This is minimized example from quick-xml's serde serializer -- in this line I have to use &mut W instead of W because of this bug:
https://github.com/tafia/quick-xml/blob/ea34144ac4c66db596137acf587bb3fa4301ac03/src/se/content.rs#L62-L63

The idea of this code is to use serializer that redirect everything to the generic writer. In some cases we need to create child serializer, that shares part of the state. Because generic writer is not restricted by any traits (except std::fmt::Write), we use &mut W to share it

I expected to see this happen: successful compilation

Instead, this happened:
When -Znext-solver in not used, compilation is failed with

$ cargo build
   Compiling overflow-bug v0.1.0 (D:\Projects\overflow-bug)
error[E0275]: overflow evaluating the requirement `&mut Dummy: std::fmt::Write`
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`overflow_bug`)
   = note: required for `&mut &mut Dummy` to implement `std::fmt::Write`
   = note: 127 redundant requirements hidden
   = note: required for `&mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut ...` to implement `std::fmt::Write`
note: required for `ElementSerializer<'_, &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut ...>` to implement `Serializer`
  --> src\main.rs:45:20
   |
45 | impl<'w, W: Write> Serializer for ElementSerializer<'w, W> {
   |             -----  ^^^^^^^^^^     ^^^^^^^^^^^^^^^^^^^^^^^^
   |             |
   |             unsatisfied trait bound introduced here
   = note: the full name for the type has been written to 'D:\Projects\overflow-bug\target\debug\deps\overflow_bug.long-type-11055775272101050917.txt'
   = note: consider using `--verbose` to print the full type name to the console

For more information about this error, try `rustc --explain E0275`.
error: could not compile `overflow-bug` (bin "overflow-bug") due to 1 previous error

When using RUSTFLAGS="-Znext-solver" cargo +nightly build, the ICE happens, the full stack in the attacked archive and under spoiler at the end.

Meta

rustc --version --verbose:

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: x86_64-pc-windows-msvc
release: 1.89.0
LLVM version: 20.1.7
Backtrace

Note: this backtrace was produced without activating RUST_BACKTRACE=1. Activating it does not change the result:

thread 'rustc' panicked at compiler\rustc_middle\src\ty\normalize_erasing_regions.rs:171:13:
Box<dyn Any>
stack backtrace:
   0:     0x7ffbae5ce13e - std::backtrace_rs::backtrace::win64::trace
                               at /rustc/de3efa79f95852c7427587f1d535bfea7c0d6779/library\std\src\..\..\backtrace\src\backtrace\win64.rs:85
   1:     0x7ffbae5ce13e - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/de3efa79f95852c7427587f1d535bfea7c0d6779/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2:     0x7ffbae5ce13e - std::backtrace::Backtrace::create
                               at /rustc/de3efa79f95852c7427587f1d535bfea7c0d6779/library\std\src\backtrace.rs:331
   3:     0x7ffbae5ce07a - std::backtrace::Backtrace::force_capture
                               at /rustc/de3efa79f95852c7427587f1d535bfea7c0d6779/library\std\src\backtrace.rs:312
   4:     0x7ffbafdbdbf1 - core[308fd5da220cbda0]::slice::sort::unstable::heapsort::heapsort::<((rustc_lint_defs[7735fd7ef52282c8]::Level, &str), usize), <((rustc_lint_defs[7735fd7ef52282c8]::Level, &str), usize) as core[308fd5da220cbda0]::cmp::PartialOrd>::lt>
   5:     0x7ffbae5ed1da - std::panicking::panic_with_hook
                               at /rustc/de3efa79f95852c7427587f1d535bfea7c0d6779/library\std\src\panicking.rs:842
   6:     0x7ffbb162d4b3 - RINvNtNtNtNtCs4auBuzsMBK6_4core5slice4sort6stable9quicksort9quicksortTjRNtNtCsfxg4X3VBQGy_12rustc_errors7snippet10AnnotationENCINvMNtCs3R19SsYomuP_5alloc5sliceSB15_11sort_by_keyTINtNtBa_3cmp7ReversejEbENCNCNvMs4_NtB1c_7emitterNtB3o_12HumanEmitter18render_s
   7:     0x7ffbb1622269 - std[b028560bea50fc7d]::sys::backtrace::__rust_end_short_backtrace::<std[b028560bea50fc7d]::panicking::begin_panic<rustc_errors[b4f66901acd1a2e8]::ExplicitBug>::{closure#0}, !>
   8:     0x7ffbb1622248 - std[b028560bea50fc7d]::panicking::begin_panic::<rustc_errors[b4f66901acd1a2e8]::ExplicitBug>
   9:     0x7ffbb1639d65 - <rustc_errors[b4f66901acd1a2e8]::diagnostic::BugAbort as rustc_errors[b4f66901acd1a2e8]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  10:     0x7ffbb15870b9 - rustc_middle[4d2ab591be2d6cb7]::util::bug::span_bug_fmt::<rustc_span[13b5939d1f839a47]::span_encoding::Span>
  11:     0x7ffbb155d32d - <rustc_middle[4d2ab591be2d6cb7]::ty::consts::Const>::walk
  12:     0x7ffbb155d0f6 - <rustc_middle[4d2ab591be2d6cb7]::ty::consts::Const>::walk
  13:     0x7ffbb1586fb2 - rustc_middle[4d2ab591be2d6cb7]::util::bug::bug_fmt
  14:     0x7ffbafc182b9 - <rustc_middle[4d2ab591be2d6cb7]::ty::normalize_erasing_regions::NormalizeAfterErasingRegionsFolder as rustc_type_ir[896ecbb9bf80b18d]::fold::TypeFolder<rustc_middle[4d2ab591be2d6cb7]::ty::context::TyCtxt>>::fold_ty
  15:     0x7ffbaea78258 - rustc_monomorphize[8799e712d668ce6e]::mono_checks::check_mono_item
  16:     0x7ffbaf553ea3 - rustc_query_impl[dd24cba0b59fe983]::plumbing::query_key_hash_verify_all
  17:     0x7ffbaf46aa35 - RINvNtNtCs7JHiGDUvQrx_18rustc_query_system5query8plumbing17try_execute_queryINtCsiZ8RikB5PkR_16rustc_query_impl13DynamicConfigINtNtB4_6caches12DefaultCacheNtNtNtCs6CKWPQDzIa1_12rustc_middle2ty8instance8InstanceINtNtNtB2z_5query5erase6ErasedAhj0_EEKb0_KB3Y_
  18:     0x7ffbaf565ec6 - rustc_query_impl[dd24cba0b59fe983]::plumbing::query_key_hash_verify_all
  19:     0x7ffbaea71ada - rustc_monomorphize[8799e712d668ce6e]::collector::items_of_instance
  20:     0x7ffbaf554687 - rustc_query_impl[dd24cba0b59fe983]::plumbing::query_key_hash_verify_all
  21:     0x7ffbaf484e1a - RINvNtNtCs7JHiGDUvQrx_18rustc_query_system5query8plumbing17try_execute_queryINtCsiZ8RikB5PkR_16rustc_query_impl13DynamicConfigINtNtB4_6caches12DefaultCacheTNtNtNtCs6CKWPQDzIa1_12rustc_middle2ty8instance8InstanceNtNtNtB2A_3mir4mono14CollectionModeEINtNtNtB2
  22:     0x7ffbaf566135 - rustc_query_impl[dd24cba0b59fe983]::plumbing::query_key_hash_verify_all
  23:     0x7ffbaea915a9 - rustc_monomorphize[8799e712d668ce6e]::partitioning::collect_and_partition_mono_items
(repeated many times)
 395:     0x7ffbaea938c5 - rustc_monomorphize[8799e712d668ce6e]::partitioning::collect_and_partition_mono_items
 396:     0x7ffbade5738f - RINvNtNtNtNtCs4auBuzsMBK6_4core5slice4sort6stable9quicksort9quicksortNtNtNtCs6CKWPQDzIa1_12rustc_middle3mir4mono11CodegenUnitNCINvMNtCs3R19SsYomuP_5alloc5sliceSB15_7sort_byNCNvNtCsbDNIxMdcH4o_18rustc_monomorphize12partitioning19merge_codegen_unitss4_0E0EB2
 397:     0x7ffbaea841db - rustc_monomorphize[8799e712d668ce6e]::partitioning::collect_and_partition_mono_items
 398:     0x7ffbae187757 - <alloc[2ce738592d37a551]::sync::Arc<rustc_session[e1a36a7c714f07f1]::config::OutputFilenames>>::drop_slow
 399:     0x7ffbae16868d - <alloc[2ce738592d37a551]::sync::Arc<rustc_session[e1a36a7c714f07f1]::config::OutputFilenames>>::drop_slow
 400:     0x7ffbae0861c1 - RINvNtNtCs7JHiGDUvQrx_18rustc_query_system5query8plumbing17try_execute_queryINtCsiZ8RikB5PkR_16rustc_query_impl13DynamicConfigINtNtB4_6caches11SingleCacheINtNtNtCs6CKWPQDzIa1_12rustc_middle5query5erase6ErasedAhj28_EEKb0_KB3t_KB3t_ENtNtB1f_8plumbing9QueryCt
 401:     0x7ffbb0dfeac0 - <rustc_span[13b5939d1f839a47]::def_id::DefIndex as rustc_query_impl[dd24cba0b59fe983]::profiling_support::SpecIntoSelfProfilingString>::spec_to_self_profile_string
 402:     0x7ffbaaa881ac - std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::_Construct_n<std::basic_string<char,std::char_traits<char>,std::allocator<char> > * __
 403:     0x7ffbaaaed8b3 - <rustc_codegen_llvm[f861d3fc921da346]::LlvmCodegenBackend as rustc_codegen_ssa[77247dce9a7752a6]::traits::backend::CodegenBackend>::codegen_crate
 404:     0x7ffbaaa70a17 - <rustc_interface[6fd35e3621401be0]::queries::Linker>::codegen_and_build_linker
 405:     0x7ffbaa9f7ea1 - std[b028560bea50fc7d]::sys::backtrace::__rust_begin_short_backtrace::<<std[b028560bea50fc7d]::thread::Builder>::spawn_unchecked_<ctrlc[51cec7ca00d482d9]::set_handler_inner<rustc_driver_impl[4363688e7eded069]::install_ctrlc_handler::{closure#0}>::{closure#0}, ()>::{closure#1}::{closure#0}::{closure#0}, ()>
 406:     0x7ffbaa9f253d - std[b028560bea50fc7d]::sys::backtrace::__rust_begin_short_backtrace::<<std[b028560bea50fc7d]::thread::Builder>::spawn_unchecked_<ctrlc[51cec7ca00d482d9]::set_handler_inner<rustc_driver_impl[4363688e7eded069]::install_ctrlc_handler::{closure#0}>::{closure#0}, ()>::{closure#1}::{closure#0}::{closure#0}, ()>
 407:     0x7ffbaa9ee70f - RINvNtNtCsf7GjOwGFsjp_3std3sys9backtrace28___rust_begin_short_backtraceNCNCINvNtCs9Bfd6sTAGXs_15rustc_interface4util26run_in_thread_with_globalsNCINvB1e_31run_in_thread_pool_with_globalsNCINvNtB1g_9interface12run_compileruNCNvCs5MHNsti0jgX_17rustc_driver_i
 408:     0x7ffbaa9fc0fd - std[b028560bea50fc7d]::sys::backtrace::__rust_begin_short_backtrace::<<std[b028560bea50fc7d]::thread::Builder>::spawn_unchecked_<ctrlc[51cec7ca00d482d9]::set_handler_inner<rustc_driver_impl[4363688e7eded069]::install_ctrlc_handler::{closure#0}>::{closure#0}, ()>::{closure#1}::{closure#0}::{closure#0}, ()>
 409:     0x7ffbae5f0d2d - alloc::boxed::impl$28::call_once
                               at /rustc/de3efa79f95852c7427587f1d535bfea7c0d6779/library\alloc\src\boxed.rs:1971
 410:     0x7ffbae5f0d2d - alloc::boxed::impl$28::call_once
                               at /rustc/de3efa79f95852c7427587f1d535bfea7c0d6779/library\alloc\src\boxed.rs:1971
 411:     0x7ffbae5f0d2d - std::sys::pal::windows::thread::impl$0::new::thread_start
                               at /rustc/de3efa79f95852c7427587f1d535bfea7c0d6779/library\std\src\sys\pal\windows\thread.rs:60
 412:     0x7ffc1ace7374 - BaseThreadInitThunk
 413:     0x7ffc1ccfcc91 - RtlUserThreadStart

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the attached overflow-bug.zip and reproduce the failure from src/main.rs using cargo build, then compare it with RUSTFLAGS="-Znext-solver" cargo +nightly build. Review the related quick-xml serializer context at src/se/content.rs lines 62-63. Done means the reproducer no longer reports the trait overflow or ICE, with regression coverage for both solver modes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.