Skip to content

Commit 716e11f

Browse files
[Constraint System] Recording SpecifyObjectLiteralTypeImport fix when attempting literal result type variable binding and handle object literal in MissingArgumentsFailure
1 parent 94dd355 commit 716e11f

File tree

7 files changed

+57
-70
lines changed

7 files changed

+57
-70
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,13 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
10451045
cs.DefaultedConstraints.push_back(srcLocator);
10461046

10471047
if (type->isHole()) {
1048+
if (auto * OLE = dyn_cast<ObjectLiteralExpr>(srcLocator->getAnchor())) {
1049+
auto *fix = SpecifyObjectLiteralTypeImport::create(
1050+
cs, TypeVar->getImpl().getLocator());
1051+
if (cs.recordFix(fix))
1052+
return true;
1053+
}
1054+
10481055
if (auto *GP = TypeVar->getImpl().getGenericParameter()) {
10491056
auto path = dstLocator->getPath();
10501057
// Drop `generic parameter` locator element so that all missing

lib/Sema/CSDiagnostics.cpp

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3882,7 +3882,7 @@ bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const {
38823882

38833883
auto *anchor = getRawAnchor();
38843884
if (!(isa<CallExpr>(anchor) || isa<SubscriptExpr>(anchor) ||
3885-
isa<UnresolvedMemberExpr>(anchor)))
3885+
isa<UnresolvedMemberExpr>(anchor) || isa<ObjectLiteralExpr>(anchor)))
38863886
return false;
38873887

38883888
if (SynthesizedArgs.size() != 1)
@@ -4219,6 +4219,9 @@ MissingArgumentsFailure::getCallInfo(Expr *anchor) const {
42194219
} else if (auto *SE = dyn_cast<SubscriptExpr>(anchor)) {
42204220
return std::make_tuple(SE, SE->getIndex(), SE->getNumArguments(),
42214221
SE->hasTrailingClosure());
4222+
} else if (auto *OLE = dyn_cast<ObjectLiteralExpr>(anchor)) {
4223+
return std::make_tuple(OLE, OLE->getArg(), OLE->getNumArguments(),
4224+
OLE->hasTrailingClosure());
42224225
}
42234226

42244227
return std::make_tuple(nullptr, nullptr, 0, false);
@@ -6039,46 +6042,48 @@ bool UnableToInferClosureReturnType::diagnoseAsError() {
60396042
return true;
60406043
}
60416044

6042-
static std::tuple<StringRef, StringRef>
6043-
getImportModuleAndDefaultType(const ASTContext &ctx, ProtocolDecl *protocol) {
6045+
static std::pair<StringRef, StringRef>
6046+
getImportModuleAndDefaultType(const ASTContext &ctx, ObjectLiteralExpr *expr) {
60446047
const auto &target = ctx.LangOpts.Target;
6045-
if (protocol ==
6046-
ctx.getProtocol(KnownProtocolKind::ExpressibleByColorLiteral)) {
6047-
if (target.isMacOSX()) {
6048-
return std::make_tuple("AppKit", "NSColor");
6049-
} else if (target.isiOS() || target.isTvOS()) {
6050-
return std::make_tuple("UIKit", "UIColor");
6048+
6049+
switch (expr->getLiteralKind()) {
6050+
case ObjectLiteralExpr::colorLiteral: {
6051+
if (target.isMacOSX()) {
6052+
return std::make_pair("AppKit", "NSColor");
6053+
} else if (target.isiOS() || target.isTvOS()) {
6054+
return std::make_pair("UIKit", "UIColor");
6055+
}
6056+
break;
60516057
}
6052-
} else if (protocol ==
6053-
ctx.getProtocol(KnownProtocolKind::ExpressibleByImageLiteral)) {
6054-
if (target.isMacOSX()) {
6055-
return std::make_tuple("AppKit", "NSImage");
6056-
} else if (target.isiOS() || target.isTvOS()) {
6057-
return std::make_tuple("UIKit", "UIImage");
6058+
6059+
case ObjectLiteralExpr::imageLiteral: {
6060+
if (target.isMacOSX()) {
6061+
return std::make_pair("AppKit", "NSImage");
6062+
} else if (target.isiOS() || target.isTvOS()) {
6063+
return std::make_pair("UIKit", "UIImage");
6064+
}
6065+
break;
6066+
}
6067+
6068+
case ObjectLiteralExpr::fileLiteral: {
6069+
return std::make_pair("Foundation", "URL");
60586070
}
6059-
} else if (protocol ==
6060-
ctx.getProtocol(
6061-
KnownProtocolKind::ExpressibleByFileReferenceLiteral)) {
6062-
return std::make_tuple("Foundation", "URL");
60636071
}
6064-
return std::make_tuple("", "");
6072+
6073+
return std::make_pair("", "");
60656074
}
60666075

60676076
bool UnableToInferProtocolLiteralType::diagnoseAsError() {
6068-
auto *locator = getLocator();
60696077
auto &cs = getConstraintSystem();
6070-
auto *expr = cast<ObjectLiteralExpr>(locator->getAnchor());
6071-
60726078
auto &ctx = cs.getASTContext();
6073-
auto protocol = TypeChecker::getLiteralProtocol(ctx, expr);
6074-
assert(protocol);
6075-
auto plainName = expr->getLiteralKindPlainName();
6079+
auto *expr = cast<ObjectLiteralExpr>(getLocator()->getAnchor());
60766080

60776081
StringRef importModule;
60786082
StringRef importDefaultTypeName;
60796083
std::tie(importModule, importDefaultTypeName) =
6080-
getImportModuleAndDefaultType(ctx, protocol);
6084+
getImportModuleAndDefaultType(ctx, expr);
60816085

6086+
auto plainName = expr->getLiteralKindPlainName();
60826087
emitDiagnostic(expr->getLoc(), diag::object_literal_default_type_missing,
60836088
plainName);
60846089
if (!importModule.empty()) {

lib/Sema/CSFix.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,20 +1176,7 @@ bool SpecifyObjectLiteralTypeImport::diagnose(bool asNote) const {
11761176
}
11771177

11781178
SpecifyObjectLiteralTypeImport *
1179-
SpecifyObjectLiteralTypeImport::attempt(ConstraintSystem &cs,
1180-
ConstraintLocator *locator) {
1181-
// FIXME: ArgumentMismatchFailure is currently used from CSDiag, meaning
1182-
// we could endup emiting this diagnostic duplicated.
1183-
if (cs.Options.contains(ConstraintSystemFlags::SubExpressionDiagnostics))
1184-
return nullptr;
1185-
1186-
auto *anchor = locator->getAnchor();
1187-
if (!isa<ObjectLiteralExpr>(anchor))
1188-
return nullptr;
1189-
1190-
// * The object literal has no contextual type
1191-
if (cs.getContextualType())
1192-
return nullptr;
1193-
1179+
SpecifyObjectLiteralTypeImport::create(ConstraintSystem &cs,
1180+
ConstraintLocator *locator) {
11941181
return new (cs.getAllocator()) SpecifyObjectLiteralTypeImport(cs, locator);
11951182
}

lib/Sema/CSFix.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,9 @@ enum class FixKind : uint8_t {
236236
/// inferred in current context e.g. because it's a multi-statement closure.
237237
SpecifyClosureReturnType,
238238

239-
/// Object literal type coudn't be infered because the module where it's
240-
/// protocol type was not imported.
239+
/// Object literal type coudn't be infered because the module where
240+
/// the default type that implements the associated literal protocol
241+
/// is declared was not imported.
241242
SpecifyObjectLiteralTypeImport,
242243

243244
};
@@ -1649,8 +1650,8 @@ class SpecifyObjectLiteralTypeImport final : public ConstraintFix {
16491650

16501651
bool diagnose(bool asNote = false) const;
16511652

1652-
static SpecifyObjectLiteralTypeImport *attempt(ConstraintSystem &cs,
1653-
ConstraintLocator *locator);
1653+
static SpecifyObjectLiteralTypeImport *create(ConstraintSystem &cs,
1654+
ConstraintLocator *locator);
16541655

16551656
};
16561657

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,8 @@ namespace {
12771277

12781278
auto tv = CS.createTypeVariable(exprLoc,
12791279
TVO_PrefersSubtypeBinding |
1280-
TVO_CanBindToNoEscape);
1280+
TVO_CanBindToNoEscape |
1281+
TVO_CanBindToHole);
12811282

12821283
CS.addConstraint(ConstraintKind::LiteralConformsTo, tv,
12831284
protocol->getDeclaredType(),

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,18 +1027,7 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
10271027
argsWithLabels, params, paramInfo, argInfo->HasTrailingClosure,
10281028
cs.shouldAttemptFixes(), listener, parameterBindings))
10291029
return cs.getTypeMatchFailure(locator);
1030-
1031-
if (!callee) {
1032-
// If we couldn't find a callee, diagnose and maybe suggest
1033-
// import a module.
1034-
if (cs.shouldAttemptFixes()) {
1035-
if (auto *fix = SpecifyObjectLiteralTypeImport::attempt(cs, loc)) {
1036-
if (cs.recordFix(fix))
1037-
return cs.getTypeMatchFailure(locator);
1038-
}
1039-
}
1040-
}
1041-
1030+
10421031
auto extraArguments = listener.getExtraneousArguments();
10431032
if (!extraArguments.empty()) {
10441033
if (RemoveExtraneousArguments::isMinMaxNameShadowing(cs, locator))
@@ -1058,7 +1047,6 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
10581047
return cs.getTypeMatchFailure(locator);
10591048
}
10601049
}
1061-
10621050
}
10631051

10641052
// If this application is part of an operator, then we allow an implicit

lib/Sema/ConstraintLocatorPathElts.def

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,11 @@ CUSTOM_LOCATOR_PATH_ELT(ProtocolRequirement)
135135

136136
/// Type parameter requirements.
137137
ABSTRACT_LOCATOR_PATH_ELT(AnyRequirement)
138+
/// The Nth conditional requirement in the parent locator's conformance.
139+
CUSTOM_LOCATOR_PATH_ELT(ConditionalRequirement)
138140

139-
/// The Nth conditional requirement in the parent locator's conformance.
140-
CUSTOM_LOCATOR_PATH_ELT(ConditionalRequirement)
141-
142-
/// A single requirement placed on the type parameters.
143-
CUSTOM_LOCATOR_PATH_ELT(TypeParameterRequirement)
141+
/// A single requirement placed on the type parameters.
142+
CUSTOM_LOCATOR_PATH_ELT(TypeParameterRequirement)
144143

145144
/// RValue adjustment.
146145
SIMPLE_LOCATOR_PATH_ELT(RValueAdjustment)
@@ -156,12 +155,11 @@ CUSTOM_LOCATOR_PATH_ELT(SynthesizedArgument)
156155

157156
/// Tuple elements.
158157
ABSTRACT_LOCATOR_PATH_ELT(AnyTupleElement)
158+
/// A tuple element referenced by position.
159+
CUSTOM_LOCATOR_PATH_ELT(TupleElement)
159160

160-
/// A tuple element referenced by position.
161-
CUSTOM_LOCATOR_PATH_ELT(TupleElement)
162-
163-
/// A tuple element referenced by name.
164-
CUSTOM_LOCATOR_PATH_ELT(NamedTupleElement)
161+
/// A tuple element referenced by name.
162+
CUSTOM_LOCATOR_PATH_ELT(NamedTupleElement)
165163

166164
/// An unresolved member.
167165
SIMPLE_LOCATOR_PATH_ELT(UnresolvedMember)

0 commit comments

Comments
 (0)