-
-
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.
Browse files
Browse the repository at this point in the history
This reverts commit 72ce28f.
- Loading branch information
Showing
19 changed files
with
599 additions
and
74 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
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,81 @@ | ||
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 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
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 | ||
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
Oops, something went wrong.