Skip to content

const generic parameter is not ordered correctly on async fn with body #134

Closed
@zjijz

Description

@zjijz
#[async_trait]
trait Test {
    async fn run<const DUMMY: bool>(self) -> ()
    where
        Self: Sized
    {
        println!("what")
    }
}

expands to

trait Test {
    #[allow(clippy :: used_underscore_binding)]
    #[must_use]
    fn run<'async_trait, const DUMMY : bool>(self)
     ->
         ::core::pin::Pin<Box<dyn ::core::future::Future<Output = ()> +
                              ::core::marker::Send + 'async_trait>> where
     Self: Sized, Self: ::core::marker::Send + 'async_trait {
        #[allow(unused_parens, clippy :: missing_docs_in_private_items, clippy
                :: needless_lifetimes, clippy :: ptr_arg, clippy ::
                trivially_copy_pass_by_ref, clippy ::
                type_repetition_in_bounds, clippy ::
                used_underscore_binding,)]
        async fn __run<const DUMMY : bool, AsyncTrait: ?Sized + Test +
                       ::core::marker::Send>(_self: AsyncTrait) -> () where
         (): Sized, AsyncTrait: Sized {

            {
                ::std::io::_print(::core::fmt::Arguments::new_v1(&["what\n"],
                                                                 &match () {
                                                                      () =>
                                                                      [],
                                                                  }));
            }
        }
        Box::pin(__run::<Self, DUMMY>(self))
    }
}

Example: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=8958651135342165990e5cf7d6735145

The compiler gives these error messages:

error: type parameters must be declared prior to const parameters
 --> src/main.rs:5:1
  |
5 | #[async_trait]
  | ^^^^^^^^^^^^^^ help: reorder the parameters: lifetimes, then types, then consts: `<AsyncTrait: ?Sized + Test + ::core::marker::Send, const DUMMY: bool>`
  |
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0747]: type provided when a constant was expected
 --> src/main.rs:5:1
  |
5 | #[async_trait]
  | ^^^^^^^^^^^^^^
  |
  = note: type arguments must be provided before constant arguments
  = help: reorder the arguments: types, then consts: `<AsyncTrait, DUMMY>`
  = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

This issue seems to specifically be the param ordering on the inner async function: async fn __run<const DUMMY : bool, AsyncTrait: ?Sized + Test ....

I think expand.rs:420 is the offending line as it simply appends the _self type to the param list. I imagine this isn't an issue when only using lifetimes and types because this upholds the required order. This also means prepending doesn't solve the issue as well (since it would break using lifetime generics).

My idea would be to assume the user orders the manual generics properly and find insert the _self type right before const generics start. Another option is to sort the params after inserting, which would also fix an incorrect ordering provided by the user.

EDIT: This is while using #![feature(min_const_generics)].

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions