diff --git a/spec/compiler/semantic/automatic_cast_spec.cr b/spec/compiler/semantic/automatic_cast_spec.cr index c39b0e4bff8b..a4f27ead8b44 100644 --- a/spec/compiler/semantic/automatic_cast_spec.cr +++ b/spec/compiler/semantic/automatic_cast_spec.cr @@ -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 diff --git a/src/compiler/crystal/semantic/main_visitor.cr b/src/compiler/crystal/semantic/main_visitor.cr index e9dbd442d15f..9e54b460333d 100644 --- a/src/compiler/crystal/semantic/main_visitor.cr +++ b/src/compiler/crystal/semantic/main_visitor.cr @@ -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