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

"internal compiler error" on Higher-Ranked Trait Bound for impl Trait in return position #67830

Closed
TOETOE55 opened this issue Jan 3, 2020 · 6 comments · Fixed by #94081
Closed
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-traits Working group: Traits, https://internals.rust-lang.org/t/announcing-traits-working-group/6804

Comments

@TOETOE55
Copy link

TOETOE55 commented Jan 3, 2020

I needed a Fn like trait, and return an impl trait with Higher-Ranked Trait Bound. But rustc panicked.

I tried this code:

trait MyFn<Arg> {
    type Output;
    fn call(&self, arg: Arg) -> Self::Output;
}

struct Wrap<F>(F);

impl<A, B, F> MyFn<A> for Wrap<F>
where
    F: Fn(A) -> B
{
    type Output = B;

    fn call(&self, arg: A) -> Self::Output {
        (self.0)(arg)
    }
}


struct A;
fn test() -> impl for<'a> MyFn<&'a A, Output=impl Iterator + 'a> {
    Wrap(|a| Some(a).into_iter())
}

I expected it was checked, but I received this compiler bug:

thread 'rustc' panicked at 'assertion failed: !erased_self_ty.has_escaping_bound_vars()', src\librustc\ty\util.rs:441:9
stack backtrace:
   0: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
   1: core::fmt::write
   2: <std::io::IoSliceMut as core::fmt::Debug>::fmt
   3: std::panicking::take_hook
   4: std::panicking::take_hook
   5: rustc_driver::report_ice
   6: std::panicking::rust_panic_with_hook
   7: <rustc::ty::query::Providers as core::default::Default>::default
   8: rustc::ty::util::<impl rustc::ty::context::TyCtxt>::required_region_bounds
   9: rustc::infer::opaque_types::Instantiator::fold_opaque_ty
  10: rustc::ty::binding::BindingMode::convert
  11: rustc::infer::opaque_types::Instantiator::fold_opaque_ty
  12: <rustc_typeck::collect::find_opaque_ty_constraints::ConstraintLocator as rustc::hir::intravisit::Visitor>::visit_trait_item
  13: rustc_typeck::check::FnCtxt::apply_adjustments
  14: <rustc_typeck::check::GatherLocalsVisitor as rustc::hir::intravisit::Visitor>::visit_pat
  15: <rustc_typeck::outlives::explicit::ExplicitPredicatesMap as core::fmt::Debug>::fmt
  16: <rustc_typeck::check::CheckItemTypesVisitor as rustc::hir::itemlikevisit::ItemLikeVisitor>::visit_item
  17: <rustc_typeck::check::autoderef::AutoderefKind as core::fmt::Debug>::fmt
  18: <rustc_typeck::outlives::explicit::ExplicitPredicatesMap as core::fmt::Debug>::fmt
  19: <rustc_typeck::variance::test::VarianceTest as rustc::hir::itemlikevisit::ItemLikeVisitor>::visit_item
  20: <rustc_typeck::outlives::explicit::ExplicitPredicatesMap as core::fmt::Debug>::fmt
  21: rustc_typeck::collect::checked_type_of
  22: <rustc_typeck::collect::has_late_bound_regions::LateBoundRegionsDetector as rustc::hir::intravisit::Visitor>::visit_lifetime
  23: rustc::middle::weak_lang_items::<impl rustc::ty::context::TyCtxt>::is_weak_lang_item
  24: rustc::dep_graph::graph::DepGraph::assert_ignored
  25: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::try_print_query_stack
  26: rustc::ty::util::Discr::checked_add
  27: rustc::ty::util::<impl rustc::ty::context::TyCtxt>::try_expand_impl_trait_type
  28: <rustc_typeck::check::check_opaque_for_inheriting_lifetimes::ProhibitOpaqueVisitor as rustc::ty::fold::TypeVisitor>::visit_region
  29: <rustc_typeck::check::autoderef::AutoderefKind as core::fmt::Debug>::fmt
  30: <rustc_typeck::check::CheckItemTypesVisitor as rustc::hir::itemlikevisit::ItemLikeVisitor>::visit_item
  31: <rustc_typeck::check::autoderef::AutoderefKind as core::fmt::Debug>::fmt
  32: <rustc_typeck::outlives::explicit::ExplicitPredicatesMap as core::fmt::Debug>::fmt
  33: <rustc_typeck::variance::test::VarianceTest as rustc::hir::itemlikevisit::ItemLikeVisitor>::visit_item
  34: <rustc_typeck::outlives::explicit::ExplicitPredicatesMap as core::fmt::Debug>::fmt
  35: <rustc_typeck::outlives::explicit::ExplicitPredicatesMap as core::fmt::Debug>::fmt
  36: rustc_typeck::check_crate
  37: rustc_typeck::check_crate
  38: rustc_interface::passes::BoxedResolver::to_resolver_outputs
  39: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  40: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  41: <rustc_traits::chalk_context::ChalkContext as chalk_engine::context::ContextOps<rustc_traits::chalk_context::ChalkArenas>>::canonical
  42: <syntax_pos::symbol::LocalInternedString as core::fmt::Display>::fmt
  43: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  44: rustc_interface::passes::BoxedGlobalCtxt::complete
  45: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  46: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  47: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  48: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  49: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  50: _rust_maybe_catch_panic
  51: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  52: ZN244_$LT$std..error..$LT$impl$u20$core..convert..From$LT$alloc..string..String$GT$$u20$for$u20$alloc..boxed..Box$LT$dyn$u20$std..error..Error$u2b$core..marker..Send$u2b$core..marker..Sync$GT$$GT$..from..StringError$u20$as$u20$core..fmt..Display$GT$3fmt17
  53: std::sys::windows::thread::Thread::new
  54: BaseThreadInitThunk
  55: RtlUserThreadStart
  
  
  
query stack during panic:
#0 [typeck_tables_of] processing `test`
#1 [type_of] processing `test::{{opaque}}#0`
#2 [check_mod_item_types] checking item types in top-level module
#3 [analysis] running analysis passes on this crate
end of query stack

Meta

rustc --version --verbose:

rustc 1.40.0 (73528e339 2019-12-16)
binary: rustc
commit-hash: 73528e339aae0f17a15ffa49a8ac608f50c6cf14
commit-date: 2019-12-16
host: x86_64-pc-windows-msvc
release: 1.40.0
LLVM version: 9.0
@jonas-schievink jonas-schievink added A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. 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. I-nominated labels Jan 3, 2020
@TOETOE55
Copy link
Author

TOETOE55 commented Jan 3, 2020

hmmm, maybe it is equivalent to (no!)

fn test<'a>() -> impl MyFn<&'a A, Output=impl Iterator + 'a>

but what about:

fn test(f: impl for<'a> MyFn<&'a A, Output=impl Iterator + 'a>) {
    let x = A;
    f.call(&x);
}

it couldn't check:

fn test(_: impl for<'a> MyFn<&'a A, Output=impl Iterator + 'a>) {
                                                            ^^ undeclared lifetime

but this is OK:

fn test(_: impl for<'a> MyFn<&'a A, Output=IntoIter<&'a A>>) {
    let x = A;
    f.call(&x);
}

playground here
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f15195784e3fe07799fdb4ee84d9c049

@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Jan 5, 2020
@pnkfelix
Copy link
Member

pnkfelix commented Jan 9, 2020

triage: P-high. Removing nominaton, but cc @matthewjasper and @nikomatsakis for assistance.

@pnkfelix pnkfelix added P-high High priority and removed I-nominated labels Jan 9, 2020
@smmalis37
Copy link
Contributor

smmalis37 commented Jul 31, 2020

Minimized further

trait Empty {
}

trait T<'l> {
    type Assoc;
}

fn foo() -> impl for<'a> T<'a, Assoc = impl Empty + 'a> {
    todo!()
}

It seems like pretty much any trait will work in place of Empty, but I wanted one guaranteed to have no special treatment.

@smmalis37
Copy link
Contributor

This is the exact same stack trace and error message as #54895. Should one of these be duped to the other?

@matthiaskrgr
Copy link
Member

This is a regression since 1.28.
@rustbot modify labels: +regression-from-stable-to-stable

@rustbot rustbot added the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label Dec 20, 2020
@smmalis37
Copy link
Contributor

As of the latest nightly this still ICEs, but the stack has changed thanks to #85499 :

thread 'rustc' panicked at 'Normalizing [Binder(TraitPredicate(<impl for<'a> Empty as std::marker::Sized>), []), Binder(OutlivesPredicate(impl for<'a> Empty, ReLateBound(DebruijnIndex(1), BoundRegion { var: 0, kind: BrNamed(DefId(0:9 ~ playground[14a5]::foo::{opaque#0}::'a), 'a) })), []), Binder(TraitPredicate(<impl for<'a> Empty as Empty>), [])] without wrapping in a `Binder`', compiler/rustc_trait_selection/src/traits/project.rs:332:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/50171c310cd15e1b2d3723766ce64e2e4d6696fc/library/std/src/panicking.rs:517:5
   1: std::panicking::begin_panic_fmt
             at /rustc/50171c310cd15e1b2d3723766ce64e2e4d6696fc/library/std/src/panicking.rs:460:5
   2: rustc_trait_selection::traits::project::AssocTypeNormalizer::fold
   3: rustc_trait_selection::traits::project::normalize
   4: <rustc_infer::infer::InferCtxt as rustc_trait_selection::infer::InferCtxtExt>::partially_normalize_associated_types_in
   5: rustc_trait_selection::opaque_types::Instantiator::fold_opaque_ty
   6: <rustc_middle::ty::fold::BottomUpFolder<F,G,H> as rustc_middle::ty::fold::TypeFolder>::fold_ty
   7: rustc_middle::ty::fold::TypeFoldable::fold_with
   8: rustc_middle::ty::structural_impls::<impl rustc_middle::ty::fold::TypeFoldable for rustc_middle::ty::sty::Binder<T>>::super_fold_with
   9: rustc_trait_selection::opaque_types::Instantiator::fold_opaque_ty
  10: <rustc_middle::ty::fold::BottomUpFolder<F,G,H> as rustc_middle::ty::fold::TypeFolder>::fold_ty
  11: <rustc_infer::infer::InferCtxt as rustc_trait_selection::opaque_types::InferCtxtExt>::instantiate_opaque_types
  12: rustc_typeck::check::fn_ctxt::_impl::<impl rustc_typeck::check::fn_ctxt::FnCtxt>::instantiate_opaque_types_from_value
  13: rustc_typeck::check::check::check_fn
  14: rustc_infer::infer::InferCtxtBuilder::enter
  15: rustc_typeck::check::typeck
  16: rustc_query_system::query::plumbing::get_query_impl
  17: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::typeck
  18: rustc_middle::ty::context::TyCtxt::typeck_opt_const_arg
  19: rustc_mir_build::build::mir_built
  20: rustc_query_system::query::plumbing::get_query_impl
  21: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_built
  22: rustc_mir::transform::check_unsafety::unsafety_check_result
  23: core::ops::function::FnOnce::call_once
  24: rustc_query_system::query::plumbing::get_query_impl
  25: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::unsafety_check_result
  26: rustc_mir::transform::mir_const
  27: rustc_query_system::query::plumbing::get_query_impl
  28: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_const
  29: rustc_mir::transform::mir_promoted
  30: rustc_query_system::query::plumbing::get_query_impl
  31: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_promoted
  32: rustc_mir::borrow_check::mir_borrowck
  33: core::ops::function::FnOnce::call_once
  34: rustc_query_system::query::plumbing::get_query_impl
  35: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::mir_borrowck
  36: rustc_typeck::collect::type_of::type_of
  37: rustc_query_system::query::plumbing::get_query_impl
  38: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::type_of
  39: rustc_typeck::check::check::check_item_type
  40: rustc_middle::hir::map::Map::visit_item_likes_in_module
  41: rustc_typeck::check::check::check_mod_item_types
  42: rustc_query_system::query::plumbing::get_query_impl
  43: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::check_mod_item_types
  44: rustc_session::utils::<impl rustc_session::session::Session>::time
  45: rustc_typeck::check_crate
  46: rustc_interface::passes::analysis
  47: rustc_query_system::query::plumbing::get_query_impl
  48: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::analysis
  49: rustc_interface::passes::QueryContext::enter
  50: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  51: rustc_span::with_source_map
  52: scoped_tls::ScopedKey<T>::set
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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.56.0-nightly (50171c310 2021-09-01) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [typeck] type-checking `foo`
#1 [mir_built] building MIR for `foo`
#2 [unsafety_check_result] unsafety-checking `foo`
#3 [mir_const] processing MIR for `foo`
#4 [mir_promoted] processing `foo`
#5 [mir_borrowck] borrow-checking `foo`
#6 [type_of] computing type of `foo::{opaque#0}`
#7 [check_mod_item_types] checking item types in top-level module
#8 [analysis] running analysis passes on this crate
end of query stack
For more information about this error, try `rustc --explain E0601`.

@jackh726 jackh726 added the WG-traits Working group: Traits, https://internals.rust-lang.org/t/announcing-traits-working-group/6804 label Feb 1, 2022
@bors bors closed this as completed in f132bcf Mar 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-traits Working group: Traits, https://internals.rust-lang.org/t/announcing-traits-working-group/6804
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants