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

ICE "wrong number of generic parameters" with abused auto trait #104808

Closed
jruderman opened this issue Nov 24, 2022 · 2 comments · Fixed by #105287
Closed

ICE "wrong number of generic parameters" with abused auto trait #104808

jruderman opened this issue Nov 24, 2022 · 2 comments · Fixed by #105287
Assignees
Labels
C-bug Category: This is a bug. F-auto_traits `#![feature(auto_traits)]` 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.

Comments

@jruderman
Copy link
Contributor

Found with a modified fuzz-rustc

Code

#![feature(auto_traits)]

auto trait Trait1<'a> {}

fn f()
where
    for<'a> dyn Trait1<'a>: Sized,
{
}

fn main() {}

Error output with debug assertions enabled

error[E0567]: auto traits cannot have generic parameters
  |
3 | auto trait Trait1<'a> {}
  |            ------^^^^ help: remove the parameters
  |            |
  |            auto trait cannot have generic parameters

thread 'rustc' panicked at 'assertion failed: `(left == right)`
  left: `(2, Some(2))`,
 right: `(1, Some(1))`: wrong number of generic parameters for DefId(...Trait1): [FreshTy(0)]
Did you accidentally include the self-type in the params list?', compiler/rustc_middle/src/ty/context.rs:2827:9
Backtrace
stack backtrace:
   0:        0x10de9cf21 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hcf5aac4d53eab431
   1:        0x10df5a818 - core::fmt::write::hed31846cddb4e13c
   2:        0x10deffec8 - std::io::Write::write_fmt::h01887004f97cedc7
   3:        0x10de9cd12 - std::sys_common::backtrace::print::h2141c6543b5865e5
   4:        0x10df00e45 - std::panicking::default_hook::{{closure}}::hfe93a58a10a72158
   5:        0x10df00c2e - std::panicking::default_hook::hafca10424d622dfb
   6:        0x12248d708 - rustc_driver[bfe6130b33d7dd9c]::DEFAULT_HOOK::{closure#0}::{closure#0}
   7:        0x10df01636 - std::panicking::rust_panic_with_hook::hafe3bb82e805c420
   8:        0x10deee173 - std::panicking::begin_panic_handler::{{closure}}::h2797e20f8763cc3a
   9:        0x10deee0a8 - std::sys_common::backtrace::__rust_end_short_backtrace::hde7c42687ab67a11
  10:        0x10df00f18 - _rust_begin_unwind
  11:        0x10df7f443 - core::panicking::panic_fmt::h7ec57a9a63e6db71
  12:        0x10df6e125 - core::panicking::assert_failed_inner::hb961e0acdeb20af8
  13:        0x127af841e - core[95bb6d661b676f4d]::panicking::assert_failed::<(usize, core[95bb6d661b676f4d]::option::Option<usize>), (usize, core[95bb6d661b676f4d]::option::Option<usize>)>
  14:        0x1272f5616 - <rustc_middle[42f89e9b8d5efbfa]::ty::context::TyCtxt>::mk_trait_ref::<rustc_middle[42f89e9b8d5efbfa]::ty::Ty, [rustc_middle[42f89e9b8d5efbfa]::ty::Ty; 1usize]>
  15:        0x12733f32a - <rustc_middle[42f89e9b8d5efbfa]::ty::sty::Binder<rustc_middle[42f89e9b8d5efbfa]::ty::sty::ExistentialPredicate>>::with_self_ty
  16:        0x126f72bd4 - <&mut rustc_trait_selection[47a25601aa70e18]::traits::wf::object_region_bounds::{closure#0} as core[95bb6d661b676f4d]::ops::function::FnMut<(rustc_middle[42f89e9b8d5efbfa]::ty::sty::Binder<rustc_middle[42f89e9b8d5efbfa]::ty::sty::ExistentialPredicate>,)>>::call_mut
  17:        0x126f66626 - <core[95bb6d661b676f4d]::iter::adapters::copied::Copied<core[95bb6d661b676f4d]::slice::iter::Iter<rustc_middle[42f89e9b8d5efbfa]::ty::sty::Binder<rustc_middle[42f89e9b8d5efbfa]::ty::sty::ExistentialPredicate>>> as core[95bb6d661b676f4d]::iter::traits::iterator::Iterator>::try_fold::<(), core[95bb6d661b676f4d]::iter::traits::iterator::Iterator::find_map::check<rustc_middle[42f89e9b8d5efbfa]::ty::sty::Binder<rustc_middle[42f89e9b8d5efbfa]::ty::sty::ExistentialPredicate>, rustc_middle[42f89e9b8d5efbfa]::ty::Predicate, &mut rustc_trait_selection[47a25601aa70e18]::traits::wf::object_region_bounds::{closure#0}>::{closure#0}, core[95bb6d661b676f4d]::ops::control_flow::ControlFlow<rustc_middle[42f89e9b8d5efbfa]::ty::Predicate>>
  18:        0x126f20c55 - <alloc[fd2a8df82bfdc6c8]::vec::Vec<rustc_infer[7a2db91e787c07b]::traits::Obligation<rustc_middle[42f89e9b8d5efbfa]::ty::Predicate>> as alloc[fd2a8df82bfdc6c8]::vec::spec_from_iter::SpecFromIter<rustc_infer[7a2db91e787c07b]::traits::Obligation<rustc_middle[42f89e9b8d5efbfa]::ty::Predicate>, core[95bb6d661b676f4d]::iter::adapters::map::Map<core[95bb6d661b676f4d]::iter::adapters::filter_map::FilterMap<core[95bb6d661b676f4d]::iter::adapters::copied::Copied<core[95bb6d661b676f4d]::slice::iter::Iter<rustc_middle[42f89e9b8d5efbfa]::ty::sty::Binder<rustc_middle[42f89e9b8d5efbfa]::ty::sty::ExistentialPredicate>>>, rustc_trait_selection[47a25601aa70e18]::traits::wf::object_region_bounds::{closure#0}>, rustc_infer[7a2db91e787c07b]::traits::util::elaborate_predicates<core[95bb6d661b676f4d]::iter::adapters::filter_map::FilterMap<core[95bb6d661b676f4d]::iter::adapters::copied::Copied<core[95bb6d661b676f4d]::slice::iter::Iter<rustc_middle[42f89e9b8d5efbfa]::ty::sty::Binder<rustc_middle[42f89e9b8d5efbfa]::ty::sty::ExistentialPredicate>>>, rustc_trait_selection[47a25601aa70e18]::traits::wf::object_region_bounds::{closure#0}>>::{closure#0}>>>::from_iter
  19:        0x126f6406d - rustc_infer[7a2db91e787c07b]::traits::util::elaborate_predicates::<core[95bb6d661b676f4d]::iter::adapters::filter_map::FilterMap<core[95bb6d661b676f4d]::iter::adapters::copied::Copied<core[95bb6d661b676f4d]::slice::iter::Iter<rustc_middle[42f89e9b8d5efbfa]::ty::sty::Binder<rustc_middle[42f89e9b8d5efbfa]::ty::sty::ExistentialPredicate>>>, rustc_trait_selection[47a25601aa70e18]::traits::wf::object_region_bounds::{closure#0}>>
  20:        0x126f7346d - rustc_trait_selection[47a25601aa70e18]::traits::wf::required_region_bounds::<core[95bb6d661b676f4d]::iter::adapters::filter_map::FilterMap<core[95bb6d661b676f4d]::iter::adapters::copied::Copied<core[95bb6d661b676f4d]::slice::iter::Iter<rustc_middle[42f89e9b8d5efbfa]::ty::sty::Binder<rustc_middle[42f89e9b8d5efbfa]::ty::sty::ExistentialPredicate>>>, rustc_trait_selection[47a25601aa70e18]::traits::wf::object_region_bounds::{closure#0}>>
  21:        0x126f3dd35 - rustc_trait_selection[47a25601aa70e18]::traits::wf::object_region_bounds
  22:        0x124ff43b0 - <dyn rustc_hir_analysis[4c308ea3b425d19e]::astconv::AstConv>::conv_object_ty_poly_trait_ref
  23:        0x1250006bb - <dyn rustc_hir_analysis[4c308ea3b425d19e]::astconv::AstConv>::ast_ty_to_ty_inner
  24:        0x12512bebd - rustc_hir_analysis[4c308ea3b425d19e]::collect::predicates_of::gather_explicit_predicates_of::{closure#0}
  25:        0x12512af53 - rustc_hir_analysis[4c308ea3b425d19e]::collect::predicates_of::gather_explicit_predicates_of
  26:        0x1251299cb - rustc_hir_analysis[4c308ea3b425d19e]::collect::predicates_of::explicit_predicates_of
  27:        0x126506991 - rustc_query_system[2c5b580ea3a12206]::query::plumbing::try_execute_query::<rustc_query_impl[e5b327371ad57e64]::plumbing::QueryCtxt, rustc_query_system[2c5b580ea3a12206]::query::caches::DefaultCache<rustc_span[7d7aa17bf13bea16]::def_id::DefId, rustc_middle[42f89e9b8d5efbfa]::ty::generics::GenericPredicates>>
  28:        0x1265e9e39 - rustc_query_system[2c5b580ea3a12206]::query::plumbing::get_query::<rustc_query_impl[e5b327371ad57e64]::queries::explicit_predicates_of, rustc_query_impl[e5b327371ad57e64]::plumbing::QueryCtxt>
  29:        0x126182457 - <rustc_query_impl[e5b327371ad57e64]::Queries as rustc_middle[42f89e9b8d5efbfa]::ty::query::QueryEngine>::explicit_predicates_of
  30:        0x124f4d74f - rustc_hir_analysis[4c308ea3b425d19e]::collect::predicates_defined_on
  31:        0x126506991 - rustc_query_system[2c5b580ea3a12206]::query::plumbing::try_execute_query::<rustc_query_impl[e5b327371ad57e64]::plumbing::QueryCtxt, rustc_query_system[2c5b580ea3a12206]::query::caches::DefaultCache<rustc_span[7d7aa17bf13bea16]::def_id::DefId, rustc_middle[42f89e9b8d5efbfa]::ty::generics::GenericPredicates>>
  32:        0x1265e43c4 - rustc_query_system[2c5b580ea3a12206]::query::plumbing::get_query::<rustc_query_impl[e5b327371ad57e64]::queries::predicates_defined_on, rustc_query_impl[e5b327371ad57e64]::plumbing::QueryCtxt>
  33:        0x1261816a7 - <rustc_query_impl[e5b327371ad57e64]::Queries as rustc_middle[42f89e9b8d5efbfa]::ty::query::QueryEngine>::predicates_defined_on
  34:        0x125129293 - rustc_hir_analysis[4c308ea3b425d19e]::collect::predicates_of::predicates_of
  35:        0x126506991 - rustc_query_system[2c5b580ea3a12206]::query::plumbing::try_execute_query::<rustc_query_impl[e5b327371ad57e64]::plumbing::QueryCtxt, rustc_query_system[2c5b580ea3a12206]::query::caches::DefaultCache<rustc_span[7d7aa17bf13bea16]::def_id::DefId, rustc_middle[42f89e9b8d5efbfa]::ty::generics::GenericPredicates>>
  36:        0x1265c1110 - rustc_query_system[2c5b580ea3a12206]::query::plumbing::get_query::<rustc_query_impl[e5b327371ad57e64]::queries::predicates_of, rustc_query_impl[e5b327371ad57e64]::plumbing::QueryCtxt>
  37:        0x126173757 - <rustc_query_impl[e5b327371ad57e64]::Queries as rustc_middle[42f89e9b8d5efbfa]::ty::query::QueryEngine>::predicates_of
  38:        0x124f422e9 - rustc_hir_analysis[4c308ea3b425d19e]::collect::convert_item
  39:        0x124f3f24c - <rustc_hir_analysis[4c308ea3b425d19e]::collect::CollectItemTypesVisitor as rustc_hir[112137b1c6e29e52]::intravisit::Visitor>::visit_item
  40:        0x125124675 - <rustc_middle[42f89e9b8d5efbfa]::hir::map::Map>::visit_item_likes_in_module::<rustc_hir_analysis[4c308ea3b425d19e]::collect::CollectItemTypesVisitor>
  41:        0x124f3e555 - rustc_hir_analysis[4c308ea3b425d19e]::collect::collect_mod_item_types
  42:        0x1264e6f1e - rustc_query_system[2c5b580ea3a12206]::query::plumbing::try_execute_query::<rustc_query_impl[e5b327371ad57e64]::plumbing::QueryCtxt, rustc_query_system[2c5b580ea3a12206]::query::caches::DefaultCache<rustc_span[7d7aa17bf13bea16]::def_id::LocalDefId, ()>>
  43:        0x1265e7093 - rustc_query_system[2c5b580ea3a12206]::query::plumbing::get_query::<rustc_query_impl[e5b327371ad57e64]::queries::collect_mod_item_types, rustc_query_impl[e5b327371ad57e64]::plumbing::QueryCtxt>
  44:        0x1261956cf - <rustc_query_impl[e5b327371ad57e64]::Queries as rustc_middle[42f89e9b8d5efbfa]::ty::query::QueryEngine>::collect_mod_item_types
  45:        0x125123fa6 - <rustc_middle[42f89e9b8d5efbfa]::hir::map::Map>::for_each_module::<rustc_hir_analysis[4c308ea3b425d19e]::check_crate::{closure#0}::{closure#0}::{closure#0}>
  46:        0x12506055b - <rustc_session[6f273c4587eb1b50]::session::Session>::track_errors::<rustc_hir_analysis[4c308ea3b425d19e]::check_crate::{closure#0}, ()>
  47:        0x124f8264c - rustc_hir_analysis[4c308ea3b425d19e]::check_crate
  48:        0x1225b828a - rustc_interface[fb7297f02cc960f4]::passes::analysis
  49:        0x12652c95f - rustc_query_system[2c5b580ea3a12206]::query::plumbing::try_execute_query::<rustc_query_impl[e5b327371ad57e64]::plumbing::QueryCtxt, rustc_query_system[2c5b580ea3a12206]::query::caches::DefaultCache<(), core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>>>
  50:        0x126619589 - rustc_query_system[2c5b580ea3a12206]::query::plumbing::get_query::<rustc_query_impl[e5b327371ad57e64]::queries::analysis, rustc_query_impl[e5b327371ad57e64]::plumbing::QueryCtxt>
  51:        0x1261723b7 - <rustc_query_impl[e5b327371ad57e64]::Queries as rustc_middle[42f89e9b8d5efbfa]::ty::query::QueryEngine>::analysis
  52:        0x1224811db - <rustc_interface[fb7297f02cc960f4]::passes::QueryContext>::enter::<rustc_driver[bfe6130b33d7dd9c]::run_compiler::{closure#1}::{closure#2}::{closure#3}, core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>>
  53:        0x12247b688 - <rustc_interface[fb7297f02cc960f4]::interface::Compiler>::enter::<rustc_driver[bfe6130b33d7dd9c]::run_compiler::{closure#1}::{closure#2}, core[95bb6d661b676f4d]::result::Result<core[95bb6d661b676f4d]::option::Option<rustc_interface[fb7297f02cc960f4]::queries::Linker>, rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>>
  54:        0x1224792bb - rustc_span[7d7aa17bf13bea16]::with_source_map::<core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>, rustc_interface[fb7297f02cc960f4]::interface::run_compiler<core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>, rustc_driver[bfe6130b33d7dd9c]::run_compiler::{closure#1}>::{closure#0}::{closure#1}>
  55:        0x12244facc - <scoped_tls[c451c38e2ed76225]::ScopedKey<rustc_span[7d7aa17bf13bea16]::SessionGlobals>>::set::<rustc_interface[fb7297f02cc960f4]::interface::run_compiler<core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>, rustc_driver[bfe6130b33d7dd9c]::run_compiler::{closure#1}>::{closure#0}, core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>>
  56:        0x122446240 - std[e4564114326e8c6]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[fb7297f02cc960f4]::util::run_in_thread_pool_with_globals<rustc_interface[fb7297f02cc960f4]::interface::run_compiler<core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>, rustc_driver[bfe6130b33d7dd9c]::run_compiler::{closure#1}>::{closure#0}, core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>>
  57:        0x122478d90 - std[e4564114326e8c6]::panicking::try::<core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>, core[95bb6d661b676f4d]::panic::unwind_safe::AssertUnwindSafe<<std[e4564114326e8c6]::thread::Builder>::spawn_unchecked_<rustc_interface[fb7297f02cc960f4]::util::run_in_thread_pool_with_globals<rustc_interface[fb7297f02cc960f4]::interface::run_compiler<core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>, rustc_driver[bfe6130b33d7dd9c]::run_compiler::{closure#1}>::{closure#0}, core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>>::{closure#1}::{closure#0}>>
  58:        0x122474141 - <<std[e4564114326e8c6]::thread::Builder>::spawn_unchecked_<rustc_interface[fb7297f02cc960f4]::util::run_in_thread_pool_with_globals<rustc_interface[fb7297f02cc960f4]::interface::run_compiler<core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>, rustc_driver[bfe6130b33d7dd9c]::run_compiler::{closure#1}>::{closure#0}, core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[95bb6d661b676f4d]::result::Result<(), rustc_errors[48ad5d4ad3523473]::ErrorGuaranteed>>::{closure#1} as core[95bb6d661b676f4d]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  59:        0x10deb6b5b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hd10a5c38d6c64916
  60:        0x10dea119f - std::sys::unix::thread::Thread::new::thread_start::he793914283faf21a
  61:     0x7ff816f934e1 - __pthread_start

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.67.0-dev running on x86_64-apple-darwin

query stack during panic:
#0 [explicit_predicates_of] computing explicit predicates of `f`
#1 [predicates_defined_on] computing predicates of `f`
#2 [predicates_of] computing predicates of `f`
#3 [collect_mod_item_types] collecting item types in top-level module
#4 [analysis] running analysis passes on this crate
end of query stack

Version

Built stage1 from 604d521 on x86_64-apple-darwin

config.toml
# Includes one of the default files in src/bootstrap/defaults
profile = "compiler"
changelog-seen = 2

[llvm]
download-ci-llvm = true     # Download a pre-built LLVM?
assertions = true           # LLVM assertions on?

[rust]
incremental = true          # Build rustc with incremental compilation?
debug-assertions = true
overflow-checks = true

CC @oli-obk because 7658e0f (part of #104533) changed the assertion condition

@jruderman jruderman 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 Nov 24, 2022
@oli-obk
Copy link
Contributor

oli-obk commented Nov 24, 2022

Oh fun! Glad we're catching this. It's probably going to be annoying to fix, but also not a priority considering this is about auto traits.

@compiler-errors
Copy link
Member

This repro doesn't require debug assertions, or the feature gate:

auto trait Trait1<'a> {}

fn f<'a>(x: &dyn Trait1<'a>)
{}

fn main() {
    f(&1);
}

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 6, 2022
Synthesize substitutions for bad auto traits in dyn types

Auto traits are stored as just `DefId`s inside a `dyn Trait`'s existential predicates list. This is usually fine, since auto traits are forbidden to have generics -- but this becomes a problem for an ill-formed auto trait.

But since this will always result in an error, just synthesize some dummy (error) substitutions which are used at least to keep trait selection code happy about the number of substs in a trait ref.

Fixes rust-lang#104808
@bors bors closed this as completed in 0a07ffe Dec 6, 2022
@compiler-errors compiler-errors self-assigned this Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-auto_traits `#![feature(auto_traits)]` 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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants