Skip to content

[ConstraintSystem] Detect and fix extraneous use of & #24522

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
May 6, 2019
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
6 changes: 6 additions & 0 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2674,3 +2674,9 @@ bool InvalidMethodRefInKeyPath::diagnoseAsError() {
getName(), isForKeyPathDynamicMemberLookup());
return true;
}

bool InvalidUseOfAddressOf::diagnoseAsError() {
auto *anchor = cast<AssignExpr>(getAnchor());
emitDiagnostic(anchor->getSrc()->getLoc(), diag::extraneous_address_of);
return true;
}
20 changes: 20 additions & 0 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,26 @@ class InvalidMethodRefInKeyPath final : public InvalidMemberRefInKeyPath {
bool diagnoseAsError() override;
};

/// Diagnose extraneous use of address of (`&`) which could only be
/// associated with arguments to inout parameters e.g.
///
/// ```swift
/// struct S {}
///
/// var a: S = ...
/// var b: S = ...
///
/// a = &b
/// ```
class InvalidUseOfAddressOf final : public FailureDiagnostic {
public:
InvalidUseOfAddressOf(Expr *root, ConstraintSystem &cs,
ConstraintLocator *locator)
: FailureDiagnostic(root, cs, locator) {}

bool diagnoseAsError() override;
};

} // end namespace constraints
} // end namespace swift

Expand Down
10 changes: 10 additions & 0 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,13 @@ KeyPathContextualMismatch::create(ConstraintSystem &cs, Type lhs, Type rhs,
return new (cs.getAllocator())
KeyPathContextualMismatch(cs, lhs, rhs, locator);
}

bool RemoveAddressOf::diagnose(Expr *root, bool asNote) const {
InvalidUseOfAddressOf failure(root, getConstraintSystem(), getLocator());
return failure.diagnose(asNote);
}

RemoveAddressOf *RemoveAddressOf::create(ConstraintSystem &cs,
ConstraintLocator *locator) {
return new (cs.getAllocator()) RemoveAddressOf(cs, locator);
}
17 changes: 17 additions & 0 deletions lib/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ enum class FixKind : uint8_t {

/// Introduce a '&' to take the address of an lvalue.
AddressOf,
/// Remove extraneous use of `&`.
RemoveAddressOf,

/// Replace a coercion ('as') with a forced checked cast ('as!').
CoerceToCheckedCast,
Expand Down Expand Up @@ -853,6 +855,21 @@ class AllowInvalidRefInKeyPath final : public ConstraintFix {
ConstraintLocator *locator);
};

class RemoveAddressOf final : public ConstraintFix {
RemoveAddressOf(ConstraintSystem &cs, ConstraintLocator *locator)
: ConstraintFix(cs, FixKind::RemoveAddressOf, locator) {}

public:
std::string getName() const override {
return "remove extraneous use of `&`";
}

bool diagnose(Expr *root, bool asNote = false) const override;

static RemoveAddressOf *create(ConstraintSystem &cs,
ConstraintLocator *locator);
};

} // end namespace constraints
} // end namespace swift

Expand Down
9 changes: 8 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@ bool ConstraintSystem::repairFailures(
return true;
}

if (isa<AssignExpr>(anchor)) {
if (auto *AE = dyn_cast<AssignExpr>(anchor)) {
if (auto *fnType = lhs->getAs<FunctionType>()) {
// If left-hand side is a function type but right-hand
// side isn't, let's check it would be possible to fix
Expand All @@ -2051,6 +2051,12 @@ bool ConstraintSystem::repairFailures(
return true;
}
}

if (isa<InOutExpr>(AE->getSrc())) {
conversionsOrFixes.push_back(
RemoveAddressOf::create(*this, getConstraintLocator(locator)));
return true;
}
}

return false;
Expand Down Expand Up @@ -6508,6 +6514,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
}

case FixKind::InsertCall:
case FixKind::RemoveAddressOf:
case FixKind::SkipSameTypeRequirement:
case FixKind::SkipSuperclassRequirement:
case FixKind::ContextualMismatch:
Expand Down