Skip to content

Commit 502691f

Browse files
committed
[Sema][Diagnostics] Generalize undesirable type warning to include arrays of empty tuples
https://bugs.swift.org/browse/SR-11511
1 parent 4a222a0 commit 502691f

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

lib/Sema/TypeCheckPattern.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -978,12 +978,10 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
978978
TypeChecker::checkForForbiddenPrefix(Context, var->getBaseName());
979979

980980
// If we are inferring a variable to have type AnyObject.Type,
981-
// "()", an uninhabited type, or optional thereof, emit a diagnostic.
982-
// In the first 2 cases, the coder probably forgot a cast and expected a
983-
// concrete type. In the later case, they probably didn't mean to bind to
984-
// a variable, or there is some other bug. We always tell them that they
985-
// can silence the warning with an explicit type annotation
986-
// (and provide a fixit) as a note.
981+
// "()", "[()]", an uninhabited type, or optional thereof, emit a diagnostic.
982+
// They are probably missing a cast or didn't mean to bind to a variable.
983+
// We always tell them that they can silence the warning with an
984+
// explicit type annotation (and provide a fixit) as a note.
987985
Type diagTy = type->lookThroughAllOptionalTypes();
988986
bool isOptional = !type->getOptionalObjectType().isNull();
989987
if (!diagTy) diagTy = type;
@@ -1010,6 +1008,9 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
10101008
assert((diagTy->is<EnumType>() || diagTy->is<BoundGenericEnumType>()) &&
10111009
"unknown structurally uninhabited type");
10121010
}
1011+
} else if (auto *BST = diagTy->getAs<BoundGenericStructType>()) {
1012+
if (BST->getDecl() == Context.getArrayDecl())
1013+
shouldRequireType = BST->getGenericArgs()[0]->isEqual(Context.TheEmptyTupleType);
10131014
}
10141015

10151016
if (shouldRequireType &&
@@ -1019,7 +1020,7 @@ bool TypeChecker::coercePatternToType(Pattern *&P, TypeResolution resolution,
10191020
diags.diagnose(NP->getLoc(), diag, NP->getDecl()->getName(), type,
10201021
NP->getDecl()->isLet());
10211022
diags.diagnose(NP->getLoc(), diag::add_explicit_type_annotation_to_silence)
1022-
.fixItInsertAfter(var->getNameLoc(), ": " + type->getString());
1023+
.fixItInsertAfter(var->getNameLoc(), ": " + type->getWithoutParens()->getString());
10231024
}
10241025

10251026
return false;

test/SourceKit/Sema/placeholders.swift.placeholders.response

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,29 @@
2727
key.length: 9
2828
}
2929
]
30+
},
31+
{
32+
key.line: 8,
33+
key.column: 5,
34+
key.filepath: placeholders.swift,
35+
key.severity: source.diagnostic.severity.warning,
36+
key.description: "constant 'myArray' inferred to have type '[()]', which may be unexpected",
37+
key.diagnostic_stage: source.diagnostic.stage.swift.sema,
38+
key.diagnostics: [
39+
{
40+
key.line: 8,
41+
key.column: 5,
42+
key.filepath: placeholders.swift,
43+
key.severity: source.diagnostic.severity.note,
44+
key.description: "add an explicit type annotation to silence this warning",
45+
key.fixits: [
46+
{
47+
key.offset: 236,
48+
key.length: 0,
49+
key.sourcetext: ": [()]"
50+
}
51+
]
52+
}
53+
]
3054
}
3155
]

test/decl/var/variables.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ func testAnyObjectOptional() -> AnyObject? {
6060
return x
6161
}
6262

63+
// SR-11511 Warning for inferring an array of empty tuples
64+
var arrayOfEmptyTuples = [""].map { print($0) } // expected-warning {{variable 'arrayOfEmptyTuples' inferred to have type '[()]'}} \
65+
// expected-note {{add an explicit type annotation to silence this warning}} {{23-23=: [()]}}
66+
67+
var maybeEmpty = Optional(arrayOfEmptyTuples) // expected-warning {{variable 'maybeEmpty' inferred to have type '[()]?'}} \
68+
// expected-note {{add an explicit type annotation to silence this warning}} {{15-15=: [()]?}}
69+
70+
var shouldWarnWithoutSugar = (arrayOfEmptyTuples as Array<()>) // expected-warning {{variable 'shouldWarnWithoutSugar' inferred to have type 'Array<()>'}} \
71+
// expected-note {{add an explicit type annotation to silence this warning}} {{27-27=: Array<()>}}
72+
6373
class SomeClass {}
6474

6575
// <rdar://problem/16877304> weak let's should be rejected

0 commit comments

Comments
 (0)