Skip to content

[stdlib] Workaround type checker ambiguity in Comparable SwiftNewtypeWrapper #7024

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

Merged
merged 1 commit into from
Feb 3, 2017

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Jan 25, 2017

In macOS,

import Foundation
_ = NSLocale.Key.calendar <= NSLocale.Key.countryCode

didn't typecheck because of ambiguity.

This PR adds an additional constraint to Comparable functions for _SwiftNewTypeWrapper,
just like func != for Equatable, RawRepresentable.

@rintaro rintaro changed the title [stdlib] Workaround type checker ambiguity in Comparable SwiftNewtypeWrapper [Do not merge][stdlib] Workaround type checker ambiguity in Comparable SwiftNewtypeWrapper Jan 25, 2017
@rintaro
Copy link
Member Author

rintaro commented Jan 25, 2017

@DougGregor
This fix seems reasonable?
Where and how should I add test cases? I tried test/IDE/newtype.swift, but -verify couldn't catch this because the error does not belong to that file.

@parkera
This is related to swiftlang/swift-corelibs-foundation#829 (comment)

@airspeedswift
Copy link
Member

Did it used to type check but has regressed?

@rintaro
Copy link
Member Author

rintaro commented Jan 26, 2017

@airspeedswift
I think : _SwiftNewTypeWrapper, Comparable types never worked as Comparable; not a regression.

func acceptComparable<T : Comparable>(_: T) {}
acceptComparable(NSLocale.Key.calendar)
// <unknown>:0: error: type 'NSLocale.Key' does not conform to protocol 'Comparable'
// Swift.Comparable:144:24: note: multiple matching functions named '<=' with type '(NSLocale.Key, NSLocale.Key) -> Bool'

@DougGregor
Copy link
Member

This looks like a good change to me. You can add a test to test/ClangImporter/objc_parse.swift

@rintaro rintaro force-pushed the stdlib-newtype-comparable branch from 85c249a to c784a9e Compare January 31, 2017 05:55
@rintaro
Copy link
Member Author

rintaro commented Jan 31, 2017

@DougGregor Thank you!

I added test/ClangImporter/newtype_conformance.swift.
Since these errors are not reported in this file, -verify mode ignores them.
So we can't use -verify for this test case.

<unknown>:0: error: type 'NSNotification.Name' does not conform to protocol 'Comparable'
Swift.Comparable:144:24: note: multiple matching functions named '<=' with type '(NSNotification.Name, NSNotification.Name) -> Bool'
    public static func <=(lhs: Self, rhs: Self) -> Bool
                       ^
Swift.<=:10:13: note: candidate exactly matches
public func <=<T>(lhs: T, rhs: T) -> Bool where T : Comparable
            ^
Swift.<=:1:13: note: candidate exactly matches
public func <=<T>(lhs: T, rhs: T) -> Bool where T : _SwiftNewtypeWrapper, T.RawValue : Comparable
            ^
Swift.Comparable:151:24: note: multiple matching functions named '>=' with type '(NSNotification.Name, NSNotification.Name) -> Bool'
    public static func >=(lhs: Self, rhs: Self) -> Bool
                       ^
Swift.>=:12:13: note: candidate exactly matches
public func >=<T>(lhs: T, rhs: T) -> Bool where T : Comparable
            ^
Swift.>=:1:13: note: candidate exactly matches
public func >=<T>(lhs: T, rhs: T) -> Bool where T : _SwiftNewtypeWrapper, T.RawValue : Comparable
            ^
Swift.Comparable:158:24: note: multiple matching functions named '>' with type '(NSNotification.Name, NSNotification.Name) -> Bool'
    public static func >(lhs: Self, rhs: Self) -> Bool
                       ^
Swift.>:10:13: note: candidate exactly matches
public func ><T>(lhs: T, rhs: T) -> Bool where T : Comparable
            ^
Swift.>:1:13: note: candidate exactly matches
public func ><T>(lhs: T, rhs: T) -> Bool where T : _SwiftNewtypeWrapper, T.RawValue : Comparable
            ^

@rintaro rintaro changed the title [Do not merge][stdlib] Workaround type checker ambiguity in Comparable SwiftNewtypeWrapper [stdlib] Workaround type checker ambiguity in Comparable SwiftNewtypeWrapper Jan 31, 2017
@rintaro rintaro force-pushed the stdlib-newtype-comparable branch from c784a9e to f11b741 Compare January 31, 2017 12:29
@DougGregor
Copy link
Member

@swift-ci please smoke test and merge

@DougGregor
Copy link
Member

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member

@swift-ci please smoke test

@DougGregor DougGregor merged commit 0f99d18 into swiftlang:master Feb 3, 2017
@rintaro rintaro deleted the stdlib-newtype-comparable branch March 16, 2017 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants