Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure we try destructure the right thing. #720

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/examples/access
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,27 @@ component Main {
Test.print("a")
}
}
-------------------------------------------------------------------------------
store Settings {
state settings : String = ""
}

module App.Page {
fun test {
""
}
}

enum App.Page {
Settings
Page
}

component Main {
fun render : Html {
case App.Page.Settings {
App.Page.Settings => <div></div>
App.Page.Page => <div></div>
}
}
}
59 changes: 32 additions & 27 deletions src/type_checkers/destructuring.cr
Original file line number Diff line number Diff line change
Expand Up @@ -155,36 +155,21 @@ module Mint
name =
node.name.try(&.value) || condition.name

if node.items.empty? &&
(entity_name = node.name.try(&.value)) &&
(parent = scope.resolve(entity_name, node).try(&.node)) &&
(entity = scope.resolve(node.variant.value, parent).try(&.node))
check!(parent)
lookups[node] = {entity, parent}
return destructure(entity, condition, variables)
elsif parent = ast.type_definitions.find(&.name.value.==(name))
variant =
case fields = parent.fields
type_definition =
ast.type_definitions.find(&.name.value.==(name))

variant =
if type_definition
case fields = type_definition.fields
when Array(Ast::TypeVariant)
fields.find(&.value.value.==(node.variant.value))
end
end

error! :destructuring_type_variant_missing do
block do
text "I could not find the variant"
bold %("#{node.variant.value}")
text "of type"
bold %("#{parent.name.value}")
text "for a destructuring:"
end

snippet node
snippet "The type is defined here:", parent
end unless variant

lookups[node] = {variant, parent}
if type_definition && variant
lookups[node] = {variant, type_definition}

type = resolve(parent)
type = resolve(type_definition)

unified =
Comparer.compare(type, condition)
Expand Down Expand Up @@ -253,7 +238,7 @@ module Mint

mapping = {} of String => Checkable

parent.parameters.each_with_index do |param2, index2|
type_definition.parameters.each_with_index do |param2, index2|
mapping[param2.value] = condition.parameters[index2]
end

Expand All @@ -267,7 +252,7 @@ module Mint
when Ast::Type
resolve(item)
when Ast::TypeVariable
unified.parameters[parent.parameters.index! { |variable| variable.value == item.value }]
unified.parameters[type_definition.parameters.index! { |variable| variable.value == item.value }]
else
VOID # Can't happen
end
Expand All @@ -278,6 +263,26 @@ module Mint
end

return variables
elsif node.items.empty? &&
(entity_name = node.name.try(&.value)) &&
(parent = scope.resolve(entity_name, node).try(&.node)) &&
(entity = scope.resolve(node.variant.value, parent).try(&.node))
check!(parent)
lookups[node] = {entity, parent}
return destructure(entity, condition, variables)
elsif type_definition
error! :destructuring_type_variant_missing do
block do
text "I could not find the variant"
bold %("#{node.variant.value}")
text "of type"
bold %("#{type_definition.name.value}")
text "for a destructuring:"
end

snippet node
snippet "The type is defined here:", type_definition
end
end

error! :destructuring_type_missing do
Expand Down