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

incorrect ProveInitwith delayed initialization of field #3608

Closed
jangko opened this issue Dec 1, 2015 · 12 comments · Fixed by #20480
Closed

incorrect ProveInitwith delayed initialization of field #3608

jangko opened this issue Dec 1, 2015 · 12 comments · Fixed by #20480

Comments

@jangko
Copy link
Contributor

jangko commented Dec 1, 2015

proc make[T](): T = new(result)

produces warning:

nimpng.nim(1926, 7) Warning: Cannot prove that 'result' is initialized. This wil
l become a compile time error in the future. [ProveInit]

how could the 'result' is not initialized? 😄

@Araq
Copy link
Member

Araq commented Dec 1, 2015

Not enough context. It could be that 'result' has not-nil fields etc.

@jangko
Copy link
Contributor Author

jangko commented Dec 1, 2015

ok, here is the minimal code to reproduce the warning

type
  xx = ref object of RootObj
    a: range[0..0x7FFFFFFF]

  zz = ref object of xx
    b: range[1..0x7FFFFFFF]

proc make[T](): T = new(result)

discard make[xx]()
discard make[zz]()  #if this line is removed, the warning dissapear

shouldn't both line cause warning? and why uninitialized range should become compile time error in the future? couldn't the uninitialized range like range[1..n] just given value 1 like range[0..n] default value is 0?

@jangko
Copy link
Contributor Author

jangko commented Dec 9, 2015

this very simple example also produce incorrrect warning same as above

type
  abc* = ref object
    w: range[2..100]

proc createABC(): abc =
  new(result)
  result.w = 20 #i think the compiler missed to check this part

the warning doesn't make any sense, right?

@Araq
Copy link
Member

Araq commented Dec 9, 2015

On the contrary, it works as designed. That doesn't mean I like it, but it is not a bug.

Use this instead:

proc createABC(): abc =
  result = abc(w: 20)

@jangko
Copy link
Contributor Author

jangko commented Dec 10, 2015

then it is an unpleasant feature, reduce code/coding flexibility

@Araq
Copy link
Member

Araq commented Dec 10, 2015

IME it works well enough.

@andreaferretti
Copy link
Collaborator

@Araq Your suggestion works in this simple case, but not in more complex ones. For instance

type
  Foo* = ref object
    w: range[2..100]
  Bar* = ref object
    w: range[10..50]

template create(result: expr) =
  new(result)
  result.w = 20

proc createFoo(): Foo = create(result)

proc createBar(): Bar = create(result)

In the template here you do not know if you are creating a Foo or a Bar, but you want to perform the same operations.

This is not a theoretical possibility: it is happening always to me when dealing with similar 32-bit/64-bit code.

@refi64
Copy link
Contributor

refi64 commented Jan 7, 2016

Couldn't you do something like (untested):

result = new type(result)

@andreaferretti
Copy link
Collaborator

In my case result is one of the two following types:

type
  Matrix32*[M, N: static[int]] = object
    order: OrderType
    data: ref array[N * M, float32]
  Matrix64*[M, N: static[int]] = object
    order: OrderType
    data: ref array[M * N, float64]

Changing

template makeSMatrixPrivate(M, N, f, order, result: expr) =
  new result.data
  result.order = order
  # ...

into

template makeSMatrixPrivate(M, N, f, order, result: expr) =
  result.data = new type(result.data)
  result.order = order
  # ...

did not make the warning go away.

@Araq
Copy link
Member

Araq commented Jan 14, 2016

But that might work:

template makeSMatrixPrivate(M, N, f, orderParam, result: expr) =
  result.data = type(result.data)(order: orderParam)
  # ...

@andreaferretti
Copy link
Collaborator

Unfortunately, it doesn't seem to work. I have switched to this style in a proveinit branch of linalg, but the warnings are still there (just try nimble test on that branch).

@stale
Copy link

stale bot commented Aug 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; 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 Aug 4, 2020
@timotheecour timotheecour added Effect Tracking and removed stale Staled PR/issues; remove the label after fixing them Error Messages labels Jan 16, 2021
@timotheecour timotheecour changed the title incorrect "Cannot prove that 'result' is initialized" warning incorrect ProveInitwith delayed initialization of field Jan 16, 2021
ringabout added a commit to ringabout/Nim that referenced this issue Aug 16, 2022
Araq pushed a commit that referenced this issue Oct 4, 2022
* 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 <[email protected]>
capocasa pushed a commit to capocasa/Nim that referenced this issue 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 <[email protected]>
narimiran pushed a commit that referenced this issue 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 <[email protected]>
(cherry picked from commit f89ba2c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants