-
-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -725,13 +725,6 @@ proc newLit*(s: string): NimNode {.compileTime.} = | |
result = newNimNode(nnkStrLit) | ||
result.strVal = s | ||
|
||
when false: | ||
# the float type is not really a distinct type as described in https://github.com/nim-lang/Nim/issues/5875 | ||
proc newLit*(f: float): NimNode {.compileTime.} = | ||
## Produces a new float literal node. | ||
result = newNimNode(nnkFloatLit) | ||
result.floatVal = f | ||
|
||
proc newLit*(f: float32): NimNode {.compileTime.} = | ||
## Produces a new float literal node. | ||
result = newNimNode(nnkFloat32Lit) | ||
|
@@ -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 = | ||
## convert and distinct value to its base type. | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. since: (1,3) |
||
result = newCall(bindSym"T", newLit(undistinct(arg))) | ||
|
||
proc nestList*(op: NimNode; pack: NimNode): NimNode {.compileTime.} = | ||
## Nests the list `pack` into a tree of call expressions: | ||
## ``[a, b, c]`` is transformed into ``op(a, op(c, d))``. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 importantlydistinctBase
works ontypedesc
.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.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.