Skip to content

Nonsensical rustc error: expected and found are same #144550

@papaymaguire

Description

@papaymaguire

Code

use statig::{
    Outcome::Super,
    awaitable::{IntoStateMachine, Outcome, State, Superstate},
};

enum BaseEvent {
    Pause,
    Resume,
    Confirm,
    Recover,
    Cancel,
}

pub enum AllEvents<FE> {
    Base(BaseEvent),
    FlowEvent(FE),
}

pub enum DoneStates {
    Finished,
    Cancelled,
    Panicked,
}

pub enum NotRunningStates {
    Idle,
    Paused,
    Blocked,
    Errored,
}

pub enum AllStates<FS> {
    Done(DoneStates),
    NotRunning(NotRunningStates),
    FlowState(FS),
}

pub enum AllSuperstates<FSS> {
    Common,
    Done,
    NotRunning,
    Running,
    FlowSuperstate(FSS),
}

pub trait BaseHandlers<S>
where
    for<'sub, 'evt> S: IntoStateMachine<
            State = AllStates<S>,
            Superstate<'sub> = AllSuperstates<S>,
            Event<'evt> = AllEvents<S>,
        > + 'sub,
    for<'b> S::Superstate<'b>: Superstate<S>,
{
    fn finished(event: &S::Event<'_>) -> Outcome<AllStates<S>> {
        Outcome::Handled
    }

    fn cancelled(event: &S::Event<'_>) -> Outcome<AllStates<S>> {
        Outcome::Handled
    }

    fn panicked(event: &S::Event<'_>) -> Outcome<AllStates<S>> {
        Outcome::Handled
    }
}

impl<S> State<S> for AllStates<S>
where
    // for some lifetime, S must be a state machine with AllStates as its State type, AllSuperstates as its Superstate type, implement BaseHandlers, and have the same lifetime
    for<'sub, 'evt> S: IntoStateMachine<
            State = AllStates<S>,
            Superstate<'sub> = AllSuperstates<S>,
            Event<'evt> = AllEvents<S>,
        > + BaseHandlers<S>
        + 'sub,
    for<'b> S::Superstate<'b>: Superstate<S>,
{
    fn call_handler(
        &mut self,
        shared_storage: &mut S,
        event: &S::Event<'_>,
        context: &mut S::Context<'_>,
    ) -> impl Future<Output = statig::Outcome<Self>> {
        async move {
            match self {
                AllStates::Done(done_state) => match done_state {
                    DoneStates::Finished => S::finished(event),
                    DoneStates::Cancelled => S::cancelled(event),
                    DoneStates::Panicked => S::panicked(event),
                },
                _ => panic!(),
            }
        }
    }

    fn superstate(&mut self) -> Option<<S as IntoStateMachine>::Superstate<'_>> {
        match self {
            AllStates::Done(_) => Some(AllSuperstates::Done),
            AllStates::NotRunning(_) => Some(AllSuperstates::NotRunning),
            AllStates::FlowState(_) => S::State::superstate(self),
        }
    }
}

impl<S> Superstate<S> for AllSuperstates<S>
where
    for<'sub, 'evt> S: IntoStateMachine<
            State = AllStates<S>,
            Superstate<'sub> = AllSuperstates<S>,
            Event<'evt> = AllEvents<S>,
        > + BaseHandlers<S>
        + 'sub,
    for<'b> S::Superstate<'b>: Superstate<S>,
{
    fn call_handler(
        &mut self,
        shared_storage: &mut S,
        event: &S::Event<'_>,
        context: &mut S::Context<'_>,
    ) -> impl Future<Output = Outcome<<S as IntoStateMachine>::State>> {
        async move {
            match self {
                AllSuperstates::Common => {
                    // Handle common superstate events
                    Outcome::Handled
                }
                _ => panic!(),
            }
        }
    }
}

Current output

method `call_handler` has an incompatible type for trait
expected signature `fn(&mut core::flows::AllSuperstates<_>, &mut _, &core::flows::AllEvents<_>, &mut _) -> impl futures::Future<Output = Outcome<<S as statig::awaitable::IntoStateMachine>::State>>`
   found signature `fn(&mut core::flows::AllSuperstates<_>, &mut _, &core::flows::AllEvents<_>, &mut _) -> impl futures::Future<Output = Outcome<<S as statig::awaitable::IntoStateMachine>::State>>`

Desired output

No error

Rationale and extra context

Trying to do some work extending Statig, pretty intense type and lifetime bounds. It looks like the compiler became confused at some point sorting through the type bounds, and shows an error that the expected and found signature do not match, except that they do.

Other cases

Rust Version

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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