Inconsistent document about autocasting

reading Type autocasting - Crystal

class Foo
  @x : Int64 = 10 # OK, 10 fits in an Int64

  def set_x(y)
    @x = y
end 1 # Error: "at line 5: instance variable '@x' of Foo must be Int64, not Int32"

but the program runs just fine now, does anything changes between versions?

Hello @femto, I think is related to this discussion:

And this PR that changed the autocasting behavior.

Hope that helps.

It works if you type the setter parameter. E.g. def set_x(y : Int64).

Hello, it works even without type the setter parameter.
what I mean is it runs just fine,
but the docs says it should report an error.

Ah okay, then yea the linked PR seemed to have changed that. Documentation probably needs updated to reflect it. Mind filing an issue on the crystal-book repo?


This probably a new introduced bug?

I always set env like this:

 ╰─ $ echo $CRYSTAL_OPTS 
-Dstrict_multi_assign -Dno_number_autocast

It should no number autocast, why not raise error on 1.11.0 ?

If not mistaken the new feature is behind a different flag: -Dno_restrictions_augmenter


This should deff be added to Compile-time flags - Crystal then.

1 Like

How confusing it is! to introduce such a new compile time flag.

What is the obvious difference of above issue with this ?

I know why this compile time flag named no_restrictions_augmenter after read Experimental: restriction augmenter by asterite · Pull Request #12103 · crystal-lang/crystal · GitHub

Anyway, i consider this flag (by default, enabled currently) probably cause some run-time error.

class Foo
  @x : Int32 = 10 # OK, 10 fits in an Int64

  def set_x(y)
    @x = y
end Int32::MAX + 1

Unhandled exception: Arithmetic overflow (OverflowError)
from in ‘__crystal_main’
from /home/zw963/Crystal/share/crystal/src/crystal/ in ‘main_user_code’
from /home/zw963/Crystal/share/crystal/src/crystal/ in ‘main’
from /home/zw963/Crystal/share/crystal/src/crystal/ in ‘main’
from /usr/lib/ in ‘??’
from /usr/lib/ in ‘__libc_start_main’
from /home/zw963/.cache/crystal/crystal-run-1.tmp in ‘_start’
from ???

I created a issue for fix document about this.