File: typo_correction.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 (222 lines) | stat: -rw-r--r-- 6,916 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// RUN: %target-typecheck-verify-swift -typo-correction-limit 23
// RUN: not %target-swift-frontend -typecheck -disable-typo-correction -diagnostic-style llvm %s 2>&1 | %FileCheck %s -check-prefix=DISABLED
// RUN: not %target-swift-frontend -typecheck -typo-correction-limit 0 -diagnostic-style llvm%s 2>&1 | %FileCheck %s -check-prefix=DISABLED
// RUN: not %target-swift-frontend -typecheck -DIMPORT_FAIL %s -diagnostic-style llvm 2>&1 | %FileCheck %s -check-prefix=DISABLED
// DISABLED-NOT: did you mean

#if IMPORT_FAIL
import NoSuchModule
#endif

// This is close enough to get typo-correction.
func test_short_and_close() {
  let plop = 4 // expected-note {{'plop' declared here}}
  let bab = plob + 1
  // expected-error@-1 {{cannot find 'plob' in scope}}
}

// This is not.
func test_too_different() {
  let moo = 4
  let bbb = mbb + 1 // expected-error {{cannot find 'mbb' in scope}}
}

struct Whatever {}
func *(x: Whatever, y: Whatever) {}

// This works even for single-character identifiers.
func test_very_short() {
  // Note that we don't suggest operators.
  let x = 0 // expected-note {{did you mean 'x'?}}
  let longer = y
  // expected-error@-1 {{cannot find 'y' in scope}}
}

// It does not trigger in a variable's own initializer.
func test_own_initializer() {
  let x = y // expected-error {{cannot find 'y' in scope}}
}

// Report candidates that are the same distance in different ways.
func test_close_matches() {
  let match1 = 0 // expected-note {{did you mean 'match1'?}}
  let match22 = 0 // expected-note {{did you mean 'match22'?}}
  let x = match2 // expected-error {{cannot find 'match2' in scope}}
}

// Report not-as-good matches if they're still close enough to the best.
func test_keep_if_not_too_much_worse() {
  let longmatch1 = 0 // expected-note {{did you mean 'longmatch1'?}}
  let longmatch22 = 0 // expected-note {{did you mean 'longmatch22'?}}
  let x = longmatch // expected-error {{cannot find 'longmatch' in scope}}
}

// Report not-as-good matches if they're still close enough to the best.
func test_drop_if_too_different() {
  let longlongmatch1 = 0 // expected-note {{'longlongmatch1' declared here}}
  let longlongmatch2222 = 0
  let x = longlongmatch
  // expected-error@-1 {{cannot find 'longlongmatch' in scope; did you mean 'longlongmatch1'?}}
}

// Candidates are suppressed if we have too many that are the same distance.
func test_too_many_same() {
  let match1 = 0
  let match2 = 0
  let match3 = 0
  let match4 = 0
  let match5 = 0
  let match6 = 0
  let x = match // expected-error {{cannot find 'match' in scope}}
}

// But if some are better than others, just drop the worse tier.
func test_too_many_but_some_better() {
  let mtch1 = 0 // expected-note {{did you mean 'mtch1'?}}
  let mtch2 = 0 // expected-note {{did you mean 'mtch2'?}}
  let match3 = 0
  let match4 = 0
  let match5 = 0
  let match6 = 0
  let x = mtch // expected-error {{cannot find 'mtch' in scope}}
}

// rdar://problem/28387684
// Don't crash performing typo correction on bound generic types with
// type variables.
_ = [Any]().withUnsafeBufferPointer { (buf) -> [Any] in
  guard let base = buf.baseAddress else { return [] }
  return (base ..< base + buf.count).m // expected-error {{value of type 'Range<UnsafePointer<Any>>' has no member 'm'}}
}

// Typo correction with class-bound archetypes.
class SomeClass {
  func match1() {} // expected-note {{'match1' declared here}}
}

func takesSomeClassArchetype<T : SomeClass>(_ t: T) {
  t.match0()
  // expected-error@-1 {{value of type 'T' has no member 'match0'; did you mean 'match1'?}}{{5-11=match1}}
}

// Typo correction of unqualified lookup from generic context.
func match1() {}
// expected-note@-1 {{'match1' declared here}}
struct Generic<T> { // expected-note {{'T' declared as parameter to type 'Generic'}}
  class Inner {
    func doStuff() {
      match0()
      // expected-error@-1 {{cannot find 'match0' in scope; did you mean 'match1'?}}
    }
  }
}

protocol P { // expected-note {{'P' previously declared here}}
  // expected-note@-1 2{{did you mean 'P'?}}
  // expected-note@-2 {{'P' declared here}}
  typealias a = Generic
}

protocol P {} // expected-error {{invalid redeclaration of 'P'}}

func hasTypo() {
  _ = P.a.a // expected-error {{type 'Generic<T>' has no member 'a'}}
  // expected-error@-1 {{generic parameter 'T' could not be inferred}}
}

// Typo correction with AnyObject.
func takesAnyObject(_ t: AnyObject) {
  _ = t.rawPointer
  // expected-error@-1 {{value of type 'AnyObject' has no member 'rawPointer'}}
}

func takesAnyObjectArchetype<T : AnyObject>(_ t: T) {
  _ = t.rawPointer
  // expected-error@-1 {{value of type 'T' has no member 'rawPointer'}}
}

// Typo correction with an UnresolvedDotExpr.
enum Foo {
  case flashing // expected-note {{'flashing' declared here}}
}

func foo(_ a: Foo) {
}

func bar() {
  foo(.flashin)
  // expected-error@-1 {{type 'Foo' has no member 'flashin'; did you mean 'flashing'?}}{{8-15=flashing}}
}

// Verify that we emit a fixit even if there are multiple
// declarations with the corrected name.
func overloaded(_: Int) {} // expected-note {{'overloaded' declared here}}
func overloaded(_: Float) {} // expected-note {{'overloaded' declared here}}
func test_overloaded() {
  overloadd(0)
  // expected-error@-1 {{cannot find 'overloadd' in scope; did you mean 'overloaded'?}}{{3-12=overloaded}}
}

// This is one of the backtraces from rdar://36434823 but got fixed along
// the way.
class CircularValidationWithTypo {
  var cdcdcdcd = ababab { // expected-error {{cannot find 'ababab' in scope}}
    didSet { }
  }

  var abababab = cdcdcdc { // expected-error {{cannot find 'cdcdcdc' in scope}}
    didSet { }
  }
}

// https://github.com/apple/swift/issues/51488
// Crash with invalid extension that has not been bound

protocol PP {}

func boo() {
  extension PP { // expected-error {{declaration is only valid at file scope}}
    func g() {
      booo() // expected-error {{cannot find 'booo' in scope}}
    }
  }
}

// Don't show underscored names as typo corrections unless the typed name also
// begins with an underscore.
func test_underscored_no_match() {
  let _ham = 0
  _ = ham
  // expected-error@-1 {{cannot find 'ham' in scope}}
}

func test_underscored_match() {
  let _eggs = 4 // expected-note {{'_eggs' declared here}}
  _ = _fggs + 1
  // expected-error@-1 {{cannot find '_fggs' in scope; did you mean '_eggs'?}}
}

// Don't show values before declaration.
func testFwdRef() {
  let _ = forward_refX + 1 // expected-error {{cannot find 'forward_refX' in scope}}
  let forward_ref1 = 4
}

// Crash with protocol members.
protocol P1 {
  associatedtype A1
  associatedtype A2
}

protocol P2 {
  associatedtype A1
  associatedtype A2

  func method<T: P1>(_: T) where T.A1 == A1, T.A2 == A2
}

extension P2 {
  func f() { // expected-note {{did you mean 'f'?}}
    _ = a // expected-error {{cannot find 'a' in scope}}
  }
}