From 8d7a8c91a618805a80707496d62ab8e62bd850d9 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 28 Jan 2021 14:45:38 -0300 Subject: [PATCH] Compiler: use path lookup when looking up type for autocast match (#10318) --- spec/compiler/semantic/automatic_cast_spec.cr | 20 +++++++++++++++++++ src/compiler/crystal/semantic/main_visitor.cr | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/spec/compiler/semantic/automatic_cast_spec.cr b/spec/compiler/semantic/automatic_cast_spec.cr index bdb0a02f3d8e..c39b0e4bff8b 100644 --- a/spec/compiler/semantic/automatic_cast_spec.cr +++ b/spec/compiler/semantic/automatic_cast_spec.cr @@ -596,4 +596,24 @@ describe "Semantic: automatic cast" do ), "ambiguous call, implicit cast of 255 matches all of UInt64, Int64" end + + it "autocasts nested type from non-nested type (#10315)" do + assert_no_errors(%( + module Moo + enum Color + Red + end + + abstract class Foo + def initialize(color : Color = :red) + end + end + end + + class Bar < Moo::Foo + end + + Bar.new + )) + end end diff --git a/src/compiler/crystal/semantic/main_visitor.cr b/src/compiler/crystal/semantic/main_visitor.cr index 7f57bd8f8054..e9dbd442d15f 100644 --- a/src/compiler/crystal/semantic/main_visitor.cr +++ b/src/compiler/crystal/semantic/main_visitor.cr @@ -769,7 +769,7 @@ module Crystal # OK else # Check autocast too - restriction_type = scope.lookup_type(restriction, free_vars: free_vars) + restriction_type = (path_lookup || scope).lookup_type(restriction, free_vars: free_vars) if casted_value = check_automatic_cast(value, restriction_type, node) value = casted_value else