Skip to content

Commit 16dee0d

Browse files
[tests] Fixing tests under test/type, test/expr and test/Parse
1 parent bdf8502 commit 16dee0d

File tree

7 files changed

+196
-18
lines changed

7 files changed

+196
-18
lines changed

test/Parse/omit_return.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ func ff_implicitIsExpr<T>(t: T) -> Bool {
510510
}
511511

512512
func ff_implicitCoerceExpr<T>() -> T.Type {
513-
T.self as T.Type
513+
T.self as T.Type // expected-warning {{redundant cast to 'T.Type' has no effect}} {{12-22=}}
514514
}
515515

516516
func ff_implicitConditionalCheckedCastExprAs<T>(t: T) -> Int? {

test/expr/cast/array_downcast.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var ta = [t]
2424

2525
va = ta
2626

27-
var va2: ([V])? = va as [V]
27+
var va2: ([V])? = va as [V] // expected-warning {{redundant cast to '[V]' has no effect}} {{22-29=}}
2828
var v2: V = va2![0]
2929

3030
var ua2: ([U])? = va as? [U]

test/expr/cast/as_coerce.swift

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ c3 as C4 // expected-error {{'C3' is not convertible to 'C4'; did you mean to us
8181
// <rdar://problem/19495142> Various incorrect diagnostics for explicit type conversions
8282
1 as Double as Float // expected-error{{cannot convert value of type 'Double' to type 'Float' in coercion}}
8383
1 as Int as String // expected-error{{cannot convert value of type 'Int' to type 'String' in coercion}}
84-
Double(1) as Double as String // expected-error{{cannot convert value of type 'Double' to type 'String' in coercion}}
84+
Double(1) as Double as String // expected-error{{cannot convert value of type 'Double' to type 'String' in coercion}} expected-warning {{redundant cast to 'Double' has no effect}} {{11-21=}}
8585
["awd"] as [Int] // expected-error{{cannot convert value of type 'String' to expected element type 'Int'}}
8686
([1, 2, 1.0], 1) as ([String], Int)
8787
// expected-error@-1 2 {{cannot convert value of type 'Int' to expected element type 'String'}}
@@ -135,3 +135,182 @@ _ = sr6022 as! AnyObject // expected-warning {{forced cast from '() -> Any' to '
135135
_ = sr6022 as? AnyObject // expected-warning {{conditional cast from '() -> Any' to 'AnyObject' always succeeds}}
136136
_ = sr6022_1 as! Any // expected-warning {{forced cast from '() -> ()' to 'Any' always succeeds; did you mean to use 'as'?}}
137137
_ = sr6022_1 as? Any // expected-warning {{conditional cast from '() -> ()' to 'Any' always succeeds}}
138+
139+
// SR-11295
140+
let sr11295a = "Hello"
141+
_ = sr11295a as String // expected-warning {{redundant cast to 'String' has no effect}} {{14-24=}}
142+
143+
let sr11295b = 1
144+
_ = sr11295b as Int // expected-warning {{redundant cast to 'Int' has no effect}} {{14-21=}}
145+
146+
typealias Type = String
147+
148+
let sr11295c: Type = "Hello Typealias"
149+
_ = sr11295c as String // expected-warning {{redundant cast to 'String' has no effect}} {{14-24=}}
150+
151+
let sr11295d = "Hello Typealias"
152+
_ = sr11295d as Type // expected-warning {{redundant cast to 'Type' (aka 'String') has no effect}} {{14-22=}}
153+
154+
_ = "Hello" as String // Ok
155+
_ = 1 as Int64 // Ok
156+
_ = [] as Set<Int> // Ok
157+
158+
class SR11295A {}
159+
class SR11295B: SR11295A {}
160+
161+
var sr11295ap = SR11295A()
162+
var sr11295bc = SR11295B()
163+
164+
_ = sr11295bc as SR11295A // Ok
165+
166+
_ = 1 as Double as Double // expected-warning {{redundant cast to 'Double' has no effect}} {{17-27=}}
167+
_ = Double(1) as Double // expected-warning {{redundant cast to 'Double' has no effect}} {{15-25=}}
168+
_ = Int(1) as Int // expected-warning {{redundant cast to 'Int' has no effect}} {{12-19=}}
169+
170+
typealias Double1 = Double
171+
typealias Double2 = Double
172+
173+
let sr11295ta1: Double1 = 1.0
174+
_ = sr11295ta1 as Double2 // expected-warning {{redundant cast from 'Double1' (aka 'Double') to 'Double2' (aka 'Double') has no effect}} {{16-27=}}
175+
_ = sr11295ta1 as Double1 // expected-warning {{redundant cast to 'Double1' (aka 'Double') has no effect}} {{16-27=}}
176+
177+
func ff11295_0<T>(_ t: T) -> T { return t }
178+
func ff11295_1(_ i: Int) -> Int { return i }
179+
180+
// Function call expressions
181+
_ = ff11295_0(1 as Int) as Int // expected-warning {{redundant cast to 'Int' has no effect}} {{25-32=}}
182+
_ = ff11295_1(1) as Int // expected-warning {{redundant cast to 'Int' has no effect}} {{18-25=}}
183+
184+
func ff11295_3(i: Int) {
185+
let a: [Int] = []
186+
_ = a.count - ((i + 1) as Int) // Ok
187+
}
188+
189+
struct SR11295C {
190+
var i: Int
191+
}
192+
struct SR11295D<T> {
193+
var t: T
194+
195+
func f() -> T { return t }
196+
}
197+
enum SR11295_E: Int {
198+
case a
199+
}
200+
201+
// Coerce members
202+
_ = SR11295C(i: 1).i as Int // expected-warning {{redundant cast to 'Int' has no effect}} {{22-29=}}
203+
_ = SR11295D<Int>(t: 1).t as Int // expected-warning {{redundant cast to 'Int' has no effect}} {{27-34=}}
204+
_ = SR11295D(t: 1 as Int).t as Int // expected-warning {{redundant cast to 'Int' has no effect}} {{29-36=}}
205+
_ = SR11295D(t: 1).t as Int // Ok
206+
_ = SR11295D(t: 1).t as UInt // Ok
207+
_ = SR11295_E.a.rawValue as Int // expected-warning {{redundant cast to 'Int' has no effect}} {{26-33=}}
208+
_ = SR11295D(t: 1 as Int).f() as Int // expected-warning {{redundant cast to 'Int' has no effect}} {{31-38=}}
209+
_ = SR11295D(t: 1).f() as Int // Ok
210+
211+
// Overload decl expr
212+
func f11295_Overload(a: Int, b: Int) -> Int { }
213+
func f11295_Overload(c: Double, d: Double) -> Double { }
214+
215+
_ = (f11295_Overload as (Int, Int) -> Int)(0, 0)
216+
_ = (f11295_Overload as (Double, Double) -> Double)(0, 0)
217+
218+
_ = (1 - 2 / 3 * 6) as UInt
219+
_ = 1/4 as Float > 3/2 as Float // Ok
220+
_ = 1/4 as Int > 3/2 as Int // Ok
221+
222+
// Special cases where the coerced expression type is inferred by context.
223+
224+
var f11295: (Float) -> Float = { $0 as Float } // expected-warning {{redundant cast to 'Float' has no effect}} {{37-46=}}
225+
var f11295_1 = { $0 as Float } // Ok
226+
227+
func ff11295() -> (Int) -> Int {
228+
return { $0 as Int } // expected-warning {{redundant cast to 'Int' has no effect}} {{15-22=}}
229+
}
230+
231+
func f11295t_2<A: Hashable, R>(f: @escaping ((A) -> R, A) -> R) {}
232+
233+
f11295t_2 { (f, n) in
234+
n < 2 ? n : 0 // Ok
235+
}
236+
237+
f11295t_2 { (f, n) in
238+
n < 2 ? n as Int: 0 // Ok
239+
}
240+
241+
f11295t_2 { (_, n) in
242+
_ = n as Int // Ok
243+
}
244+
245+
f11295t_2 { (f, n: Int) in
246+
n < 2 ? n as Int : 0 // expected-warning {{redundant cast to 'Int' has no effect}} {{13-20=}}
247+
}
248+
249+
f11295t_2 { (_, n) in
250+
_ = SR11295D(t: n as Int).t as Int // expected-warning {{redundant cast to 'Int' has no effect}} {{31-38=}}
251+
}
252+
253+
f11295t_2 { (_, n) in
254+
_ = SR11295D(t: n).t as Int // Ok
255+
}
256+
257+
func ff11295_g<T>(_ v: T) {
258+
let _ = { v as T } // expected-warning {{redundant cast to 'T' has no effect}} {{15-20=}}
259+
}
260+
261+
func ff11295_g1(_ v: Int) {
262+
let _ = { v as Int } // expected-warning {{redundant cast to 'Int' has no effect}} {{15-22=}}
263+
}
264+
265+
func ff11295_g2<T>(_ v: T) {
266+
let _ = { $0 as T } // Ok
267+
}
268+
269+
func ff11295_g2<T>(_ v: T) -> (T) -> T {
270+
let _ : (T) -> T = { $0 as T } // expected-warning {{redundant cast to 'T' has no effect}} {{27-32=}}
271+
return { $0 as T } // expected-warning {{redundant cast to 'T' has no effect}} {{15-20=}}
272+
}
273+
274+
func ff11295_g3(_ v: Int) {
275+
let _ = { $0 as Int } // Ok
276+
}
277+
278+
func ff11295_g4<T>(_ v: T) {
279+
let c = v as T // expected-warning {{redundant cast to 'T' has no effect}} {{13-18=}}
280+
let _ = { c as T } // expected-warning {{redundant cast to 'T' has no effect}} {{15-20=}}
281+
}
282+
283+
func ff11295_g5<T>(_ v: T) {
284+
// Nested closures
285+
let _ = { { { $0 as T } } } // Ok
286+
let _ = { { { v as T } } } // expected-warning {{redundant cast to 'T' has no effect}} {{19-24=}}
287+
}
288+
289+
func ff11295_6(_ i: Int) -> Int { i }
290+
func ff11295_g6<T>(_ i: T) -> T { i }
291+
292+
let _ = { ff11295_g6($0 as Int) } // Ok
293+
294+
let _ = { ff11295_6($0) } // Ok
295+
296+
let _ = { ff11295_6($0 as Int) } // Ok
297+
298+
// A similar test case comming from validation-test/Sema/type_checker_perf/fast/rdar17077404.swift
299+
func f11295_3<A: Hashable, R>(
300+
f: @escaping ((A) -> R, A) -> R
301+
) -> ((A) -> R) {
302+
return { (a: A) -> R in
303+
let r: R!
304+
return r
305+
}
306+
}
307+
308+
_ = f11295_3 {
309+
(f, n) in
310+
f(n as Int) as Int // OK
311+
}
312+
313+
_ = f11295_3 {
314+
(f, n) in
315+
n < 2 ? n as Int : f(n - 1) + f(n - 2) // OK
316+
}

test/expr/postfix/dot/dot_keywords.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ClassWithDeinitFunc {
6868
let instanceWithDeinitFunc = ClassWithDeinitFunc()
6969
instanceWithDeinitFunc.deinit()
7070
_ = instanceWithDeinitFunc.deinit(a:)
71-
_ = instanceWithDeinitFunc.deinit as () -> Void
71+
_ = instanceWithDeinitFunc.deinit as () -> Void // expected-warning {{redundant cast to '() -> Void' has no effect}} {{35-49=}}
7272
SR3043Derived.deinit() // expected-error {{deinitializers cannot be accessed}}
7373

7474
class ClassWithDeinitMember {

test/expr/primary/selector/fixits.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
// RUN: %empty-directory(%t.overlays)
55

66
// FIXME: BEGIN -enable-source-import hackaround
7-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %clang-importer-sdk-path/swift-modules/ObjectiveC.swift -disable-objc-attr-requires-foundation-module
8-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %clang-importer-sdk-path/swift-modules/CoreGraphics.swift
9-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %clang-importer-sdk-path/swift-modules/Foundation.swift
7+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %clang-importer-sdk-path/swift-modules/ObjectiveC.swift -disable-objc-attr-requires-foundation-module -disable-redundant-coercion-warning
8+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %clang-importer-sdk-path/swift-modules/CoreGraphics.swift -disable-redundant-coercion-warning
9+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %clang-importer-sdk-path/swift-modules/Foundation.swift -disable-redundant-coercion-warning
1010
// FIXME: END -enable-source-import hackaround
1111

12-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %S/Inputs/fixits_helper.swift -module-name Helper
12+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t.overlays %S/Inputs/fixits_helper.swift -module-name Helper -disable-redundant-coercion-warning
1313

1414
// Make sure we get the right diagnostics.
15-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t.overlays) -typecheck %s -verify
15+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t.overlays) -disable-redundant-coercion-warning -typecheck %s -verify
1616

1717
// Copy the source, apply the Fix-Its, and compile it again, making
1818
// sure that we've cleaned up all of the deprecation warnings.
1919
// RUN: %empty-directory(%t.sources)
2020
// RUN: %empty-directory(%t.remapping)
2121
// RUN: cp %s %t.sources/fixits.swift
22-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t.overlays) -typecheck %t.sources/fixits.swift -fixit-all -emit-fixits-path %t.remapping/fixits.remap
22+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t.overlays) -disable-redundant-coercion-warning -typecheck %t.sources/fixits.swift -fixit-all -emit-fixits-path %t.remapping/fixits.remap
2323
// RUN: %{python} %utils/apply-fixit-edits.py %t.remapping
24-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t.overlays) -typecheck %t.sources/fixits.swift 2> %t.result
24+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t.overlays) -disable-redundant-coercion-warning -typecheck %t.sources/fixits.swift 2> %t.result
2525

2626
// RUN: %FileCheck %s < %t.result
2727
// RUN: grep -c "warning:" %t.result | grep 3
@@ -55,7 +55,6 @@ func testDeprecatedStringLiteralSelector() {
5555
// We don't need coercion here because we get the right selector
5656
// from the static method.
5757
_ = "staticOrNonStatic:" as Selector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-39=#selector(Bar.staticOrNonStatic(_:))}}
58-
5958
// We need coercion here because we asked for a selector from an
6059
// instance method with the same name as (but a different selector
6160
// from) a static method.

test/type/infer/local_variables.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ func infer_generic_args() {
3030

3131
// Function types
3232
let f : (Dictionary) -> Array = dict_to_array
33-
_ = f(d) as [(String, Int)]
33+
_ = f(d) as [(String, Int)] // expected-warning {{redundant cast to '[(String, Int)]' has no effect}} {{12-31=}}
3434
}
3535

test/type/subclass_composition.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func basicSubtyping(
126126
let _: Derived & AnyObject = derived
127127

128128
let _ = base as Base<Int> & P1
129-
let _ = base as Base<Int> & AnyObject
130-
let _ = derived as Derived & AnyObject
129+
let _ = base as Base<Int> & AnyObject // expected-warning {{redundant cast to 'Base<Int>' has no effect}} {{16-41=}}
130+
let _ = derived as Derived & AnyObject // expected-warning {{redundant cast to 'Derived' has no effect}} {{19-42=}}
131131

132132
let _ = base as? Base<Int> & P1 // expected-warning {{always succeeds}}
133133
let _ = base as? Base<Int> & AnyObject // expected-warning {{always succeeds}}
@@ -170,7 +170,7 @@ func basicSubtyping(
170170
let _ = baseAndP1 as Base<Int>
171171
let _ = derivedAndP3 as Base<Int>
172172
let _ = derivedAndP2 as Derived
173-
let _ = derivedAndAnyObject as Derived
173+
let _ = derivedAndAnyObject as Derived // expected-warning {{redundant cast to 'Derived' has no effect}} {{31-42=}}
174174

175175
let _ = baseAndP1 as? Base<Int> // expected-warning {{always succeeds}}
176176
let _ = derivedAndP3 as? Base<Int> // expected-warning {{always succeeds}}
@@ -343,8 +343,8 @@ func metatypeSubtyping(
343343

344344
let _ = baseIntAndP2 as Base<Int>.Type
345345
let _ = baseIntAndP2AndAnyObject as Base<Int>.Type
346-
let _ = derivedAndAnyObject as Derived.Type
347-
let _ = baseIntAndP2AndAnyObject as BaseAndP2<Int>.Type
346+
let _ = derivedAndAnyObject as Derived.Type // expected-warning {{redundant cast to 'Derived.Type' has no effect}} {{31-47=}}
347+
let _ = baseIntAndP2AndAnyObject as BaseAndP2<Int>.Type // expected-warning {{redundant cast to 'BaseAndP2<Int>.Type' (aka '(Base<Int> & P2).Type') has no effect}} {{36-59=}}
348348

349349
let _ = baseIntAndP2 as? Base<Int>.Type // expected-warning {{always succeeds}}
350350
let _ = baseIntAndP2AndAnyObject as? Base<Int>.Type // expected-warning {{always succeeds}}

0 commit comments

Comments
 (0)