File: redeclaration-checking.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 (154 lines) | stat: -rw-r--r-- 4,886 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// RUN: %target-typecheck-verify-swift

// Test redeclaration checking in local context.

func test1() {
  let x = 123 // expected-note{{'x' previously declared here}}
  func f() {} // expected-note{{'f()' previously declared here}}
  struct S {} // expected-note{{'S' previously declared here}}
  let x = 321 // expected-error{{invalid redeclaration of 'x'}}
  func f() {} // expected-error{{invalid redeclaration of 'f()'}}
  struct S {} // expected-error{{invalid redeclaration of 'S'}}
}

func test2() {
  let x = 123 // expected-warning {{never used}}
  func f() {}
  struct S {}
  do {
    let x = 321 // expected-warning {{never used}}
    func f() {}
    struct S {}
  }
}

func test3<T, T>(_: T, _: T) {}
// expected-note@-1 {{'T' previously declared here}}
// expected-error@-2 {{invalid redeclaration of 'T'}}
// expected-error@-3 {{generic parameter 'T' is not used in function signature}}

func test4(x: Int, x: Int) {}
// expected-note@-1 {{'x' previously declared here}}
// expected-error@-2 {{invalid redeclaration of 'x'}}

struct Test4<T, T> {}
// expected-note@-1 {{'T' previously declared here}}
// expected-error@-2 {{invalid redeclaration of 'T'}}

typealias Test5<T, T> = ()
// expected-note@-1 {{'T' previously declared here}}
// expected-error@-2 {{invalid redeclaration of 'T'}}

enum E {
  case test6(x: Int, x: Int)
  // expected-note@-1 {{'x' previously declared here}}
  // expected-error@-2 {{invalid redeclaration of 'x'}}

  subscript(x: Int, x: Int) -> Int { return 0 }
  // expected-note@-1 {{'x' previously declared here}}
  // expected-error@-2 {{invalid redeclaration of 'x'}}
}

_ = { (x: Int, x: Int) in }
// expected-note@-1 {{'x' previously declared here}}
// expected-error@-2 {{invalid redeclaration of 'x'}}

enum MyError : Error {
  case error(Int, Int)
}

func stmtTest() {
  let n: (Int, Int)? = nil

  if case (let x, let x)? = n {}
  // expected-note@-1 {{'x' previously declared here}}
  // expected-error@-2 {{invalid redeclaration of 'x'}}

  for case (let x, let x) in [(Int, Int)]() {}
  // expected-note@-1 {{'x' previously declared here}}
  // expected-error@-2 {{invalid redeclaration of 'x'}}

  switch n {
  case (let x, let x)?: _ = ()
  // expected-note@-1 {{'x' previously declared here}}
  // expected-error@-2 {{invalid redeclaration of 'x'}}
  case nil: _ = ()
  }

  while case (let x, let x)? = n {}
  // expected-note@-1 {{'x' previously declared here}}
  // expected-error@-2 {{invalid redeclaration of 'x'}}

  guard case (let x, let x)? = n else {}
  // expected-note@-1 {{'x' previously declared here}}
  // expected-error@-2 {{invalid redeclaration of 'x'}}

  do {} catch MyError.error(let x, let x) {}
  // expected-note@-1 {{'x' previously declared here}}
  // expected-error@-2 {{invalid redeclaration of 'x'}}
  // expected-warning@-3 {{unreachable}}
}

func fullNameTest() {
  let x = 123 // expected-warning {{never used}}
  func x() {}
}

// For source compatibility, allow duplicate parameter labels on
// protocol requirements.
protocol SillyProtocol {
  init(x: Int, x: Int)
  init(a x: Int, b x: Int)

  func foo(x: Int, x: Int)
  func foo(a x: Int, b x: Int)

  subscript(x: Int, x: Int) -> Int { get }
  subscript(a x: Int, b x: Int) -> Int { get }
}

// https://github.com/apple/swift/issues/63750
let issue63750 = {
  for (x,x) in [(0,0)] {}
  // expected-error@-1 {{invalid redeclaration of 'x'}}
  // expected-note@-2 {{'x' previously declared here}}

  if case let (x,x) = (0,0) {}
  // expected-error@-1 {{invalid redeclaration of 'x'}}
  // expected-note@-2 {{'x' previously declared here}}

  switch (0,0) {
  case let (x,x):
    // expected-error@-1 {{invalid redeclaration of 'x'}}
    // expected-note@-2 {{'x' previously declared here}}
    ()
  }
  
  func bar(_ x: Int) -> Int { x }
  if case (bar(let x), let x) = (0,0) {}
  // expected-error@-1 {{'let' binding pattern cannot appear in an expression}}
  // expected-error@-2 {{invalid redeclaration of 'x'}}
  // expected-note@-3 {{'x' previously declared here}}
}
func issue63750fn() {
  // Make sure the behavior is consistent with the multi-statement closure case.
  for (x,x) in [(0,0)] {}
  // expected-error@-1 {{invalid redeclaration of 'x'}}
  // expected-note@-2 {{'x' previously declared here}}

  if case let (x,x) = (0,0) {} // expected-warning {{'if' condition is always true}}
  // expected-error@-1 {{invalid redeclaration of 'x'}}
  // expected-note@-2 {{'x' previously declared here}}

  switch (0,0) {
  case let (x,x):
    // expected-error@-1 {{invalid redeclaration of 'x'}}
    // expected-note@-2 {{'x' previously declared here}}
    ()
  }
  func bar(_ x: Int) -> Int { x }
  if case (bar(let x), let x) = (0,0) {}
  // expected-error@-1 {{'let' binding pattern cannot appear in an expression}}
  // expected-error@-2 {{invalid redeclaration of 'x'}}
  // expected-note@-3 {{'x' previously declared here}}
}