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
end

Foo.new.set_x 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.
Cheers.

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?

2 Likes

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

Cheers.

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
end

Foo.new.set_x Int32::MAX + 1

Unhandled exception: Arithmetic overflow (OverflowError)
from 1.cr:9:26 in ‘__crystal_main’
from /home/zw963/Crystal/share/crystal/src/crystal/main.cr:129:5 in ‘main_user_code’
from /home/zw963/Crystal/share/crystal/src/crystal/main.cr:115:7 in ‘main’
from /home/zw963/Crystal/share/crystal/src/crystal/main.cr:141:3 in ‘main’
from /usr/lib/libc.so.6 in ‘??’
from /usr/lib/libc.so.6 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.