-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatic cast of numbers to other numbers, and symbols to enums (#6074)
- 1.15.1
- 1.15.0
- 1.14.1
- 1.14.0
- 1.13.3
- 1.13.2
- 1.13.1
- 1.13.0
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.1
- 1.10.0
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- 0.36.1
- 0.36.0
- 0.35.1
- 0.35.0
- 0.34.0
- 0.33.0
- 0.32.1
- 0.32.0
- 0.31.1
- 0.31.0
- 0.30.1
- 0.30.0
- 0.29.0
- 0.28.0
- 0.27.2
- 0.27.1
- 0.27.0
- 0.26.1
- 0.26.0
- 0.25.1
- 0.25.0
Showing
26 changed files
with
1,022 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
require "../../spec_helper" | ||
|
||
describe "Code gen: automatic cast" do | ||
it "casts literal integer (Int32 -> Int64)" do | ||
run(%( | ||
def foo(x : Int64) | ||
x | ||
end | ||
foo(12345) | ||
)).to_i.should eq(12345) | ||
end | ||
|
||
it "casts literal integer (Int64 -> Int32, ok)" do | ||
run(%( | ||
def foo(x : Int32) | ||
x | ||
end | ||
foo(2147483647_i64) | ||
)).to_i.should eq(2147483647) | ||
end | ||
|
||
it "casts literal integer (Int32 -> Float32)" do | ||
run(%( | ||
def foo(x : Float32) | ||
x | ||
end | ||
foo(12345).to_i | ||
)).to_i.should eq(12345) | ||
end | ||
|
||
it "casts literal integer (Int32 -> Float64)" do | ||
run(%( | ||
def foo(x : Float64) | ||
x | ||
end | ||
foo(12345).to_i | ||
)).to_i.should eq(12345) | ||
end | ||
|
||
it "casts literal float (Float32 -> Float64)" do | ||
run(%( | ||
def foo(x : Float64) | ||
x | ||
end | ||
foo(12345.0_f32).to_i | ||
)).to_i.should eq(12345) | ||
end | ||
|
||
it "casts literal float (Float64 -> Float32)" do | ||
run(%( | ||
def foo(x : Float32) | ||
x | ||
end | ||
foo(12345.0).to_i | ||
)).to_i.should eq(12345) | ||
end | ||
|
||
it "casts symbol literal to enum" do | ||
run(%( | ||
:four | ||
enum Foo | ||
One | ||
Two | ||
Three | ||
end | ||
def foo(x : Foo) | ||
x | ||
end | ||
foo(:three) | ||
)).to_i.should eq(2) | ||
end | ||
|
||
it "casts Int32 to Int64 in ivar assignment" do | ||
run(%( | ||
class Foo | ||
@x : Int64 | ||
def initialize | ||
@x = 10 | ||
end | ||
def x | ||
@x | ||
end | ||
end | ||
Foo.new.x | ||
)).to_i.should eq(10) | ||
end | ||
|
||
it "casts Symbol to Enum in ivar assignment" do | ||
run(%( | ||
enum E | ||
One | ||
Two | ||
Three | ||
end | ||
class Foo | ||
@x : E | ||
def initialize | ||
@x = :three | ||
end | ||
def x | ||
@x | ||
end | ||
end | ||
Foo.new.x | ||
)).to_i.should eq(2) | ||
end | ||
|
||
it "casts Int32 to Int64 in cvar assignment" do | ||
run(%( | ||
class Foo | ||
@@x : Int64 = 0_i64 | ||
def self.x | ||
@@x = 10 | ||
@@x | ||
end | ||
end | ||
Foo.x | ||
)).to_i.should eq(10) | ||
end | ||
|
||
it "casts Int32 to Int64 in lvar assignment" do | ||
run(%( | ||
x : Int64 | ||
x = 123 | ||
x | ||
)).to_i.should eq(123) | ||
end | ||
|
||
it "casts Int32 to Int64 in ivar type declaration" do | ||
run(%( | ||
class Foo | ||
@x : Int64 = 10 | ||
def x | ||
@x | ||
end | ||
end | ||
Foo.new.x | ||
)).to_i.should eq(10) | ||
end | ||
|
||
it "casts Symbol to Enum in ivar type declaration" do | ||
run(%( | ||
enum Color | ||
Red | ||
Green | ||
Blue | ||
end | ||
class Foo | ||
@x : Color = :blue | ||
def x | ||
@x | ||
end | ||
end | ||
Foo.new.x | ||
)).to_i.should eq(2) | ||
end | ||
|
||
it "casts Int32 to Int64 in cvar type declaration" do | ||
run(%( | ||
class Foo | ||
@@x : Int64 = 10 | ||
def self.x | ||
@@x | ||
end | ||
end | ||
Foo.x | ||
)).to_i.should eq(10) | ||
end | ||
|
||
it "casts Int32 -> Int64 in arg restriction" do | ||
run(%( | ||
def foo(x : Int64 = 123) | ||
x | ||
end | ||
foo | ||
)).to_i.should eq(123) | ||
end | ||
|
||
it "casts Int32 to Int64 in ivar type declaration in generic" do | ||
run(%( | ||
class Foo(T) | ||
@x : T = 10 | ||
def x | ||
@x | ||
end | ||
end | ||
Foo(Int64).new.x | ||
)).to_i.should eq(10) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,370 @@ | ||
require "../../spec_helper" | ||
|
||
describe "Semantic: automatic cast" do | ||
it "casts literal integer (Int32 -> no restriction)" do | ||
assert_type(%( | ||
def foo(x) | ||
x + 1 | ||
end | ||
foo(12345) | ||
), inject_primitives: true) { int32 } | ||
end | ||
|
||
it "casts literal integer (Int32 -> Int64)" do | ||
assert_type(%( | ||
def foo(x : Int64) | ||
x | ||
end | ||
foo(12345) | ||
)) { int64 } | ||
end | ||
|
||
it "casts literal integer (Int64 -> Int32, ok)" do | ||
assert_type(%( | ||
def foo(x : Int32) | ||
x | ||
end | ||
foo(2147483647_i64) | ||
)) { int32 } | ||
end | ||
|
||
it "casts literal integer (Int64 -> Int32, too big)" do | ||
assert_error %( | ||
def foo(x : Int32) | ||
x | ||
end | ||
foo(2147483648_i64) | ||
), | ||
"no overload matches" | ||
end | ||
|
||
it "casts literal integer (Int32 -> Float32)" do | ||
assert_type(%( | ||
def foo(x : Float32) | ||
x | ||
end | ||
foo(12345) | ||
)) { float32 } | ||
end | ||
|
||
it "casts literal integer (Int32 -> Float64)" do | ||
assert_type(%( | ||
def foo(x : Float64) | ||
x | ||
end | ||
foo(12345) | ||
)) { float64 } | ||
end | ||
|
||
it "casts literal float (Float32 -> Float64)" do | ||
assert_type(%( | ||
def foo(x : Float64) | ||
x | ||
end | ||
foo(1.23_f32) | ||
)) { float64 } | ||
end | ||
|
||
it "casts literal float (Float64 -> Float32)" do | ||
assert_type(%( | ||
def foo(x : Float32) | ||
x | ||
end | ||
foo(1.23) | ||
)) { float32 } | ||
end | ||
|
||
it "matches correct overload" do | ||
assert_type(%( | ||
def foo(x : Int32) | ||
x | ||
end | ||
def foo(x : Int64) | ||
x | ||
end | ||
foo(1_i64) | ||
)) { int64 } | ||
end | ||
|
||
it "casts literal integer through alias with union" do | ||
assert_type(%( | ||
alias A = Int64 | String | ||
def foo(x : A) | ||
x | ||
end | ||
foo(12345) | ||
)) { int64 } | ||
end | ||
|
||
it "says ambiguous call for integer" do | ||
assert_error %( | ||
def foo(x : Int8) | ||
x | ||
end | ||
def foo(x : UInt8) | ||
x | ||
end | ||
foo(1) | ||
), | ||
"ambiguous" | ||
end | ||
|
||
it "says ambiguous call for integer (2)" do | ||
assert_error %( | ||
def foo(x : Int8 | UInt8) | ||
x | ||
end | ||
foo(1) | ||
), | ||
"ambiguous" | ||
end | ||
|
||
it "casts symbol literal to enum" do | ||
assert_type(%( | ||
enum Foo | ||
One | ||
Two | ||
Three | ||
end | ||
def foo(x : Foo) | ||
x | ||
end | ||
foo(:one) | ||
)) { types["Foo"] } | ||
end | ||
|
||
it "casts literal integer through alias with union" do | ||
assert_type(%( | ||
enum Foo | ||
One | ||
Two | ||
end | ||
alias A = Foo | String | ||
def foo(x : A) | ||
x | ||
end | ||
foo(:two) | ||
)) { types["Foo"] } | ||
end | ||
|
||
it "errors if symbol name doesn't match enum member" do | ||
assert_error %( | ||
enum Foo | ||
One | ||
Two | ||
Three | ||
end | ||
def foo(x : Foo) | ||
x | ||
end | ||
foo(:four) | ||
), | ||
"no overload matches" | ||
end | ||
|
||
it "says ambiguous call for symbol" do | ||
assert_error %( | ||
enum Foo | ||
One | ||
Two | ||
Three | ||
end | ||
enum Foo2 | ||
One | ||
Two | ||
Three | ||
end | ||
def foo(x : Foo) | ||
x | ||
end | ||
def foo(x : Foo2) | ||
x | ||
end | ||
foo(:one) | ||
), | ||
"ambiguous" | ||
end | ||
|
||
it "casts Int32 to Int64 in ivar assignment" do | ||
assert_type(%( | ||
class Foo | ||
@x : Int64 | ||
def initialize | ||
@x = 10 | ||
end | ||
def x | ||
@x | ||
end | ||
end | ||
Foo.new.x | ||
)) { int64 } | ||
end | ||
|
||
it "casts Symbol to Enum in ivar assignment" do | ||
assert_type(%( | ||
enum E | ||
One | ||
Two | ||
Three | ||
end | ||
class Foo | ||
@x : E | ||
def initialize | ||
@x = :two | ||
end | ||
def x | ||
@x | ||
end | ||
end | ||
Foo.new.x | ||
)) { types["E"] } | ||
end | ||
|
||
it "casts Int32 to Int64 in cvar assignment" do | ||
assert_type(%( | ||
class Foo | ||
@@x : Int64 = 0_i64 | ||
def self.x | ||
@@x = 10 | ||
@@x | ||
end | ||
end | ||
Foo.x | ||
)) { int64 } | ||
end | ||
|
||
it "casts Int32 to Int64 in lvar assignment" do | ||
assert_type(%( | ||
x : Int64 | ||
x = 123 | ||
x | ||
)) { int64 } | ||
end | ||
|
||
it "casts Int32 to Int64 in ivar type declaration" do | ||
assert_type(%( | ||
class Foo | ||
@x : Int64 = 10 | ||
def x | ||
@x | ||
end | ||
end | ||
Foo.new.x | ||
)) { int64 } | ||
end | ||
|
||
it "casts Symbol to Enum in ivar type declaration" do | ||
assert_type(%( | ||
enum Color | ||
Red | ||
Green | ||
Blue | ||
end | ||
class Foo | ||
@x : Color = :red | ||
def x | ||
@x | ||
end | ||
end | ||
Foo.new.x | ||
)) { types["Color"] } | ||
end | ||
|
||
it "casts Int32 to Int64 in cvar type declaration" do | ||
assert_type(%( | ||
class Foo | ||
@@x : Int64 = 10 | ||
def self.x | ||
@@x | ||
end | ||
end | ||
Foo.x | ||
)) { int64 } | ||
end | ||
|
||
it "casts Symbol to Enum in cvar type declaration" do | ||
assert_type(%( | ||
enum Color | ||
Red | ||
Green | ||
Blue | ||
end | ||
class Foo | ||
@@x : Color = :red | ||
def self.x | ||
@@x | ||
end | ||
end | ||
Foo.x | ||
)) { types["Color"] } | ||
end | ||
|
||
it "casts Int32 -> Int64 in arg restriction" do | ||
assert_type(%( | ||
def foo(x : Int64 = 0) | ||
x | ||
end | ||
foo | ||
)) { int64 } | ||
end | ||
|
||
it "casts Int32 to Int64 in ivar type declaration in generic" do | ||
assert_type(%( | ||
class Foo(T) | ||
@x : T = 10 | ||
def x | ||
@x | ||
end | ||
end | ||
Foo(Int64).new.x | ||
)) { int64 } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters