-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
implementation for newLit on distinct types (fixes #13266) #13274
Conversation
@Araq @narimiran @nitely @cooldome any feedback on this? Is it ok? Is it not ok? Since I originally introduced most overloads of newLit, I would normally be resonsible to review this. The only reason that I didn't do this earlier is, I wasn't very familiar enough with |
Needs a |
@@ -798,6 +791,14 @@ proc newLit*(arg: tuple): NimNode {.compileTime.} = | |||
for a,b in arg.fieldPairs: | |||
result.add nnkExprColonExpr.newTree(newIdentNode(a), newLit(b)) | |||
|
|||
macro undistinct[T: distinct](arg: T): untyped = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use distinctBase instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well distinctBase
requires a new import/dependency. I don't like that. But more importantly distinctBase
works on typedesc
. typedesc
is a source of problems, bugs and complications. If I want my code to work reliably I avoid it. That is possible here without adding much boilerplate code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
distinctBase
doesn't require an import dependency now that it's a magic.
proc distinctBase(T: typedesc): typedesc {.magic: "TypeTrait".}
Having 2 different ways to do implement the safe feature is not good (if it's broken, please show an example where it breaks)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timotheecour Just an example: #12582
That is a very typical bug that you hit just because you are using typedesc. If you want to avoid accidentally hitting compiler bugs like that, the only advice that works is, don't use typedesc
.
But here are more typedesc related problems. Of cours the reported problems are only showing the tip of the iceberg. I encountered several more problems of typedesc
that I did not report.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not aware of a bug involving distinctBase
specifically, if you find one, please file an issue.
6d58376
to
a8fe648
Compare
@Araq this PR is ready and can be merged. |
good PR, but:
block: # test 2 level distinct
type Rune2 = distinct Rune
macro testNewLitDistinct2(): untyped = newLit(Rune2(123))
doAssert testNewLitDistinct2() is Rune2 (i checked it works, but useful for regression testing)
|
let baseTyp = getTypeImpl(arg)[0] | ||
result = newCall(baseTyp, arg) | ||
|
||
proc newLit*[T : distinct](arg: T): NimNode {.compileTime, since: (1,1).} = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since: (1,3)
But why should this work? It feels like you don't understand |
@Araq what about containers of distinct types, and objects with distinct type fields? I don't know why distinct types cannot be literals, or why adding a distinct type field to an object makes it not a literal anymore. Aside from that, it'd be nice if macros had some way to do this conversion. On the other hand I'm the first one to ever encounter this (or at least report it), so maybe is not common/worth while. |
+1 to that, no need to add arbitrary restrictions on litterals |
|
This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions. |
newLit
should work on all types, includingdistinct
types. This fixes it.Minor change, I removed some dead code of mine.