Skip to content

Commit

Permalink
Showing 2 changed files with 31 additions and 1 deletion.
24 changes: 24 additions & 0 deletions spec/compiler/semantic/no_return_spec.cr
Original file line number Diff line number Diff line change
@@ -295,4 +295,28 @@ describe "Semantic: NoReturn" do
end
)) { no_return }
end

it "types as NoReturn even if Nil return type is forced (#3096)" do
assert_type(%(
lib LibC
fun exit(Int32) : NoReturn
end
def foo : Nil
LibC.exit(0)
yield
end
def bar(x)
x
end
def baz
foo { }
bar 0
end
baz
)) { no_return }
end
end
8 changes: 7 additions & 1 deletion src/compiler/crystal/semantic/bindings.cr
Original file line number Diff line number Diff line change
@@ -243,7 +243,13 @@ module Crystal
class Def
def update(from = nil)
if freeze_type.try &.nil_type?
# Nothing
# When we have Nil forced as a return type, NoReturn still
# wins, so we must account for this case.
# Otherwise we simply keep having the Nil type.
computed_type = Type.merge(dependencies)
if computed_type.try &.no_return?
super
end
else
super
end

0 comments on commit a7aa936

Please sign in to comment.