Skip to content

Commit d3d6405

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-rebranch
2 parents f696ebd + f88a016 commit d3d6405

File tree

8 files changed

+79
-81
lines changed

8 files changed

+79
-81
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,19 +2199,6 @@ typeCheckArgumentChildIndependently(Expr *argExpr, Type argType,
21992199
if (!exprResult)
22002200
return nullptr;
22012201

2202-
// If the caller expected something inout, but we didn't have
2203-
// something of inout type, diagnose it.
2204-
if (auto IOE =
2205-
dyn_cast<InOutExpr>(exprResult->getSemanticsProvidingExpr())) {
2206-
if (!param.isInOut()) {
2207-
diagnose(exprResult->getLoc(), diag::extra_address_of,
2208-
CS.getType(exprResult)->getInOutObjectType())
2209-
.highlight(exprResult->getSourceRange())
2210-
.fixItRemove(IOE->getStartLoc());
2211-
return nullptr;
2212-
}
2213-
}
2214-
22152202
auto resultTy = CS.getType(exprResult);
22162203
resultElts[inArgNo] = exprResult;
22172204
resultEltTys[inArgNo] = {resultTy->getInOutObjectType(),
@@ -4592,13 +4579,6 @@ bool FailureDiagnosis::visitInOutExpr(InOutExpr *IOE) {
45924579
}
45934580
} else if (contextualType->is<InOutType>()) {
45944581
contextualType = contextualType->getInOutObjectType();
4595-
} else {
4596-
// If the caller expected something inout, but we didn't have
4597-
// something of inout type, diagnose it.
4598-
diagnose(IOE->getLoc(), diag::extra_address_of, contextualType)
4599-
.highlight(IOE->getSourceRange())
4600-
.fixItRemove(IOE->getStartLoc());
4601-
return true;
46024582
}
46034583
}
46044584

@@ -6015,19 +5995,6 @@ bool FailureDiagnosis::visitTupleExpr(TupleExpr *TE) {
60155995
CS.getContextualTypePurpose(), options);
60165996
// If there was an error type checking this argument, then we're done.
60175997
if (!exprResult) return true;
6018-
6019-
// If the caller expected something inout, but we didn't have
6020-
// something of inout type, diagnose it.
6021-
if (auto IOE =
6022-
dyn_cast<InOutExpr>(exprResult->getSemanticsProvidingExpr())) {
6023-
if (!contextualTT->getElement(i).isInOut()) {
6024-
diagnose(exprResult->getLoc(), diag::extra_address_of,
6025-
CS.getType(exprResult)->getInOutObjectType())
6026-
.highlight(exprResult->getSourceRange())
6027-
.fixItRemove(IOE->getStartLoc());
6028-
return true;
6029-
}
6030-
}
60315998
}
60325999

60336000
return false;

lib/Sema/CSDiagnostics.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3606,6 +3606,16 @@ SourceLoc InvalidUseOfAddressOf::getLoc() const {
36063606
}
36073607

36083608
bool InvalidUseOfAddressOf::diagnoseAsError() {
3609+
if (auto argApplyInfo = getFunctionArgApplyInfo(getLocator())) {
3610+
if (!argApplyInfo->getParameterFlags().isInOut()) {
3611+
auto anchor = getAnchor();
3612+
emitDiagnostic(anchor->getLoc(), diag::extra_address_of, getToType())
3613+
.highlight(anchor->getSourceRange())
3614+
.fixItRemove(anchor->getStartLoc());
3615+
return true;
3616+
}
3617+
}
3618+
36093619
emitDiagnostic(getLoc(), diag::extraneous_address_of);
36103620
return true;
36113621
}

lib/Sema/CSDiagnostics.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,30 @@ class MissingAddressOfFailure final : public ContextualFailure {
843843
bool diagnoseAsError() override;
844844
};
845845

846+
/// Diagnose extraneous use of address of (`&`) which could only be
847+
/// associated with arguments to inout parameters e.g.
848+
///
849+
/// ```swift
850+
/// struct S {}
851+
///
852+
/// var a: S = ...
853+
/// var b: S = ...
854+
///
855+
/// a = &b
856+
/// ```
857+
class InvalidUseOfAddressOf final : public ContextualFailure {
858+
public:
859+
InvalidUseOfAddressOf(Expr *root, ConstraintSystem &cs, Type lhs, Type rhs,
860+
ConstraintLocator *locator)
861+
: ContextualFailure(root, cs, lhs, rhs, locator) {}
862+
863+
bool diagnoseAsError() override;
864+
865+
protected:
866+
/// Compute location of the failure for diagnostic.
867+
SourceLoc getLoc() const;
868+
};
869+
846870
/// Diagnose mismatches relating to tuple destructuring.
847871
class TupleContextualFailure final : public ContextualFailure {
848872
public:
@@ -1445,30 +1469,6 @@ class InvalidMethodRefInKeyPath final : public InvalidMemberRefInKeyPath {
14451469
bool diagnoseAsError() override;
14461470
};
14471471

1448-
/// Diagnose extraneous use of address of (`&`) which could only be
1449-
/// associated with arguments to inout parameters e.g.
1450-
///
1451-
/// ```swift
1452-
/// struct S {}
1453-
///
1454-
/// var a: S = ...
1455-
/// var b: S = ...
1456-
///
1457-
/// a = &b
1458-
/// ```
1459-
class InvalidUseOfAddressOf final : public FailureDiagnostic {
1460-
public:
1461-
InvalidUseOfAddressOf(Expr *root, ConstraintSystem &cs,
1462-
ConstraintLocator *locator)
1463-
: FailureDiagnostic(root, cs, locator) {}
1464-
1465-
bool diagnoseAsError() override;
1466-
1467-
protected:
1468-
/// Compute location of the failure for diagnostic.
1469-
SourceLoc getLoc() const;
1470-
};
1471-
14721472
/// Diagnose an attempt return something from a function which
14731473
/// doesn't have a return type specified e.g.
14741474
///

lib/Sema/CSFix.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,14 @@ KeyPathContextualMismatch::create(ConstraintSystem &cs, Type lhs, Type rhs,
623623
}
624624

625625
bool RemoveAddressOf::diagnose(Expr *root, bool asNote) const {
626-
InvalidUseOfAddressOf failure(root, getConstraintSystem(), getLocator());
626+
InvalidUseOfAddressOf failure(root, getConstraintSystem(), getFromType(),
627+
getToType(), getLocator());
627628
return failure.diagnose(asNote);
628629
}
629630

630-
RemoveAddressOf *RemoveAddressOf::create(ConstraintSystem &cs,
631+
RemoveAddressOf *RemoveAddressOf::create(ConstraintSystem &cs, Type lhs, Type rhs,
631632
ConstraintLocator *locator) {
632-
return new (cs.getAllocator()) RemoveAddressOf(cs, locator);
633+
return new (cs.getAllocator()) RemoveAddressOf(cs, lhs, rhs, locator);
633634
}
634635

635636
bool RemoveReturn::diagnose(Expr *root, bool asNote) const {

lib/Sema/CSFix.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,22 @@ class AddAddressOf final : public ContextualMismatch {
545545
ConstraintLocator *locator);
546546
};
547547

548+
class RemoveAddressOf final : public ContextualMismatch {
549+
RemoveAddressOf(ConstraintSystem &cs, Type lhs, Type rhs,
550+
ConstraintLocator *locator)
551+
: ContextualMismatch(cs, FixKind::RemoveAddressOf, lhs, rhs, locator) {}
552+
553+
public:
554+
std::string getName() const override {
555+
return "remove extraneous use of `&`";
556+
}
557+
558+
bool diagnose(Expr *root, bool asNote = false) const override;
559+
560+
static RemoveAddressOf *create(ConstraintSystem &cs, Type lhs, Type rhs,
561+
ConstraintLocator *locator);
562+
};
563+
548564
/// Detect situations where two type's generic arguments must
549565
/// match but are not convertible e.g.
550566
///
@@ -1165,21 +1181,6 @@ class AllowInvalidRefInKeyPath final : public ConstraintFix {
11651181
ConstraintLocator *locator);
11661182
};
11671183

1168-
class RemoveAddressOf final : public ConstraintFix {
1169-
RemoveAddressOf(ConstraintSystem &cs, ConstraintLocator *locator)
1170-
: ConstraintFix(cs, FixKind::RemoveAddressOf, locator) {}
1171-
1172-
public:
1173-
std::string getName() const override {
1174-
return "remove extraneous use of `&`";
1175-
}
1176-
1177-
bool diagnose(Expr *root, bool asNote = false) const override;
1178-
1179-
static RemoveAddressOf *create(ConstraintSystem &cs,
1180-
ConstraintLocator *locator);
1181-
};
1182-
11831184
class RemoveReturn final : public ConstraintFix {
11841185
RemoveReturn(ConstraintSystem &cs, ConstraintLocator *locator)
11851186
: ConstraintFix(cs, FixKind::RemoveReturn, locator) {}

lib/Sema/CSSimplify.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,7 +2356,8 @@ bool ConstraintSystem::repairFailures(
23562356

23572357
if (isa<InOutExpr>(AE->getSrc())) {
23582358
conversionsOrFixes.push_back(
2359-
RemoveAddressOf::create(*this, getConstraintLocator(locator)));
2359+
RemoveAddressOf::create(*this, lhs, rhs,
2360+
getConstraintLocator(locator)));
23602361
return true;
23612362
}
23622363

@@ -2429,6 +2430,23 @@ bool ConstraintSystem::repairFailures(
24292430
conversionsOrFixes.push_back(AddAddressOf::create(
24302431
*this, lhs, rhs, getConstraintLocator(locator)));
24312432
}
2433+
2434+
// If the argument is inout and the parameter is not inout or a pointer,
2435+
// suggest removing the &.
2436+
if (lhs->is<InOutType>() && !rhs->is<InOutType>()) {
2437+
auto objectType = rhs->lookThroughAllOptionalTypes();
2438+
if (objectType->getAnyPointerElementType())
2439+
break;
2440+
2441+
auto result = matchTypes(lhs->getInOutObjectType(), rhs,
2442+
ConstraintKind::ArgumentConversion,
2443+
TypeMatchFlags::TMF_ApplyingFix, locator);
2444+
2445+
if (result.isSuccess())
2446+
conversionsOrFixes.push_back(RemoveAddressOf::create(*this, lhs,
2447+
rhs, getConstraintLocator(locator)));
2448+
}
2449+
24322450
break;
24332451
}
24342452

@@ -2987,7 +3005,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
29873005

29883006
if (kind == ConstraintKind::OperatorArgumentConversion) {
29893007
conversionsOrFixes.push_back(
2990-
RemoveAddressOf::create(*this, getConstraintLocator(locator)));
3008+
RemoveAddressOf::create(*this, type1, type2,
3009+
getConstraintLocator(locator)));
29913010
break;
29923011
}
29933012

test/Constraints/diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,8 @@ func r22255907_2<T>(_ x : Int, a : T, b: Int) {}
570570

571571
func reachabilityForInternetConnection() {
572572
var variable: Int = 42
573-
r22255907_1(&variable, b: 2.1) // expected-error {{'&' used with non-inout argument of type 'Int'}} {{15-16=}}
574-
r22255907_2(1, a: &variable, b: 2.1)// expected-error {{'&' used with non-inout argument of type 'Int'}} {{21-22=}}
573+
r22255907_1(&variable, b: 2) // expected-error {{'&' used with non-inout argument of type 'Int'}} {{15-16=}}
574+
r22255907_2(1, a: &variable, b: 2)// expected-error {{'&' used with non-inout argument of type 'Int'}} {{21-22=}}
575575
}
576576

577577
// <rdar://problem/21601687> QoI: Using "=" instead of "==" in if statement leads to incorrect error message

test/Generics/materializable_restrictions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
func test15921520() {
44
var x: Int = 0
5-
func f<T>(_ x: T) {} // expected-note{{in call to function 'f'}}
6-
f(&x) // expected-error{{generic parameter 'T' could not be inferred}}
5+
func f<T>(_ x: T) {}
6+
f(&x) // expected-error{{'&' used with non-inout argument of type 'Int'}} {{7-8=}}
77
}
88

99
func test20807269() {

0 commit comments

Comments
 (0)