8000 Type declarations in function generics cause issues when names overlap · Issue #332 · luau-lang/luau · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Type declarations in function generics cause issues when names overlap #332

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

Closed
HarryXChen3 opened this issue Jan 29, 2022 · 2 comments · Fixed by #342
Closed

Type declarations in function generics cause issues when names overlap #332

HarryXChen3 opened this issue Jan 29, 2022 · 2 comments · Fixed by #342
Assignees
Labels
bug Something isn't working

Comments

@HarryXChen3
Copy link
HarryXChen3 commented Jan 29, 2022

When explicitly declaring function generics, the generic types leak outside of the function and can cause issues if other generic types for other functions are of the same name

Not 100% sure if this is a bug, but I don't believe generic types inside functions should leak and affect the behavior of generics of another even when both have the same name

Fairly sure this started happening since commit 2f989fc049772f36de1a4281834c375858507bda
(I rarely post issues so if I've done something incorrectly, please let me know)

image
image

--!nonstrict
--consider the following code under --!nonstrict
local util = {}
function util.matchDict<K, V>(lambda: (key: K, value: V) -> boolean?, dict: {[K]: V}): (K?, V?)
	for k, v in pairs(dict) do
		if lambda(k, v) == true then
			return k, v
		end
	end
	return nil, nil
end

function util.matchDictAll<K, V>(lambda: (key: K, value: V) -> boolean?, dict: {[K]: V}): {[K]: V}
	local matched = {}
	for k, v in pairs(dict) do --this will throw a "Type {[K]: V} could not be converted into {[any]: any}" on the dict
		if lambda(k, v) == true then
			matched[k] = v
		end
	end
	return matched
end
--!nonstrict
local util = {}
function util.matchDict<K, V>(lambda: (key: K, value: V) -> boolean?, dict: {[K]: V}): (K?, V?)
	for k, v in pairs(dict) do
		if lambda(k, v) == true then
			return k, v
		end
	end
	return nil, nil
end
--the generic type names are changed and no longer overlap with the previous function
function util.matchDictAll<X, Y>(lambda: (key: X, value: Y) -> boolean?, dict: {[X]: Y}): {[X]: Y}
	local matched = {}
	for k, v in pairs(dict) do --the type error no longer occurs
		if lambda(k, v) == true then
			matched[k] = v
		end
	end
	return matched
end
@HarryXChen3 HarryXChen3 added the bug Something isn't working label Jan 29, 2022
@asajeffrey
Copy link
Collaborator

Ah, we need to cache type parameters to type aliases but not functions. I have a fix, hopefully it'll land soon.

@HarryXChen3
Copy link
Author

Thanks! Glad to see this has been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants
0