Skip to content

Regression in trait bounds that are redundant with associated type's HRTB #57639

Closed
@rozbb

Description

@rozbb

The title is a mouthful, so let me explain and give some motivation. Say I have a trait MyTrait with an associated type MessageType. This message type must be deserializable (in the serde sense), i.e., MessageType: for<'a> Deserialize<'a>. Now suppose I have a generic struct GenericStruct<T> where T: MyTrait. I want the struct to contain a message, so I put a thing of type T::MessageType inside the struct. I would like GenericStruct to also be Deserializeable now, so I #[derive(Deserialize)].

Concretely, the code

use serde::Deserialize;

//trait DeserializeOwned: for<'a> Deserialize<'a> {}

trait MyTrait {
    type MessageType: Sized + for<'a> Deserialize<'a>;
    //type MessageType: Sized + DeserializeOwned;
}

#[derive(Deserialize)]
struct GenericStruct<T: MyTrait>(T::MessageType);

compiles in 1.31.1-stable and fails in 1.33.0-nightly with error message

error[E0308]: mismatched types
  |
  | #[derive(Deserialize)]
  |          ^^^^^^^^^^^ one type is more general than the other
  |
  = note: expected type `for<'a> _IMPL_DESERIALIZE_FOR_GenericStruct::_serde::Deserialize<'a>`
             found type `_IMPL_DESERIALIZE_FOR_GenericStruct::_serde::Deserialize<'de>`

Furthermore, if the commented lines are uncommented, and the first type line in MyTrait is commented out, the code now compiles in stable and nightly.

I don't believe this is version-specific behavior in serde. I would like to make a neater minimal testcase, but I don't know of any auto-derivable traits that only take a lifetime as a parameter.

Metadata

Metadata

Labels

P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions