Skip to content

[CSDiagnostics] Produce a diagnostic for patterns with extraneous ele… #71892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5984,6 +5984,18 @@ bool ExtraneousArgumentsFailure::diagnoseAsError() {
return true;
}

if (auto *pattern = getAsPattern<EnumElementPattern>(anchor)) {
if (isa<TuplePattern>(pattern->getSubPattern())) {
auto paramTuple = FunctionType::composeTuple(
getASTContext(), ContextualType->getParams(),
ParameterFlagHandling::IgnoreNonEmpty);

emitDiagnostic(diag::tuple_pattern_length_mismatch,
paramTuple);
return true;
}
}

if (isContextualMismatch()) {
auto *locator = getLocator();
emitDiagnostic(locator->isLastElement<LocatorPathElt::ContextualType>()
Expand Down
17 changes: 17 additions & 0 deletions test/Constraints/patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -774,3 +774,20 @@ func issue66750(_ x: Result<String, Error>) {
break
}
}

// rdar://123466496 - `type of expression is ambiguous without a type annotation` with extra elements
do {
enum E {
case test(a: Int, b: String)
}

func test(_: (E) -> Void) {
}

test {
switch $0 {
case .test(a: 42, b: "", c: 0.0): break
// expected-error@-1 {{tuple pattern has the wrong length for tuple type '(Int, String)'}}
}
}
}
5 changes: 2 additions & 3 deletions test/Constraints/rdar107724970.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ enum E {
case e(Int)
}
func foo(_ x: E) {
// FIXME: We need to handle pattern arguments in a bunch of places in argument
// list diagnostic logic.
// https://github.com/apple/swift/issues/65062
let fn = { // expected-error {{unable to infer closure type without a type annotation}}
let fn = {
switch x {
case E.e(_, _):
// expected-error@-1 {{tuple pattern has the wrong length for tuple type 'Int'}}
break
}
}
Expand Down