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

Exclude variables' final types inside while true if re-assigned before first break #10538

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions spec/compiler/semantic/while_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,80 @@ describe "Semantic: while" do
)) { nilable int32 }
end

it "doesn't use type at end of endless while if variable is reassigned" do
assert_type(%(
while true
a = 1
if 1 == 1
break
end
a = 'x'
end
a
)) { int32 }
end

it "doesn't use type at end of endless while if variable is reassigned (2)" do
assert_type(%(
a = ""
while true
a = 1
if 1 == 1
break
end
a = 'x'
end
a
)) { int32 }
end

it "doesn't use type at end of endless while if variable is reassigned (3)" do
assert_type(%(
a = {1}
while true
a = a[0]
if 1 == 1
break
end
a = {'x'}
end
a
)) { union_of(int32, char) }
end

it "uses type at end of endless while if variable is reassigned, but not before first break" do
assert_type(%(
while true
if 1 == 1
break
end
a = 1
if 1 == 1
break
end
a = 'x'
end
a
)) { nilable union_of(int32, char) }
end

it "uses type at end of endless while if variable is reassigned, but not before first break (2)" do
assert_type(%(
a = ""
while true
if 1 == 1
break
end
a = 1
if 1 == 1
break
end
a = 'x'
end
a
)) { union_of(int32, char, string) }
end

it "rebinds condition variable after while body (#6158)" do
assert_type(%(
class Foo
Expand Down
15 changes: 12 additions & 3 deletions src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2139,8 +2139,14 @@ module Crystal

# If the loop is endless
if endless
after_while_var.bind_to(while_var)
after_while_var.nil_if_read = while_var.nil_if_read?
# If a variable was re-assigned before the first break expression,
# the type at the end of the while body is inaccessible upon exit
HertzDevil marked this conversation as resolved.
Show resolved Hide resolved
# because the assignment must have executed before the next chance
# to break
HertzDevil marked this conversation as resolved.
Show resolved Hide resolved
unless all_break_vars.try(&.first?.try &.[name]?.try { |var| !while_var.same?(var) })
HertzDevil marked this conversation as resolved.
Show resolved Hide resolved
after_while_var.bind_to(while_var)
after_while_var.nil_if_read = while_var.nil_if_read?
end
else
# We need to bind to the variable *before* the condition, even
# after before the variables that are used in the condition
Expand All @@ -2159,7 +2165,10 @@ module Crystal
# outside it must be nilable, unless the loop is endless.
else
after_while_var = MetaVar.new(name)
after_while_var.bind_to(while_var)
unless endless && all_break_vars.try(&.first?.try &.[name]?.try { |var| !while_var.same?(var) })
after_while_var.bind_to(while_var)
end

nilable = false
if endless
# In an endless loop if not all variable with the given name end up
Expand Down