File: FixedPointDiagnostics.swift.gyb

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (81 lines) | stat: -rw-r--r-- 3,505 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// RUN: %empty-directory(%t)
// RUN: %gyb %s -o %t/main.swift
// RUN: %line-directive %t/main.swift -- %target-swift-frontend -typecheck -verify -swift-version 4.2 %t/main.swift

func testUnaryMinusInUnsigned() {
  var a: UInt8 = -(1) // expected-error {{no '-' candidates produce the expected contextual result type 'UInt8'}} expected-note * {{}} expected-warning * {{}}

  var b: UInt16 = -(1) // expected-error {{no '-' candidates produce the expected contextual result type 'UInt16'}} expected-note * {{}} expected-warning * {{}}

  var c: UInt32 = -(1) // expected-error {{no '-' candidates produce the expected contextual result type 'UInt32'}} expected-note * {{}} expected-warning * {{}}

  var d: UInt64 = -(1) // expected-error {{no '-' candidates produce the expected contextual result type 'UInt64'}} expected-note * {{}} expected-warning * {{}}
}

// Int and UInt are not identical to any fixed-size integer type
var i   :  Int   = 0
var i64 :  Int64 = i // expected-error {{}}
var i32 :  Int32 = i // expected-error {{}}
var i16 :  Int16 = i // expected-error {{}}
var i8  :  Int8  = i // expected-error {{}}

var u   : UInt   = 0
var u64 : UInt64 = u // expected-error {{}}
var u32 : UInt32 = u // expected-error {{}}
var u16 : UInt16 = u // expected-error {{}}
var u8  : UInt8  = u // expected-error {{}}

func testMixedSignArithmetic() {
  // Ensure that the generic arithmetic operators for Strideable don't
  // allow mixed-sign arithmetic to compile.  We create a deliberate
  // ambiguity in these cases.
% for T in "UInt UInt64 UInt32 UInt16 UInt8".split():
  do {
    typealias Stride = ${T}.Stride
    _ = ${T}(1) + 0     // OK
    _ = 0 + ${T}(1)     // OK
    _ = ${T}(1) + Stride(0) // expected-error {{}} expected-note {{}}
    _ = Stride(1) + ${T}(0) // expected-error {{}} expected-note {{}}
    _ = ${T}(1) - Stride(0) // expected-error {{}} expected-note {{}}
    var x: ${T} = 0
    x += 1              // OK
    x += Stride(1) // expected-error {{cannot convert value of type 'Stride' (aka 'Int') to expected argument type '${T}'}} {{10-10=${T}(}} {{19-19=)}}
    x -= Stride(1) // expected-error {{cannot convert value of type 'Stride' (aka 'Int') to expected argument type '${T}'}} {{10-10=${T}(}} {{19-19=)}}

    _ = (x - x) as Stride // expected-error {{}}

    //===------------------------------------------------------------------===//
    // The following errors are different because they're not being
    // disabled by the ambiguity trick.
    //===------------------------------------------------------------------===//

    (x + x) as Stride   // expected-error {{}}
    Stride(1) - ${T}(0) // expected-error {{}} expected-note {{}}

    var y: Stride = 0
    y += ${T}(1)        // expected-error {{cannot convert value of type '${T}' to expected argument type 'Int'}} {{10-10=Int(}}
    y -= ${T}(1)        // expected-error {{cannot convert value of type '${T}' to expected argument type 'Int'}} {{10-10=Int(}}
  }
% end
}

func testOps<T : FixedWidthInteger>(_ x: T, _ y: T) -> T {
  let a = x + y
  let s = x - y
  let m = x * y
  let d = x / y
  let r = x % y
  return a + s + m + d + r
}

let   s_ops:    Int = testOps(5, 2)
let   u_ops:   UInt = testOps(5, 2)
let  s8_ops:   Int8 = testOps(5, 2)
let  u8_ops:  UInt8 = testOps(5, 2)
let s16_ops:  Int16 = testOps(5, 2)
let u16_ops: UInt16 = testOps(5, 2)
let s32_ops:  Int32 = testOps(5, 2)
let u32_ops: UInt32 = testOps(5, 2)
let s64_ops:  Int64 = testOps(5, 2)
let u64_ops: UInt64 = testOps(5, 2)