Skip to content

Commit 6dc3f4a

Browse files
committed
Fixup the destructuring diagnostics
Use the isInvalid() bit on the TypeRepr to signal that a closure parameter is potentially a tuple destructure. This has two benefits 1) Parse is no longer using the isInvalid() bit on Decl 2) Invalidating the type repr itself means that we no longer spuriously diagnose variable patterns in destructures as missing types.
1 parent 5beb6f0 commit 6dc3f4a

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

Diff for: lib/Parse/ParseExpr.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -2656,11 +2656,10 @@ parseClosureSignatureIfPresent(SmallVectorImpl<CaptureListEntry> &captureList,
26562656
// to detect any tuple splat or destructuring as early as
26572657
// possible and give a proper fix-it. See SE-0110 for more details.
26582658
auto isTupleDestructuring = [](ParamDecl *param) -> bool {
2659-
if (!param->isInvalid())
2659+
auto *typeRepr = param->getTypeRepr();
2660+
if (!(typeRepr && typeRepr->isInvalid()))
26602661
return false;
2661-
if (auto *typeRepr = param->getTypeRepr())
2662-
return !param->hasName() && isa<TupleTypeRepr>(typeRepr);
2663-
return false;
2662+
return !param->hasName() && isa<TupleTypeRepr>(typeRepr);
26642663
};
26652664

26662665
for (unsigned i = 0, e = params->size(); i != e; ++i) {

Diff for: lib/Parse/ParsePattern.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,9 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
361361
// on is most likely argument destructuring, we are going
362362
// to diagnose that after all of the parameters are parsed.
363363
if (param.Type) {
364-
// Mark current parameter as invalid so it is possible
364+
// Mark current parameter type as invalid so it is possible
365365
// to diagnose it as destructuring of the closure parameter list.
366+
param.Type->setInvalid();
366367
param.isInvalid = true;
367368
if (!isClosure) {
368369
// Unnamed parameters must be written as "_: Type".
@@ -521,7 +522,7 @@ mapParsedParameters(Parser &parser,
521522
// or typealias with underlying function type.
522523
param->setAutoClosure(attrs.has(TypeAttrKind::TAK_autoclosure));
523524
}
524-
} if (paramInfo.SpecifierLoc.isValid()) {
525+
} else if (paramInfo.SpecifierLoc.isValid()) {
525526
StringRef specifier;
526527
switch (paramInfo.SpecifierKind) {
527528
case ParamDecl::Specifier::InOut:
@@ -698,7 +699,7 @@ Parser::parseSingleParameterClause(ParameterContextKind paramContext,
698699
// Parse the parameter clause.
699700
status |= parseParameterClause(leftParenLoc, params, rightParenLoc,
700701
defaultArgs, paramContext);
701-
702+
702703
// Turn the parameter clause into argument and body patterns.
703704
auto paramList = mapParsedParameters(*this, leftParenLoc, params,
704705
rightParenLoc, namePieces, paramContext);

Diff for: test/Constraints/tuple_arguments.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -1468,8 +1468,6 @@ let _ = sr4745.enumerated().map { (count, element) in "\(count): \(element)" }
14681468
// SR-4738
14691469

14701470
let sr4738 = (1, (2, 3))
1471-
// expected-error@+2{{use of undeclared type 'y'}}
1472-
// expected-error@+1{{use of undeclared type 'z'}}
14731471
[sr4738].map { (x, (y, z)) -> Int in x + y + z }
14741472
// expected-error@-1 {{closure tuple parameter does not support destructuring}} {{20-26=arg1}} {{38-38=let (y, z) = arg1; }}
14751473

@@ -1478,10 +1476,8 @@ let r31892961_1 = [1: 1, 2: 2]
14781476
r31892961_1.forEach { (k, v) in print(k + v) }
14791477

14801478
let r31892961_2 = [1, 2, 3]
1481-
// expected-error@+1{{use of undeclared type 'val'}}
14821479
let _: [Int] = r31892961_2.enumerated().map { ((index, val)) in
14831480
// expected-error@-1 {{closure tuple parameter does not support destructuring}} {{48-60=arg0}} {{3-3=\n let (index, val) = arg0\n }}
1484-
// expected-error@-2 {{use of undeclared type 'index'}}
14851481
val + 1
14861482
}
14871483

@@ -1690,6 +1686,7 @@ class Mappable<T> {
16901686

16911687
let x = Mappable(())
16921688
_ = x.map { (_: Void) in return () }
1689+
_ = x.map { (_: ()) in () }
16931690

16941691
// https://bugs.swift.org/browse/SR-9470
16951692
do {
@@ -1756,3 +1753,4 @@ func noescapeSplat() {
17561753
takesEscaping(t.0)
17571754
}
17581755
}
1756+

0 commit comments

Comments
 (0)