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

[SR-7711] Redundant redeclaration checking for overloads #50251

Open
AnthonyLatsis opened this issue May 17, 2018 · 3 comments
Open

[SR-7711] Redundant redeclaration checking for overloads #50251

AnthonyLatsis opened this issue May 17, 2018 · 3 comments
Labels
compiler The Swift compiler in itself improvement type checker Area → compiler: Semantic analysis

Comments

@AnthonyLatsis
Copy link
Collaborator

Previous ID SR-7711
Radar None
Original Reporter @AnthonyLatsis
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, TypeChecker
Assignee None
Priority Medium

md5: e8a0661c20ca16fb283a2301dbe43739

Issue Description:

It is sufficient to check each overload pair in a context once. Currently each function decl is checked subsequently, which results in overload pairs being checked multiple times.

func foo(arg: Int) {} // this will be 'a'
func foo(arg: Bool) {} // this will be 'b'
func foo(arg: String) {} // this will be 'c'

// CHECK FOR REDECL a: (a, a), (a, b), (a, c)
// CHECK FOR REDECL b: (b, a), (b, b), (b, c) // (b, a) is redundant
// CHECK FOR REDECL c: (c, a), (c, b), (c, c) // all redundant

n^2 checks instead of n*(n+1)/2
@belkadan
Copy link
Contributor

That's not quite true because of private functions, which are only checked in the file where they're defined, but yes, there are improvements to be made here.

@AnthonyLatsis
Copy link
Collaborator Author

@belkadan The part about redundant checks when we have a number of overloads is true, right? It seems I badly formulated the issue.
I am going to try to change the logic to detect redeclarations the first time such a pair is compared. I've also encountered something strange: when I test with two functions, they are both present in the DeclMap from the first check. With two or more variables, they appear gradually, one by one along the way as if nothing after the current decl is visible. Is this the way the type checker works? This is the reason the issue doesn't apply to variables.

@AnthonyLatsis
Copy link
Collaborator Author

OK, now I see this is a double-edged sword. Some variables may yet not even be part of the AST when 'checkRedeclaration' is invoked, functions are often not type-checked and thus don't have interface types, especially in combinations when the current decl comes earlier than others (a, b). Since we cannot guarantee that everything is type-checked, there is little place for improvement and the function will have to be invoked several times anyways (Everything might finish type-checking only when the last decl in this context is checked by this function).

Still, I can change this to catch redeclarations at (a, b) instead of (b, a), but I am not even sure there will be a use case. Everything that I've tried ends up in 'b' either being not type-checked or not present in lookup results.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants