Skip to content

Commit

Permalink
begin cleaning up generics in type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Aug 18, 2024
1 parent eb2e296 commit 841cfd6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
7 changes: 7 additions & 0 deletions spec/declaration/local_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -551,4 +551,11 @@ describe("local", function()
{ tag = "redeclaration", msg = "variable shadows previous declaration of 'integer'" },
{ tag = "unused", msg = "unused type integer" },
}))

it("does not accept type arguments declared twice", util.check_syntax_error([[
local type Foo<T> = record<T>
end
]], {
{ y = 1, msg = "cannot declare type arguments twice in type declaration" },
}))
end)
16 changes: 14 additions & 2 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4049,6 +4049,7 @@ do
return fail(ps, i, "expected a type name")
end
local typeargs
local itypeargs = i
if ps.tokens[i].tk == "<" then
i, typeargs = parse_anglebracket_list(ps, i, parse_typearg)
end
Expand Down Expand Up @@ -4090,11 +4091,22 @@ do

local nt = asgn.value.newtype
if nt.typename == "typedecl" then
local def = nt.def

if typeargs then
nt.typeargs = typeargs
if def.typeargs then
if def.typeargs then
fail(ps, itypeargs, "cannot declare type arguments twice in type declaration")
else
def.typeargs = typeargs
end
else


nt.typeargs = typeargs
end
end

local def = nt.def
if def.fields or def.typename == "enum" then
if not def.declname then
def.declname = asgn.var.tk
Expand Down
16 changes: 14 additions & 2 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -4049,6 +4049,7 @@ parse_type_declaration = function(ps: ParseState, i: integer, node_name: NodeKin
return fail(ps, i, "expected a type name")
end
local typeargs: {TypeArgType}
local itypeargs = i
if ps.tokens[i].tk == "<" then
i, typeargs = parse_anglebracket_list(ps, i, parse_typearg)
end
Expand Down Expand Up @@ -4090,11 +4091,22 @@ parse_type_declaration = function(ps: ParseState, i: integer, node_name: NodeKin

local nt = asgn.value.newtype
if nt is TypeDeclType then
local def = nt.def

if typeargs then
nt.typeargs = typeargs
if def is HasTypeArgs then
if def.typeargs then
fail(ps, itypeargs, "cannot declare type arguments twice in type declaration")
else
def.typeargs = typeargs
end
else
-- FIXME how to resolve type arguments in unions properly
-- fail(ps, itypeargs, def.typename .. " does not accept type arguments")
nt.typeargs = typeargs
end
end

local def = nt.def
if def is RecordLikeType or def is EnumType then
if not def.declname then
def.declname = asgn.var.tk
Expand Down

0 comments on commit 841cfd6

Please sign in to comment.