diff --git a/lib/Sema/MiscDiagnostics.cpp b/lib/Sema/MiscDiagnostics.cpp index dc1b49968f399..955f9e68953c2 100644 --- a/lib/Sema/MiscDiagnostics.cpp +++ b/lib/Sema/MiscDiagnostics.cpp @@ -110,15 +110,13 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC, SmallPtrSet AlreadyDiagnosedBitCasts; bool IsExprStmt; - bool HasReachedSemanticsProvidingExpr; ASTContext &Ctx; const DeclContext *DC; public: DiagnoseWalker(const DeclContext *DC, bool isExprStmt) - : IsExprStmt(isExprStmt), HasReachedSemanticsProvidingExpr(false), - Ctx(DC->getASTContext()), DC(DC) {} + : IsExprStmt(isExprStmt), Ctx(DC->getASTContext()), DC(DC) {} PreWalkResult walkToPatternPre(Pattern *P) override { return Action::SkipChildren(P); @@ -133,16 +131,6 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC, bool shouldWalkIntoTapExpression() override { return false; } PreWalkResult walkToExprPre(Expr *E) override { - if (auto collection = dyn_cast(E)) { - if (collection->isTypeDefaulted()) { - // Diagnose type defaulted collection literals in subexpressions as - // warnings to preserve source compatibility. - diagnoseTypeDefaultedCollectionExpr( - collection, Ctx, - /*downgradeToWarning=*/HasReachedSemanticsProvidingExpr); - } - } - // See through implicit conversions of the expression. We want to be able // to associate the parent of this expression with the ultimate callee. auto Base = E; @@ -345,11 +333,6 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC, checkMoveExpr(moveExpr); } - if (!HasReachedSemanticsProvidingExpr && - E == E->getSemanticsProvidingExpr()) { - HasReachedSemanticsProvidingExpr = true; - } - return Action::Continue(E); } @@ -472,34 +455,25 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC, return false; } - /// Diagnose a collection literal with a defaulted type such as \c [Any]. - static void diagnoseTypeDefaultedCollectionExpr(CollectionExpr *c, - ASTContext &ctx, - bool downgradeToWarning) { - // Produce a diagnostic with a fixit to add the defaulted type as an - // explicit annotation. - auto &diags = ctx.Diags; - - if (c->getNumElements() == 0) { - InFlightDiagnostic inFlight = - diags.diagnose(c->getLoc(), diag::collection_literal_empty); - inFlight.highlight(c->getSourceRange()); - - if (downgradeToWarning) { - inFlight.limitBehavior(DiagnosticBehavior::Warning); - } - } else { + /// We have a collection literal with a defaulted type, e.g. of [Any]. Emit + /// an error if it was inferred to this type in an invalid context, which is + /// one in which the parent expression is not itself a collection literal. + void checkTypeDefaultedCollectionExpr(CollectionExpr *c) { + // If the parent is a non-expression, or is not itself a literal, then + // produce an error with a fixit to add the type as an explicit + // annotation. + if (c->getNumElements() == 0) + Ctx.Diags.diagnose(c->getLoc(), diag::collection_literal_empty) + .highlight(c->getSourceRange()); + else { assert(c->getType()->hasTypeRepr() && "a defaulted type should always be printable"); - InFlightDiagnostic inFlight = diags.diagnose( - c->getLoc(), diag::collection_literal_heterogeneous, c->getType()); - inFlight.highlight(c->getSourceRange()); - inFlight.fixItInsertAfter(c->getEndLoc(), - " as " + c->getType()->getString()); - - if (downgradeToWarning) { - inFlight.limitBehavior(DiagnosticBehavior::Warning); - } + Ctx.Diags + .diagnose(c->getLoc(), diag::collection_literal_heterogeneous, + c->getType()) + .highlight(c->getSourceRange()) + .fixItInsertAfter(c->getEndLoc(), + " as " + c->getType()->getString()); } } @@ -1374,6 +1348,16 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC, DiagnoseWalker Walker(DC, isExprStmt); const_cast(E)->walk(Walker); + + // Diagnose uses of collection literals with defaulted types at the top + // level. + if (auto collection = + dyn_cast(E->getSemanticsProvidingExpr())) { + if (collection->isTypeDefaulted()) { + Walker.checkTypeDefaultedCollectionExpr( + const_cast(collection)); + } + } } diff --git a/test/Constraints/array_literal.swift b/test/Constraints/array_literal.swift index f06e77678a50e..842a4cea1f5f5 100644 --- a/test/Constraints/array_literal.swift +++ b/test/Constraints/array_literal.swift @@ -126,6 +126,11 @@ func defaultToAny(i: Int, s: String) { // expected-error@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} let _: Int = a1 // expected-error{{value of type '[Any]'}} + let _ = ([1, "a"]) + // expected-error@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} + let _ = [1, true, []] + // expected-error@-1:11 {{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} + let a2: Array = [1, "a", 3.5] // expected-error@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} let _: Int = a2 // expected-error{{value of type '[Any]'}} @@ -146,9 +151,7 @@ func defaultToAny(i: Int, s: String) { let _: [Any] = [1, "a", 3.5] let _: [Any] = [1, "a", [3.5, 3.7, 3.9]] let _: [Any] = [1, "a", [3.5, "b", 3]] - // expected-warning@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} let _: [Any] = [1, [2, [3]]] - // expected-warning@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} func f1() -> [Any] { [] @@ -157,41 +160,25 @@ func defaultToAny(i: Int, s: String) { let _: [Any?] = [1, "a", nil, 3.5] let _: [Any?] = [1, "a", nil, [3.5, 3.7, 3.9]] let _: [Any?] = [1, "a", nil, [3.5, "b", nil]] - // expected-warning@-1{{heterogeneous collection literal could only be inferred to '[Any?]'; add explicit type annotation if this is intentional}} let _: [Any?] = [1, [2, [3]]] - // expected-warning@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} let _: [Any?] = [1, nil, [2, nil, [3]]] - // expected-warning@-1{{heterogeneous collection literal could only be inferred to '[Any?]'; add explicit type annotation if this is intentional}} let a6 = [B(), C()] let _: Int = a6 // expected-error{{value of type '[A]'}} let a7: some Collection = [1, "Swift"] - // expected-warning@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} {{41-41= as [Any]}} let _: (any Sequence)? = [1, "Swift"] - // expected-warning@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} let _: any Sequence = [1, nil, "Swift"] - // expected-warning@-1{{heterogeneous collection literal could only be inferred to '[Any?]'; add explicit type annotation if this is intentional}} - let _ = [1, true, ([], 1)] - // expected-error@-1 {{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} - // expected-warning@-2 {{empty collection literal requires an explicit type}} let _ = true ? [] : [] - // expected-warning@-1{{empty collection literal requires an explicit type}} let _ = (true, ([1, "Swift"])) - //expected-warning@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} - let _ = ([1, true]) - //expected-error@-1{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} func f2(_: [T]) {} func f3() -> [T]? {} f2([]) - // expected-warning@-1{{empty collection literal requires an explicit type}} f2([1, nil, ""]) - // expected-warning@-1{{heterogeneous collection literal could only be inferred to '[Any?]'; add explicit type annotation if this is intentional}} _ = f3() ?? [] - // expected-warning@-1{{empty collection literal requires an explicit type}} } func noInferAny(iob: inout B, ioc: inout C) { diff --git a/test/Constraints/casts.swift b/test/Constraints/casts.swift index 1f0709955f4cf..3727987a127b4 100644 --- a/test/Constraints/casts.swift +++ b/test/Constraints/casts.swift @@ -338,7 +338,6 @@ func test_compatibility_coercions(_ arr: [Int], _ optArr: [Int]?, _ dict: [Strin // The array can also be inferred to be [Any]. _ = ([] ?? []) as Array // expected-warning {{left side of nil coalescing operator '??' has non-optional type '[Any]', so the right side is never used}} - // expected-warning@-1{{empty collection literal requires an explicit type}} // rdar://88334481 – Don't apply the compatibility logic for collection literals. typealias Magic = T diff --git a/test/Constraints/casts_swift6.swift b/test/Constraints/casts_swift6.swift index 000484e984943..496eeb90b7059 100644 --- a/test/Constraints/casts_swift6.swift +++ b/test/Constraints/casts_swift6.swift @@ -57,7 +57,6 @@ func test_compatibility_coercions(_ arr: [Int], _ optArr: [Int]?, _ dict: [Strin // The array can also be inferred to be [Any]. _ = ([] ?? []) as Array // expected-warning {{left side of nil coalescing operator '??' has non-optional type '[Any]', so the right side is never used}} - // expected-warning@-1 {{empty collection literal requires an explicit type}} // Cases from rdar://88334481 typealias Magic = T diff --git a/test/Constraints/construction.swift b/test/Constraints/construction.swift index 053fba8b7f7da..463a0de41fedb 100644 --- a/test/Constraints/construction.swift +++ b/test/Constraints/construction.swift @@ -175,7 +175,6 @@ do { // rdar://problem/34670592 - Compiler crash on heterogeneous collection literal _ = Array([1, "hello"]) // Ok -// expected-warning@-1 {{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} func init_via_non_const_metatype(_ s1: S1.Type) { _ = s1(i: 42) // expected-error {{initializing from a metatype value must reference 'init' explicitly}} {{9-9=.init}} diff --git a/test/Constraints/dictionary_literal.swift b/test/Constraints/dictionary_literal.swift index da68bf59fa57a..3eeb3c972d349 100644 --- a/test/Constraints/dictionary_literal.swift +++ b/test/Constraints/dictionary_literal.swift @@ -120,6 +120,8 @@ class B : A { } class C : A { } func testDefaultExistentials() { + let _ = ["a": ["b": ["c": ["d", 1, true]]]] + let _ = ["a" : 1, "b" : 2.5, "c" : "hello"] // expected-error@-1{{heterogeneous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional}}{{46-46= as [String : Any]}} @@ -139,7 +141,6 @@ func testDefaultExistentials() { "b": ["a", 2, 3.14159], "c": ["a": 2, "b": 3.5]] // expected-error@-3{{heterogeneous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional}} - // expected-warning@-3{{heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional}} let d3 = ["b" : B(), "c" : C()] let _: Int = d3 // expected-error{{value of type '[String : A]'}} diff --git a/test/Constraints/subscript.swift b/test/Constraints/subscript.swift index 31cd0f06013c9..82a330e71feef 100644 --- a/test/Constraints/subscript.swift +++ b/test/Constraints/subscript.swift @@ -104,7 +104,6 @@ extension Int { let _ = 1["1"] // expected-error {{ambiguous use of 'subscript(_:)'}} let squares = [ 1, 2, 3 ].reduce([:]) { (dict, n) in - // expected-warning@-1 {{empty collection literal requires an explicit type}} var dict = dict dict[n] = n * n return dict diff --git a/test/IDE/print_usrs_opaque_types.swift b/test/IDE/print_usrs_opaque_types.swift index f6bc21c72759b..5723746352632 100644 --- a/test/IDE/print_usrs_opaque_types.swift +++ b/test/IDE/print_usrs_opaque_types.swift @@ -9,20 +9,17 @@ func testUnifyingGenericParams(x: T) -> some Collection where T == U { // expected-warning@-1 {{same-type requirement makes generic parameters 'U' and 'T' equivalent}} return [] - // expected-warning@-1 {{empty collection literal requires an explicit type}} } // CHECK: [[@LINE+1]]:{{[0-9]+}} s:14swift_ide_test0C22UnifyingGenericParams21xQrx_tSlRz7ElementQzRs_r0_lF func testUnifyingGenericParams2(x: T) -> some Collection where T: Collection, U == T.Element { return [] - // expected-warning@-1 {{empty collection literal requires an explicit type}} } // CHECK: [[@LINE+1]]:{{[0-9]+}} s:14swift_ide_test0C24ConcretizingGenericParam1xQrSi_tSiRszlF func testConcretizingGenericParam(x: T) -> some Collection where T == Int { // expected-warning@-1 {{same-type requirement makes generic parameter 'T' non-generic}} return [] - // expected-warning@-1 {{empty collection literal requires an explicit type}} } struct GenericContext { @@ -30,13 +27,11 @@ struct GenericContext { func testUnifyingGenericParams(x: T) -> some Collection where T == U { // expected-warning@-1 {{same-type requirement makes generic parameters 'U' and 'T' equivalent}} return [] - // expected-warning@-1 {{empty collection literal requires an explicit type}} } // CHECK: [[@LINE+1]]:{{[0-9]+}} s:14swift_ide_test14GenericContextV0c8UnifyingD7Params21xQrx_tSlRz7ElementQzRsd__lF func testUnifyingGenericParams2(x: T) -> some Collection where T: Collection, U == T.Element { return [] - // expected-warning@-1 {{empty collection literal requires an explicit type}} } // CHECK: [[@LINE+1]]:{{[0-9]+}} s:14swift_ide_test14GenericContextVyQrxcqd__Rszluip @@ -45,7 +40,6 @@ struct GenericContext { // CHECK: [[@LINE+1]]:{{[0-9]+}} s:14swift_ide_test14GenericContextVyQrxcqd__Rszluig get { return [] - // expected-warning@-1 {{empty collection literal requires an explicit type}} } } } @@ -54,7 +48,6 @@ extension GenericContext where T == Int { // CHECK: [[@LINE+1]]:{{[0-9]+}} s:14swift_ide_test14GenericContextVAASiRszlE0c12ConcretizingD5Param1xQrSi_tF func testConcretizingGenericParam(x: T) -> some Collection { return [] - // expected-warning@-1 {{empty collection literal requires an explicit type}} } } @@ -65,7 +58,6 @@ extension TooGenericTooContext where T == U { // CHECK: [[@LINE+1]]:{{[0-9]+}} s:14swift_ide_test010TooGenericD7ContextVAAq_RszrlE0c8UnifyingE6Params1xQrx_tF func testUnifyingGenericParams(x: T) -> some Collection { return [] - // expected-warning@-1 {{empty collection literal requires an explicit type}} } } @@ -73,14 +65,12 @@ extension TooGenericTooContext where T: Collection, U == T.Element { // CHECK: [[@LINE+1]]:{{[0-9]+}} s:14swift_ide_test010TooGenericD7ContextVAASlRz7ElementQzRs_rlE0c8UnifyingE7Params21xQrx_tF func testUnifyingGenericParams2(x: T) -> some Collection { return [] - // expected-warning@-1 {{empty collection literal requires an explicit type}} } } extension TooGenericTooContext where T == Int { // CHECK: [[@LINE+1]]:{{[0-9]+}} s:14swift_ide_test010TooGenericD7ContextVAASiRszrlE0c12ConcretizingE5Param1xQrSi_tF func testConcretizingGenericParam(x: T) -> some Collection { return [] - // expected-warning@-1 {{empty collection literal requires an explicit type}} } } diff --git a/test/IRGen/virtual-function-elimination-two-modules.swift b/test/IRGen/virtual-function-elimination-two-modules.swift index 2e6657e6c0924..74f6b53ad2926 100644 --- a/test/IRGen/virtual-function-elimination-two-modules.swift +++ b/test/IRGen/virtual-function-elimination-two-modules.swift @@ -20,7 +20,7 @@ // (4) Now produce the .dylib with just the symbols needed by the client // RUN: %target-build-swift -parse-as-library -Xfrontend -enable-llvm-vfe -Xfrontend -internalize-at-link \ // RUN: %s -DLIBRARY -lto=llvm-full %lto_flags -module-name Library \ -// RUN: -emit-library -o %t/libLibrary.dylib \ +// RUN: -emit-library -o %t/libLibrary.dylib -runtime-compatibility-version none \ // RUN: -Xlinker -exported_symbols_list -Xlinker %t/used-symbols -Xlinker -dead_strip // (5) Check list of symbols in library diff --git a/test/IRGen/witness-method-elimination-two-modules.swift b/test/IRGen/witness-method-elimination-two-modules.swift index 680eaaeb54356..e056e2a4333da 100644 --- a/test/IRGen/witness-method-elimination-two-modules.swift +++ b/test/IRGen/witness-method-elimination-two-modules.swift @@ -21,7 +21,7 @@ // (4) Now produce the .dylib with just the symbols needed by the client // RUN: %target-build-swift -parse-as-library -Onone -Xfrontend -enable-llvm-wme -Xfrontend -internalize-at-link \ // RUN: %s -DLIBRARY -lto=llvm-full %lto_flags -module-name Library \ -// RUN: -emit-library -o %t/libLibrary.dylib \ +// RUN: -emit-library -o %t/libLibrary.dylib -runtime-compatibility-version none \ // RUN: -Xlinker -exported_symbols_list -Xlinker %t/used-symbols -Xlinker -dead_strip // (5) Check list of symbols in library diff --git a/test/Parse/matching_patterns.swift b/test/Parse/matching_patterns.swift index 4773c0d86aa62..a6cbc04cfef0f 100644 --- a/test/Parse/matching_patterns.swift +++ b/test/Parse/matching_patterns.swift @@ -307,8 +307,6 @@ do { while case let _ as [Derived] = arr {} // expected-warning@-1 {{'let' pattern has no effect; sub-pattern didn't bind any variables}} - // FIXME: https://github.com/apple/swift/issues/61850 - // expected-warning@+1 {{heterogeneous collection literal could only be inferred to '[[Base]]'; add explicit type annotation if this is intentional}} for case _ as [Derived] in [arr] {} if case is [Derived] = arr {} diff --git a/test/expr/cast/objc_coerce_array.swift b/test/expr/cast/objc_coerce_array.swift index b34d6ca08e742..22f3c5eb0aadb 100644 --- a/test/expr/cast/objc_coerce_array.swift +++ b/test/expr/cast/objc_coerce_array.swift @@ -7,13 +7,9 @@ var x = 1 _ = [x] as [NSNumber] _ = ["x":["y":"z","a":1]] as [String : [String : AnyObject]] -// expected-warning@-1{{heterogeneous collection literal could only be inferred to '[String : AnyObject]'; add explicit type annotation if this is intentiona}} _ = ["x":["z",1]] as [String : [AnyObject]] -// expected-warning@-1{{heterogeneous collection literal could only be inferred to '[AnyObject]'; add explicit type annotation if this is intentional}} _ = [["y":"z","a":1]] as [[String : AnyObject]] -// expected-warning@-1{{heterogeneous collection literal could only be inferred to '[String : AnyObject]'; add explicit type annotation if this is intentional}} _ = [["z",1]] as [[AnyObject]] -// expected-warning@-1{{heterogeneous collection literal could only be inferred to '[AnyObject]'; add explicit type annotation if this is intentional}} var y: Any = 1