mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Merge pull request #42228 from xedin/enable-se-0347-by-default
[TypeChecker] SE-0347: Enable type inference from default expressions
This commit is contained in:
15
CHANGELOG.md
15
CHANGELOG.md
@@ -5,6 +5,20 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
|
|||||||
|
|
||||||
## Swift 5.7
|
## Swift 5.7
|
||||||
|
|
||||||
|
* [SE-0347][]:
|
||||||
|
|
||||||
|
It's now possible to use a default value expression with a generic parameter type
|
||||||
|
to default the argument and its type:
|
||||||
|
|
||||||
|
```
|
||||||
|
func compute<C: Collection>(_ values: C = [0, 1, 2]) {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`compute` is now accepted by compiler and `[Int]` is going to be inferred
|
||||||
|
for `C` at call sites that do not provide the argument explicitly.
|
||||||
|
|
||||||
* [SE-0326][]:
|
* [SE-0326][]:
|
||||||
|
|
||||||
It's now possible to infer parameter and result types from the body of a multi-statement
|
It's now possible to infer parameter and result types from the body of a multi-statement
|
||||||
@@ -9150,6 +9164,7 @@ Swift 1.0
|
|||||||
[SE-0340]: <https://github.com/apple/swift-evolution/blob/main/proposals/0340-swift-noasync.md>
|
[SE-0340]: <https://github.com/apple/swift-evolution/blob/main/proposals/0340-swift-noasync.md>
|
||||||
[SE-0345]: <https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md>
|
[SE-0345]: <https://github.com/apple/swift-evolution/blob/main/proposals/0345-if-let-shorthand.md>
|
||||||
[SE-0326]: <https://github.com/apple/swift-evolution/blob/main/proposals/0326-extending-multi-statement-closure-inference.md>
|
[SE-0326]: <https://github.com/apple/swift-evolution/blob/main/proposals/0326-extending-multi-statement-closure-inference.md>
|
||||||
|
[SE-0347]: <https://github.com/apple/swift-evolution/blob/main/proposals/0347-type-inference-from-default-exprs.md>
|
||||||
|
|
||||||
[SR-75]: <https://bugs.swift.org/browse/SR-75>
|
[SR-75]: <https://bugs.swift.org/browse/SR-75>
|
||||||
[SR-106]: <https://bugs.swift.org/browse/SR-106>
|
[SR-106]: <https://bugs.swift.org/browse/SR-106>
|
||||||
|
|||||||
@@ -752,10 +752,6 @@ namespace swift {
|
|||||||
/// closures.
|
/// closures.
|
||||||
bool EnableMultiStatementClosureInference = true;
|
bool EnableMultiStatementClosureInference = true;
|
||||||
|
|
||||||
/// Enable experimental support for generic parameter inference in
|
|
||||||
/// parameter positions from associated default expressions.
|
|
||||||
bool EnableTypeInferenceFromDefaultArguments = false;
|
|
||||||
|
|
||||||
/// See \ref FrontendOptions.PrintFullConvention
|
/// See \ref FrontendOptions.PrintFullConvention
|
||||||
bool PrintFullConvention = false;
|
bool PrintFullConvention = false;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1107,9 +1107,6 @@ static bool ParseTypeCheckerArgs(TypeCheckerOptions &Opts, ArgList &Args,
|
|||||||
Opts.EnableMultiStatementClosureInference |=
|
Opts.EnableMultiStatementClosureInference |=
|
||||||
Args.hasArg(OPT_experimental_multi_statement_closures);
|
Args.hasArg(OPT_experimental_multi_statement_closures);
|
||||||
|
|
||||||
Opts.EnableTypeInferenceFromDefaultArguments |=
|
|
||||||
Args.hasArg(OPT_experimental_type_inference_from_defaults);
|
|
||||||
|
|
||||||
Opts.PrintFullConvention |=
|
Opts.PrintFullConvention |=
|
||||||
Args.hasArg(OPT_experimental_print_full_convention);
|
Args.hasArg(OPT_experimental_print_full_convention);
|
||||||
|
|
||||||
|
|||||||
@@ -1780,7 +1780,8 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
|
|||||||
if (parameterBindings[paramIdx].empty() && callee) {
|
if (parameterBindings[paramIdx].empty() && callee) {
|
||||||
auto &ctx = cs.getASTContext();
|
auto &ctx = cs.getASTContext();
|
||||||
|
|
||||||
if (ctx.TypeCheckerOpts.EnableTypeInferenceFromDefaultArguments) {
|
// Type inference from default value expressions.
|
||||||
|
{
|
||||||
auto *paramList = getParameterList(callee);
|
auto *paramList = getParameterList(callee);
|
||||||
if (!paramList)
|
if (!paramList)
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -464,10 +464,6 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
|
|||||||
return defaultValue->getType();
|
return defaultValue->getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If inference is disabled, fail.
|
|
||||||
if (!ctx.TypeCheckerOpts.EnableTypeInferenceFromDefaultArguments)
|
|
||||||
return Type();
|
|
||||||
|
|
||||||
// Caller-side defaults are always type-checked based on the concrete
|
// Caller-side defaults are always type-checked based on the concrete
|
||||||
// type of the argument deduced at a particular call site.
|
// type of the argument deduced at a particular call site.
|
||||||
if (isa<MagicIdentifierLiteralExpr>(defaultValue))
|
if (isa<MagicIdentifierLiteralExpr>(defaultValue))
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// RUN: %empty-directory(%t)
|
// RUN: %empty-directory(%t)
|
||||||
// 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
|
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/InferViaDefaults.swiftmodule -module-name InferViaDefaults %S/Inputs/type_inference_via_defaults_other_module.swift
|
||||||
// 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
|
// RUN: %target-swift-frontend -module-name main -typecheck -verify -I %t %s %S/Inputs/type_inference_via_defaults_other_module.swift
|
||||||
|
|
||||||
func testInferFromResult<T>(_: T = 42) -> T { fatalError() } // Ok
|
func testInferFromResult<T>(_: T = 42) -> T { fatalError() } // Ok
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// RUN: %empty-directory(%t)
|
// RUN: %empty-directory(%t)
|
||||||
// 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
|
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/InferViaDefaults.swiftmodule -module-name InferViaDefaults %S/Inputs/type_inference_via_defaults_other_module.swift
|
||||||
// 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
|
// 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
|
||||||
// RUN: %target-run %t/a.out | %FileCheck %s --color
|
// RUN: %target-run %t/a.out | %FileCheck %s --color
|
||||||
|
|
||||||
// REQUIRES: OS=macosx && CPU=x86_64
|
// REQUIRES: OS=macosx && CPU=x86_64
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// RUN: %target-typecheck-verify-swift
|
// RUN: %target-typecheck-verify-swift
|
||||||
// RUN: %target-typecheck-verify-swift -enable-experimental-type-inference-from-defaults
|
|
||||||
|
|
||||||
var t1a: (Int...) = (1)
|
var t1a: (Int...) = (1)
|
||||||
// expected-error@-1 {{cannot create a variadic tuple}}
|
// expected-error@-1 {{cannot create a variadic tuple}}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// RUN: %target-typecheck-verify-swift
|
// RUN: %target-typecheck-verify-swift
|
||||||
// RUN: %target-typecheck-verify-swift -enable-experimental-type-inference-from-defaults
|
|
||||||
|
|
||||||
func takeIntToInt(_ f: (Int) -> Int) { }
|
func takeIntToInt(_ f: (Int) -> Int) { }
|
||||||
func takeIntIntToInt(_ f: (Int, Int) -> Int) { }
|
func takeIntIntToInt(_ f: (Int, Int) -> Int) { }
|
||||||
|
|||||||
Reference in New Issue
Block a user