File: one_way_constraints.swift

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 (28 lines) | stat: -rw-r--r-- 1,323 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
// RUN: %target-typecheck-verify-swift -swift-version 4 -enable-builtin-module
import Builtin

func int8Or16(_ x: Int8) -> Int8 { return x }
func int8Or16(_ x: Int16) -> Int16 { return x }

// Explicit one-way constraints for testing purposes.
func testTernaryOneWay(b: Bool) {
  // Okay: backward inference works.
  let _: Float = b ? 3.14159 : 17

  // Errors due to one-way inference.
  let _: Float = b ? Builtin.one_way(3.14159) // expected-error{{cannot convert value of type 'Double' to specified type 'Float'}}
                   : 17
  let _: Float = b ? 3.14159 // expected-error {{cannot convert value of type 'Int' to specified type 'Float'}}
                   : Builtin.one_way(17)
  let _: Float = b ? Builtin.one_way(3.14159) // expected-error {{cannot convert value of type 'Int' to specified type 'Float'}}
                   : Builtin.one_way(17)

  // Okay: default still works.
  let _: Double = b ? Builtin.one_way(3.14159) : 17
  let _: Double = b ? 3.14159 : Builtin.one_way(17)
  // FIXME: expected-error@-1{{cannot convert value of type 'Int' to specified type 'Double'}}
  // The above fails because we are minimizing the set of partial solutions
  // for the integer literal 17, so we don't try it as a Double

  let _: Int8 = b ? Builtin.one_way(int8Or16(17)) : Builtin.one_way(int8Or16(42))
}