Nim icon indicating copy to clipboard operation
Nim copied to clipboard

stricter `var`/pointer type conversions, subranges under preview define

Open metagn opened this issue 1 year ago • 4 comments

closes #24032, split from #24036

var/pointer types are no longer implicitly convertible to each other if their element types either:

  • require an int conversion or another conversion operation as long as it's not to openarray,
  • are subtypes with pointer indirection,
  • are a base type/subrange pair if -d:nimPreviewStrictVarRange is not defined.

Previously any conversion below a subrange match would match if the element type wasn't a pointer type.

The range mismatch is opt-in for now because:

  1. it breaks a lot of code, things like +=/-=/atomicInc/fromJson etc aren't implemented for range types,
  2. there aren't any mechanisms for addressably converting ranges to their base types, #24037 adds one, there also isn't a rangeBase typetrait or a typeclass for range types with a specific base type

So more work is needed before this is usable.

Only normalize needs to be patched for the non-opt-in changes: https://github.com/nitely/nim-normalize/pull/9

metagn avatar Aug 31 '24 17:08 metagn

Any chance this could emit a warning without the define, that way it does not surprise anyone when it eventually becomes the default?

beef331 avatar Aug 31 '24 20:08 beef331

Didn't think of that, will look into it. My intention was to include in the mismatch error message "incompatible range type, convert to base type doing int(foo)" or whatever but there's not even an alternative you can suggest yet. I only made this PR so it could be standalone from the draft, we could technically merge this since it's opt-in but realistically it won't be usable until like 3 more PRs. And because it breaks so much code it won't be default for a while and will most likely be a legacy switch even when it is default.

So there shouldn't be too much cause for alarm, I tried to word the changelog so it wouldn't seem urgent but I don't know. It would be a lot easier to explain it if we had concrete alternatives.

metagn avatar Aug 31 '24 21:08 metagn

This pull request is stale because it has been open for 1 year with no activity. Contribute more commits on the pull request and rebase it on the latest devel, or it will be closed in 30 days. Thank you for your contributions.

github-actions[bot] avatar Oct 03 '25 00:10 github-actions[bot]

This pull request has been marked as stale and closed due to inactivity after 395 days.

github-actions[bot] avatar Nov 03 '25 00:11 github-actions[bot]