From 1c3ba3d1509770151628928aa3f451274bd669a0 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 10 Sep 2024 11:55:25 -0300 Subject: [PATCH] refactor: store_field_in_record --- tl.lua | 28 ++++++++++++++++------------ tl.tl | 28 ++++++++++++++++------------ 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/tl.lua b/tl.lua index a51134444..16db20282 100644 --- a/tl.lua +++ b/tl.lua @@ -3690,24 +3690,28 @@ do return i, node end - local function store_field_in_record(ps, i, field_name, t, fields, field_order) + local function store_field_in_record(ps, i, field_name, newt, fields, field_order) if not fields[field_name] then - fields[field_name] = t + fields[field_name] = newt table.insert(field_order, field_name) - else - local prev_t = fields[field_name] - if t.typename == "function" and prev_t.typename == "function" then + return true + end + + local oldt = fields[field_name] + + if newt.typename == "function" then + if oldt.typename == "function" then local p = new_type(ps, i, "poly") - p.types = { prev_t, t } + p.types = { oldt, newt } fields[field_name] = p - elseif t.typename == "function" and prev_t.typename == "poly" then - table.insert(prev_t.types, t) - else - fail(ps, i, "attempt to redeclare field '" .. field_name .. "' (only functions can be overloaded)") - return false + return true + elseif oldt.typename == "poly" then + table.insert(oldt.types, newt) + return true end end - return true + fail(ps, i, "attempt to redeclare field '" .. field_name .. "' (only functions can be overloaded)") + return false end local function set_declname(def, declname) diff --git a/tl.tl b/tl.tl index cf7e331fe..abe65ebc4 100644 --- a/tl.tl +++ b/tl.tl @@ -3690,24 +3690,28 @@ local function parse_return(ps: ParseState, i: integer): integer, Node return i, node end -local function store_field_in_record(ps: ParseState, i: integer, field_name: string, t: Type, fields: {string: Type}, field_order: {string}): boolean +local function store_field_in_record(ps: ParseState, i: integer, field_name: string, newt: Type, fields: {string: Type}, field_order: {string}): boolean if not fields[field_name] then - fields[field_name] = t + fields[field_name] = newt table.insert(field_order, field_name) - else - local prev_t = fields[field_name] - if t is FunctionType and prev_t is FunctionType then + return true + end + + local oldt = fields[field_name] + + if newt is FunctionType then + if oldt is FunctionType then local p = new_type(ps, i, "poly") as PolyType - p.types = { prev_t, t } + p.types = { oldt, newt } fields[field_name] = p - elseif t is FunctionType and prev_t is PolyType then - table.insert(prev_t.types, t) - else - fail(ps, i, "attempt to redeclare field '" .. field_name .. "' (only functions can be overloaded)") - return false + return true + elseif oldt is PolyType then + table.insert(oldt.types, newt) + return true end end - return true + fail(ps, i, "attempt to redeclare field '" .. field_name .. "' (only functions can be overloaded)") + return false end local function set_declname(def: Type, declname: string)