Skip to content

Incorrect use of trait object makes rust-analyzer panic #21605

@JayanAXHF

Description

@JayanAXHF

The following (the code is incorrect) makes the rust-analyzer request handler panic with the following error:

rust-analyzer: -32603: request handler panicked: could not replace AliasTerm { args: [dyn [Binder { value: Trait(ExistentialTraitRef("Fn"[[(&'?0 !0,)]])), bound_vars: [] }, Binder
 { value: Projection(ExistentialProjection { def_id: TypeAliasId("Output"), args: [(&'?0 !0,)], term: bool, use_existential_projection_new_instead: () }), bound_vars: [] }] + '?1,
 (&'?2 Alias(Projection, AliasTy { args: [Iter<'?3, !0>], def_id: TypeAliasId("Item"), .. }),)], def_id: TypeAliasId("Output"), .. } with term from from dyn [Binder { value: Trait
(ExistentialTraitRef("Fn"[[(&'?0 !0,)]])), bound_vars: [] }, Binder { value: Projection(ExistentialProjection { def_id: TypeAliasId("Output"), args: [(&'?0 !0,)], term: bool, use_
existential_projection_new_instead: () }), bound_vars: [] }] + '?1

rust-analyzer version: rust-analyzer 1.93.0 (254b5960 2026-01-19)

rustc version: rustc 1.93.0 (254b59607 2026-01-19)

editor or extension: neovim with rustaceanvim

relevant settings: No particular settings for cargo, rustc or rust-analyzer

repository link (if public, optional): not public yet

code snippet to reproduce:

pub struct Filter<'a, 'b, T>
where
    T: 'b,
    'a: 'b,
{
    filter_fn: dyn Fn(&'a T) -> bool,
    _phantom: std::marker::PhantomData<T>,
    _marker: std::marker::PhantomData<&'b ()>,
}

impl<'a, 'b, T> Filter<'a, 'b, T>
where
    T: 'b,
    'a: 'b,
{
    pub fn new(filter_fn: dyn Fn(&T) -> bool) -> Self {
        Self {
            filter_fn: filter_fn,
            _phantom: Default::default(),
            _marker: Default::default(),
        }
    }
}

pub trait FilterExt<T> {
    type Output;
    fn filter(&self, filter: &Filter<T>) -> Self::Output;
}

impl<T> FilterExt<T> for Vec<T>
where
    T: IntoIterator,
{
    type Output = T;
    fn filter(&self, filter: &Filter<T>) -> Self::Output {
        self.into_iter().filter(filter.filter_fn).collect()
    }
}

Metadata

Metadata

Assignees

Labels

A-tytype system / type inference / traits / method resolutionC-bugCategory: bugI-panic

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions