Skip to content

ICE: Generic function traits using borrowing HRTB traits #109738

Closed
@ten3roberts

Description

@ten3roberts

Encountered an ICE when working with a Borrow -> Deref -> Execute trait chain for a generic System with dependencies for an ECS with queries using HRTBs.

This file caused the issue: https://github.com/ten3roberts/flax/blob/0fb4c6c92e913b0ef0b30965393e58d48416e114/examples/query/transform.rs

I managed to minimize the example to this:
https://github.com/ten3roberts/rustc_1_68_2_ice

Removing the SystemFn trait for a normal Fn or FnMut does not reproduce the issue.

Code

use core::marker::PhantomData;

struct Component<T>(PhantomData<T>);

struct Wrapper<T>(T);

impl<'a, Q: 'a> AsBorrow<'a> for Q {
    type Borrowed = QueryBorrow<'a, Q>;
}

pub struct QueryData<'a, Q> {
    marker: PhantomData<&'a Q>,
}

struct QueryBorrow<'w, Q> {
    marker: PhantomData<&'w Q>,
}

pub trait AsBorrow<'a> {
    /// The dereference target
    type Borrowed: 'a;
}

/// A callable function with 'self lifetime
/// Is not reproducible with Fn trait
trait SystemFn<'this, Args> {
    // fn execute(&'this mut self, args: Args) -> Ret;
}

pub fn build<Args, Func>(data: Args, func: Func)
where
    Func: for<'this, 'a> SystemFn<'this, Args>,
{
}

impl<'this, Func, A> SystemFn<'this, A> for Func
where
    for<'x> A: AsBorrow<'x>,
    for<'x> Func: Fn(<A as AsBorrow<'x>>::Borrowed),
{
}

fn main() {
    let component: Component<i32> = Component(PhantomData);

    let query = Wrapper(component);

    // Component instead of the actual Wrapper<Component<i32>, i32>>
    let system = build(query, |query: QueryBorrow<Component<i32>>| {});
}

Meta

The bug does not persist using cargo +nighly build

rustc --version --verbose:

rustc 1.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: x86_64-unknown-linux-gnu
release: 1.68.2
LLVM version: 15.0.6

Error output

error: internal compiler error: compiler/rustc_infer/src/infer/region_constraints/mod.rs:568:17: cannot relate bound region: ReLateBound(DebruijnIndex(0), BoundRegion { var
: 0, kind: BrNamed(DefId(0:43 ~ ice_bug[bed9]::main::'_), '_) }) <= ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:40 ~ ice_bug[bed9]::{impl#1}::
'x#1), 'x) })

thread 'rustc' panicked at 'Box<dyn Any>', /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/compiler/rustc_errors/src/lib.rs:987:33
Backtrace

stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: std::panic::panic_any::<rustc_errors::ExplicitBug>
   2: <rustc_errors::HandlerInner>::span_bug::<rustc_span::span_encoding::Span, &alloc::string::String>
   3: <rustc_errors::Handler>::span_bug::<rustc_span::span_encoding::Span, &alloc::string::String>
   4: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   5: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   6: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>
::{closure#0}, !>::{closure#0}, !>
   7: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>
   8: rustc_middle::util::bug::span_bug_fmt::<rustc_span::span_encoding::Span>
   9: <rustc_middle::ty::sty::Region as rustc_middle::ty::relate::Relate>::relate::<rustc_infer::infer::equate::Equate>
  10: rustc_middle::ty::relate::super_relate_tys::<rustc_infer::infer::equate::Equate>
  11: <rustc_infer::infer::equate::Equate as rustc_middle::ty::relate::TypeRelation>::tys
  12: <rustc_infer::infer::InferCtxt>::commit_if_ok::<rustc_infer::infer::InferOk<()>, rustc_middle::ty::error::TypeError, <rustc_infer::infer::at::Trace>::eq<rustc_middle:
:ty::Ty>::{closure#0}>
  13: <rustc_infer::infer::at::At>::eq::<rustc_middle::ty::Ty>
  14: <rustc_infer::infer::InferCtxt>::can_eq::<rustc_middle::ty::Ty>
  15: rustc_trait_selection::traits::error_reporting::suggestions::hint_missing_borrow
  16: <rustc_infer::infer::error_reporting::TypeErrCtxt as rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt>::report_closure_arg_mismatch
  17: <rustc_infer::infer::error_reporting::TypeErrCtxt as rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt>::report_selection_error
  18: <rustc_infer::infer::error_reporting::TypeErrCtxt as rustc_trait_selection::traits::error_reporting::InferCtxtPrivExt>::report_fulfillment_error
  19: <rustc_infer::infer::error_reporting::TypeErrCtxt as rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt>::report_fulfillment_errors
  20: <rustc_hir_typeck::fn_ctxt::FnCtxt>::demand_coerce
  21: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_decl
  22: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_block_with_expected
  23: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  24: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_return_expr
  25: rustc_hir_typeck::check::check_fn
  26: rustc_hir_typeck::typeck
  27: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_span::def_id::Local
DefId, &rustc_middle::ty::typeck_results::TypeckResults>
  28: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::queries::typeck, rustc_query_impl::plumbing::QueryCtxt>
  29: rustc_data_structures::sync::par_for_each_in::<&[rustc_span::def_id::LocalDefId], <rustc_middle::hir::map::Map>::par_body_owners<rustc_hir_typeck::typeck_item_bodies:
:{closure#0}>::{closure#0}>
  30: rustc_hir_typeck::typeck_item_bodies
  31: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, (), ()>
  32: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::queries::typeck_item_bodies, rustc_query_impl::plumbing::QueryCtxt>
  33: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::typeck_item_bodies
  34: <rustc_session::session::Session>::time::<(), rustc_hir_analysis::check_crate::{closure#7}>
  35: rustc_hir_analysis::check_crate
  36: rustc_interface::passes::analysis
  37: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, (), core::result::Result<
(), rustc_errors::ErrorGuaranteed>>
  38: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::queries::analysis, rustc_query_impl::plumbing::QueryCtxt>
  39: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::analysis
  40: <rustc_interface::passes::QueryContext>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}::{closure#2}, core::result::Result<(), rustc_errors::ErrorGuaran
teed>>
  41: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors:
:ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
  42: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_dri
ver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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.68.2 (9eb3afe9e 2023-03-27) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

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

query stack during panic:
#0 [typeck] type-checking `main`
#1 [typeck_item_bodies] type-checking all item bodies
#2 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `ice_bug`

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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions