Skip to content

Return type from assert breaks after asserting function further down in the file #1032

@sindrets

Description

@sindrets

It seems assert() breaks after asserting a method defined further down in the file. The returned type from assert() after that point becomes ().

--- @namespace assert_with_method_defined_later

--- @class Foo
local Foo = {}

--- @type string?
local nullable_string

--- @return boolean
function Foo:defined_before() return false end

function Foo:main()
  local _v1 = assert(nullable_string)
  --    ^ type: `string` ✅

  assert(self:defined_before())

  local _v2 = assert(nullable_string)
  --    ^ type: `string` ✅

  assert(self:defined_later())

  local _v3 = assert(nullable_string)
  --    ^ type: `()` ❌
end

--- @return boolean
function Foo:defined_later() return false end

Curiously it works if the returned value from the assert is assigned to a variable:

--- @namespace assert_with_method_defined_later

--- @class Foo
local Foo = {}

--- @type string?
local nullable_string

function Foo:main()
  local _ = assert(self:defined_later())

  local _v1 = assert(nullable_string)
  --    ^ type: `string` ✅
end

--- @return boolean
function Foo:defined_later() return false end

Reproduced on 0.22.0 and 2f9cb6d.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions