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-12291] Regression in overload resolution #54719

Closed
swift-ci opened this issue Feb 27, 2020 · 10 comments
Closed

[SR-12291] Regression in overload resolution #54719

swift-ci opened this issue Feb 27, 2020 · 10 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12291
Radar rdar://problem/59874357
Original Reporter saeta (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee saeta (JIRA)
Priority Medium

md5: 099d7ee6ac1244f5c09cf1cf30b1e5be

relates to:

  • SR-13421 Type checker regression and crash in Xcode 12 beta 5

Issue Description:

Previous versions of the compiler have been able to correctly disambiguate between the following two overloads:

public func expect<T>(_ expression: @autoclosure @escaping () throws -> T?, file: StaticString = "a", line: UInt = 1) -> T {
  print("First")
  return try! expression()!
}

/// Make an expectation on a given actual value. The closure is lazily invoked.
public func expect<T>(_ file: StaticString = "b", line: UInt = 2, expression: @escaping () throws -> T?) -> T {
  print("Second")
  return try! expression()!
}

print("Hello world!")
let x = expect { 1 }
print("x: \(x)")
let y = expect(2)
print("y: \(y)")

But a recent version of the compiler built from master does not.

Expected:

Hello world!
Second
x: 1
First
y: 2
{code} 

Actual:

experimental/users/saeta/foo/foo.swift:14:9: error: ambiguous use of 'expect'
let x = expect { 1 }
^
experimental/users/saeta/foo/foo.swift:2:13: note: found this candidate
public func expect<T>(_ expression: @autoclosure @escaping () throws -> T?, file: StaticString = "a", line: UInt = 1) -> T {
^
experimental/users/saeta/foo/foo.swift:8:13: note: found this candidate
public func expect<T>(_ file: StaticString = "b", line: UInt = 2, expression: @escaping () throws -> T?) -> T {
^

@hborla
Copy link
Member

hborla commented Feb 28, 2020

@swift-ci create

@beccadax
Copy link
Contributor

I think this was caused by [#29845 [ConstraintSystem] Accept trailing closure if multiple defaulted parameters after last function parameter|#29845], which landed in this range and would have made the first overload a candidate when it previously wasn't.

@omochi @xedin Do we have a good way to fix this other than reverting? For instance, giving the previously-preferred overload a better score?

@xedin
Copy link
Member

xedin commented Mar 26, 2020

Yeah, I think this should be possible to fix by ranking first overload lower in case like "expect { 1 }" because having @autoclosure means that T is going to be `(() -> Int)?` which is not what anybody would expect...

@omochi
Copy link
Collaborator

omochi commented Mar 26, 2020

Certainly it is a regression by my code. I'm sorry.

More generally, I've noticed that my patches have produced the following regressions.

func f(aa: () -> Int, bb: Int = 0, cc: Int = 0) {
  print(1)
}

func f(bb: Int = 0, cc: Int = 0, aa: () -> Int) {
  print(2)
}

f { 0 }

// Xcode11.3 => 2
// master => ambiguous use of 'f'

If this is permissible, then in this case, as @xedin says, the inference for @autoclosure should be patched.

@omochi
Copy link
Collaborator

omochi commented Mar 26, 2020

When Trailing closure is followed by a defaulted argument instead of an actual last argument, how about increasing the score proportionally to its number?

@xedin
Copy link
Member

xedin commented Mar 26, 2020

Unfortunately to maintain source compatibility we'd have to revert your changes @omochi. Once that's done I'd add these test-case as a separate PR.

@omochi
Copy link
Collaborator

omochi commented Mar 26, 2020

OK🙂

@xedin
Copy link
Member

xedin commented Mar 26, 2020

Increasing score like that would be arbitrary, solver would already rank based on number of defaulted arguments, it shouldn't matter where they are positionally.

@xedin
Copy link
Member

xedin commented Mar 27, 2020

Revert is located at - #30656

@xedin
Copy link
Member

xedin commented Mar 27, 2020

saeta (JIRA User) Changes have been reverted and I have merged new test-cases via #30680 Please verify using next available nightly snapshot of master branch and close.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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 A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

5 participants