rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.58k stars 12.74k forks source link

ICE `DefId(2:2587 ~ core[22a2]::default) does not have a "generics_of"` #113610

Closed matthiaskrgr closed 1 year ago

matthiaskrgr commented 1 year ago

Code

fn test5() {
    let t = (String::new(), Default::default());
}

Meta

rustc --version --verbose:

rustc 1.73.0-nightly (e571544f4 2023-07-11)
binary: rustc
commit-hash: e571544f4448d35af55e5ea3f35b92a2e784944a
commit-date: 2023-07-11
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

Error output

<output>
Backtrace

``` error[E0601]: `main` function not found in crate `treereduce` --> treereduce.out:3:2 | 3 | } | ^ consider adding a `main` function to `treereduce.out` thread 'rustc' panicked at 'DefId(2:2587 ~ core[22a2]::default) does not have a "generics_of"', compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs:205:1 stack backtrace: 0: 0x7f2664763641 - std::backtrace_rs::backtrace::libunwind::trace::hca29094809d9477a at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5 1: 0x7f2664763641 - std::backtrace_rs::backtrace::trace_unsynchronized::h9c93fbc346e32491 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f2664763641 - std::sys_common::backtrace::_print_fmt::h0fe57bfefd15b201 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/sys_common/backtrace.rs:65:5 3: 0x7f2664763641 - ::fmt::h3a49877b0c514b39 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f26647c974f - core::fmt::rt::Argument::fmt::h7f9f051156e40db0 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/core/src/fmt/rt.rs:138:9 5: 0x7f26647c974f - core::fmt::write::ha9b9d271548a73c5 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/core/src/fmt/mod.rs:1094:21 6: 0x7f2664755ed7 - std::io::Write::write_fmt::hc362527f94e7bd48 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/io/mod.rs:1714:15 7: 0x7f2664763455 - std::sys_common::backtrace::_print::h7d0ea49c208d574d at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f2664763455 - std::sys_common::backtrace::print::he03f8dd1ce343041 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f2664766233 - std::panicking::default_hook::{{closure}}::h07a8a0e03842f254 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/panicking.rs:269:22 10: 0x7f2664765fc4 - std::panicking::default_hook::hbe55e2858525ddb4 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/panicking.rs:288:9 11: 0x7f26679e0d8b - >::call_once::{shim:vtable#0} 12: 0x7f2664766a5e - as core::ops::function::Fn>::call::hdae7ac5f67d0fe74 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/alloc/src/boxed.rs:2007:9 13: 0x7f2664766a5e - std::panicking::rust_panic_with_hook::haff772a1c5ad3ee6 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/panicking.rs:709:13 14: 0x7f26647667e7 - std::panicking::begin_panic_handler::{{closure}}::h8a44092b35d95f6d at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/panicking.rs:597:13 15: 0x7f2664763aa6 - std::sys_common::backtrace::__rust_end_short_backtrace::h696f84c223c173c4 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/sys_common/backtrace.rs:151:18 16: 0x7f2664766532 - rust_begin_unwind at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/panicking.rs:593:5 17: 0x7f26647c59a3 - core::panicking::panic_fmt::hb3d524c9bc244750 at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/core/src/panicking.rs:67:14 18: 0x7f2667ed8a02 - rustc_metadata[9d79889a4dcec7f8]::rmeta::decoder::cstore_impl::provide_extern::generics_of::{closure#2} 19: 0x7f26664059dd - rustc_query_impl[43d7f214d4bda649]::plumbing::__rust_begin_short_backtrace::> 20: 0x7f26664054a0 - >::call_once 21: 0x7f2665a6a2f5 - rustc_query_system[b940c10253c7f285]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[43d7f214d4bda649]::plumbing::QueryCtxt, false> 22: 0x7f2667334dc3 - rustc_query_impl[43d7f214d4bda649]::query_impl::generics_of::get_query_non_incr::__rust_end_short_backtrace 23: 0x7f2667d5c270 - rustc_middle[30f3b0c932b04da5]::query::plumbing::query_get_at::>> 24: 0x7f2667d5f505 - rustc_infer[a89e4bd38e23cb11]::infer::error_reporting::need_type_info::fmt_printer::{closure#0} 25: 0x7f2665b3457a - ::pretty_print_type 26: 0x7f2665b354a6 - ::comma_sep::>> 27: 0x7f2665b32975 - ::pretty_print_type 28: 0x7f2667d5f9e9 - rustc_infer[a89e4bd38e23cb11]::infer::error_reporting::need_type_info::ty_to_string 29: 0x7f2667d5fd42 - ::ty_localized_msg 30: 0x7f2667d181a6 - ::emit_inference_failure_err 31: 0x7f26686298bb - ::maybe_report_ambiguity 32: 0x7f266861c1b4 - ::report_fulfillment_errors 33: 0x7f2666535116 - rustc_hir_typeck[6f9a7622c205e574]::typeck 34: 0x7f2665a6d79e - rustc_query_impl[43d7f214d4bda649]::plumbing::__rust_begin_short_backtrace::> 35: 0x7f2665a6d76e - >::call_once 36: 0x7f2665b37fad - rustc_query_system[b940c10253c7f285]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[43d7f214d4bda649]::plumbing::QueryCtxt, false> 37: 0x7f266730d9d1 - rustc_query_impl[43d7f214d4bda649]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace 38: 0x7f2665baffed - rustc_query_impl[43d7f214d4bda649]::plumbing::__rust_begin_short_backtrace::> 39: 0x7f2665baff4e - >::call_once 40: 0x7f2665b37fad - rustc_query_system[b940c10253c7f285]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[43d7f214d4bda649]::plumbing::QueryCtxt, false> 41: 0x7f2667340da1 - rustc_query_impl[43d7f214d4bda649]::query_impl::used_trait_imports::get_query_non_incr::__rust_end_short_backtrace 42: 0x7f2666e7ef92 - rustc_hir_analysis[82b5e16c7bfda3fb]::check_unused::check_crate 43: 0x7f2666e7c431 - rustc_hir_analysis[82b5e16c7bfda3fb]::check_crate 44: 0x7f2666e758ba - rustc_interface[4d7121c82a192bc1]::passes::analysis 45: 0x7f2666e717da - rustc_query_impl[43d7f214d4bda649]::plumbing::__rust_begin_short_backtrace::> 46: 0x7f2666e717c9 - >::call_once 47: 0x7f26670259e8 - rustc_query_system[b940c10253c7f285]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[43d7f214d4bda649]::plumbing::QueryCtxt, false> 48: 0x7f26670257b9 - rustc_query_impl[43d7f214d4bda649]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace 49: 0x7f2666c15ec2 - >::enter::, rustc_driver_impl[b031cc87a12e3f83]::run_compiler::{closure#1}::{closure#2}::{closure#4}> 50: 0x7f2666c14bd8 - ::enter::, rustc_span[1515c77db744f4cd]::ErrorGuaranteed>> 51: 0x7f2666c0e0c5 - rustc_span[1515c77db744f4cd]::set_source_map::, rustc_interface[4d7121c82a192bc1]::interface::run_compiler, rustc_driver_impl[b031cc87a12e3f83]::run_compiler::{closure#1}>::{closure#0}::{closure#0}> 52: 0x7f2666c0db36 - >::set::, rustc_driver_impl[b031cc87a12e3f83]::run_compiler::{closure#1}>::{closure#0}, core[22a2682dd47ad7ab]::result::Result<(), rustc_span[1515c77db744f4cd]::ErrorGuaranteed>> 53: 0x7f2666c0d0fc - std[671b5e6e5c144e0c]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[b031cc87a12e3f83]::run_compiler::{closure#1}>::{closure#0}, core[22a2682dd47ad7ab]::result::Result<(), rustc_span[1515c77db744f4cd]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[22a2682dd47ad7ab]::result::Result<(), rustc_span[1515c77db744f4cd]::ErrorGuaranteed>> 54: 0x7f2667204985 - <::spawn_unchecked_, rustc_driver_impl[b031cc87a12e3f83]::run_compiler::{closure#1}>::{closure#0}, core[22a2682dd47ad7ab]::result::Result<(), rustc_span[1515c77db744f4cd]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[22a2682dd47ad7ab]::result::Result<(), rustc_span[1515c77db744f4cd]::ErrorGuaranteed>>::{closure#1} as core[22a2682dd47ad7ab]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 55: 0x7f26647710f5 - as core::ops::function::FnOnce>::call_once::h94c741346229c08a at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/alloc/src/boxed.rs:1993:9 56: 0x7f26647710f5 - as core::ops::function::FnOnce>::call_once::h1fff289ef1c1875a at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/alloc/src/boxed.rs:1993:9 57: 0x7f26647710f5 - std::sys::unix::thread::Thread::new::thread_start::hc34b652bcf2ee8ff at /rustc/e571544f4448d35af55e5ea3f35b92a2e784944a/library/std/src/sys/unix/thread.rs:108:17 58: 0x7f266450344b - 59: 0x7f2664586e40 - 60: 0x0 - error: 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.73.0-nightly (e571544f4 2023-07-11) running on x86_64-unknown-linux-gnu query stack during panic: #0 [generics_of] computing generics of `core::default` #1 [typeck] type-checking `test5` #2 [used_trait_imports] finding used_trait_imports `test5` #3 [analysis] running analysis passes on this crate end of query stack error: aborting due to previous error For more information about this error, try `rustc --explain E0601`. ```

matthiaskrgr commented 1 year ago

https://github.com/rust-lang/rust/pull/113310 cc @jieyouxu

jieyouxu commented 1 year ago

Thanks for the ping. It seems to be that here https://github.com/rust-lang/rust/blob/993deaa0bf8bab9dd3eadfd1fbeb093328e95afe/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs#L165-L168 it's not always the parent tcx.parent(def_id) which has generics_of, sometimes its on the def_id as-is. I'm not entirely sure when we need the tcx.parent (IIRC it doesn't work for my test case if I don't add tcx.parent).

jieyouxu commented 1 year ago

Now if I stick to only using def_id and not its parent,

debug!(?def_id);
let generics = infcx.tcx.generics_of(def_id);
debug!("generics: {:?}", generics);
let idx = generics.param_def_id_to_index(infcx.tcx, def_id).unwrap();

the param_def_id_to_index line ICEs on my PR's test case because it can't find the param index for the def_id.

trait T {}

struct S {}

impl S {
    fn owo(&self, _: Option<&impl T>) {}
}

fn main() {
    (S {}).owo(None)
    //~^ ERROR type annotations needed
}
├─0ms DEBUG rustc_infer::infer::error_reporting::need_type_info def_id=DefId(0:10 ~ issue_113264_incorrect_impl_trait_in_path_suggestion[bb87]::{impl#0}::owo::{opaque#0})
├─0ms DEBUG rustc_infer::infer::error_reporting::need_type_info generics: Generics { parent: None, parent_count: 0, params: [], param_def_id_to_index: {}, has_self: false, has_late_bound_regions: None, host_effect_index: None }

cc @lcnr sorry for the ping, but do you know if impl T here is special somehow? The DefId debug path for my PR's test case seems to have another layer compared to this issue's ICE example, namely {opaque#0}. I think this is why getting param_def_id_to_index for my PR test case's parent worked but not for this ICE?

lcnr commented 1 year ago

going to look into this, I think using parent is correct and the bug is somewhere else :sweat_smile: