Skip to content

Commit 72b8813

Browse files
authored
Restore operator '+' family for UnsafePointer. (#3719)
The reverts part of my previous patch. Removing the operators is too much of a performance penalty to take. The difference is that the Strideable operators are not transparent. I still need to remove the UnsafeRawPointer operators, so -Onone performance will be bad in some cases until this is fixed: <rdar://problem/27513184> [perf] Strideable operators are not transparent. This is a huge -Onone performance penalty.
1 parent 0ad1ff7 commit 72b8813

File tree

5 files changed

+49
-14
lines changed

5 files changed

+49
-14
lines changed

stdlib/public/core/UnsafePointer.swift.gyb

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,12 @@ public struct ${Self}<Pointee>
420420
421421
/// Return `end - self`.
422422
public func distance(to x: ${Self}) -> Int {
423-
return Int(Builtin.sub_Word(Builtin.ptrtoint_Word(x._rawValue),
424-
Builtin.ptrtoint_Word(_rawValue)))
425-
/ strideof(Pointee.self)
423+
return x - self
426424
}
427425
428426
/// Return `self + n`.
429427
public func advanced(by n: Int) -> ${Self} {
430-
return ${Self}(Builtin.gep_Word(
431-
_rawValue, (n &* strideof(Pointee.self))._builtinWordValue))
428+
return self + n
432429
}
433430
}
434431
@@ -476,6 +473,44 @@ public func < <Pointee>(lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Bool {
476473
return Bool(Builtin.cmp_ult_RawPointer(lhs._rawValue, rhs._rawValue))
477474
}
478475
476+
/// - Note: The following family of operator overloads are redundant
477+
/// with Strideable. However, optimizer improvements are needed
478+
/// before they can be removed without affecting performance.
479+
@_transparent
480+
public func + <Pointee>(lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
481+
return ${Self}(Builtin.gep_Word(
482+
lhs._rawValue, (rhs &* strideof(Pointee.self))._builtinWordValue))
483+
}
484+
485+
@_transparent
486+
public func + <Pointee>(lhs: Int,
487+
rhs: ${Self}<Pointee>) -> ${Self}<Pointee> {
488+
return rhs + lhs
489+
}
490+
491+
@_transparent
492+
public func - <Pointee>(lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
493+
return lhs + -rhs
494+
}
495+
496+
@_transparent
497+
public func - <Pointee>(lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Int {
498+
return
499+
Int(Builtin.sub_Word(Builtin.ptrtoint_Word(lhs._rawValue),
500+
Builtin.ptrtoint_Word(rhs._rawValue)))
501+
/ strideof(Pointee.self)
502+
}
503+
504+
@_transparent
505+
public func += <Pointee>(lhs: inout ${Self}<Pointee>, rhs: Int) {
506+
lhs = lhs + rhs
507+
}
508+
509+
@_transparent
510+
public func -= <Pointee>(lhs: inout ${Self}<Pointee>, rhs: Int) {
511+
lhs = lhs - rhs
512+
}
513+
479514
extension ${Self} {
480515
@available(*, unavailable, renamed: "Pointee")
481516
public typealias Memory = Pointee

test/1_stdlib/UnicodeScalarDiagnostics.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ func test_UnicodeScalarDoesNotImplementArithmetic(_ us: UnicodeScalar, i: Int) {
2121
let b4 = us / us // expected-error {{binary operator '/' cannot be applied to two 'UnicodeScalar' operands}}
2222
// expected-note @-1 {{overloads for '/' exist with these partially matching parameter lists:}}
2323

24-
let c1 = us + i // expected-error {{binary operator '+' cannot be applied to operands of type 'UnicodeScalar' and 'Int'}} expected-note{{expected an argument list of type '(Int, Int)'}}
25-
let c2 = us - i // expected-error {{binary operator '-' cannot be applied to operands of type 'UnicodeScalar' and 'Int'}} expected-note{{expected an argument list of type '(Int, Int)'}}
24+
let c1 = us + i // expected-error {{binary operator '+' cannot be applied to operands of type 'UnicodeScalar' and 'Int'}} expected-note{{overloads for '+' exist with these partially matching parameter lists:}}
25+
let c2 = us - i // expected-error {{binary operator '-' cannot be applied to operands of type 'UnicodeScalar' and 'Int'}} expected-note{{overloads for '-' exist with these partially matching parameter lists: (Int, Int), (UnsafeMutablePointer<Pointee>, Int), (UnsafePointer<Pointee>, Int)}}
2626
let c3 = us * i // expected-error {{binary operator '*' cannot be applied to operands of type 'UnicodeScalar' and 'Int'}} expected-note {{expected an argument list of type '(Int, Int)'}}
2727
let c4 = us / i // expected-error {{binary operator '/' cannot be applied to operands of type 'UnicodeScalar' and 'Int'}} expected-note {{expected an argument list of type '(Int, Int)'}}
2828

29-
let d1 = i + us // expected-error {{binary operator '+' cannot be applied to operands of type 'Int' and 'UnicodeScalar'}} expected-note{{expected an argument list of type '(Int, Int)'}}
29+
let d1 = i + us // expected-error {{binary operator '+' cannot be applied to operands of type 'Int' and 'UnicodeScalar'}} expected-note{{overloads for '+' exist with these partially matching parameter lists:}}
3030
let d2 = i - us // expected-error {{binary operator '-' cannot be applied to operands of type 'Int' and 'UnicodeScalar'}} expected-note {{expected an argument list of type '(Int, Int)'}}
3131
let d3 = i * us // expected-error {{binary operator '*' cannot be applied to operands of type 'Int' and 'UnicodeScalar'}} expected-note {{expected an argument list of type '(Int, Int)'}}
3232
let d4 = i / us // expected-error {{binary operator '/' cannot be applied to operands of type 'Int' and 'UnicodeScalar'}} expected-note {{expected an argument list of type '(Int, Int)'}}

test/Constraints/bridging.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,12 @@ func rdar19770981(_ s: String, ns: NSString) {
260260

261261
// <rdar://problem/19831919> Fixit offers as! conversions that are known to always fail
262262
func rdar19831919() {
263-
var s1 = 1 + "str"; // expected-error{{binary operator '+' cannot be applied to operands of type 'Int' and 'String'}} expected-note{{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)}}
263+
var s1 = 1 + "str"; // expected-error{{binary operator '+' cannot be applied to operands of type 'Int' and 'String'}} expected-note{{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
264264
}
265265

266266
// <rdar://problem/19831698> Incorrect 'as' fixits offered for invalid literal expressions
267267
func rdar19831698() {
268-
var v70 = true + 1 // expected-error{{binary operator '+' cannot be applied to operands of type 'Bool' and 'Int'}} expected-note {{expected an argument list of type '(Int, Int)'}}
268+
var v70 = true + 1 // expected-error{{binary operator '+' cannot be applied to operands of type 'Bool' and 'Int'}} expected-note {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (UnsafeMutablePointer<Pointee>, Int), (UnsafePointer<Pointee>, Int)}}
269269
var v71 = true + 1.0 // expected-error{{binary operator '+' cannot be applied to operands of type 'Bool' and 'Double'}}
270270
// expected-note@-1{{overloads for '+'}}
271271
var v72 = true + true // expected-error{{binary operator '+' cannot be applied to two 'Bool' operands}}

test/Constraints/closures.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func r22162441(_ lines: [String]) {
9494
func testMap() {
9595
let a = 42
9696
[1,a].map { $0 + 1.0 } // expected-error {{binary operator '+' cannot be applied to operands of type 'Int' and 'Double'}}
97-
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (Double, Double)}}
97+
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (Double, Double), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
9898
}
9999

100100
// <rdar://problem/22414757> "UnresolvedDot" "in wrong phase" assertion from verifier

test/Constraints/diagnostics.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func recArea(_ h: Int, w : Int) {
186186
// <rdar://problem/17224804> QoI: Error In Ternary Condition is Wrong
187187
func r17224804(_ monthNumber : Int) {
188188
// expected-error @+2 {{binary operator '+' cannot be applied to operands of type 'String' and 'Int'}}
189-
// expected-note @+1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)}}
189+
// expected-note @+1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String), (UnsafeMutablePointer<Pointee>, Int), (UnsafePointer<Pointee>, Int)}}
190190
let monthString = (monthNumber <= 9) ? ("0" + monthNumber) : String(monthNumber)
191191
}
192192

@@ -535,13 +535,13 @@ func testTypeSugar(_ a : Int) {
535535

536536
let x = Stride(a)
537537
x+"foo" // expected-error {{binary operator '+' cannot be applied to operands of type 'Stride' (aka 'Int') and 'String'}}
538-
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String)}}
538+
// expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (String, String), (Int, UnsafeMutablePointer<Pointee>), (Int, UnsafePointer<Pointee>)}}
539539
}
540540

541541
// <rdar://problem/21974772> SegFault in FailureDiagnosis::visitInOutExpr
542542
func r21974772(_ y : Int) {
543543
let x = &(1.0 + y) // expected-error {{binary operator '+' cannot be applied to operands of type 'Double' and 'Int'}}
544-
//expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (Double, Double)}}
544+
//expected-note @-1 {{overloads for '+' exist with these partially matching parameter lists: (Int, Int), (Double, Double), (UnsafeMutablePointer<Pointee>, Int), (UnsafePointer<Pointee>, Int)}}
545545
}
546546

547547
// <rdar://problem/22020088> QoI: missing member diagnostic on optional gives worse error message than existential/bound generic/etc

0 commit comments

Comments
 (0)