Skip to content

Commit 08e09fc

Browse files
committed
[Diagnostics] Switch getType to use TypedNode
1 parent 66a07ba commit 08e09fc

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,8 @@ SourceRange FailureDiagnostic::getSourceRange(TypedNode anchor) {
9696
}
9797
}
9898

99-
Type FailureDiagnostic::getType(Expr *expr, bool wantRValue) const {
100-
return resolveType(S.getType(expr), /*reconstituteSugar=*/false, wantRValue);
101-
}
102-
103-
Type FailureDiagnostic::getType(const TypeLoc &loc, bool wantRValue) const {
104-
return resolveType(S.getType(&loc), /*reconstituteSugar=*/false, wantRValue);
99+
Type FailureDiagnostic::getType(TypedNode node, bool wantRValue) const {
100+
return resolveType(S.getType(node), /*reconstituteSugar=*/false, wantRValue);
105101
}
106102

107103
template <typename... ArgTypes>
@@ -175,15 +171,17 @@ Type FailureDiagnostic::restoreGenericParameters(
175171
}
176172

177173
Type RequirementFailure::getOwnerType() const {
178-
auto *anchor = getRawAnchor().get<const Expr *>();
174+
auto anchor = getRawAnchor();
179175

180176
// If diagnostic is anchored at assignment expression
181177
// it means that requirement failure happend while trying
182178
// to convert source to destination, which means that
183179
// owner type is actually not an assignment expression
184180
// itself but its source.
185-
if (auto *assignment = dyn_cast<AssignExpr>(anchor))
186-
anchor = assignment->getSrc();
181+
if (auto *E = anchor.dyn_cast<Expr *>()) {
182+
if (auto *assignment = dyn_cast<AssignExpr>(E))
183+
anchor = assignment->getSrc();
184+
}
187185

188186
return getType(anchor)->getInOutObjectType()->getMetatypeInstanceType();
189187
}
@@ -524,8 +522,8 @@ bool MissingConformanceFailure::diagnoseTypeCannotConform(
524522
}
525523

526524
bool MissingConformanceFailure::diagnoseAsAmbiguousOperatorRef() {
527-
auto *anchor = getRawAnchor().get<const Expr *>();
528-
auto *ODRE = dyn_cast<OverloadedDeclRefExpr>(anchor);
525+
auto anchor = getRawAnchor();
526+
auto *ODRE = dyn_cast<OverloadedDeclRefExpr>(anchor.get<const Expr *>());
529527
if (!ODRE)
530528
return false;
531529

@@ -994,7 +992,7 @@ bool MissingExplicitConversionFailure::diagnoseAsError() {
994992
}
995993

996994
bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() {
997-
auto *anchor = getAnchor().get<const Expr *>();
995+
auto anchor = getAnchor();
998996
auto baseType = getType(anchor);
999997
bool resultIsOptional = ResultTypeIsOptional;
1000998

@@ -2017,7 +2015,6 @@ bool ContextualFailure::diagnoseAsError() {
20172015
if (isa<OptionalTryExpr>(anchor) || isa<OptionalEvaluationExpr>(anchor)) {
20182016
auto objectType = fromType->getOptionalObjectType();
20192017
if (objectType->isEqual(toType)) {
2020-
auto &cs = getConstraintSystem();
20212018
MissingOptionalUnwrapFailure failure(getSolution(), getType(anchor),
20222019
toType,
20232020
getConstraintLocator(anchor));
@@ -2352,7 +2349,8 @@ bool ContextualFailure::diagnoseCoercionToUnrelatedType() const {
23522349

23532350
if (auto *coerceExpr = dyn_cast<CoerceExpr>(anchor)) {
23542351
auto fromType = getType(coerceExpr->getSubExpr());
2355-
auto toType = getType(coerceExpr->getCastTypeLoc());
2352+
const auto &typeLoc = coerceExpr->getCastTypeLoc();
2353+
auto toType = getType(&typeLoc);
23562354

23572355
auto diagnostic = getDiagnosticFor(CTP_CoerceOperand, toType);
23582356

@@ -2513,7 +2511,7 @@ bool ContextualFailure::diagnoseYieldByReferenceMismatch() const {
25132511
if (CTP != CTP_YieldByReference)
25142512
return false;
25152513

2516-
auto *anchor = getAnchor().get<const Expr *>();
2514+
auto anchor = getAnchor();
25172515
auto exprType = getType(anchor, /*wantRValue=*/false);
25182516
auto contextualType = getToType();
25192517

@@ -3740,8 +3738,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() {
37403738

37413739
// If the rhs of '~=' is the enum type, a single dot suffixes
37423740
// since the type can be inferred
3743-
Type secondArgType =
3744-
cs.getType(binaryExpr->getArg()->getElement(1));
3741+
Type secondArgType = getType(binaryExpr->getArg()->getElement(1));
37453742
if (secondArgType->isEqual(baseTy)) {
37463743
Diag->fixItInsert(loc, ".");
37473744
return true;
@@ -3859,9 +3856,9 @@ bool MissingArgumentsFailure::diagnoseAsError() {
38593856
if (isMisplacedMissingArgument(getSolution(), locator))
38603857
return false;
38613858

3862-
auto *anchor = getAnchor().get<const Expr *>();
3859+
auto anchor = getAnchor();
38633860

3864-
if (auto *closure = dyn_cast<ClosureExpr>(anchor))
3861+
if (auto *closure = dyn_cast<ClosureExpr>(anchor.dyn_cast<Expr *>()))
38653862
return diagnoseClosure(closure);
38663863

38673864
// This is a situation where function type is passed as an argument
@@ -4113,7 +4110,7 @@ bool MissingArgumentsFailure::diagnoseClosure(ClosureExpr *closure) {
41134110
locator->isLastElement<LocatorPathElt::ClosureBody>()) {
41144111
// Based on the locator we know this this is something like this:
41154112
// `let _: () -> ((Int) -> Void) = { return {} }`.
4116-
funcType = getType(getRawAnchor().get<const Expr *>())
4113+
funcType = getType(getRawAnchor())
41174114
->castTo<FunctionType>()
41184115
->getResult()
41194116
->castTo<FunctionType>();
@@ -4582,8 +4579,9 @@ bool OutOfOrderArgumentFailure::diagnoseAsError() {
45824579
bool ExtraneousArgumentsFailure::diagnoseAsError() {
45834580
// Simplified anchor would point directly to the
45844581
// argument in case of contextual mismatch.
4585-
auto *anchor = getAnchor().get<const Expr *>();
4586-
if (auto *closure = dyn_cast<ClosureExpr>(anchor)) {
4582+
auto *anchor = getAnchor();
4583+
4584+
if (auto *closure = dyn_cast<ClosureExpr>(anchor.dyn_cast<Expr *>()) {
45874585
auto fnType = ContextualType;
45884586
auto params = closure->getParameters();
45894587

@@ -5090,7 +5088,8 @@ bool MissingGenericArgumentsFailure::diagnoseParameter(
50905088

50915089
if (auto *CE =
50925090
dyn_cast<ExplicitCastExpr>(getRawAnchor().get<const Expr *>())) {
5093-
auto castTo = getType(CE->getCastTypeLoc());
5091+
const auto &typeLoc = CE->getCastTypeLoc();
5092+
auto castTo = getType(&typeLoc);
50945093
auto *NTD = castTo->getAnyNominal();
50955094
emitDiagnosticAt(loc, diag::unbound_generic_parameter_cast, GP,
50965095
NTD ? NTD->getDeclaredType() : castTo);
@@ -5532,7 +5531,7 @@ bool ArgumentMismatchFailure::diagnoseAsError() {
55325531
// If argument is an l-value type and parameter is a pointer type,
55335532
// let's match up its element type to the argument to see whether
55345533
// it would be appropriate to suggest adding `&`.
5535-
auto *argExpr = getAnchor().get<const Expr *>();
5534+
auto argExpr = getAnchor();
55365535
if (getType(argExpr, /*wantRValue=*/false)->is<LValueType>()) {
55375536
auto elementTy = paramType->getAnyPointerElementType();
55385537
if (elementTy && argType->isEqual(elementTy)) {
@@ -5784,7 +5783,7 @@ bool ExpandArrayIntoVarargsFailure::diagnoseAsNote() {
57845783
}
57855784

57865785
bool ExtraneousCallFailure::diagnoseAsError() {
5787-
auto *anchor = getAnchor().get<const Expr *>();
5786+
auto anchor = getAnchor();
57885787
auto *locator = getLocator();
57895788

57905789
// If this is something like `foo()` where `foo` is a variable
@@ -5813,7 +5812,7 @@ bool ExtraneousCallFailure::diagnoseAsError() {
58135812
}
58145813
}
58155814

5816-
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor)) {
5815+
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor.dyn_cast<Expr *>())) {
58175816
auto *baseExpr = UDE->getBase();
58185817
auto *call = cast<CallExpr>(getRawAnchor().get<const Expr *>());
58195818

lib/Sema/CSDiagnostics.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ class FailureDiagnostic {
8383

8484
ConstraintLocator *getLocator() const { return Locator; }
8585

86-
Type getType(Expr *expr, bool wantRValue = true) const;
87-
Type getType(const TypeLoc &loc, bool wantRValue = true) const;
86+
Type getType(TypedNode node, bool wantRValue = true) const;
8887

8988
/// Resolve type variables present in the raw type, if any.
9089
Type resolveType(Type rawType, bool reconstituteSugar = false,

0 commit comments

Comments
 (0)