Skip to content

Commit ea1a46c

Browse files
committed
[ConstraintFix] Rather than creating a coalesced fix right before
diagnosing failures in applySolutionFixes, coalesce fixes and diagnose failures in one method on ConstraintFix. This eliminates the need for the `DefaultGenericArgument` fix (which was renamed from `ExplicitlySpecifyGenericArguments`) to have an array of missing parameters, which was only used when the fixes were coalesced. Instead, the coalesced arguments are used to create the `MissingGenericArgumentsFailure` diagnostic directly.
1 parent 212f202 commit ea1a46c

File tree

5 files changed

+48
-75
lines changed

5 files changed

+48
-75
lines changed

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7379,9 +7379,8 @@ bool ConstraintSystem::applySolutionFixes(Expr *E, const Solution &solution) {
73797379
auto *primaryFix = fixes[0];
73807380
ArrayRef<ConstraintFix *> secondaryFixes{fixes.begin() + 1, fixes.end()};
73817381

7382-
auto *coalescedFix = primaryFix->coalescedWith(secondaryFixes);
7383-
auto diagnosed = coalescedFix->diagnose();
7384-
if (coalescedFix->isWarning()) {
7382+
auto diagnosed = primaryFix->coalesceAndDiagnose(secondaryFixes);
7383+
if (primaryFix->isWarning()) {
73857384
assert(diagnosed && "warnings should always be diagnosed");
73867385
(void)diagnosed;
73877386
} else {

lib/Sema/CSBindings.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -991,11 +991,11 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
991991
cs.DefaultedConstraints.push_back(Binding.DefaultableBinding);
992992

993993
if (locator->isForGenericParameter() && type->isHole()) {
994-
// Drop `generic parameter '...'` part of the locator to group all of the
995-
// missing generic parameters related to the same path together.
994+
// Drop `generic parameter` locator element so that all missing
995+
// generic parameters related to the same path can be coalesced later.
996996
auto path = locator->getPath();
997997
auto genericParam = locator->getGenericParameter();
998-
auto *fix = ExplicitlySpecifyGenericArguments::create(cs, {genericParam},
998+
auto *fix = DefaultGenericArgument::create(cs, genericParam,
999999
cs.getConstraintLocator(locator->getAnchor(), path.drop_back()));
10001000
if (cs.recordFix(fix))
10011001
return true;

lib/Sema/CSFix.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -702,39 +702,28 @@ CollectionElementContextualMismatch::create(ConstraintSystem &cs, Type srcType,
702702
CollectionElementContextualMismatch(cs, srcType, dstType, locator);
703703
}
704704

705-
bool ExplicitlySpecifyGenericArguments::diagnose(bool asNote) const {
706-
auto &cs = getConstraintSystem();
707-
MissingGenericArgumentsFailure failure(cs, getParameters(),
708-
getLocator());
709-
return failure.diagnose(asNote);
710-
}
705+
bool DefaultGenericArgument::coalesceAndDiagnose(
706+
ArrayRef<ConstraintFix *> fixes, bool asNote) const {
707+
llvm::SmallVector<GenericTypeParamType *, 4> missingParams{Param};
711708

712-
ConstraintFix *
713-
ExplicitlySpecifyGenericArguments::coalescedWith(ArrayRef<ConstraintFix *> fixes) {
714-
if (fixes.empty())
715-
return this;
716-
717-
auto params = getParameters();
718-
llvm::SmallVector<GenericTypeParamType *, 4> missingParams{params.begin(),
719-
params.end()};
720709
for (auto *otherFix : fixes) {
721-
if (auto *fix = otherFix->getAs<ExplicitlySpecifyGenericArguments>()) {
722-
auto additionalParams = fix->getParameters();
723-
missingParams.append(additionalParams.begin(), additionalParams.end());
724-
}
710+
if (auto *fix = otherFix->getAs<DefaultGenericArgument>())
711+
missingParams.push_back(fix->Param);
725712
}
726713

727-
return ExplicitlySpecifyGenericArguments::create(getConstraintSystem(),
728-
missingParams, getLocator());
714+
auto &cs = getConstraintSystem();
715+
MissingGenericArgumentsFailure failure(cs, missingParams, getLocator());
716+
return failure.diagnose(asNote);
729717
}
730718

731-
ExplicitlySpecifyGenericArguments *ExplicitlySpecifyGenericArguments::create(
732-
ConstraintSystem &cs, ArrayRef<GenericTypeParamType *> params,
733-
ConstraintLocator *locator) {
734-
unsigned size = totalSizeToAlloc<GenericTypeParamType *>(params.size());
735-
void *mem = cs.getAllocator().Allocate(
736-
size, alignof(ExplicitlySpecifyGenericArguments));
737-
return new (mem) ExplicitlySpecifyGenericArguments(cs, params, locator);
719+
bool DefaultGenericArgument::diagnose(bool asNote) const {
720+
return coalesceAndDiagnose({}, asNote);
721+
}
722+
723+
DefaultGenericArgument *
724+
DefaultGenericArgument::create(ConstraintSystem &cs, GenericTypeParamType *param,
725+
ConstraintLocator *locator) {
726+
return new (cs.getAllocator()) DefaultGenericArgument(cs, param, locator);
738727
}
739728

740729
SkipUnhandledConstructInFunctionBuilder *

lib/Sema/CSFix.h

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,8 @@ enum class FixKind : uint8_t {
183183
/// function to `Void` to conform to expected result type.
184184
RemoveReturn,
185185

186-
/// Generic parameters could not be inferred and have to be explicitly
187-
/// specified in the source. This fix groups all of the missing arguments
188-
/// associated with single declaration.
189-
ExplicitlySpecifyGenericArguments,
186+
/// Default ambiguous generic arguments to \c Any
187+
DefaultGenericArgument,
190188

191189
/// Skip any unhandled constructs that occur within a closure argument that
192190
/// matches up with a
@@ -254,14 +252,18 @@ class ConstraintFix {
254252

255253
virtual std::string getName() const = 0;
256254

257-
/// Diagnose a failure associated with this fix given
258-
/// root expression and information from constraint system.
259-
virtual bool diagnose(bool asNote = false) const = 0;
260-
261-
virtual ConstraintFix *coalescedWith(ArrayRef<ConstraintFix *> fixes) {
262-
return this;
255+
/// Coalesce this fix with the given secondary fixes and diagnose the failure.
256+
///
257+
/// The default implementation ignores \c secondaryFixes and calls
258+
/// \c diagnose.
259+
virtual bool coalesceAndDiagnose(ArrayRef<ConstraintFix *> secondaryFixes,
260+
bool asNote = false) const {
261+
return diagnose(asNote);
263262
}
264263

264+
/// Diagnose a failure associated with this fix.
265+
virtual bool diagnose(bool asNote = false) const = 0;
266+
265267
void print(llvm::raw_ostream &Out) const;
266268

267269
SWIFT_DEBUG_DUMP;
@@ -1251,49 +1253,32 @@ class CollectionElementContextualMismatch final : public ContextualMismatch {
12511253
ConstraintLocator *locator);
12521254
};
12531255

1254-
class ExplicitlySpecifyGenericArguments final
1255-
: public ConstraintFix,
1256-
private llvm::TrailingObjects<ExplicitlySpecifyGenericArguments,
1257-
GenericTypeParamType *> {
1258-
friend TrailingObjects;
1256+
class DefaultGenericArgument final : public ConstraintFix {
1257+
GenericTypeParamType *Param;
12591258

1260-
unsigned NumMissingParams;
1261-
1262-
ExplicitlySpecifyGenericArguments(ConstraintSystem &cs,
1263-
ArrayRef<GenericTypeParamType *> params,
1264-
ConstraintLocator *locator)
1265-
: ConstraintFix(cs, FixKind::ExplicitlySpecifyGenericArguments, locator),
1266-
NumMissingParams(params.size()) {
1267-
assert(!params.empty());
1268-
std::uninitialized_copy(params.begin(), params.end(),
1269-
getParametersBuf().begin());
1270-
}
1259+
DefaultGenericArgument(ConstraintSystem &cs, GenericTypeParamType *param,
1260+
ConstraintLocator *locator)
1261+
: ConstraintFix(cs, FixKind::DefaultGenericArgument, locator),
1262+
Param(param) {}
12711263

12721264
public:
12731265
static bool classof(const ConstraintFix *fix) {
1274-
return fix->getKind() == FixKind::ExplicitlySpecifyGenericArguments;
1266+
return fix->getKind() == FixKind::DefaultGenericArgument;
12751267
}
12761268

12771269
std::string getName() const override {
1278-
return "default missing generic arguments to `Any`";
1270+
auto paramName = Param->getString();
1271+
return "default generic argument '" + paramName + "' to 'Any'";
12791272
}
12801273

1281-
ArrayRef<GenericTypeParamType *> getParameters() const {
1282-
return {getTrailingObjects<GenericTypeParamType *>(), NumMissingParams};
1283-
}
1274+
bool coalesceAndDiagnose(ArrayRef<ConstraintFix *> secondaryFixes,
1275+
bool asNote = false) const override;
12841276

12851277
bool diagnose(bool asNote = false) const override;
12861278

1287-
ConstraintFix *coalescedWith(ArrayRef<ConstraintFix *> fixes) override;
1288-
1289-
static ExplicitlySpecifyGenericArguments *
1290-
create(ConstraintSystem &cs, ArrayRef<GenericTypeParamType *> params,
1291-
ConstraintLocator *locator);
1292-
1293-
private:
1294-
MutableArrayRef<GenericTypeParamType *> getParametersBuf() {
1295-
return {getTrailingObjects<GenericTypeParamType *>(), NumMissingParams};
1296-
}
1279+
static DefaultGenericArgument *create(ConstraintSystem &cs,
1280+
GenericTypeParamType *param,
1281+
ConstraintLocator *locator);
12971282
};
12981283

12991284
class SkipUnhandledConstructInFunctionBuilder final : public ConstraintFix {

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8390,7 +8390,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
83908390
case FixKind::AllowAnyObjectKeyPathRoot:
83918391
case FixKind::TreatKeyPathSubscriptIndexAsHashable:
83928392
case FixKind::AllowInvalidRefInKeyPath:
8393-
case FixKind::ExplicitlySpecifyGenericArguments:
8393+
case FixKind::DefaultGenericArgument:
83948394
case FixKind::GenericArgumentsMismatch:
83958395
case FixKind::AllowMutatingMemberOnRValueBase:
83968396
case FixKind::AllowTupleSplatForSingleParameter:

0 commit comments

Comments
 (0)