Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types a and Player cannot be compared because they do not have the same metatable #83

Closed
ghost opened this issue Jul 22, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@ghost
Copy link

ghost commented Jul 22, 2022

No warnings occur in Studio.

--!strict
local event = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteEvent")
assert(event:IsA("RemoteEvent"), "") -- LSP does now recognize `event` as a `RemoteEvent`.

-- Won't warn if `player` is typed explicitly, but `Player` type is inferred anyway.
event.OnServerEvent:Connect(function(player, target: Player)
	print(player == target) -- Warning.
end)

local working_event: RemoteEvent = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteEvent")
working_event.OnServerEvent:Connect(function(player, target: Player)
	print(player == target) -- No warning.
end)
TypeError: Types a and Player cannot be compared with == because they do not have the same metatable
@JohnnyMorganz JohnnyMorganz added the bug Something isn't working label Jul 22, 2022
@JohnnyMorganz
Copy link
Owner

This is because the typechecker actually resolves event as any, although the hover/autocomplete resolves it to a stricter type.
So typechecker doesn't actually recognise event as a RemoteEvent, so cannot infer player is Player.

Its a bit strange that the generic type cannot be compared. We could make the warning disappear by removing the metatable from Player, but it currently causes issues as we it no longer type checks against comparing with other types of no metatable (string/number etc.)

@JohnnyMorganz
Copy link
Owner

JohnnyMorganz commented Jul 22, 2022

The type diagnostics system and the hovering/autocomplete systems are two separate Luau typecheckers. The latter has stricter types than the former.

Unfortunately, the type diagnostics system infers all DataModel indexes as any: game, workspace, game.ReplicatedStorage.Modules, or FFC/WFC variants will all be typed as any in the eyes of the type diagnostics system. Since game is any, game.ReplicatedStorage is any, and game.ReplicatedStorage:FindFirstChild("RemoteEvent") will be any.

Currently, there is no solution to that yet. We have to type it as any because we give the data model instances special types providing context where they are in the hierarchy, i.e. including pointers to their Parent and Children. This then makes the condition Part' == Part false, where Part' is the special datamodel type, since Part' has extra properties representing its children. This difference causes spurious errors to be thrown, so is currently disabled in the diagnostics system.

You will find the same in Studio:
image
It just doesn't warn because of the metatable differences

@JohnnyMorganz
Copy link
Owner

Unlike in Studio, we use the autocomplete typechecker to power hover information, which is why Luau LSP's hover information is typically better than Studio, but it doesn't exactly represent what the diagnostic system sees

@ghost ghost closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant