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

add default field support for object in ARC/ORC #20480

Merged
merged 61 commits into from
Oct 4, 2022
Merged

Conversation

ringabout
Copy link
Member

@ringabout ringabout commented Oct 2, 2022

closes nim-lang/RFCs#126
closes nim-lang/RFCs#48
closes nim-lang/RFCs#233
closes nim-lang/RFCs#252
closes #12378
fixes #19763
fixes #16744
fixes #3608
ref nim-lang/RFCs#437

inspired by #12378

default fields for object

  • object variant

the addition compared to #12378

  • var
  • omit type for default fields
  • default(Type)
  • new(ref)
  • nested object definition
  • array
  • tuples
  • an object of array etc. in the definition of the object
  • threadvar
  • noinit
  • range
  • fieldVisible issue
  • distinct
  • newFinalize
  • seq(var, result, setLen)
  • tests

notes for me only

abstractInst vs {tyGenericInst, tyAlias, tySink}

@ringabout
Copy link
Member Author

ringabout commented Oct 2, 2022

In the following up PRs, there are few improvements which can be applied:

  • provide a typetrait proc to detect whether the type has a default value
  • provides a cache table for analyzed types
  • provides a {.noInitDefaultValue.} (?) for procs
  • documentation

@@ -680,6 +689,9 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
addToVarSection(c, result, n, a)
continue
var v = semIdentDef(c, a[j], symkind, false)
if {sfThread, sfNoInit} * v.flags != {}:
a[^1] = c.graph.emptyNode
def = c.graph.emptyNode
Copy link
Member

Choose a reason for hiding this comment

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

Seems to be a bad idea, what about var x {.noInit.} = f()? You turn this into var x {.noInit.} = <default>? Why?

Copy link
Member Author

@ringabout ringabout Oct 3, 2022

Choose a reason for hiding this comment

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

My bad, I forget to consider that situation. Now I use a node flag to distinguish whether the node is from default field initialization or from explicit initialization ee53173

nfUseDefaultField is originally used for object fields and the two usages don't overlap.

@@ -1639,6 +1651,20 @@ proc addResult(c: PContext, n: PNode, t: PType, owner: TSymKind) =
n.add newSymNode(c.p.resultSym)
addParamOrResult(c, c.p.resultSym, owner)

proc addDefaultFieldForResult(c: PContext, n: PNode, t: PType) =
Copy link
Member

Choose a reason for hiding this comment

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

Why?

Copy link
Member Author

Choose a reason for hiding this comment

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

addDefaultFieldForResult adds default values for implicit variables within procs:

transform

proc foo(): Object =
  discard

into

proc foo(): Object =
  result = Object()
  discard

So that default values are applied.

Copy link
Member

Choose a reason for hiding this comment

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

This is unnecessary if we do nim-lang/RFCs#378 and I think we should.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will undo the default fields for results.

let field = defaultNodeField(c, a[^2])
if field != nil:
a[^1] = field
let m = copyTree(a[0])
Copy link
Member

Choose a reason for hiding this comment

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

Why does this need a copyTree here?

Copy link
Member Author

@ringabout ringabout Oct 4, 2022

Choose a reason for hiding this comment

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

It is an old change and has been removed since ee53173. And it didn't work since semIdentDef(c, m, symkind, false) affected the ic tests.

@Araq Araq merged commit f89ba2c into nim-lang:devel Oct 4, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Oct 4, 2022

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

Hint: mm: orc; opt: speed; options: -d:release
162695 lines; 8.563s; 665.113MiB peakmem

@metagn
Copy link
Collaborator

metagn commented Oct 28, 2022

Was this documented? Not in changelog either

@ringabout
Copy link
Member Author

It will be documented after bugs are fixed.

capocasa pushed a commit to capocasa/Nim that referenced this pull request Mar 31, 2023
* fresh start

* add cpp target

* add result support

* add nimPreviewRangeDefault

* reduce

* use orc

* refactor common parts

* add tuple support

* add testcase for tuple

* cleanup; fixes nimsuggest tests

* there is something wrong with cpp

* remove

* add support for seqs

* fixes style

* addd initial distinct support

* remove links

* typo

* fixes tuple defaults

* add rangedefault

* add cpp support

* fixes one more bugs

* add more hasDefaults

* fixes ordinal types

* add testcase for nim-lang#16744

* add testcase for nim-lang#3608

* fixes docgen

* small fix

* recursive

* fixes

* cleanup and remove tuple support

* fixes nimsuggest

* fixes generics procs

* refactor

* increases timeout

* refactor hasDefault

* zero default; disable i386

* add tuples back

* fixes bugs

* fixes tuple

* add more tests

* fix one more bug regarding tuples

* more tests and cleanup

* remove messy distinct types which must be initialized by original types

* add tests

* fixes zero default

* fixes grammar

* fixes tests

* fixes tests

* fixes tests

* fixes comments

* fixes and add testcase

* undo default values for results

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>
narimiran pushed a commit that referenced this pull request May 29, 2023
* fresh start

* add cpp target

* add result support

* add nimPreviewRangeDefault

* reduce

* use orc

* refactor common parts

* add tuple support

* add testcase for tuple

* cleanup; fixes nimsuggest tests

* there is something wrong with cpp

* remove

* add support for seqs

* fixes style

* addd initial distinct support

* remove links

* typo

* fixes tuple defaults

* add rangedefault

* add cpp support

* fixes one more bugs

* add more hasDefaults

* fixes ordinal types

* add testcase for #16744

* add testcase for #3608

* fixes docgen

* small fix

* recursive

* fixes

* cleanup and remove tuple support

* fixes nimsuggest

* fixes generics procs

* refactor

* increases timeout

* refactor hasDefault

* zero default; disable i386

* add tuples back

* fixes bugs

* fixes tuple

* add more tests

* fix one more bug regarding tuples

* more tests and cleanup

* remove messy distinct types which must be initialized by original types

* add tests

* fixes zero default

* fixes grammar

* fixes tests

* fixes tests

* fixes tests

* fixes comments

* fixes and add testcase

* undo default values for results

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>
(cherry picked from commit f89ba2c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment