|
| 1 | +//===--- CSDiagnostics.cpp - Constraint Diagnostics -----------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file implements diagnostics for constraint system. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "ConstraintSystem.h" |
| 18 | +#include "CSDiagnostics.h" |
| 19 | +#include "MiscDiagnostics.h" |
| 20 | +#include "swift/AST/Expr.h" |
| 21 | +#include "swift/AST/Types.h" |
| 22 | +#include "llvm/ADT/ArrayRef.h" |
| 23 | + |
| 24 | +using namespace swift; |
| 25 | +using namespace constraints; |
| 26 | + |
| 27 | +FailureDiagnostic::~FailureDiagnostic() {} |
| 28 | + |
| 29 | +Type FailureDiagnostic::getType(Expr *expr) const { |
| 30 | + auto &cs = getConstraintSystem(); |
| 31 | + return solution.simplifyType(cs.getType(expr)); |
| 32 | +} |
| 33 | + |
| 34 | +template <typename... ArgTypes> |
| 35 | +InFlightDiagnostic |
| 36 | +FailureDiagnostic::emitDiagnostic(ArgTypes &&... Args) const { |
| 37 | + auto &cs = getConstraintSystem(); |
| 38 | + return cs.TC.diagnose(std::forward<ArgTypes>(Args)...); |
| 39 | +} |
| 40 | + |
| 41 | +Type RequirementFailure::getOwnerType() const { |
| 42 | + return getType(getAnchor())->getRValueInstanceType(); |
| 43 | +} |
| 44 | + |
| 45 | +const Requirement &RequirementFailure::getRequirement() { |
| 46 | + auto *genericCtx = AffectedDecl->getAsGenericContext(); |
| 47 | + return genericCtx->getGenericRequirements()[getRequirementIndex()]; |
| 48 | +} |
| 49 | + |
| 50 | +ValueDecl *RequirementFailure::getDeclRef() const { |
| 51 | + auto &cs = getConstraintSystem(); |
| 52 | + |
| 53 | + auto *anchor = getAnchor(); |
| 54 | + auto *locator = cs.getConstraintLocator(anchor); |
| 55 | + if (auto *AE = dyn_cast<CallExpr>(anchor)) { |
| 56 | + assert(isa<TypeExpr>(AE->getFn())); |
| 57 | + ConstraintLocatorBuilder ctor(locator); |
| 58 | + locator = cs.getConstraintLocator( |
| 59 | + ctor.withPathElement(PathEltKind::ApplyFunction) |
| 60 | + .withPathElement(PathEltKind::ConstructorMember)); |
| 61 | + } else if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor)) { |
| 62 | + ConstraintLocatorBuilder member(locator); |
| 63 | + locator = |
| 64 | + cs.getConstraintLocator(member.withPathElement(PathEltKind::Member)); |
| 65 | + } |
| 66 | + |
| 67 | + auto overload = getOverloadChoiceIfAvailable(locator); |
| 68 | + if (overload) |
| 69 | + return overload->choice.getDecl(); |
| 70 | + |
| 71 | + auto ownerType = getOwnerType(); |
| 72 | + if (auto *NA = dyn_cast<NameAliasType>(ownerType.getPointer())) |
| 73 | + return NA->getDecl(); |
| 74 | + |
| 75 | + return ownerType->getAnyGeneric(); |
| 76 | +} |
| 77 | + |
| 78 | +bool MissingConformanceFailure::diagnose() { |
| 79 | + auto *anchor = getAnchor(); |
| 80 | + auto ownerType = getOwnerType(); |
| 81 | + auto type = getNonConformingType(); |
| 82 | + auto protocolType = getProtocolType(); |
| 83 | + |
| 84 | + // Find `ApplyExpr` based on a function expression attached to it. |
| 85 | + auto findApplyExpr = [](Expr *parent, Expr *fnExpr) -> ApplyExpr * { |
| 86 | + ApplyExpr *applyExpr = nullptr; |
| 87 | + parent->forEachChildExpr([&applyExpr, &fnExpr](Expr *subExpr) -> Expr * { |
| 88 | + auto *AE = dyn_cast<ApplyExpr>(subExpr); |
| 89 | + if (!AE || AE->getFn() != fnExpr) |
| 90 | + return subExpr; |
| 91 | + |
| 92 | + applyExpr = AE; |
| 93 | + return nullptr; |
| 94 | + }); |
| 95 | + return applyExpr; |
| 96 | + }; |
| 97 | + |
| 98 | + auto getArgumentAt = [](ApplyExpr *AE, unsigned index) -> Expr * { |
| 99 | + assert(AE); |
| 100 | + |
| 101 | + auto *arg = AE->getArg(); |
| 102 | + if (auto *TE = dyn_cast<TupleExpr>(arg)) |
| 103 | + return TE->getElement(index); |
| 104 | + |
| 105 | + assert(index == 0); |
| 106 | + if (auto *PE = dyn_cast<ParenExpr>(arg)) |
| 107 | + return PE->getSubExpr(); |
| 108 | + |
| 109 | + return arg; |
| 110 | + }; |
| 111 | + |
| 112 | + auto *applyExpr = findApplyExpr(getParentExpr(), anchor); |
| 113 | + |
| 114 | + Optional<unsigned> atParameterPos; |
| 115 | + // Sometimes fix is recorded by type-checking sub-expression |
| 116 | + // during normal diagnostics, in such case call expression |
| 117 | + // is unavailable. |
| 118 | + if (applyExpr) { |
| 119 | + // If this is a static, initializer or operator call, |
| 120 | + // let's not try to diagnose it here, but refer to expression |
| 121 | + // diagnostics. |
| 122 | + if (isa<BinaryExpr>(applyExpr) || isa<TypeExpr>(anchor)) |
| 123 | + return false; |
| 124 | + |
| 125 | + if (auto *fnType = ownerType->getAs<AnyFunctionType>()) { |
| 126 | + auto parameters = fnType->getParams(); |
| 127 | + for (auto index : indices(parameters)) { |
| 128 | + if (parameters[index].getType()->isEqual(type)) { |
| 129 | + atParameterPos = index; |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (type->isExistentialType()) { |
| 137 | + auto diagnostic = diag::protocol_does_not_conform_objc; |
| 138 | + if (type->isObjCExistentialType()) |
| 139 | + diagnostic = diag::protocol_does_not_conform_static; |
| 140 | + |
| 141 | + emitDiagnostic(anchor->getLoc(), diagnostic, type, protocolType); |
| 142 | + } else if (atParameterPos) { |
| 143 | + // Requirement comes from one of the parameter types, |
| 144 | + // let's try to point diagnostic to the argument expression. |
| 145 | + auto *argExpr = getArgumentAt(applyExpr, *atParameterPos); |
| 146 | + emitDiagnostic(argExpr->getLoc(), |
| 147 | + diag::cannot_convert_argument_value_protocol, type, |
| 148 | + protocolType); |
| 149 | + } else { |
| 150 | + emitDiagnostic(anchor->getLoc(), diag::type_does_not_conform_owner, |
| 151 | + ownerType, type, protocolType); |
| 152 | + } |
| 153 | + return true; |
| 154 | +} |
| 155 | + |
| 156 | +bool LabelingFailure::diagnose() { |
| 157 | + auto &cs = getConstraintSystem(); |
| 158 | + auto *call = cast<CallExpr>(getAnchor()); |
| 159 | + return diagnoseArgumentLabelError(cs.getASTContext(), call->getArg(), |
| 160 | + CorrectLabels, |
| 161 | + isa<SubscriptExpr>(call->getFn())); |
| 162 | +} |
0 commit comments