Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove defaults from trait params when using them in function definition #123

Merged
merged 3 commits into from
Aug 28, 2020
Merged

Remove defaults from trait params when using them in function definition #123

merged 3 commits into from
Aug 28, 2020

Conversation

tyranron
Copy link
Contributor

@tyranron tyranron commented Aug 28, 2020

Synopsis

At the moment, if trait has type/const parameters with default values and also has a default method implementation, like the following:

#[async_trait]
trait Trait<T = ()> {
    async fn f(&self) -> &str
    where
        T: 'async_trait,
    {
        "default"
    }
}

The expanded code is:

trait Trait<T = ()> {
    #[must_use]
    fn f<'life0, 'async_trait>(
        &'life0 self,
    ) -> ::core::pin::Pin<
        Box<dyn ::core::future::Future<Output = &str> + ::core::marker::Send + 'async_trait>,
    >
    where
        T: 'async_trait,
        'life0: 'async_trait,
        Self: ::core::marker::Sync + '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 __f<
            'async_trait,
            T = (),
            AsyncTrait: ?Sized + Trait<T> + ::core::marker::Sync,
        >(
            _self: &AsyncTrait,
        ) -> &str
        where
            (): Sized,
            T: 'async_trait,
        {
            "default"
        }
        Box::pin(__f::<T, Self>(self))
    }
}

And rustc complains with the error:

error: type parameters with a default must be trailing
   --> tests/test.rs:987:17
    |
987 |     trait Trait<T = ()> {
    |                 ^
error: could not compile `async-trait`.

error: defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions.
   --> tests/test.rs:987:17
    |
987 |     trait Trait<T = ()> {
    |                 ^
    |
    = note: `#[deny(invalid_type_param_default)]` on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>

Solution

Filter out defaults when reusing trait generics for function definition.

Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dtolnay dtolnay merged commit 6d67b02 into dtolnay:master Aug 28, 2020
@tyranron tyranron deleted the remove-defaults-from-typarams-in-traits branch August 29, 2020 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants