diff --git a/spec/compiler/semantic/def_overload_spec.cr b/spec/compiler/semantic/def_overload_spec.cr index 5af1e8b38aad..213c7033ddbc 100644 --- a/spec/compiler/semantic/def_overload_spec.cr +++ b/spec/compiler/semantic/def_overload_spec.cr @@ -1356,6 +1356,15 @@ describe "Semantic: def overload" do CRYSTAL end + it "says `no overload matches` instead of `use a more specific type` on union restriction that cannot be stored" do + assert_error <<-CRYSTAL, "expected argument #1 to 'foo' to be Array | String, not Int32" + def foo(x : Array | String) + end + + foo(1) + CRYSTAL + end + it "finds method after including module in generic module (#1201)" do assert_type(<<-CRYSTAL) { char } module Bar diff --git a/src/compiler/crystal/semantic/type_lookup.cr b/src/compiler/crystal/semantic/type_lookup.cr index ba538ed0323d..8ecdeaa0f0dd 100644 --- a/src/compiler/crystal/semantic/type_lookup.cr +++ b/src/compiler/crystal/semantic/type_lookup.cr @@ -150,7 +150,10 @@ class Crystal::Type return if !@raise && !type type = type.not_nil! - check_type_can_be_stored(ident, type, "can't use #{type} in unions") + unless type.can_be_stored? + ident.raise "can't use #{type} in unions yet, use a more specific type" if @raise + return + end type.virtual_type end