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

make sigmatch use prepareNode for tyFromExpr #24095

Merged
merged 2 commits into from
Sep 11, 2024

Conversation

metagn
Copy link
Collaborator

@metagn metagn commented Sep 10, 2024

fixes regression remaining after #24092

In #24092 prepareNode was updated so it wouldn't try to instantiate generic type symbols (like Generic when type Generic[T] = object, and prepareNode is what tyFromExpr uses in most of the compiler. An exception is in sigmatch, which is now changed to use prepareNode to make generic type symbols work in the same way as usual. However this requires another change to work:

Dot fields and matches to typedesc on generic types generate tyFromExpr in generic contexts since #24005, including generic type symbols. But this means when we try to instantiate the tyFromExpr in sigmatch, which increases c.inGenericContext for potentially remaining unresolved expressions, dotcalls stay as tyFromExpr and so never match. To fix this, we change the "generic type" check in dot fields and typedesc matching to an "unresolved type" check which excludes generic body types; and for generic body types, we only generate tyFromExpr if the dot field is a generic parameter of the generic type (so that it gets resolved only at instantiation).

Notes for the future:

  • Sigmatch shouldn't have to inc c.inGenericContext, if a tyFromExpr can't instantiate it's fine if we just fail the match (i.e. redirect the instantiation errors from semtypinst to a match failure). Then again maybe this is the best way to check for inability to instantiate.
  • The elif c.inGenericContext > 0 and t.containsUnresolvedType check in dotfields could maybe be simplified to just checking for tyFromExpr and tyGenericParam, but I don't know if this is an exhaustive list.

@metagn metagn changed the title test refactor to fix regression remaining after #24092 make sigmatch use prepareNode for tyFromExpr Sep 11, 2024
@metagn metagn marked this pull request as ready for review September 11, 2024 09:11
@Araq Araq merged commit 9dda7ff into nim-lang:devel Sep 11, 2024
19 checks passed
Copy link
Contributor

Thanks for your hard work on this PR!
The lines below are statistics of the Nim compiler built from 9dda7ff

Hint: mm: orc; opt: speed; options: -d:release
174289 lines; 8.095s; 654.352MiB peakmem

metagn added a commit to metagn/Nim that referenced this pull request Sep 11, 2024
Araq pushed a commit that referenced this pull request Sep 11, 2024
follows up #24095

In #24095 a check was added that used `iterOverType` to check if a type
contained unresolved types, with the aim of always treating
`tyGenericBody` as resolved. But the body of the `tyGenericBody` is also
iterated over in `iterOverType`, so if the body of the type actually
used generic parameters (which isn't the case in the test added in
#24095, but is now), the check would still count the type as unresolved.

This is handled by not iterating over the children of `tyGenericBody`,
the only users of `iterOverType` are `containsGenericType` and
`containsUnresolvedType`, the first one always returns true for
`tyGenericBody` and the second one aims to always return false.
Unfortunately this means `iterOverType` isn't as generic of an API
anymore but maybe it shouldn't be used anymore for these procs.
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