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

implementation for newLit on distinct types (fixes #13266) #13274

Closed
wants to merge 3 commits into from

Conversation

krux02
Copy link
Contributor

@krux02 krux02 commented Jan 28, 2020

newLit should work on all types, including distinct types. This fixes it.

Minor change, I removed some dead code of mine.

@krux02
Copy link
Contributor Author

krux02 commented Feb 5, 2020

@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 distinct back then.

@Araq
Copy link
Member

Araq commented Feb 6, 2020

Needs a .since annotation and a changelog entry.

@@ -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 =
Copy link
Contributor

@Clyybber Clyybber Feb 6, 2020

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?

Copy link
Contributor Author

@krux02 krux02 Feb 6, 2020

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.

Copy link
Member

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)

Copy link
Contributor Author

@krux02 krux02 Mar 9, 2020

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.

Copy link
Member

@timotheecour timotheecour Apr 5, 2020

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.

@krux02
Copy link
Contributor Author

krux02 commented Apr 5, 2020

@Araq this PR is ready and can be merged.

@timotheecour
Copy link
Member

timotheecour commented Apr 5, 2020

good PR, but:

  • needs rebase

  • macro undistinct[T: distinct](arg: T): untyped = (even if private) should definitely reference typetraits.distinctBase as a doc comment (doc comments still useful for those) since it does the same thing as template distinctBase[T](a: T): untyped (recently added overload)

  • can you please add:

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)

  • your tests would fail with --styleCheck:error

let baseTyp = getTypeImpl(arg)[0]
result = newCall(baseTyp, arg)

proc newLit*[T : distinct](arg: T): NimNode {.compileTime, since: (1,1).} =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since: (1,3)

@timotheecour
Copy link
Member

timotheecour commented Apr 12, 2020

@Araq @krux02 this PR is useful, can we move towards merging it after the needed conflict rebase? either addressing the comments i raised or doing that in followup PR's (which i can do if necessary)

@Araq
Copy link
Member

Araq commented Apr 12, 2020

But why should this work? It feels like you don't understand distinct types. Distinct types hide their implementation, yes newLit(D(1)) doesn't work. So what? That's not a literal... Maybe construct D(1) as newCall(bindSym"D", newLit(1)) then. A macro system is somewhat like a type system ... it works better if not everything compiles just because "omg it's useful in generic programming" (what isn't?)

@nitely
Copy link
Contributor

nitely commented Nov 17, 2020

@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.

@timotheecour
Copy link
Member

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.

+1 to that, no need to add arbitrary restrictions on litterals

@Araq
Copy link
Member

Araq commented Nov 18, 2020

D(1) is not a literal, it's like you don't know what a literal is ... "restrictions on literals" -- give me a break. "Abitrary restrictions on what a tree can be. Restrictions on what a house can look like".

@stale
Copy link

stale bot commented Nov 18, 2021

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.

@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label Nov 18, 2021
@stale stale bot closed this Dec 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Staled PR/issues; remove the label after fixing them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants