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

min_const_generics infers the wrong type depending on trait bounds in scope #104044

Closed
fee1-dead opened this issue Nov 6, 2022 · 2 comments
Closed
Labels
A-const-generics Area: const generics (parameters and arguments) A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fee1-dead
Copy link
Member

I tried this code: play

pub struct Optional<const IS_PRESENT: usize, T> where (): UsizeBool<IS_PRESENT> {
    inner: [T; IS_PRESENT]
}

pub trait UsizeBool<const X: usize> {}

impl UsizeBool<0> for () {}
impl UsizeBool<1> for () {}

impl<T> Optional<1, T> {
    pub fn some(x: T) -> Self {
        Self { inner: [x] } 
    }
}

pub struct Main<const A: usize, const B: usize> where (): UsizeBool<A> + UsizeBool<B> {
    // A is used as a field in the actual code, but irrelevant to this error
    value: Optional<B, u32>,
}

impl<const A: usize> Main<A, 0> where (): UsizeBool<A> /* + UsizeBool<1> */ {
    pub fn foo(mut self, x: u32) -> Main<A, 1> {
        Main {
            value: Optional::some(x),
        }
    }
}

I expected to see this happen: successfully compiles

Instead, this happened: compiler errors

error[E0308]: mismatched types
  --> src/lib.rs:25:20
   |
25 |             value: Optional::some(x),
   |                    ^^^^^^^^^^^^^^^^^ expected `A`, found `1`
   |
   = note: expected struct `Optional<_, A>`
              found struct `Optional<_, 1>`

error[E0308]: mismatched types
  --> src/lib.rs:24:9
   |
23 |       pub fn foo(mut self, x: u32) -> Main<A, 1> {
   |                                       ---------- expected `Main<A, 1>` because of return type
24 | /         Main {
25 | |             value: Optional::some(x),
26 | |         }
   | |_________^ expected `1`, found `A`
   |
   = note: expected struct `Main<_, 1>`
              found struct `Main<_, A>`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to 2 previous errors

If I uncomment the + UsizeBool<1> bound, it would compile successfully.

Meta

rustc --version --verbose:

rustc 1.66.0-nightly (4b8f43199 2022-10-19)
binary: rustc
commit-hash: 4b8f4319954ff2642690b9e5cbe4af352d095bf6
commit-date: 2022-10-19
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.2
Backtrace

rustc a.rs --crate-type lib -Z treat-err-as-bug
error: internal compiler error: `InferCtxt` incorrectly tainted by errors

thread 'rustc' panicked at 'aborting due to `-Z treat-err-as-bug=1`', compiler/rustc_errors/src/lib.rs:1575:30
stack backtrace:
   0:     0x7f6fdae3b6e0 - std::backtrace_rs::backtrace::libunwind::trace::h2119464896b45b57
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   1:     0x7f6fdae3b6e0 - std::backtrace_rs::backtrace::trace_unsynchronized::h99b1d9b13310118f
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f6fdae3b6e0 - std::sys_common::backtrace::_print_fmt::hbe6d08ff752ef116
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/sys_common/backtrace.rs:65:5
   3:     0x7f6fdae3b6e0 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h6dd4a5ce328f2e15
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f6fdae975fe - core::fmt::write::h4593006cca976c9b
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/core/src/fmt/mod.rs:1209:17
   5:     0x7f6fdae2b855 - std::io::Write::write_fmt::h75d064e88170c1b4
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/io/mod.rs:1682:15
   6:     0x7f6fdae3b4a5 - std::sys_common::backtrace::_print::hfe138f63dad5fb4c
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/sys_common/backtrace.rs:47:5
   7:     0x7f6fdae3b4a5 - std::sys_common::backtrace::print::he836ff7168a1f810
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/sys_common/backtrace.rs:34:9
   8:     0x7f6fdae3e2af - std::panicking::default_hook::{{closure}}::h4a0ff709ff17404d
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/panicking.rs:267:22
   9:     0x7f6fdae3dfea - std::panicking::default_hook::h45465b3e5f51d8d9
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/panicking.rs:286:9
  10:     0x7f6fdd7bd811 - <rustc_driver[2feadf6a045f69f8]::DEFAULT_HOOK::{closure#0}::{closure#0} as core[b141a5f448cbfdbc]::ops::function::FnOnce<(&core[b141a5f448cbfdbc]::panic::panic_info::PanicInfo,)>>::call_once::{shim:vtable#0}
  11:     0x7f6fdae3ead9 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h2f9ca2b8279bca39
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/alloc/src/boxed.rs:2001:9
  12:     0x7f6fdae3ead9 - std::panicking::rust_panic_with_hook::he67ffd9c867b0e23
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/panicking.rs:692:13
  13:     0x7f6fdae3e811 - std::panicking::begin_panic_handler::{{closure}}::hd5726cc546d97607
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/panicking.rs:577:13
  14:     0x7f6fdae3bb8c - std::sys_common::backtrace::__rust_end_short_backtrace::h1ec30e3932d3b5f1
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/sys_common/backtrace.rs:137:18
  15:     0x7f6fdae3e572 - rust_begin_unwind
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/panicking.rs:575:5
  16:     0x7f6fdae93fe3 - core::panicking::panic_fmt::h64e692afa97ebc63
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/core/src/panicking.rs:65:14
  17:     0x7f6fdc24ae93 - <rustc_errors[3c325fb77ff98e3b]::HandlerInner>::panic_if_treat_err_as_bug
  18:     0x7f6fdc248fbe - <rustc_errors[3c325fb77ff98e3b]::HandlerInner>::emit_diagnostic
  19:     0x7f6fde713141 - <rustc_errors[3c325fb77ff98e3b]::HandlerInner>::emit_diag_at_span::<rustc_span[f76be64242de1050]::span_encoding::Span>
  20:     0x7f6fde713265 - <rustc_errors[3c325fb77ff98e3b]::HandlerInner>::span_bug::<rustc_span[f76be64242de1050]::span_encoding::Span, &str>
  21:     0x7f6fde712ed3 - <rustc_errors[3c325fb77ff98e3b]::Handler>::delay_span_bug::<rustc_span[f76be64242de1050]::span_encoding::Span, &str>
  22:     0x7f6fde74e32d - <rustc_infer[d3358ef915d99f18]::infer::InferCtxt>::set_tainted_by_errors
  23:     0x7f6fdc477334 - <rustc_hir_analysis[4ff8e3b274fef4a]::check::fn_ctxt::FnCtxt>::demand_coerce_diag
  24:     0x7f6fdc4725ec - <rustc_hir_analysis[4ff8e3b274fef4a]::check::fn_ctxt::FnCtxt>::check_expr_struct_fields
  25:     0x7f6fdc3a8b45 - <rustc_hir_analysis[4ff8e3b274fef4a]::check::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  26:     0x7f6fdc3d1e1f - <rustc_hir_analysis[4ff8e3b274fef4a]::check::fn_ctxt::FnCtxt>::check_block_with_expected
  27:     0x7f6fdc3a6711 - <rustc_hir_analysis[4ff8e3b274fef4a]::check::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  28:     0x7f6fdcd43636 - <rustc_hir_analysis[4ff8e3b274fef4a]::check::fn_ctxt::FnCtxt>::check_return_expr
  29:     0x7f6fdcd39c9c - rustc_hir_analysis[4ff8e3b274fef4a]::check::check::check_fn
  30:     0x7f6fdcd2a0f4 - <rustc_hir_analysis[4ff8e3b274fef4a]::check::inherited::InheritedBuilder>::enter::<rustc_hir_analysis[4ff8e3b274fef4a]::check::typeck_with_fallback<rustc_hir_analysis[4ff8e3b274fef4a]::check::typeck::{closure#0}>::{closure#1}, &rustc_middle[d605453e4543da8c]::ty::context::TypeckResults>
  31:     0x7f6fdcd28fbc - rustc_hir_analysis[4ff8e3b274fef4a]::check::typeck
  32:     0x7f6fdcfbc495 - rustc_query_system[e96ca7e2da71ed3f]::query::plumbing::try_execute_query::<rustc_query_impl[cd5064426dba224b]::plumbing::QueryCtxt, rustc_query_system[e96ca7e2da71ed3f]::query::caches::DefaultCache<rustc_span[f76be64242de1050]::def_id::LocalDefId, &rustc_middle[d605453e4543da8c]::ty::context::TypeckResults>>
  33:     0x7f6fdcfbc19e - <rustc_query_impl[cd5064426dba224b]::Queries as rustc_middle[d605453e4543da8c]::ty::query::QueryEngine>::typeck
  34:     0x7f6fdd4f7507 - rustc_data_structures[1cc047c6d78b3c56]::sync::par_for_each_in::<&[rustc_span[f76be64242de1050]::def_id::LocalDefId], <rustc_middle[d605453e4543da8c]::hir::map::Map>::par_body_owners<rustc_hir_analysis[4ff8e3b274fef4a]::check::typeck_item_bodies::{closure#0}>::{closure#0}>
  35:     0x7f6fdd4f72c3 - rustc_hir_analysis[4ff8e3b274fef4a]::check::typeck_item_bodies
  36:     0x7f6fdd4057ca - rustc_query_system[e96ca7e2da71ed3f]::query::plumbing::try_execute_query::<rustc_query_impl[cd5064426dba224b]::plumbing::QueryCtxt, rustc_query_system[e96ca7e2da71ed3f]::query::caches::DefaultCache<(), ()>>
  37:     0x7f6fdd4054f7 - rustc_query_system[e96ca7e2da71ed3f]::query::plumbing::get_query::<rustc_query_impl[cd5064426dba224b]::queries::typeck_item_bodies, rustc_query_impl[cd5064426dba224b]::plumbing::QueryCtxt>
  38:     0x7f6fdd1a8eff - <rustc_session[407507c50234e03]::session::Session>::time::<(), rustc_hir_analysis[4ff8e3b274fef4a]::check_crate::{closure#7}>
  39:     0x7f6fdd1a8acf - rustc_hir_analysis[4ff8e3b274fef4a]::check_crate
  40:     0x7f6fdd1a85d7 - rustc_interface[a32f49e72690e02b]::passes::analysis
  41:     0x7f6fdd549354 - rustc_query_system[e96ca7e2da71ed3f]::query::plumbing::try_execute_query::<rustc_query_impl[cd5064426dba224b]::plumbing::QueryCtxt, rustc_query_system[e96ca7e2da71ed3f]::query::caches::DefaultCache<(), core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>>>
  42:     0x7f6fdd549087 - rustc_query_system[e96ca7e2da71ed3f]::query::plumbing::get_query::<rustc_query_impl[cd5064426dba224b]::queries::analysis, rustc_query_impl[cd5064426dba224b]::plumbing::QueryCtxt>
  43:     0x7f6fdc5038e3 - <rustc_interface[a32f49e72690e02b]::passes::QueryContext>::enter::<rustc_driver[2feadf6a045f69f8]::run_compiler::{closure#1}::{closure#2}::{closure#3}, core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>>
  44:     0x7f6fdc4ff826 - <rustc_interface[a32f49e72690e02b]::interface::Compiler>::enter::<rustc_driver[2feadf6a045f69f8]::run_compiler::{closure#1}::{closure#2}, core[b141a5f448cbfdbc]::result::Result<core[b141a5f448cbfdbc]::option::Option<rustc_interface[a32f49e72690e02b]::queries::Linker>, rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>>
  45:     0x7f6fdc4f6edc - rustc_span[f76be64242de1050]::with_source_map::<core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>, rustc_interface[a32f49e72690e02b]::interface::run_compiler<core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>, rustc_driver[2feadf6a045f69f8]::run_compiler::{closure#1}>::{closure#0}::{closure#1}>
  46:     0x7f6fdc4f68a2 - <scoped_tls[98436e20d4fee4d6]::ScopedKey<rustc_span[f76be64242de1050]::SessionGlobals>>::set::<rustc_interface[a32f49e72690e02b]::interface::run_compiler<core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>, rustc_driver[2feadf6a045f69f8]::run_compiler::{closure#1}>::{closure#0}, core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>>
  47:     0x7f6fdc4f4fcf - std[5adef4932411bbc2]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[a32f49e72690e02b]::util::run_in_thread_pool_with_globals<rustc_interface[a32f49e72690e02b]::interface::run_compiler<core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>, rustc_driver[2feadf6a045f69f8]::run_compiler::{closure#1}>::{closure#0}, core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>>
  48:     0x7f6fdc4f4e3f - <<std[5adef4932411bbc2]::thread::Builder>::spawn_unchecked_<rustc_interface[a32f49e72690e02b]::util::run_in_thread_pool_with_globals<rustc_interface[a32f49e72690e02b]::interface::run_compiler<core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>, rustc_driver[2feadf6a045f69f8]::run_compiler::{closure#1}>::{closure#0}, core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[b141a5f448cbfdbc]::result::Result<(), rustc_errors[3c325fb77ff98e3b]::ErrorGuaranteed>>::{closure#1} as core[b141a5f448cbfdbc]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  49:     0x7f6fdae485f3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hecb24e4d2d235ab4
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/alloc/src/boxed.rs:1987:9
  50:     0x7f6fdae485f3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h05e0c493dc58e461
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/alloc/src/boxed.rs:1987:9
  51:     0x7f6fdae485f3 - std::sys::unix::thread::Thread::new::thread_start::h6913f7fb82d0a425
                               at /rustc/4b8f4319954ff2642690b9e5cbe4af352d095bf6/library/std/src/sys/unix/thread.rs:108:17
  52:     0x7f6fdabb6e86 - start_thread
  53:     0x7f6fdac3dc60 - __clone3
  54:                0x0 - <unknown>

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

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: rustc 1.66.0-nightly (4b8f43199 2022-10-19) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -Z treat-err-as-bug

query stack during panic:
#0 [typeck] type-checking `<impl at a.rs:22:1: 22:32>::foo`
#1 [typeck_item_bodies] type-checking all item bodies
#2 [analysis] running analysis passes on this crate
end of query stack

@fee1-dead fee1-dead added C-bug Category: This is a bug. A-const-generics Area: const generics (parameters and arguments) F-min_const_generics T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 6, 2022
@fee1-dead fee1-dead changed the title min_const_generics infers the wrong type depending on traits in scope min_const_generics infers the wrong type depending on trait bounds in scope Nov 6, 2022
@compiler-errors
Copy link
Member

Possibly/probably related: #103741

@fmease
Copy link
Member

fmease commented Jan 26, 2024

Closing as a duplicate of #24066. If you feel it's worth keeping this open anyway, I can of course reopen it.

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Jan 26, 2024
@fmease fmease added A-trait-system Area: Trait system and removed F-min_const_generics labels Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) A-trait-system Area: Trait system C-bug Category: This is a bug. 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

3 participants