File: class_constraint.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 (60 lines) | stat: -rw-r--r-- 1,590 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
// RUN: %target-typecheck-verify-swift

struct X<T: AnyObject> { } // expected-note 4{{requirement specified as 'T' : 'AnyObject'}}

class C { }
struct S { }

protocol P { }

let okay0: X<C>

struct Y<T: AnyObject> {
  let okay1: X<T>
}

struct Y2<T: C> {
  let okay2: X<T>
}

let bad0: X<C & P> // expected-error{{'X' requires that 'any C & P' be a class type}}
let bad1: X<P> // expected-error{{'X' requires that 'any P' be a class type}}
let bad2: X<S> // expected-error{{'X' requires that 'S' be a class type}}

struct Z<U> {
  let bad3: X<U> // expected-error{{'X' requires that 'U' be a class type}}
}

// https://github.com/apple/swift/issues/49716
// Layout constraints weren't getting merged.

protocol P1 {
  associatedtype A
  var a: A { get }
}

protocol P2 {
  associatedtype B: P1
  var b: B { get }
}

func requiresAnyObject<T: AnyObject>(_: T) { }

func anyObjectConstraint<T: P2, U: P2>(_ t: T, _ u: U)
where T.B.A: AnyObject, U.B: AnyObject, T.B == T.B.A, U.B.A == U.B {
  requiresAnyObject(t.b)
  requiresAnyObject(u.b)
  requiresAnyObject(t.b.a)
  requiresAnyObject(u.b.a)
}

func test_class_constraint_diagnostics_with_contextual_type() {
  func foo<T : AnyObject>(_: AnyObject) -> T {} // expected-note 2 {{where 'T' = 'any P'}}

  class A : P {}

  // TODO(diagnostics): We could also add a note here that protocols do not conform to themselves

  let _: P = foo(A() as AnyObject) // expected-error {{local function 'foo' requires that 'any P' be a class type}}
  let _: P = foo(A()) // expected-error {{local function 'foo' requires that 'any P' be a class type}}
}