Benjamin-Dobell / IntelliJ-Luanalysis

Type-safe Lua IDE — IntelliJ IDEA plugin
Apache License 2.0
155 stars 21 forks source link

Missing 'nil' in inferred type of function #130

Closed rillig closed 2 years ago

rillig commented 2 years ago

Environment

Name Version
IDEA version CL2022.2
Luanalysis version 1.3.0
OS Windows 11

Preferences

Name Setting Remarks
Language level 5.3
Strict nil checks ☑️ / ❎ both trigger the bug

What are the steps to reproduce this issue?

local function return_inconsistent(name)
  if name == "" then return end
  return {}
end

return_inconsistent("")

What happens?

The inferred return type of the function is table.

What were you expecting to happen?

The inferred return type of the function is nil | table or () | table. There is a warning that _The function 'inconsistentreturn' sometimes returns a value. (Don't know whether that would be practical in general though.)

Benjamin-Dobell commented 2 years ago

This is unfortunately a complicated case. The return type of that function isn't actually nil | table, it's void | table.

The problem is void is the absence of a value i.e. the empty set. When you put it in a union, it makes no contribution: https://github.com/Benjamin-Dobell/IntelliJ-Luanalysis/issues/45

I'd have to change the representation of void at a pretty low level to make this work.

In this case if you actually want nil | table, then you ought to be able to change the return statement to return nil rather than nothing.

rillig commented 2 years ago

Ah, that's tricky indeed. Feel free to close it as a duplicate then.