Skip to content

[ConstraintSystem] Teach getFunctionArgApplyInfo about inout expr… #36459

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
Mar 17, 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: 7 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3449,6 +3449,13 @@ static bool repairOutOfOrderArgumentsInBinaryFunction(

bool isOperatorRef = overload->choice.getDecl()->isOperator();

// If one of the parameters is `inout`, we can't flip the arguments.
{
auto params = fnType->getParams();
if (params[0].isInOut() != params[1].isInOut())
return false;
}

auto matchArgToParam = [&](Type argType, Type paramType, ASTNode anchor) {
auto *loc = cs.getConstraintLocator(anchor);
// If argument (and/or parameter) is a generic type let's not even try this
Expand Down
54 changes: 53 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4542,6 +4542,59 @@ Type Solution::resolveInterfaceType(Type type) const {

Optional<FunctionArgApplyInfo>
Solution::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
auto &cs = getConstraintSystem();

// It's only valid to use `&` in argument positions, but we need
// to figure out exactly where it was used.
if (auto *argExpr = getAsExpr<InOutExpr>(locator->getAnchor())) {
auto *argList = cs.getParentExpr(argExpr);
assert(argList);

// `inout` expression might be wrapped in a number of
// parens e.g. `test(((&x)))`.
if (isa<ParenExpr>(argList)) {
for (;;) {
auto nextParent = cs.getParentExpr(argList);
assert(nextParent && "Incorrect use of `inout` expression");

// e.g. `test((&x), x: ...)`
if (isa<TupleExpr>(nextParent)) {
argList = nextParent;
break;
}

// e.g. `test(((&x)))`
if (isa<ParenExpr>(nextParent)) {
argList = nextParent;
continue;
}

break;
}
}

unsigned argIdx = 0;
if (auto *tuple = dyn_cast<TupleExpr>(argList)) {
auto arguments = tuple->getElements();

for (auto idx : indices(arguments)) {
if (arguments[idx]->getSemanticsProvidingExpr() == argExpr) {
argIdx = idx;
break;
}
}
}

auto *call = cs.getParentExpr(argList);
assert(call);

ParameterTypeFlags flags;
locator = cs.getConstraintLocator(
call, {ConstraintLocator::ApplyArgument,
LocatorPathElt::ApplyArgToParam(argIdx, argIdx,
flags.withInOut(true))});
}

auto anchor = locator->getAnchor();
auto path = locator->getPath();

Expand Down Expand Up @@ -4635,7 +4688,6 @@ Solution::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
auto argIdx = applyArgElt->getArgIdx();
auto paramIdx = applyArgElt->getParamIdx();

auto &cs = getConstraintSystem();
return FunctionArgApplyInfo(cs.getParentExpr(argExpr), argExpr, argIdx,
simplifyType(getType(argExpr)), paramIdx,
fnInterfaceType, fnType, callee);
Expand Down
24 changes: 24 additions & 0 deletions test/Constraints/optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,27 @@ func sr_12309() {
return (((nil))) // Ok
}
}

// rdar://75146811 - crash due to incrrect inout type
func rdar75146811() {
func test(_: UnsafeMutablePointer<Double>) {}
func test_tuple(_: UnsafeMutablePointer<Double>, x: Int) {}
func test_named(x: UnsafeMutablePointer<Double>) {}

var arr: [Double]! = []

test(&arr) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
test((&arr)) // expected-error {{use of extraneous '&'}}
// expected-error@-1 {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
test(&(arr)) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}

test_tuple(&arr, x: 0) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
test_tuple((&arr), x: 0) // expected-error {{use of extraneous '&'}}
// expected-error@-1 {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
test_tuple(&(arr), x: 0) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}

test_named(x: &arr) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
test_named(x: (&arr)) // expected-error {{use of extraneous '&'}}
// expected-error@-1 {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
test_named(x: &(arr)) // expected-error {{cannot convert value of type '[Double]?' to expected argument type 'Double'}}
}