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

nil as Object+ as Int32 causes Bug: trying to downcast Int32 <- Object+ #2403

Closed
Veedrac opened this issue Apr 3, 2016 · 7 comments
Closed
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler

Comments

@Veedrac
Copy link

Veedrac commented Apr 3, 2016

nil as Object+ as Int32

gives

Bug: trying to downcast Int32 <- Object+
[5698614] ???
[9665895] ???
[9753974] ???
[9538525] ???
[8301014] ???
[12725667] ???
[5696909] ???
[12660264] main +40
[139740445013776] __libc_start_main +240
[5673001] _start +41
[0] ???

Error: you've found a bug in the Crystal compiler. Please open an issue, including source code that will allow us to reproduce the bug: https://github.com/crystal-lang/crystal/issues

However,

puts typeof(nil as Object+ as Int32)

correctly gives Int32.

FWIW, 1 as Object+ as Int32 also errors in this way.

@Veedrac
Copy link
Author

Veedrac commented Apr 3, 2016

Other problems with as Object+:

  • (1 as Object+) == 1 causes Invalid memory access (signal 11) at address 0x7ffcfdcc3ed0 at runtime. So does (1 as Object+).itself, impressively.
  • 1 == (1 as Object+) causes Bug: trying to downcast Int8 <- Object+.
  • (1 as Object+) == (1 as Object+) goes into an infinite loop.
  • (1 as Object+).hash gives no overload matches '(Pointer(Void), Pointer(Void) -> LibUnwind::ReasonCode)#hash', which may or may not be a bug.
  • (1 as Object+).to_s fails compiling when instantiating Int#abs() with undefined method '-' for Int, which seems wrong.
  • puts typeof(0 as Object+) crashes.
  • typeof((raise "") as Object+) gives Object+, not NoReturn. The behaviour of NoReturn with other casts suggests that is wrong.

@refi64
Copy link
Contributor

refi64 commented Apr 3, 2016

I think the TYPE+ types are internal compiler stuff that's not meant for you to actually use.

@asterite
Copy link
Member

asterite commented Apr 3, 2016

@kirbyfan64 is right here. The + notation is only available because in some cases the compiler wasn't behaving right and having + to check if it fixes the problem really helped. In one of the next releases we will remove this from the language (and it's not even documented).

@asterite
Copy link
Member

asterite commented Apr 3, 2016

(I'll still keep this issue open because it triggers a bug and shouldn't be allowed)

@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Apr 3, 2016
@Veedrac
Copy link
Author

Veedrac commented Apr 3, 2016

puts typeof(nil as Object) also throws Invalid memory access (signal 11) at address 0x7fffc2a9edb0. Is this a separate problem?

@refi64
Copy link
Contributor

refi64 commented Apr 4, 2016

@Veedrac Looks like a recursion issue:

...
#46 0x0000000000e9062e in lookup_matches_checking_expansion ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/call.cr:311
#47 0x0000000000e87b4a in lookup_matches_in_type ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/call.cr:222
#48 0x0000000000e8784e in lookup_matches_in ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/call.cr:192
#49 0x0000000000ea1690 in lookup_matches_in ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/call.cr:191
#50 0x0000000000e62a94 in lookup_matches_without_splat ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/call.cr:137
#51 0x0000000000e6268b in lookup_matches ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/call.cr:111
#52 0x0000000000e45ace in recalculate ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/call.cr:80
---Type <return> to continue, or q <return> to quit---
#53 0x0000000000e4543e in on_new_subclass ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/call.cr:607
#54 0x0000000001278bd3 in notify_subclass_added ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:792
#55 0x000000000128161d in add_subclass ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:805
#56 0x0000000000f5051c in force_add_subclass ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:988
#57 0x0000000000f50fe1 in initialize ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:984
#58 0x0000000000f50e46 in *Crystal::ClassType@Crystal::ClassType#initialize<Crystal::MetaclassType, Crystal::Program, Crystal::Program, String, Crystal::Type+>:Nil () at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:2521
#59 0x0000000000f50a7d in initialize ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:2520
#60 0x0000000000f50625 in *Crystal::MetaclassType::new<Crystal::Program, Crystal::Type+, Nil, Nil>:Crystal::MetaclassType ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:2529
#61 0x0000000000f5059d in *Crystal::MetaclassType::new<Crystal::Program, Crystal::Type+>:Crystal::MetaclassType ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:2529
#62 0x0000000000adc531 in metaclass ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:21
---Type <return> to continue, or q <return> to quit---
#63 0x0000000000f528ca in virtual_type ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:2547
#64 0x0000000000b0b8ee in lookup_matches ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/method_lookup.cr:234
#65 0x0000000000b0a3e9 in *Crystal::VirtualMetaclassType@Crystal::VirtualTypeLookup#lookup_matches<Crystal::VirtualMetaclassType, Crystal::CallSignature>:Crystal::Matches () at /media/ryan/stuff/crystal/src/compiler/crystal/types.cr:718
#66 0x0000000000e90c88 in lookup_matches_with_signature ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/call.cr:334
#67 0x0000000000e9062e in lookup_matches_checking_expansion ()
    at /media/ryan/stuff/crystal/src/compiler/crystal/semantic/call.cr:311
#68 0x0000000000e87b4a in lookup_matches_in_type ()
... (stack trace repeats here again)

@asterite
Copy link
Member

I just removed the + syntax, and also made sure a compile error happens when trying to cast something to Object (and to Reference, which is similar). These in the future will probably work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler
Projects
None yet
Development

No branches or pull requests

3 participants