Skip to content

Commit 3190433

Browse files
committed
[Constraint system] Give SolutionApplicationTarget purpose.
A contextual type purpose, that is, so it captures more about what the entity is that is being solved.
1 parent c5ed8d6 commit 3190433

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

lib/Sema/ConstraintSystem.h

+30-3
Original file line numberDiff line numberDiff line change
@@ -1149,8 +1149,11 @@ class SolutionApplicationTarget {
11491149
struct {
11501150
Expr *expression;
11511151

1152+
/// The purpose of the contextual type.
1153+
ContextualTypePurpose contextualPurpose;
1154+
11521155
/// The type to which the expression should be converted.
1153-
Type convertType;
1156+
TypeLoc convertType;
11541157

11551158
/// Whether the expression result will be discarded at the end.
11561159
bool isDiscarded;
@@ -1163,9 +1166,18 @@ class SolutionApplicationTarget {
11631166
};
11641167

11651168
public:
1166-
SolutionApplicationTarget(Expr *expr, Type convertType, bool isDiscarded) {
1169+
SolutionApplicationTarget(Expr *expr,
1170+
ContextualTypePurpose contextualPurpose,
1171+
Type convertType, bool isDiscarded)
1172+
: SolutionApplicationTarget(expr, contextualPurpose,
1173+
TypeLoc::withoutLoc(convertType),
1174+
isDiscarded) { }
1175+
1176+
SolutionApplicationTarget(Expr *expr, ContextualTypePurpose contextualPurpose,
1177+
TypeLoc convertType, bool isDiscarded) {
11671178
kind = Kind::expression;
11681179
expression.expression = expr;
1180+
expression.contextualPurpose = contextualPurpose;
11691181
expression.convertType = convertType;
11701182
expression.isDiscarded = isDiscarded;
11711183
}
@@ -1189,16 +1201,31 @@ class SolutionApplicationTarget {
11891201
}
11901202
}
11911203

1204+
ContextualTypePurpose getExprContextualTypePurpose() const {
1205+
assert(kind == Kind::expression);
1206+
return expression.contextualPurpose;
1207+
}
1208+
11921209
Type getExprConversionType() const {
1210+
assert(kind == Kind::expression);
1211+
return expression.convertType.getType();
1212+
}
1213+
1214+
TypeLoc getExprConversionTypeLoc() const {
11931215
assert(kind == Kind::expression);
11941216
return expression.convertType;
11951217
}
11961218

11971219
void setExprConversionType(Type type) {
1220+
assert(kind == Kind::expression);
1221+
expression.convertType = TypeLoc::withoutLoc(type);
1222+
}
1223+
1224+
void setExprConversionTypeLoc(TypeLoc type) {
11981225
assert(kind == Kind::expression);
11991226
expression.convertType = type;
12001227
}
1201-
1228+
12021229
bool isDiscardedExpr() const {
12031230
assert(kind == Kind::expression);
12041231
return expression.isDiscarded;

lib/Sema/TypeCheckConstraints.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -2183,7 +2183,7 @@ Type TypeChecker::typeCheckExpression(Expr *&expr, DeclContext *dc,
21832183

21842184
// Attempt to solve the constraint system.
21852185
SolutionApplicationTarget target(
2186-
expr, convertTo,
2186+
expr, convertTypePurpose, convertTo,
21872187
options.contains(TypeCheckExprFlags::IsDiscarded));
21882188
auto viable = cs.solve(target, listener, allowFreeTypeVariables);
21892189
if (!viable)
@@ -2283,7 +2283,8 @@ getTypeOfExpressionWithoutApplying(Expr *&expr, DeclContext *dc,
22832283
// re-check.
22842284
if (needClearType)
22852285
expr->setType(Type());
2286-
SolutionApplicationTarget target(expr, Type(), /*isDiscarded=*/false);
2286+
SolutionApplicationTarget target(
2287+
expr, CTP_Unused, Type(), /*isDiscarded=*/false);
22872288
auto viable = cs.solve(target, listener, allowFreeTypeVariables);
22882289
if (!viable) {
22892290
recoverOriginalType();
@@ -2363,7 +2364,8 @@ void TypeChecker::getPossibleTypesOfExpressionWithoutApplying(
23632364
if (originalType && originalType->hasError())
23642365
expr->setType(Type());
23652366

2366-
SolutionApplicationTarget target(expr, Type(), /*isDiscarded=*/false);
2367+
SolutionApplicationTarget target(
2368+
expr, CTP_Unused, Type(), /*isDiscarded=*/false);
23672369
if (auto viable = cs.solve(target, listener, allowFreeTypeVariables)) {
23682370
expr = target.getAsExpr();
23692371
for (auto &solution : *viable) {

0 commit comments

Comments
 (0)