Skip to content

Commit bc1d2a4

Browse files
authored
Merge pull request #71892 from xedin/rdar-123466496
[CSDiagnostics] Produce a diagnostic for patterns with extraneous ele…
2 parents f13904b + 10c794e commit bc1d2a4

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5984,6 +5984,18 @@ bool ExtraneousArgumentsFailure::diagnoseAsError() {
59845984
return true;
59855985
}
59865986

5987+
if (auto *pattern = getAsPattern<EnumElementPattern>(anchor)) {
5988+
if (isa<TuplePattern>(pattern->getSubPattern())) {
5989+
auto paramTuple = FunctionType::composeTuple(
5990+
getASTContext(), ContextualType->getParams(),
5991+
ParameterFlagHandling::IgnoreNonEmpty);
5992+
5993+
emitDiagnostic(diag::tuple_pattern_length_mismatch,
5994+
paramTuple);
5995+
return true;
5996+
}
5997+
}
5998+
59875999
if (isContextualMismatch()) {
59886000
auto *locator = getLocator();
59896001
emitDiagnostic(locator->isLastElement<LocatorPathElt::ContextualType>()

test/Constraints/patterns.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,20 @@ func issue66750(_ x: Result<String, Error>) {
774774
break
775775
}
776776
}
777+
778+
// rdar://123466496 - `type of expression is ambiguous without a type annotation` with extra elements
779+
do {
780+
enum E {
781+
case test(a: Int, b: String)
782+
}
783+
784+
func test(_: (E) -> Void) {
785+
}
786+
787+
test {
788+
switch $0 {
789+
case .test(a: 42, b: "", c: 0.0): break
790+
// expected-error@-1 {{tuple pattern has the wrong length for tuple type '(Int, String)'}}
791+
}
792+
}
793+
}

test/Constraints/rdar107724970.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ enum E {
55
case e(Int)
66
}
77
func foo(_ x: E) {
8-
// FIXME: We need to handle pattern arguments in a bunch of places in argument
9-
// list diagnostic logic.
108
// https://github.com/apple/swift/issues/65062
11-
let fn = { // expected-error {{unable to infer closure type without a type annotation}}
9+
let fn = {
1210
switch x {
1311
case E.e(_, _):
12+
// expected-error@-1 {{tuple pattern has the wrong length for tuple type 'Int'}}
1413
break
1514
}
1615
}

0 commit comments

Comments
 (0)