Skip to content

ICE: coherence: did not expect successful goal when collecting ambiguity errors #124834

Closed
@matthiaskrgr

Description

@matthiaskrgr

auto-reduced (treereduce-rust):

pub trait Layer<C> {
    type Service;
}

pub trait ParticularService {}

pub trait ParticularServiceLayer<C>: Layer<C> {}

impl<T> ParticularServiceLayer<C> for T
where
    T: Layer<C>,
    T::Service: ParticularService,
{
}

struct ALayer<C>(C);
impl<C> Client<C>
where
    ALayer<C>: ParticularServiceLayer<C>,
{
    fn check(&self) {}
}

struct Client<Service>(C);

impl<C> Client<C> {
    fn check(&self) {}
}
original code

original:

// These are simplifications of the tower traits by the same name:

pub trait Service<Request> {
    type Response;
}

pub trait Layer<C> {
    type Service;
}

// Any type will do here:

pub struct Req;
pub struct Res;

// This is encoding a trait alias.

pub trait ParticularService:
    Service<Req, Response = Res> {
}

impl<T> ParticularService for T
where
    T: Service<Req, Response = Res>,
{
}

// This is also a trait alias.
// The weird = <Self as ...> bound is there so that users of the trait do not
// need to repeat the bounds. See https://github.com/rust-lang/rust/issues/20671
// for context, and in particular the workaround in:
// https://github.com/rust-lang/rust/issues/20671#issuecomment-529752828

pub trait ParticularServiceLayer<C>:
    Layer<C>
{
    type Service: ParticularService;
}

impl<T> ParticularServiceLayer<C> for T
where
    T: Layer<C>,
    T::Service: ParticularService,
{
    type Service = T::Service;
}

// These are types that implement the traits that the trait aliases refer to.
// They should also implement the alias traits due to the blanket impls.

struct ALayer<C>(C);
impl<C> Client<C>
where
    ALayer<C>: ParticularServiceLayer<C>,
{
    fn check(&self) {}
}

struct AService;
impl Service<Req> for AService {
    // However, AService does _not_ meet the blanket implementation,
    // since its Response type is bool, not Res as it should be.
    type Response = bool;
}

// This is a wrapper type around ALayer that uses the trait alias
// as a way to communicate the requirements of the provided types.
struct Client<Service>(C);

// The method and the free-standing function below both have the same bounds.

impl<C> Client<C>
where
    ALayer<C>: ParticularServiceLayer<C>,
{
    fn check(&self) {}
}

fn check<C>(_: C) where ALayer<C>: ParticularServiceLayer<C> {}

// But, they give very different error messages.

fn main() {
    // This gives a very poor error message that does nothing to point the user
    // at the underlying cause of why the types involved do not meet the bounds.
    Client(()).check(); //~ ERROR E0599

    // This gives a good(ish) error message that points the user at _why_ the
    // bound isn't met, and thus how they might fix it.
    check(()); //~ ERROR E0271
}

Version information

rustc 1.80.0-nightly (5ae5d1353 2024-05-07)
binary: rustc
commit-hash: 5ae5d135372c4b576edc73c191d2dc86b1d16b5c
commit-date: 2024-05-07
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc -Znext-solver=coherence

Program output

error[E0412]: cannot find type `C` in this scope
 --> /tmp/icemaker_global_tempdir.nqKA2IuAyBos/rustc_testrunner_tmpdir_reporting.Dot0Yklzc5hp/mvce.rs:9:32
  |
9 | impl<T> ParticularServiceLayer<C> for T
  |      -                         ^
  |      |
  |      similarly named type parameter `T` defined here
  |
help: a type parameter with a similar name exists
  |
9 | impl<T> ParticularServiceLayer<T> for T
  |                                ~
help: you might be missing a type parameter
  |
9 | impl<T, C> ParticularServiceLayer<C> for T
  |       +++

error[E0412]: cannot find type `C` in this scope
  --> /tmp/icemaker_global_tempdir.nqKA2IuAyBos/rustc_testrunner_tmpdir_reporting.Dot0Yklzc5hp/mvce.rs:11:14
   |
9  | impl<T> ParticularServiceLayer<C> for T
   |      - similarly named type parameter `T` defined here
10 | where
11 |     T: Layer<C>,
   |              ^
   |
help: a type parameter with a similar name exists
   |
11 |     T: Layer<T>,
   |              ~
help: you might be missing a type parameter
   |
9  | impl<T, C> ParticularServiceLayer<C> for T
   |       +++

error[E0412]: cannot find type `C` in this scope
  --> /tmp/icemaker_global_tempdir.nqKA2IuAyBos/rustc_testrunner_tmpdir_reporting.Dot0Yklzc5hp/mvce.rs:24:24
   |
24 | struct Client<Service>(C);
   |                        ^ not found in this scope
   |
help: you might be missing a type parameter
   |
24 | struct Client<Service, C>(C);
   |                      +++

error[E0601]: `main` function not found in crate `mvce`
  --> /tmp/icemaker_global_tempdir.nqKA2IuAyBos/rustc_testrunner_tmpdir_reporting.Dot0Yklzc5hp/mvce.rs:28:2
   |
28 | }
   |  ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.nqKA2IuAyBos/rustc_testrunner_tmpdir_reporting.Dot0Yklzc5hp/mvce.rs`

error: internal compiler error: compiler/rustc_trait_selection/src/solve/fulfill.rs:261:17: did not expect successful goal when collecting ambiguity errors

thread 'rustc' panicked at compiler/rustc_trait_selection/src/solve/fulfill.rs:261:17:
Box<dyn Any>
stack backtrace:
   0:     0x7a8d43d7ab35 - std::backtrace_rs::backtrace::libunwind::trace::h898f790b668aa360
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x7a8d43d7ab35 - std::backtrace_rs::backtrace::trace_unsynchronized::h37e2d3a328252c3c
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7a8d43d7ab35 - std::sys_common::backtrace::_print_fmt::h8001ddad7b617164
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7a8d43d7ab35 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::ha1863cfb25e180ba
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7a8d43dc9cfb - core::fmt::rt::Argument::fmt::hed259047e582e1e9
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/core/src/fmt/rt.rs:165:63
   5:     0x7a8d43dc9cfb - core::fmt::write::hc5409d094221c111
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/core/src/fmt/mod.rs:1157:21
   6:     0x7a8d43d6f87f - std::io::Write::write_fmt::h186f3d4838dbd4c1
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/io/mod.rs:1835:15
   7:     0x7a8d43d7a90e - std::sys_common::backtrace::_print::hdcd4a776fda551b4
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7a8d43d7a90e - std::sys_common::backtrace::print::h32eb678d18f393f0
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7a8d43d7d279 - std::panicking::default_hook::{{closure}}::h93a0745beb866f63
  10:     0x7a8d43d7cfbd - std::panicking::default_hook::h3a7275adeb4af036
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/panicking.rs:298:9
  11:     0x7a8d406b898f - std[5264994d61690aef]::panicking::update_hook::<alloc[f2c8b6b7fb69080e]::boxed::Box<rustc_driver_impl[1a38415264a51164]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7a8d43d7d976 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h2e6bab372b2f3333
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/alloc/src/boxed.rs:2036:9
  13:     0x7a8d43d7d976 - std::panicking::rust_panic_with_hook::h6f02a76ae371ed1e
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/panicking.rs:799:13
  14:     0x7a8d406e7f64 - std[5264994d61690aef]::panicking::begin_panic::<rustc_errors[4530deb080854ecc]::ExplicitBug>::{closure#0}
  15:     0x7a8d406e4ac6 - std[5264994d61690aef]::sys_common::backtrace::__rust_end_short_backtrace::<std[5264994d61690aef]::panicking::begin_panic<rustc_errors[4530deb080854ecc]::ExplicitBug>::{closure#0}, !>
  16:     0x7a8d406e0076 - std[5264994d61690aef]::panicking::begin_panic::<rustc_errors[4530deb080854ecc]::ExplicitBug>
  17:     0x7a8d406f12b1 - <rustc_errors[4530deb080854ecc]::diagnostic::BugAbort as rustc_errors[4530deb080854ecc]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  18:     0x7a8d40bb401c - rustc_middle[c1626214f4cb476]::util::bug::opt_span_bug_fmt::<rustc_span[ec3d05ef96c4664f]::span_encoding::Span>::{closure#0}
  19:     0x7a8d40b9785a - rustc_middle[c1626214f4cb476]::ty::context::tls::with_opt::<rustc_middle[c1626214f4cb476]::util::bug::opt_span_bug_fmt<rustc_span[ec3d05ef96c4664f]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  20:     0x7a8d40b976db - rustc_middle[c1626214f4cb476]::ty::context::tls::with_context_opt::<rustc_middle[c1626214f4cb476]::ty::context::tls::with_opt<rustc_middle[c1626214f4cb476]::util::bug::opt_span_bug_fmt<rustc_span[ec3d05ef96c4664f]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  21:     0x7a8d3e96fbf0 - rustc_middle[c1626214f4cb476]::util::bug::bug_fmt
  22:     0x7a8d411ed367 - <rustc_trait_selection[5e368060ce18af72]::solve::fulfill::FulfillmentCtxt as rustc_infer[52c6ee6384d84e04]::traits::engine::TraitEngine>::collect_remaining_errors
  23:     0x7a8d41db98ee - <rustc_trait_selection[5e368060ce18af72]::traits::engine::ObligationCtxt>::select_all_or_error
  24:     0x7a8d41f886d4 - rustc_trait_selection[5e368060ce18af72]::traits::coherence::overlap
  25:     0x7a8d42a1318e - rustc_trait_selection[5e368060ce18af72]::traits::coherence::overlapping_impls
  26:     0x7a8d42a12579 - <rustc_hir_analysis[bed0fd88d68629cc]::coherence::inherent_impls_overlap::InherentOverlapChecker>::check_for_overlapping_inherent_impls
  27:     0x7a8d4207cfba - rustc_hir_analysis[bed0fd88d68629cc]::coherence::inherent_impls_overlap::crate_inherent_impls_overlap_check
  28:     0x7a8d4207c88f - rustc_query_impl[76a2b42baa80c404]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[76a2b42baa80c404]::query_impl::crate_inherent_impls_overlap_check::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c1626214f4cb476]::query::erase::Erased<[u8; 1usize]>>
  29:     0x7a8d427e5025 - rustc_query_system[c302203c886e3cb8]::query::plumbing::try_execute_query::<rustc_query_impl[76a2b42baa80c404]::DynamicConfig<rustc_query_system[c302203c886e3cb8]::query::caches::SingleCache<rustc_middle[c1626214f4cb476]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[76a2b42baa80c404]::plumbing::QueryCtxt, false>
  30:     0x7a8d427e4e13 - rustc_query_impl[76a2b42baa80c404]::query_impl::crate_inherent_impls_overlap_check::get_query_non_incr::__rust_end_short_backtrace
  31:     0x7a8d41fee171 - rustc_hir_analysis[bed0fd88d68629cc]::check_crate
  32:     0x7a8d423ba647 - rustc_interface[25c67061aa4894d2]::passes::analysis
  33:     0x7a8d423ba187 - rustc_query_impl[76a2b42baa80c404]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[76a2b42baa80c404]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c1626214f4cb476]::query::erase::Erased<[u8; 1usize]>>
  34:     0x7a8d427e5025 - rustc_query_system[c302203c886e3cb8]::query::plumbing::try_execute_query::<rustc_query_impl[76a2b42baa80c404]::DynamicConfig<rustc_query_system[c302203c886e3cb8]::query::caches::SingleCache<rustc_middle[c1626214f4cb476]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[76a2b42baa80c404]::plumbing::QueryCtxt, false>
  35:     0x7a8d427e4d89 - rustc_query_impl[76a2b42baa80c404]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  36:     0x7a8d4262604e - rustc_interface[25c67061aa4894d2]::interface::run_compiler::<core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>, rustc_driver_impl[1a38415264a51164]::run_compiler::{closure#0}>::{closure#1}
  37:     0x7a8d426027c9 - std[5264994d61690aef]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[25c67061aa4894d2]::util::run_in_thread_with_globals<rustc_interface[25c67061aa4894d2]::util::run_in_thread_pool_with_globals<rustc_interface[25c67061aa4894d2]::interface::run_compiler<core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>, rustc_driver_impl[1a38415264a51164]::run_compiler::{closure#0}>::{closure#1}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>::{closure#0}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>
  38:     0x7a8d42602578 - <<std[5264994d61690aef]::thread::Builder>::spawn_unchecked_<rustc_interface[25c67061aa4894d2]::util::run_in_thread_with_globals<rustc_interface[25c67061aa4894d2]::util::run_in_thread_pool_with_globals<rustc_interface[25c67061aa4894d2]::interface::run_compiler<core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>, rustc_driver_impl[1a38415264a51164]::run_compiler::{closure#0}>::{closure#1}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>::{closure#0}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[5e4926a15c2dbf93]::result::Result<(), rustc_span[ec3d05ef96c4664f]::ErrorGuaranteed>>::{closure#2} as core[5e4926a15c2dbf93]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  39:     0x7a8d43d877bb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hcefa1c6fdca2a888
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/alloc/src/boxed.rs:2022:9
  40:     0x7a8d43d877bb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3decfc9e691a0d82
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/alloc/src/boxed.rs:2022:9
  41:     0x7a8d43d877bb - std::sys::pal::unix::thread::Thread::new::thread_start::hcc86eaf6d07d8e1c
                               at /rustc/5ae5d135372c4b576edc73c191d2dc86b1d16b5c/library/std/src/sys/pal/unix/thread.rs:108:17
  42:     0x7a8d3d4a955a - <unknown>
  43:     0x7a8d3d526a3c - <unknown>
  44:                0x0 - <unknown>

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: rustc 1.80.0-nightly (5ae5d1353 2024-05-07) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z next-solver=coherence -Z dump-mir-dir=dir

query stack during panic:
#0 [crate_inherent_impls_overlap_check] check for overlap between inherent impls defined in this crate
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0412, E0601.
For more information about an error, try `rustc --explain E0412`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-trait-system-refactorThe Rustc Trait System Refactor Initiative (-Znext-solver)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions