Skip to content

[SE-0328] Enable structural opaque result types. #40361

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
Dec 2, 2021
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
7 changes: 4 additions & 3 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1874,9 +1874,6 @@ WARNING(spi_attribute_on_import_of_public_module,none,
(DeclName, StringRef))

// Opaque return types
ERROR(structural_opaque_types_are_experimental,none,
"'opaque' types cannot be nested inside other types; "
"structural 'opaque' types are an experimental feature", ())
ERROR(opaque_type_invalid_constraint,none,
"an 'opaque' type must specify only 'Any', 'AnyObject', protocols, "
"and/or a base class", ())
Expand Down Expand Up @@ -4868,6 +4865,10 @@ ERROR(opaque_type_unsupported_pattern,none,
ERROR(opaque_type_in_protocol_requirement,none,
"'some' type cannot be the return type of a protocol requirement; did you mean to add an associated type?",
())
ERROR(opaque_type_in_parameter,none,
"'some' cannot appear in parameter position in result "
"type %0",
(Type))

// Function differentiability
ERROR(attr_only_on_parameters_of_differentiable,none,
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 @@ -310,10 +310,6 @@ namespace swift {
/// `func f() -> <T> T`.
bool EnableExperimentalNamedOpaqueTypes = false;

/// Enable experimental support for structural opaque result types, e.g.
/// `func f() -> (some P)?`.
bool EnableExperimentalStructuralOpaqueTypes = false;

/// Enable experimental flow-sensitive concurrent captures.
bool EnableExperimentalFlowSensitiveConcurrentCaptures = 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 @@ -438,9 +438,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.EnableExperimentalNamedOpaqueTypes |=
Args.hasArg(OPT_enable_experimental_named_opaque_types);

Opts.EnableExperimentalStructuralOpaqueTypes |=
Args.hasArg(OPT_enable_experimental_structural_opaque_types);

Opts.EnableExperimentalDistributed |=
Args.hasArg(OPT_enable_experimental_distributed);

Expand Down
30 changes: 22 additions & 8 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
auto *dc = originatingDecl->getInnermostDeclContext();
auto &ctx = dc->getASTContext();

// Support for structural opaque result types is hidden behind a compiler flag
// until the proposal gets approved.
if (!ctx.LangOpts.EnableExperimentalStructuralOpaqueTypes &&
!isa<OpaqueReturnTypeRepr>(repr)) {
ctx.Diags.diagnose(repr->getLoc(), diag::structural_opaque_types_are_experimental);
return nullptr;
}

// Protocol requirements can't have opaque return types.
//
// TODO: Maybe one day we could treat this as sugar for an associated type.
Expand Down Expand Up @@ -285,6 +277,28 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
/*unboundTyOpener*/ nullptr,
/*placeholderHandler*/ nullptr)
.resolveType(repr);

// Opaque types cannot be used in parameter position.
Type desugared = interfaceType->getDesugaredType();
bool hasError = desugared.findIf([&](Type type) -> bool {
if (auto *fnType = type->getAs<FunctionType>()) {
for (auto param : fnType->getParams()) {
if (!param.getPlainType()->hasOpaqueArchetype())
continue;

ctx.Diags.diagnose(repr->getLoc(),
diag::opaque_type_in_parameter,
interfaceType);
return true;
}
}

return false;
});

if (hasError)
return nullptr;

auto metatype = MetatypeType::get(interfaceType);
opaqueDecl->setInterfaceType(metatype);
return opaqueDecl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift -enable-experimental-structural-opaque-types -disable-availability-checking
// RUN: %target-typecheck-verify-swift -disable-availability-checking

@resultBuilder
struct TupleBuilder {
Expand Down
6 changes: 2 additions & 4 deletions test/type/opaque.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ struct Test {
let zingle = {() -> some P in 1 } // expected-error{{'some' types are only implemented}}


// Support for structural opaque result types is hidden behind a compiler flag
// until the proposal gets approved.
func twoOpaqueTypes() -> (some P, some P) { return (1, 2) } // expected-error{{'opaque' types cannot be nested inside other types}}
func asArrayElem() -> (some P)! { return [1] } // expected-error{{'opaque' types cannot be nested inside other types}}
func twoOpaqueTypes() -> (some P, some P) { return (1, 2) } // expected-error{{'(some P, some P)' contains multiple 'opaque' types, but only one 'opaque' type is supported}}
func asArrayElem() -> [some P] { return [1] }

// Invalid positions

Expand Down
13 changes: 12 additions & 1 deletion test/type/opaque_return_structural.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift -enable-experimental-structural-opaque-types -disable-availability-checking
// RUN: %target-typecheck-verify-swift -disable-availability-checking

// Tests for experimental extensions to opaque return type support.

Expand Down Expand Up @@ -85,3 +85,14 @@ func structuralMemberLookupBad() {
tup.0.paul();
tup.0.d(); // expected-error{{value of type 'some P' has no member 'd'}}
}

// expected-error@+1 {{'some' cannot appear in parameter position in result type '(some P) -> Void'}}
func opaqueParameter() -> (some P) -> Void {}

// expected-error@+1 {{'some' cannot appear in parameter position in result type '((some P) -> Void) -> Void'}}
func opaqueParameter() -> ((some P) -> Void) -> Void {}

typealias Takes<T> = (T) -> Void

// expected-error@+1 {{'some' cannot appear in parameter position in result type 'Takes<some P>' (aka '(some P) -> ()')}}
func indirectOpaqueParameter() -> Takes<some P> {}