Skip to content

[TypeChecker] SE-0347: Enable type inference from default expressions #42228

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 2 commits into from
Apr 8, 2022
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ _**Note:** This is in reverse chronological order, so newer entries are added to

## 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][]:

It's now possible to infer parameter and result types from the body of a multi-statement
Expand Down Expand Up @@ -9150,6 +9164,7 @@ Swift 1.0
[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-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-106]: <https://bugs.swift.org/browse/SR-106>
Expand Down
4 changes: 0 additions & 4 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,6 @@ namespace swift {
/// closures.
bool EnableMultiStatementClosureInference = true;

/// Enable experimental support for generic parameter inference in
/// parameter positions from associated default expressions.
bool EnableTypeInferenceFromDefaultArguments = false;

/// See \ref FrontendOptions.PrintFullConvention
bool PrintFullConvention = false;
};
Expand Down
3 changes: 0 additions & 3 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,9 +1104,6 @@ static bool ParseTypeCheckerArgs(TypeCheckerOptions &Opts, ArgList &Args,
Opts.EnableMultiStatementClosureInference |=
Args.hasArg(OPT_experimental_multi_statement_closures);

Opts.EnableTypeInferenceFromDefaultArguments |=
Args.hasArg(OPT_experimental_type_inference_from_defaults);

Opts.PrintFullConvention |=
Args.hasArg(OPT_experimental_print_full_convention);

Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,8 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
if (parameterBindings[paramIdx].empty() && callee) {
auto &ctx = cs.getASTContext();

if (ctx.TypeCheckerOpts.EnableTypeInferenceFromDefaultArguments) {
// Type inference from default value expressions.
{
auto *paramList = getParameterList(callee);
if (!paramList)
continue;
Expand Down
4 changes: 0 additions & 4 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,6 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
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
// type of the argument deduced at a particular call site.
if (isa<MagicIdentifierLiteralExpr>(defaultValue))
Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/type_inference_from_default_exprs.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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 -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-emit-module -emit-module-path %t/InferViaDefaults.swiftmodule -module-name InferViaDefaults %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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 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-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-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 -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

// REQUIRES: OS=macosx && CPU=x86_64
Expand Down
1 change: 0 additions & 1 deletion test/decl/func/vararg.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -enable-experimental-type-inference-from-defaults

var t1a: (Int...) = (1)
// expected-error@-1 {{cannot create a variadic tuple}}
Expand Down
1 change: 0 additions & 1 deletion test/expr/closure/basic.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -enable-experimental-type-inference-from-defaults

func takeIntToInt(_ f: (Int) -> Int) { }
func takeIntIntToInt(_ f: (Int, Int) -> Int) { }
Expand Down