Skip to content

Commit f7253f2

Browse files
authored
Merge pull request #42228 from xedin/enable-se-0347-by-default
[TypeChecker] SE-0347: Enable type inference from default expressions
2 parents cdfb181 + 5acdd11 commit f7253f2

File tree

9 files changed

+21
-18
lines changed

9 files changed

+21
-18
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
55

66
## Swift 5.7
77

8+
* [SE-0347][]:
9+
10+
It's now possible to use a default value expression with a generic parameter type
11+
to default the argument and its type:
12+
13+
```
14+
func compute<C: Collection>(_ values: C = [0, 1, 2]) {
15+
...
16+
}
17+
```
18+
19+
`compute` is now accepted by compiler and `[Int]` is going to be inferred
20+
for `C` at call sites that do not provide the argument explicitly.
21+
822
* [SE-0326][]:
923

1024
It's now possible to infer parameter and result types from the body of a multi-statement
@@ -9150,6 +9164,7 @@ Swift 1.0
91509164
[SE-0340]: <https://github.com/apple/swift-evolution/blob/main/proposals/0340-swift-noasync.md>
91519165
[SE-0345]: <https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md>
91529166
[SE-0326]: <https://github.com/apple/swift-evolution/blob/main/proposals/0326-extending-multi-statement-closure-inference.md>
9167+
[SE-0347]: <https://github.com/apple/swift-evolution/blob/main/proposals/0347-type-inference-from-default-exprs.md>
91539168

91549169
[SR-75]: <https://bugs.swift.org/browse/SR-75>
91559170
[SR-106]: <https://bugs.swift.org/browse/SR-106>

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,6 @@ namespace swift {
752752
/// closures.
753753
bool EnableMultiStatementClosureInference = true;
754754

755-
/// Enable experimental support for generic parameter inference in
756-
/// parameter positions from associated default expressions.
757-
bool EnableTypeInferenceFromDefaultArguments = false;
758-
759755
/// See \ref FrontendOptions.PrintFullConvention
760756
bool PrintFullConvention = false;
761757
};

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,9 +1107,6 @@ static bool ParseTypeCheckerArgs(TypeCheckerOptions &Opts, ArgList &Args,
11071107
Opts.EnableMultiStatementClosureInference |=
11081108
Args.hasArg(OPT_experimental_multi_statement_closures);
11091109

1110-
Opts.EnableTypeInferenceFromDefaultArguments |=
1111-
Args.hasArg(OPT_experimental_type_inference_from_defaults);
1112-
11131110
Opts.PrintFullConvention |=
11141111
Args.hasArg(OPT_experimental_print_full_convention);
11151112

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,8 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
17801780
if (parameterBindings[paramIdx].empty() && callee) {
17811781
auto &ctx = cs.getASTContext();
17821782

1783-
if (ctx.TypeCheckerOpts.EnableTypeInferenceFromDefaultArguments) {
1783+
// Type inference from default value expressions.
1784+
{
17841785
auto *paramList = getParameterList(callee);
17851786
if (!paramList)
17861787
continue;

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,6 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
464464
return defaultValue->getType();
465465
}
466466

467-
// If inference is disabled, fail.
468-
if (!ctx.TypeCheckerOpts.EnableTypeInferenceFromDefaultArguments)
469-
return Type();
470-
471467
// Caller-side defaults are always type-checked based on the concrete
472468
// type of the argument deduced at a particular call site.
473469
if (isa<MagicIdentifierLiteralExpr>(defaultValue))

test/Constraints/type_inference_from_default_exprs.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/InferViaDefaults.swiftmodule -enable-experimental-type-inference-from-defaults -module-name InferViaDefaults %S/Inputs/type_inference_via_defaults_other_module.swift
3-
// RUN: %target-swift-frontend -enable-experimental-type-inference-from-defaults -module-name main -typecheck -verify -I %t %s %S/Inputs/type_inference_via_defaults_other_module.swift
2+
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/InferViaDefaults.swiftmodule -module-name InferViaDefaults %S/Inputs/type_inference_via_defaults_other_module.swift
3+
// RUN: %target-swift-frontend -module-name main -typecheck -verify -I %t %s %S/Inputs/type_inference_via_defaults_other_module.swift
44

55
func testInferFromResult<T>(_: T = 42) -> T { fatalError() } // Ok
66

test/Constraints/type_inference_from_default_exprs_executable_test.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/InferViaDefaults.swiftmodule -enable-experimental-type-inference-from-defaults -module-name InferViaDefaults %S/Inputs/type_inference_via_defaults_other_module.swift
3-
// RUN: %target-build-swift -module-name main -Xfrontend -enable-experimental-type-inference-from-defaults -parse-as-library -I %t %s %S/Inputs/type_inference_via_defaults_other_module.swift -o %t/a.out
2+
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/InferViaDefaults.swiftmodule -module-name InferViaDefaults %S/Inputs/type_inference_via_defaults_other_module.swift
3+
// RUN: %target-build-swift -module-name main -Xfrontend -parse-as-library -I %t %s %S/Inputs/type_inference_via_defaults_other_module.swift -o %t/a.out
44
// RUN: %target-run %t/a.out | %FileCheck %s --color
55

66
// REQUIRES: OS=macosx && CPU=x86_64

test/decl/func/vararg.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %target-typecheck-verify-swift
2-
// RUN: %target-typecheck-verify-swift -enable-experimental-type-inference-from-defaults
32

43
var t1a: (Int...) = (1)
54
// expected-error@-1 {{cannot create a variadic tuple}}

test/expr/closure/basic.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %target-typecheck-verify-swift
2-
// RUN: %target-typecheck-verify-swift -enable-experimental-type-inference-from-defaults
32

43
func takeIntToInt(_ f: (Int) -> Int) { }
54
func takeIntIntToInt(_ f: (Int, Int) -> Int) { }

0 commit comments

Comments
 (0)