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

Block-local types with the same name can silently give wrong runtime results #13340

Closed
timotheecour opened this issue Feb 6, 2020 · 4 comments

Comments

@timotheecour
Copy link
Member

timotheecour commented Feb 6, 2020

Block-local types with the same name can silently give wrong runtime results.

this is directly caused by #5170 but hesistant to call it a duplicate because in following case the result is worse:

  • no warning, no error
  • wrong results at runtime; and this could be detected only in production (eg in following case, you'd never know there was a bug until you tried a value > int8.max)

Example

  type Foo = object
    bar: int8
  var f1: Foo
  block:
    type Foo = object
      bar: int
    var f2 = Foo(bar: 300)
    echo f2.bar # prints 44 instead of 300

Current Output

44

Expected Output

300

Additional Information

@timotheecour timotheecour changed the title Block-local types with the same name silently gives wrong runtime results Block-local types with the same name can silently give wrong runtime results Feb 6, 2020
@juancarlospaco
Copy link
Collaborator

Should type declarations be allowed inside blocks ?, Whats does it solves?.

@timotheecour
Copy link
Member Author

that feature is used heavily, removing it would be a pointless breaking change; it's a sensible feature IMO, for scoping (eg very useful in tests, but not just that)

@narimiran
Copy link
Member

narimiran commented Mar 5, 2020

@timotheecour why the hell your code examples (in multiple issues that I've opened) have indentation??

Maybe it is indented in your files so it is easier and faster for you to just copy-paste them as they are, but it is harder and slower for everybody else to reproduce it.
Please, slow yourself down, read and double-check both your issues and PRs before submitting them, remove all unneeded stuff, fix quirks, and only then post them.

Thank you in advance.

@narimiran
Copy link
Member

this is directly caused by #5170 but hesistant to call it a duplicate because in following case the result is worse

To me it looks like a duplicate. In all cases the wrong Foo is picked up, and now we're making different examples of the same behaviour: one is Foo with different field (baz vs bar), other is with different int type (int8 vs int) and the last one is with different types (array vs int).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants