Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: handle pointers in NilableCast codegen #14499

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 39 additions & 25 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1405,40 +1405,54 @@ module Crystal

obj_type = node.obj.type
to_type = node.to.type

resulting_type = node.type

filtered_type = obj_type.filter_by(to_type)

unless filtered_type
@last = upcast llvm_nil, resulting_type, @program.nil
return false
end

non_nilable_type = node.non_nilable_type

if node.upcast?
@last = upcast last_value, non_nilable_type, obj_type
if to_type.pointer?
if obj_type.nil_type?
@last = llvm_type(to_type).null
else
@last = upcast last_value, non_nilable_type, obj_type
end
@last = upcast @last, resulting_type, non_nilable_type
elsif obj_type != non_nilable_type
type_id = type_id last_value, obj_type
cmp = match_type_id obj_type, non_nilable_type, type_id

Phi.open(self, node, @needs_value) do |phi|
matches_block, doesnt_match_block = new_blocks "matches", "doesnt_match"
cond cmp, matches_block, doesnt_match_block
elsif obj_type.pointer?
if to_type.nil_type?
@last = llvm_type(to_type).null
else
@last = upcast last_value, non_nilable_type, obj_type
end
@last = upcast @last, resulting_type, non_nilable_type
else
filtered_type = obj_type.filter_by(to_type)

position_at_end doesnt_match_block
unless filtered_type
@last = upcast llvm_nil, resulting_type, @program.nil
phi.add @last, resulting_type
return false
end

position_at_end matches_block
@last = downcast last_value, non_nilable_type, obj_type, true
if node.upcast?
@last = upcast last_value, non_nilable_type, obj_type
@last = upcast @last, resulting_type, non_nilable_type
phi.add @last, resulting_type, last: true
elsif obj_type != non_nilable_type
type_id = type_id last_value, obj_type
cmp = match_type_id obj_type, non_nilable_type, type_id

Phi.open(self, node, @needs_value) do |phi|
matches_block, doesnt_match_block = new_blocks "matches", "doesnt_match"
cond cmp, matches_block, doesnt_match_block

position_at_end doesnt_match_block
@last = upcast llvm_nil, resulting_type, @program.nil
phi.add @last, resulting_type

position_at_end matches_block
@last = downcast last_value, non_nilable_type, obj_type, true
@last = upcast @last, resulting_type, non_nilable_type
phi.add @last, resulting_type, last: true
end
else
@last = upcast last_value, resulting_type, obj_type
end
else
@last = upcast last_value, resulting_type, obj_type
end

false
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/crystal/semantic/bindings.cr
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,14 @@ module Crystal

@upcast = false

if obj_type
if obj_type && !(obj_type.pointer? || to_type.pointer?)
filtered_type = obj_type.filter_by(to_type)

# If the filtered type didn't change it means that an
# upcast is being made, for example:
#
# 1 as Int32 | Float64
# Bar.new as Foo # where Bar < Foo
# 1.as?(Int32 | Float64)
# Bar.new.as?(Foo) # where Bar < Foo
if obj_type == filtered_type && !to_type.is_a?(GenericClassType) &&
to_type.can_be_stored?
filtered_type = to_type.virtual_type
Expand Down