Skip to content

Commit 032f4ac

Browse files
committed
[CSDiagnostics] Produce a diagnostic for patterns with extraneous elements
Extend `ExtraneousArgumentsFailure` to handle enum element pattern mismatches. Resolves: rdar://123466496
1 parent 03194eb commit 032f4ac

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
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+
}

0 commit comments

Comments
 (0)