Skip to content

[Sema] Downgrade multi-param default inference diag for methods #81887

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
Jun 1, 2025
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
11 changes: 9 additions & 2 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,19 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
affectedParams, [&](const unsigned index) { params << "#" << index; },
[&] { params << ", "; });

ctx.Diags.diagnose(
auto diag = ctx.Diags.diagnose(
defaultValue->getLoc(),
diag::
cannot_default_generic_parameter_inferrable_from_another_parameter,
paramInterfaceTy, params.str());
return Type();

// In Swift 6.2 and below we incorrectly missed checking this rule for
// methods, downgrade to a warning until the next language mode.
auto futureVersion = version::Version::getFutureMajorLanguageVersion();
if (!anchor->hasCurriedSelf() || ctx.isSwiftVersionAtLeast(futureVersion))
return Type();

diag.warnUntilFutureSwiftVersion();
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/type_inference_from_default_exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func testInferenceFromClosureVarInvalid<T>(x: T = { let x = "" as Int; return x
// https://github.com/swiftlang/swift/issues/72199
enum S72199_1 {
func testS72199_1<T>(_: T = 42, _: [T]) {}
// expected-error@-1 {{cannot use default expression for inference of 'T' because it is inferrable from parameters #0, #1}}
// expected-warning@-1 {{cannot use default expression for inference of 'T' because it is inferrable from parameters #0, #1; this will be an error in a future Swift language mode}}
}

func testS72199_2<T: P>(x: T.X, y: T = S()) { } // Ok
Expand Down
11 changes: 11 additions & 0 deletions test/Constraints/type_inference_from_default_exprs_swift7.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-typecheck-verify-swift -swift-version 7
// REQUIRES: swift7

// https://github.com/swiftlang/swift/issues/72199
enum S72199_1 {
func testS72199_1<T>(_: T = 42, _: [T]) {}
// expected-error@-1 {{cannot use default expression for inference of 'T' because it is inferrable from parameters #0, #1}}
}

func testS72199<T>(_: T = 42, _: [T]) {}
// expected-error@-1 {{cannot use default expression for inference of 'T' because it is inferrable from parameters #0, #1}}