Skip to content

ICE with GATs #50115

Closed
Closed
@Boscop

Description

@Boscop

What is the right way to do this?

pub trait ChanFx2: Clone + Default {
	type Ctx<'a> = ChannelProcessParams<'a>; // GAT with default type
	fn process<'a>(&mut self, msgs: Vec<MidiMsg>, params: &ChannelProcessParams<'a>, ctx: &Self::Ctx<'a>) -> Vec<MidiMsg>;
}

error: lifetime parameters are not allowed on this type

It's underlinining &Self::Ctx<'a> but how else am I supposed to instantiate the GAT with a lifetime?
Now I tried to omit the <'a>:

pub trait ChanFx2: Clone + Default {
	type Ctx<'a> = ChannelProcessParams<'a>;
	fn process<'a>(&mut self, msgs: Vec<MidiMsg>, params: &ChannelProcessParams<'a>, ctx: &Self::Ctx/*<'a>*/) -> Vec<MidiMsg>;
}

struct MidiMerger {}

struct SidechainCtx<'a> {
	inherited: ChannelProcessParams<'a>,
	sc_msgs: Vec<MidiMsg>,
}

impl ChanFx2 for MidiMerger {
	type Ctx<'a> = SidechainCtx<'a>;
	fn process<'a>(&mut self, mut msgs: Vec<MidiMsg>, params: &ChannelProcessParams<'a>, ctx: &Self::Ctx/*<'a>*/) -> Vec<MidiMsg> {
		msgs.extend(ctx.sc_msgs);
		msgs
	}
}

But I get this (ICE):

error: internal compiler error: src\main.rs:1: librustc\ty\subst.rs:427: Region parameter out of range when substituting in region 'a (root type=Some(fx::multichanfx::SidechainCtx<'a>)) (index=0)
thread 'rustc' panicked at 'Box<Any>', librustc_errors\lib.rs:482:9
stack backtrace:
   0: _rdl_shrink_in_place
   1: std::panicking::take_hook
   2: std::panicking::take_hook
   3: <unknown>
   4: std::panicking::rust_panic_with_hook
   5: <unknown>
   6: <unknown>
   7: rustc::session::bug_fmt
   8: rustc::ty::context::tls::span_debug
   9: rustc::ty::context::tls::span_debug
  10: <unknown>
  11: <unknown>
  12: rustc::ty::context::tls::span_debug
  13: rustc::ty::context::tls::span_debug
  14: rustc::session::bug_fmt
  15: rustc::session::bug_fmt
  16: <rustc::ty::subst::SubstFolder<'a, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_region
  17: rustc::ty::subst::<impl rustc::ty::Slice<rustc::ty::subst::Kind<'tcx>>>::truncate_to
  18: <rustc::ty::subst::SubstFolder<'a, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  19: rustc::traits::project::normalize_projection_type
  20: rustc::traits::project::normalize_projection_type
  21: rustc::traits::project::normalize_projection_type
  22: rustc::traits::project::normalize_projection_type
  23: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  24: rustc::ty::subst::<impl rustc::ty::Slice<rustc::ty::subst::Kind<'tcx>>>::truncate_to
  25: rustc::ty::structural_impls::<impl rustc::ty::context::Lift<'tcx> for rustc::ty::layout::LayoutError<'a>>::lift_to_tcx
  26: rustc::traits::project::poly_project_and_unify_type
  27: rustc::ty::wf::predicate_obligations
  28: <unknown>
  29: <unknown>
  30: rustc::ty::wf::trait_obligations
  31: <rustc_typeck::check::upvar::InferBorrowKind<'a, 'gcx, 'tcx> as rustc::middle::expr_use_visitor::Delegate<'tcx>>::mutate
  32: <rustc_typeck::check::wfcheck::CheckTypeWellFormedVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'v>>::visit_item
  33: rustc_typeck::check_crate
  34: rustc_typeck::check_crate
  35: <rustc_driver::derive_registrar::Finder as rustc::hir::itemlikevisit::ItemLikeVisitor<'v>>::visit_impl_item
  36: rustc_driver::driver::compile_input
  37: rustc_driver::run_compiler
  38: <unknown>
  39: _rust_maybe_catch_panic
  40: <rustc_driver::derive_registrar::Finder as rustc::hir::itemlikevisit::ItemLikeVisitor<'v>>::visit_impl_item
  41: std::sys::windows::thread::Thread::new
  42: BaseThreadInitThunk
  43: RtlUserThreadStart

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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.26.0-nightly (2789b067d 2018-03-06) running on x86_64-pc-windows-msvc

note: compiler flags: -C opt-level=1 -C debuginfo=2 -C debug-assertions=on -C incremental -C target-cpu=native --crate-type bin

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

What is the correct way to instantiate the GAT with a lifetime here?

Activity

Boscop

Boscop commented on Apr 20, 2018

@Boscop
Author

Similar situation with this, what's the right way to instantiate the GAT (with types this time)?

pub trait ToOther {
	type Other<P: AsPitch, V: AsVal, B: AsPitchBend>;

	fn to_other<Pitch: AsPitch, Val: AsVal, Pb: AsPitchBend>(&self) -> Self::Other/*<Pitch, Val, Pb>*/;
}

impl<Pitch: AsPitch, Val: AsVal, Pb: AsPitchBend> ToOther for HighLvlMsg<Pitch, Val, Pb> {
	type Other<P, V, B> = HighLvlMsg<P, V, B>;

	fn to_other<Pitch2: AsPitch, Val2: AsVal, Pb2: AsPitchBend>(&self) -> Self::Other/*<Pitch2, Val2, Pb2>*/ {
		use HighLvlMsg::*;

		match *self {
			NoteOn { pitch, vel, len } => NoteOn { pitch: AsPitch::conv(pitch), vel, len },
			NoteOff { pitch, vel } => NoteOff { pitch: AsPitch::conv(pitch), vel },
			ControlChange { cc, val } => ControlChange { cc, val: AsVal::conv(val) },
			ProgramChange { prog } => ProgramChange { prog },
			PitchBend { semitones } => PitchBend { semitones: AsPitchBend::conv(semitones) },
			PolyphonicAftertouch { pitch, val } => PolyphonicAftertouch { pitch: AsPitch::conv(pitch), val: AsVal::conv(val) },
			ChannelAftertouch { val } => ChannelAftertouch { val: AsVal::conv(val) },
		}
	}
}
error: internal compiler error: src\lib.rs:1: librustc\ty\subst.rs:481: Type parameter `P/#3` (P/3) out of range when substituting (root type=Some(highlvl::HighLvlMsg<P, V, B>)) substs=[_, _, _]
thread 'rustc' panicked at 'Box<Any>', librustc_errors\lib.rs:482:9
stack backtrace:
   0: _rdl_shrink_in_place
   1: std::panicking::take_hook
   2: std::panicking::take_hook
   3: <unknown>
   4: std::panicking::rust_panic_with_hook
   5: <unknown>
   6: <unknown>
   7: rustc::session::bug_fmt
   8: rustc::ty::context::tls::span_debug
   9: rustc::ty::context::tls::span_debug
  10: <unknown>
  11: <unknown>
  12: rustc::ty::context::tls::span_debug
  13: rustc::ty::context::tls::span_debug
  14: rustc::session::bug_fmt
  15: rustc::session::bug_fmt
  16: <rustc::ty::subst::SubstFolder<'a, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  17: rustc::ty::subst::<impl rustc::ty::Slice<rustc::ty::subst::Kind<'tcx>>>::truncate_to
  18: <rustc::ty::subst::SubstFolder<'a, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  19: rustc::traits::project::normalize_projection_type
  20: rustc::traits::project::normalize_projection_type
  21: rustc::traits::project::normalize_projection_type
  22: rustc::traits::project::normalize_projection_type
  23: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  24: rustc::ty::subst::<impl rustc::ty::Slice<rustc::ty::subst::Kind<'tcx>>>::truncate_to
  25: rustc::ty::structural_impls::<impl rustc::ty::context::Lift<'tcx> for rustc::ty::layout::LayoutError<'a>>::lift_to_tcx
  26: rustc::traits::project::poly_project_and_unify_type
  27: rustc::ty::wf::predicate_obligations
  28: <unknown>
  29: <unknown>
  30: rustc::ty::wf::trait_obligations
  31: <rustc_typeck::check::upvar::InferBorrowKind<'a, 'gcx, 'tcx> as rustc::middle::expr_use_visitor::Delegate<'tcx>>::mutate
  32: <rustc_typeck::check::wfcheck::CheckTypeWellFormedVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'v>>::visit_item
  33: rustc_typeck::check_crate
  34: rustc_typeck::check_crate
  35: <rustc_driver::derive_registrar::Finder as rustc::hir::itemlikevisit::ItemLikeVisitor<'v>>::visit_impl_item
  36: rustc_driver::driver::compile_input
  37: rustc_driver::run_compiler
  38: <unknown>
  39: _rust_maybe_catch_panic
  40: <rustc_driver::derive_registrar::Finder as rustc::hir::itemlikevisit::ItemLikeVisitor<'v>>::visit_impl_item
  41: std::sys::windows::thread::Thread::new
  42: BaseThreadInitThunk
  43: RtlUserThreadStart

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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.26.0-nightly (2789b067d 2018-03-06) running on x86_64-pc-windows-msvc

note: compiler flags: -C debuginfo=2 -C incremental --crate-type lib

note: some of the compiler flags provided by cargo are hidden
added
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.
C-bugCategory: This is a bug.
on Apr 20, 2018
pietroalbini

pietroalbini commented on Apr 20, 2018

@pietroalbini
Member

Thanks for reporting this! Can you provide a small example to reproduce the ICE? With the custom types you used it's difficult for us to bisect/reproduce ourself :)

Boscop

Boscop commented on Apr 20, 2018

@Boscop
Author
#![feature(generic_associated_types)]
#![feature(associated_type_defaults)]

struct Foo;
struct Bar;
struct Baz;

struct A<'a> {
	foo: &'a Foo,
}

struct B<'a> {
	inherited: A<'a>,
	own: Bar,
}

trait Trait: Clone + Default {
	type Ctx<'a> = A<'a>;
	fn process<'a>(&mut self, ctx: &Self::Ctx/*<'a>*/);
}

impl Trait for Baz {
	type Ctx<'a> = B<'a>;
	fn process<'a>(&mut self, ctx: &Self::Ctx/*<'a>*/) {}
}

fn main() {}

https://play.rust-lang.org/?gist=5fc07e14a633f3ec4db4de1c421053ff&version=nightly

sinkuu

sinkuu commented on Apr 21, 2018

@sinkuu
Contributor

This seems to be a known issue. GAT (#44265) is a WIP feature and probably not supposed to be usable yet.

//FIXME(#44265): "lifetime parameter not allowed on this type" errors will be addressed in a
// follow-up PR

dtolnay

dtolnay commented on Jun 16, 2018

@dtolnay
Member

Closing as a duplicate of #49362.

added
A-GATsArea: Generic associated types (GATs)
on Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)C-bugCategory: This is a bug.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsI-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

        Participants

        @Boscop@Centril@dtolnay@pietroalbini@sinkuu

        Issue actions

          ICE with GATs · Issue #50115 · rust-lang/rust