Skip to content

[ConstraintGraph] Disallow subtype constraint contractions #11855

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 1 commit into from
Sep 12, 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
22 changes: 1 addition & 21 deletions lib/Sema/ConstraintGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,13 +641,6 @@ static bool shouldContractEdge(ConstraintKind kind) {
case ConstraintKind::BindParam:
case ConstraintKind::BindToPointerType:
case ConstraintKind::Equal:

// We currently only allow subtype contractions for the purpose of
// parameter binding constraints.
// TODO: We do this because of how inout parameter bindings are handled
// for implicit closure parameters. We should consider adjusting our
// current approach to unlock more opportunities for subtype contractions.
case ConstraintKind::Subtype:
return true;

default:
Expand All @@ -671,12 +664,7 @@ static bool isStrictInoutSubtypeConstraint(Constraint *constraint) {
t1 = tt->getElementType(0).getPointer();
}

auto iot = t1->getAs<InOutType>();

if (!iot)
return false;

return !iot->getObjectType()->isTypeVariableOrMember();
return t1->is<InOutType>();
}

bool ConstraintGraph::contractEdges() {
Expand All @@ -698,14 +686,6 @@ bool ConstraintGraph::contractEdges() {
auto t1 = constraint->getFirstType()->getDesugaredType();
auto t2 = constraint->getSecondType()->getDesugaredType();

if (kind == ConstraintKind::Subtype) {
if (auto iot1 = t1->getAs<InOutType>()) {
t1 = iot1->getObjectType().getPointer();
} else {
continue;
}
}

auto tyvar1 = t1->getAs<TypeVariableType>();
auto tyvar2 = t2->getAs<TypeVariableType>();

Expand Down
42 changes: 42 additions & 0 deletions validation-test/Sema/rdar34333874.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// RUN: %target-run-simple-swift
// REQUIRES: executable_test

class C {
var a: Int = 0
var b: Int = 0
}

@inline(never)
func foo<T>(_ item: T, update: (inout T) throws -> Void) rethrows -> T {
var this = item
try update(&this)
return this
}

// Test single statement closure because it's type-checked
// together with the call to `foo`

let rdar34333874_1 = foo(C()) {
$0.a = 42
}

// The multi-statement closure which is type-checked
// separately from call to `foo`

let rdar34333874_2 = foo(C()) {
$0.a = 42
$0.b = 0
}

print(rdar34333874_1)
print(rdar34333874_2)

// Example which avoids mutating fields of the class

@inline(never)
func bar(_ o : C) {
let _ = foo(o) { (item) in
}
}

bar(C())