Skip to content

fixes #22936; Generic inheritance matching gives type mismatch when object has members#25836

Open
ringabout wants to merge 3 commits into
develfrom
pr_uiiu
Open

fixes #22936; Generic inheritance matching gives type mismatch when object has members#25836
ringabout wants to merge 3 commits into
develfrom
pr_uiiu

Conversation

@ringabout
Copy link
Copy Markdown
Member

fixes #22936

This pull request improves the compiler's handling of generic type constraints, specifically for subtypes of generics, and adds a test to cover this behavior. The main changes are an enhancement to the type relationship logic in the compiler and a new test case for generic subtyping with Future.

Compiler improvements for generic subtyping

  • Updated typeRel in compiler/sigmatch.nim to allow generic constraints (like F: Future) to accept not just direct instantiations but also descendants of the generic family, ensuring more flexible and correct overload resolution. Inheritance depth is now considered for overload ranking, making deeper descendants slightly less preferred, consistent with other inheritance-based matches.

New test coverage

  • Added a test in tests/typerel/t8905.nim to verify that generic constraints correctly accept subtypes of Future, including a custom B[T, E] = ref object of Future[T] type, and that overloads like take, takeMany, and the macro checkFutures work as expected with these types.

Copilot AI review requested due to automatic review settings May 22, 2026 14:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes issue #22936 by improving the compiler’s type relationship logic so generic constraints (e.g. F: Future) accept not only direct instantiations (Future[T]) but also subtypes/descendants of that generic family, and by adding a regression test covering the scenario.

Changes:

  • Extend typeRel handling for tyGenericBody constraints to recognize descendant types via isGenericSubtype, not just direct tyGenericInst matches.
  • Incorporate inheritance depth into overload ranking for these generic-family descendant matches (deeper descendants become slightly less preferred).
  • Add a regression test ensuring generic constraints accept subtypes of Future[T] even when the object has members, including through a macro signature.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
compiler/sigmatch.nim Updates typeRel for tyGenericBody constraints to accept generic-family descendants and apply inheritance-based ranking penalties.
tests/typerel/t8905.nim Adds a regression test for generic subtyping with Future[T] descendants (including macro parameter matching).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

Generic inheritance matching gives type mismatch when object has members

2 participants