Obj-C Lightweight Generics for NSLayoutAnchor are not available in Swift

Originator:mark.adams86
Number:rdar://24259140 Date Originated:01/20/2016
Status:Duplicate of 20031907 Resolved:01/25/2016
Product:Developer Tools Product Version:iOS 9
Classification:Other Bug Reproducible:Always
 
Summary:
The Objective-C declaration of NSLayoutAnchor uses lightweight generics to provide compile time warnings when creating invalid constraints between anchors with different axis types. The Swift version of these APIs does not use generics and uses implicitly unwrapped instances of NSLayoutAnchor providing no type safety.

Consider the declaration for the constraintEqualToAnchor method on NSLayoutAnchor.

In Objective-C it is declared as:

- (NSLayoutConstraint *)constraintEqualToAnchor:(NSLayoutAnchor<AnchorType> *)anchor

...while in Swift it is declared as:

func constraintEqualToAnchor(_ anchor: NSLayoutAnchor!) -> NSLayoutConstraint!

This means that when attempting to establish an invalid constraint between a view's top anchor and another view's leading anchor in Objective-C, we get a compile-time warning about incompatible pointer types between NSLayoutAnchor<NSLayoutYAxisAnchor> * and NSLayoutAnchor<NSLayoutXAxisAnchor> *. We get no such warning at compile-time or run-time in Swift. Not even a console message is logged noting the type mismatch.

Steps to Reproduce:
1. Create a Swift iOS project with a deployment target of iOS 9.0.
2. Initialize two instances of UIView.
3. Constrain the top anchor of the first view to the leading anchor of the second view.

Expected Results:
I expect the project not to compile in Swift because we're creating a constraint between anchors of different types. At the very least, I'd expect a runtime crash and console message noting the type mismatch.

Actual Results:
The project compiles successfully. The visual appearance of the layout between the two views is most likely incorrect because the created constraints are invalid. No message is raised in the console.

Version:
Xcode 7.2 (7C68)
OS X 10.11.3 (15D21)
iOS 9.2

Notes:
The documentation of the three NSLayoutAnchor subclasses (NSLayoutDimension, NSLayoutXAxisAnchor, and NSLayoutYAxisAnchor) specifically mention that they exist to provide additional type information. For example, from the NSLayoutDimension docs:

"In addition to providing size-specific methods for creating constraints, this class adds type information to the methods inherited from NSLayoutAnchor. Specifically, the generic methods declared by NSLayoutAnchor must now take a matching NSLayoutDimension object."

I'm not sure if this is an iOS SDK bug or a bug in the Swift component that converts Objective-C APIs but it's odd that additional type-safety is only available in Objective-C.

Configuration:
Occurs on all hardware configurations building an iOS app that targets iOS 9 and greater.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!