From 821e161915ce888cae7067b33fe0cead413cded4 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Fri, 20 Oct 2023 09:35:08 -0300 Subject: [PATCH] fix: resolve typeargs when both types in 'or' match expected type (#709) Fixes #706. --- spec/operator/or_spec.lua | 7 +++++++ tl.lua | 2 +- tl.tl | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/spec/operator/or_spec.lua b/spec/operator/or_spec.lua index 082dc80e..eea54753 100644 --- a/spec/operator/or_spec.lua +++ b/spec/operator/or_spec.lua @@ -128,4 +128,11 @@ describe("or", function() local x: string | number = "hello" or "world" ]])) + it("resolves type arguments when both types in 'or' match expected type (regression test for #706)", util.check([[ + local t1: {string:number} = { foo = 42 } + local t2: {string:number} = { foo = 42 } + for _, v in pairs(t1 or t2) do + print(v * v) + end + ]])) end) diff --git a/tl.lua b/tl.lua index d52abae0..a17a9699 100644 --- a/tl.lua +++ b/tl.lua @@ -9969,7 +9969,7 @@ tl.type_check = function(ast, opts) local a_is = is_a(a, node.expected) local b_is = is_a(b, node.expected) if a_is and b_is then - node.type = node.expected + node.type = resolve_typevars_at(node, node.expected) elseif a_is then node.type = resolve_tuple(b) else diff --git a/tl.tl b/tl.tl index f4eea84c..1c857442 100644 --- a/tl.tl +++ b/tl.tl @@ -9969,7 +9969,7 @@ tl.type_check = function(ast: Node, opts: TypeCheckOptions): Result, string local a_is = is_a(a, node.expected) local b_is = is_a(b, node.expected) if a_is and b_is then - node.type = node.expected + node.type = resolve_typevars_at(node, node.expected) elseif a_is then node.type = resolve_tuple(b) else