Skip to content

Commit

Permalink
Revert "Fixed OpenSSL bindings to recognize LibreSSL (#5676)" (#6062)
Browse files Browse the repository at this point in the history
This reverts commit 72ce28f.
  • Loading branch information
RX14 authored and asterite committed May 7, 2018
1 parent 5e518f9 commit 68dab3f
Show file tree
Hide file tree
Showing 19 changed files with 599 additions and 74 deletions.
3 changes: 1 addition & 2 deletions bin/ci
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ prepare_build() {

on_linux docker pull "jhass/crystal-build-$ARCH"

on_osx brew install crystal-lang pkg-config
on_osx brew install crystal-lang

# Make sure binaries from llvm are available in PATH
on_osx brew install jq
Expand Down Expand Up @@ -123,7 +123,6 @@ with_build_env() {
on_osx sudo systemsetup -settimezone $TZ
on_osx PATH="/usr/local/opt/llvm/bin:\$PATH" \
CRYSTAL_CACHE_DIR="/tmp/crystal" \
PKG_CONFIG_PATH="$(brew --prefix)/opt/openssl/lib/pkgconfig:$PKG_CONFIG_PATH" \
/bin/sh -c "'$command'"

}
Expand Down
81 changes: 81 additions & 0 deletions spec/compiler/codegen/automatic_cast.cr
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
213 changes: 213 additions & 0 deletions spec/compiler/semantic/automatic_cast.cr
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
33 changes: 33 additions & 0 deletions src/compiler/crystal/codegen/cast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,39 @@ class Crystal::CodeGenVisitor
target_pointer
end

# This is the case of the automatic cast between integer types
def downcast_distinct(value, to_type : IntegerType, from_type : IntegerType)
codegen_cast(from_type, to_type, value)
end

# This is the case of the automatic cast between integer type and float type
def downcast_distinct(value, to_type : FloatType, from_type : IntegerType)
codegen_cast(from_type, to_type, value)
end

# This is the case of the automatic cast between float types
def downcast_distinct(value, to_type : FloatType, from_type : FloatType)
codegen_cast(from_type, to_type, value)
end

# This is the case of the automatic cast between symbol and enum
def downcast_distinct(value, to_type : EnumType, from_type : SymbolType)
# value has the value of the symbol inside the symbol table,
# so we first get which symbol name that is, and then match
# it to one of the enum members
index = value.const_int_get_sext_value
symbol = @symbols_by_index[index].underscore

to_type.types.each do |name, value|
if name.underscore == symbol
accept(value.as(Const).value)
return @last
end
end

raise "Bug: expected to find enum member of #{to_type} matching symbol #{symbol}"
end

def downcast_distinct(value, to_type : Type, from_type : Type)
raise "BUG: trying to downcast #{to_type} <- #{from_type}"
end
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,11 @@ module Crystal
@in_lib = false
@strings = {} of StringKey => LLVM::Value
@symbols = {} of String => Int32
@symbols_by_index = [] of String
@symbol_table_values = [] of LLVM::Value
program.symbols.each_with_index do |sym, index|
@symbols[sym] = index
@symbols_by_index << sym
@symbol_table_values << build_string_constant(sym, sym)
end

Expand Down
15 changes: 15 additions & 0 deletions src/compiler/crystal/semantic/bindings.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ module Crystal
@type
end

def type(*, with_literals = false)
type = self.type

if with_literals
case self
when NumberLiteral
return NumberLiteralType.new(type.program, self)
when SymbolLiteral
return SymbolLiteralType.new(type.program, self)
end
end

type
end

def set_type(type : Type)
type = type.remove_alias_if_simple
if !type.no_return? && (freeze_type = @freeze_type) && !type.implements?(freeze_type)
Expand Down
Loading

0 comments on commit 68dab3f

Please sign in to comment.