luau icon indicating copy to clipboard operation
luau copied to clipboard

New type solver throws unclear type error when returning table with an optional Roblox data type

Open melindatrace opened this issue 1 year ago • 1 comments

This issue only seems to occur for Vector3, Vector2, CFrame, and other Roblox data types, but not for Luau primitive data types.

local function FuncA(): { Value: Vector3? }
	return {
		Value = Vector3.zero -- TypeError: Type pack '{ Value: Vector3 }' could not be converted into '{ Value: Vector3? }'; type { Value: Vector3 }[0][read "Value"] (Vector3) is not exactly { Value: Vector3? }[0][read "Value"][1] (nil) Luau(1035)
	}
end

FuncA()

local function FuncB(): { Value: number? }
	return {
		Value = 1 -- OK
	}
end

FuncB()

melindatrace avatar Sep 13 '24 00:09 melindatrace

This probably has absolutely nothing to do with Roblox data types. My immediate best guess is that this is a problem with some of the expected type code, but we'll investigate it when we can.

aatxe avatar Sep 13 '24 04:09 aatxe

Hello! Sorry to report so late, but this should be fixed as of a couple Luau releases ago. If I remember correctly, the root cause was that the aforementioned expected type code did the incorrect thing for non-literal expressions. This happened for any type, for example:

local Numbers = { zero = 0 }
local function FuncA(): { Value: number? }
    return { Value = 0 } -- As noted, worked fine for literals
end
local function FuncB(): { Value: number ? }
  return { Value = Numbers.zero } -- Used to error, claiming `number?` is incompatible with `number`
end

Feel free to re-open this issue if the problem is still occuring!

hgoldstein avatar Apr 11 '25 21:04 hgoldstein