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

allow conversions between var types of range types and base types #24037

Merged
merged 4 commits into from
Sep 3, 2024

Conversation

metagn
Copy link
Collaborator

@metagn metagn commented Aug 31, 2024

refs #24032, split from #24036

Conversion from variables of range types or base types of range types to the other are now considered mutable for var params, similar to how distinct types are mutable when converted to their base type or vice versa. There are 2 main differences:

  1. Conversions from base types to range types need to emit nkChckRange, which is not generated for things like tuple/object fields.
  2. Range types can still correspond to different types in the backend when nested in other types, such as set[range[3..5]] vs set[range[0..5]].

Since the convertibility check for var params and a check whether to emit a no-op for nkConv (and now also nkChckRange) so that the output is still addressable both use sameType, we accomplish this by adding a new flag to sameType that ignores range types, but only when they're not nested in other types. The implementation for this might be flawed, I didn't include children of some metatypes as "nested in other types", but stuff like tyGenericInst params are respected.

@metagn
Copy link
Collaborator Author

metagn commented Aug 31, 2024

Maybe cast is more appropriate since this doesn't check that the var range is still in range after the call, but it seems too verbose. I don't know if we could actually generate the checks practically, that would remove the need for a mismatch anyway.

@Araq Araq merged commit 538603e into nim-lang:devel Sep 3, 2024
19 checks passed
Copy link
Contributor

github-actions bot commented Sep 3, 2024

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

Hint: mm: orc; opt: speed; options: -d:release
174034 lines; 9.094s; 655.125MiB peakmem

metagn added a commit to metagn/Nim that referenced this pull request Sep 3, 2024
Araq pushed a commit that referenced this pull request Sep 6, 2024
refs #21682, refs #24038

The `rangeBase` typetrait added in #21682 which gives the base type of a
range type is now added publicly to `typetraits`. Previously it was only
privately used in `repr_v2` and in `enumutils` since #24052
(coincidentally I didn't see this until now). This is part of an effort
to make range types easier to work with in generics, as mentioned in
#24038. Its use combined with #24037 is also tested.

The condition for the "enum to enum conversion" warning is now also
restricted to conversions between different enum base types, i.e.
conversion between an enum type and a range type of itself doesn't give
a warning. I put this in this PR since the test gave the warning and so
works as a regression test.
Araq pushed a commit that referenced this pull request Sep 17, 2024
fixes #24097

For `nkConv` addresses where the conversion is between 2 types that are
equal between backends, treat assignments the same as assignments to the
argument of the conversion. In the VM this seems to be in `genAsgn` and
`genAsgnPatch`, as evidenced by the special logic for `nkDerefExpr` etc.

This doesn't handle ranges after #24037 because `sameBackendType` is
used and not `sameBackendTypeIgnoreRange`. This is so this is
backportable without #24037 and another PR can be opened that implements
it for ranges and adds tests as well. We can also merge
`sameBackendTypeIgnoreRange` with `sameBackendType` since it doesn't
seem like anything that uses it would be affected (only cycle checks and
the VM), but then we still have to add tests.
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