Skip to content

Commit 42bdfc9

Browse files
committed
[Diagnostics] Remove unsafe getRestrictionFor method
1 parent 6f2af1f commit 42bdfc9

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,19 +1237,41 @@ bool RValueTreatedAsLValueFailure::diagnoseAsError() {
12371237
subElementDiagID = diag::assignment_lhs_is_apply_expression;
12381238
}
12391239
} else if (auto inoutExpr = dyn_cast<InOutExpr>(diagExpr)) {
1240-
if (auto restriction = getRestrictionForType(getType(inoutExpr))) {
1241-
PointerTypeKind pointerKind;
1242-
if (restriction->second == ConversionRestrictionKind::ArrayToPointer &&
1243-
restriction->first->getAnyPointerElementType(pointerKind) &&
1244-
(pointerKind == PTK_UnsafePointer ||
1245-
pointerKind == PTK_UnsafeRawPointer)) {
1246-
// If we're converting to an UnsafePointer, then the programmer
1247-
// specified an & unnecessarily. Produce a fixit hint to remove it.
1248-
emitDiagnostic(inoutExpr->getLoc(),
1249-
diag::extra_address_of_unsafepointer, restriction->first)
1250-
.highlight(inoutExpr->getSourceRange())
1251-
.fixItRemove(inoutExpr->getStartLoc());
1252-
return true;
1240+
if (auto *parentExpr = findParentExpr(inoutExpr)) {
1241+
if (auto *call =
1242+
dyn_cast_or_null<ApplyExpr>(findParentExpr(parentExpr))) {
1243+
// Since this `inout` expression is an argument to a call/operator
1244+
// let's figure out whether this is an impliict conversion from
1245+
// array to an unsafe pointer type and diagnose it.
1246+
unsigned argIdx = 0;
1247+
if (auto *TE = dyn_cast<TupleExpr>(parentExpr)) {
1248+
for (unsigned n = TE->getNumElements(); argIdx != n; ++argIdx) {
1249+
if (TE->getElement(argIdx) == inoutExpr)
1250+
break;
1251+
}
1252+
}
1253+
1254+
auto *argLoc = getConstraintLocator(
1255+
call, {ConstraintLocator::ApplyArgument,
1256+
LocatorPathElt::ApplyArgToParam(argIdx, argIdx,
1257+
ParameterTypeFlags())});
1258+
1259+
if (auto info = getFunctionArgApplyInfo(argLoc)) {
1260+
auto &cs = getConstraintSystem();
1261+
auto paramType = info->getParamType();
1262+
auto argType = getType(inoutExpr)->getWithoutSpecifierType();
1263+
1264+
PointerTypeKind ptr;
1265+
if (cs.isArrayType(argType) &&
1266+
paramType->getAnyPointerElementType(ptr) &&
1267+
(ptr == PTK_UnsafePointer || ptr == PTK_UnsafeRawPointer)) {
1268+
emitDiagnostic(inoutExpr->getLoc(),
1269+
diag::extra_address_of_unsafepointer, paramType)
1270+
.highlight(inoutExpr->getSourceRange())
1271+
.fixItRemove(inoutExpr->getStartLoc());
1272+
return true;
1273+
}
1274+
}
12531275
}
12541276
}
12551277

lib/Sema/CSDiagnostics.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,6 @@ class FailureDiagnostic {
140140
return cs.getASTContext();
141141
}
142142

143-
Optional<std::pair<Type, ConversionRestrictionKind>>
144-
getRestrictionForType(Type type) const {
145-
for (auto &e : S.ConstraintRestrictions) {
146-
auto &location = e.first;
147-
auto &restriction = e.second;
148-
149-
if (std::get<0>(location)->isEqual(type))
150-
return std::pair<Type, ConversionRestrictionKind>(std::get<1>(location),
151-
restriction);
152-
}
153-
return None;
154-
}
155-
156143
/// Retrieve overload choice resolved for a given locator
157144
/// by the constraint solver.
158145
Optional<SelectedOverload>

0 commit comments

Comments
 (0)