Skip to content

Commit

Permalink
Improve error message for autocast error at Var assign
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Jan 28, 2021
1 parent 8d7a8c9 commit e3cbdb7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions spec/compiler/semantic/automatic_cast_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -616,4 +616,18 @@ describe "Semantic: automatic cast" do
Bar.new
))
end

it "errors when autocast default value doesn't match enum member" do
assert_error <<-CR,
enum Foo
FOO
end
def foo(foo : Foo = :bar)
end
foo
CR
"can't autocast :bar to Foo: no matching enum member"
end
end
4 changes: 4 additions & 0 deletions src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,10 @@ module Crystal
if casted_value = check_automatic_cast(value, restriction_type, node)
value = casted_value
else
if value.is_a?(SymbolLiteral) && restriction_type.is_a?(EnumType)
node.raise "can't autocast #{value} to #{restriction_type}: no matching enum member"
end

node.raise "can't restrict #{value.type} to #{restriction}"
end
end
Expand Down

0 comments on commit e3cbdb7

Please sign in to comment.