Skip to content

[Type checker] Handle 'rethrows' checks for single-parameter functions. #8980

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 24, 2017
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
16 changes: 12 additions & 4 deletions lib/Sema/TypeCheckError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ class ApplyClassifier {
// But if the expression didn't type-check, suppress diagnostics.
if (!E->getType() || E->getType()->hasError())
return Classification::forInvalidCode();

auto type = E->getFn()->getType();
if (!type) return Classification::forInvalidCode();
auto fnType = type->getAs<AnyFunctionType>();
Expand Down Expand Up @@ -673,11 +674,18 @@ class ApplyClassifier {
return classifyShuffleRethrowsArgument(shuffle, paramTupleType);
}

// Otherwise, we're passing an opaque tuple expression, and we
// should treat it as contributing to 'rethrows' if the original
// parameter type included a throwing function type.
return classifyArgumentByType(paramType,
int scalarElt = paramTupleType->getElementForScalarInit();
if (scalarElt < 0 ||
!paramTupleType->getElementType(scalarElt)->isEqual(arg->getType())) {
// Otherwise, we're passing an opaque tuple expression, and we
// should treat it as contributing to 'rethrows' if the original
// parameter type included a throwing function type.
return classifyArgumentByType(
paramType,
PotentialReason::forRethrowsArgument(arg));
}

paramType = paramTupleType->getElementType(scalarElt);
}

// Otherwise, if the original parameter type was not a throwing
Expand Down
7 changes: 7 additions & 0 deletions test/decl/func/rethrows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,10 @@ func throwWhileGettingFoo() throws -> Foo.Type { return Foo.self }

(throwWhileGettingFoo()).foo(Foo())() // expected-error {{can throw}}
(try throwWhileGettingFoo()).foo(Foo())()

// <rdar://problem/31794932> [Source compatibility] Call to sort(by):) can throw, but is not marked with 'try'
func doRethrow(fn: (Int, Int) throws -> Int) rethrows { }

func testDoRethrow() {
doRethrow(fn:) { (a, b) in return a }
}
39 changes: 22 additions & 17 deletions unittests/runtime/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,16 +443,19 @@ TEST(MetadataTest, getExistentialMetadata) {
});

// protocol compositions are order-invariant
const ProtocolDescriptor *protoList4[] = {
&ProtocolA,
&ProtocolB
};
const ProtocolDescriptor *protoList5[] = {
&ProtocolB,
&ProtocolA
};
RaceTest_ExpectEqual<const ExistentialTypeMetadata *>(
[&]() -> const ExistentialTypeMetadata * {

const ProtocolDescriptor *protoList4[] = {
&ProtocolA,
&ProtocolB
};

const ProtocolDescriptor *protoList5[] = {
&ProtocolB,
&ProtocolA
};

auto ab = swift_getExistentialTypeMetadata(ProtocolClassConstraint::Any,
/*superclass=*/nullptr,
2, protoList4);
Expand Down Expand Up @@ -514,13 +517,14 @@ TEST(MetadataTest, getExistentialMetadata) {
return noWitnessTable;
});

const ProtocolDescriptor *protoList8[] = {
&ProtocolNoWitnessTable,
&ProtocolA,
&ProtocolB
};
RaceTest_ExpectEqual<const ExistentialTypeMetadata *>(
[&]() -> const ExistentialTypeMetadata * {
const ProtocolDescriptor *protoList8[] = {
&ProtocolNoWitnessTable,
&ProtocolA,
&ProtocolB
};

auto mixedWitnessTable
= swift_getExistentialTypeMetadata(ProtocolClassConstraint::Class,
/*superclass=*/nullptr,
Expand Down Expand Up @@ -560,12 +564,13 @@ TEST(MetadataTest, getExistentialMetadata) {
return special;
});

const ProtocolDescriptor *protoList10[] = {
&ProtocolError,
&ProtocolA
};
RaceTest_ExpectEqual<const ExistentialTypeMetadata *>(
[&]() -> const ExistentialTypeMetadata * {
const ProtocolDescriptor *protoList10[] = {
&ProtocolError,
&ProtocolA
};

auto special
= swift_getExistentialTypeMetadata(ProtocolClassConstraint::Any,
/*superclass=*/nullptr,
Expand Down