Skip to content

Consider associative types in utils::GenericsSearch#474

Merged
tyranron merged 6 commits into
masterfrom
fix-generics-search
May 27, 2025
Merged

Consider associative types in utils::GenericsSearch#474
tyranron merged 6 commits into
masterfrom
fix-generics-search

Conversation

@tyranron

@tyranron tyranron commented May 27, 2025

Copy link
Copy Markdown
Collaborator

Revealed from #473

Synopsis

utils::GenericsSearch doesn't consider associative type, leading to missing trait bounds in AsRef/AsMut expansions.

#[derive(AsRef)]
#[as_ref(forward)]
struct Forward<T: Some> {
    first: T::Assoc,
}

expands as:

#[allow(deprecated)]
#[allow(unreachable_code)]
#[automatically_derived]
impl<T: Some, __AsT: ?derive_more::core::marker::Sized> derive_more::core::convert::AsRef<__AsT> for Forward<T>
{
    #[inline]
    fn as_ref(&self) -> &__AsT { <T::Assoc as derive_more::core::convert::AsRef<__AsT>>::as_ref(&self.first) }
}

but should expand as:

#[allow(deprecated)]
#[allow(unreachable_code)]
#[automatically_derived]
impl<T: Some, __AsT: ?derive_more::core::marker::Sized> derive_more::core::convert::AsRef<__AsT> for Forward<T>
where
    T::Assoc: derive_more::core::convert::AsRef<__AsT>,
{
    #[inline]
    fn as_ref(&self) -> &__AsT { <T::Assoc as derive_more::core::convert::AsRef<__AsT>>::as_ref(&self.first) }
}

Solution

This PR fixes this situation and covers associative types usage in AsRef/AsMut tests.

Additionally

Improves utils::GenericsSearch performance by early returns (stop Visiting whenever something is found).

Also, unit tests for utils::GenericsSearch are added.

Checklist

  • Documentation is updated (not required)
  • Tests are added/updated
  • CHANGELOG entry is added

@tyranron tyranron added this to the 2.1.0 milestone May 27, 2025
@tyranron tyranron self-assigned this May 27, 2025
@tyranron tyranron marked this pull request as ready for review May 27, 2025 15:04
@tyranron tyranron enabled auto-merge (squash) May 27, 2025 15:05
@tyranron tyranron requested a review from JelteF May 27, 2025 15:05
@tyranron

Copy link
Copy Markdown
Collaborator Author

ping @JelteF

@tyranron tyranron merged commit 07dc39d into master May 27, 2025
17 checks passed
@tyranron tyranron deleted the fix-generics-search branch May 27, 2025 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants