Skip to content

Commit

Permalink
Merge pull request #408 from dtolnay/assoctype
Browse files Browse the repository at this point in the history
Generate trait bounds on associated types
  • Loading branch information
dtolnay authored Jan 8, 2025
2 parents c535cec + 6b3e1e5 commit 6cd87bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
11 changes: 6 additions & 5 deletions impl/src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ impl<'a> ParamsInScope<'a> {

fn crawl(in_scope: &ParamsInScope, ty: &Type, found: &mut bool) {
if let Type::Path(ty) = ty {
if ty.qself.is_none() {
if let Some(ident) = ty.path.get_ident() {
if in_scope.names.contains(ident) {
*found = true;
}
if let Some(qself) = &ty.qself {
crawl(in_scope, &qself.ty, found);
} else {
let front = ty.path.segments.first().unwrap();
if front.arguments.is_none() && in_scope.names.contains(&front.ident) {
*found = true;
}
}
for segment in &ty.path.segments {
Expand Down
19 changes: 19 additions & 0 deletions tests/test_generics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::needless_late_init, clippy::uninlined_format_args)]

use core::fmt::{self, Debug, Display};
use core::str::FromStr;
use thiserror::Error;

pub struct NoFormat;
Expand Down Expand Up @@ -160,6 +161,24 @@ pub struct StructFromGeneric<E> {
#[error(transparent)]
pub struct StructTransparentGeneric<E>(pub E);

// Should expand to:
//
// impl<T: FromStr> Display for AssociatedTypeError<T>
// where
// T::Err: Display;
//
// impl<T: FromStr> Error for AssociatedTypeError<T>
// where
// Self: Debug + Display;
//
#[derive(Error, Debug)]
pub enum AssociatedTypeError<T: FromStr> {
#[error("couldn't parse matrix")]
Other,
#[error("couldn't parse entry: {0}")]
EntryParseError(T::Err),
}

// Regression test for https://github.com/dtolnay/thiserror/issues/345
#[test]
fn test_no_bound_on_named_fmt() {
Expand Down

0 comments on commit 6cd87bc

Please sign in to comment.