Skip to content

Commit bdeaced

Browse files
committed
[CSFix] Convert force optional unwrap fix into a contextual one
1 parent fae3590 commit bdeaced

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

lib/Sema/CSFix.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,14 @@ ForceDowncast *ForceDowncast::create(ConstraintSystem &cs, Type fromType,
6969
}
7070

7171
bool ForceOptional::diagnose(bool asNote) const {
72-
MissingOptionalUnwrapFailure failure(getConstraintSystem(), BaseType,
73-
UnwrappedType, getLocator());
72+
MissingOptionalUnwrapFailure failure(getConstraintSystem(), getFromType(),
73+
getToType(), getLocator());
7474
return failure.diagnose(asNote);
7575
}
7676

77-
ForceOptional *ForceOptional::create(ConstraintSystem &cs, Type baseType,
78-
Type unwrappedType,
79-
ConstraintLocator *locator) {
80-
return new (cs.getAllocator())
81-
ForceOptional(cs, baseType, unwrappedType, locator);
77+
ForceOptional *ForceOptional::create(ConstraintSystem &cs, Type fromType,
78+
Type toType, ConstraintLocator *locator) {
79+
return new (cs.getAllocator()) ForceOptional(cs, fromType, toType, locator);
8280
}
8381

8482
bool UnwrapOptionalBase::diagnose(bool asNote) const {

lib/Sema/CSFix.h

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -278,28 +278,6 @@ class ConstraintFix {
278278
ConstraintSystem &getConstraintSystem() const { return CS; }
279279
};
280280

281-
/// Introduce a '!' to force an optional unwrap.
282-
class ForceOptional final : public ConstraintFix {
283-
Type BaseType;
284-
Type UnwrappedType;
285-
286-
ForceOptional(ConstraintSystem &cs, Type baseType, Type unwrappedType,
287-
ConstraintLocator *locator)
288-
: ConstraintFix(cs, FixKind::ForceOptional, locator), BaseType(baseType),
289-
UnwrappedType(unwrappedType) {
290-
assert(baseType && "Base type must not be null");
291-
assert(unwrappedType && "Unwrapped type must not be null");
292-
}
293-
294-
public:
295-
std::string getName() const override { return "force optional"; }
296-
297-
bool diagnose(bool asNote = false) const override;
298-
299-
static ForceOptional *create(ConstraintSystem &cs, Type baseType,
300-
Type unwrappedType, ConstraintLocator *locator);
301-
};
302-
303281
/// Unwrap an optional base when we have a member access.
304282
class UnwrapOptionalBase final : public ConstraintFix {
305283
DeclName MemberName;
@@ -514,6 +492,26 @@ class ContextualMismatch : public ConstraintFix {
514492
ConstraintLocator *locator);
515493
};
516494

495+
/// Introduce a '!' to force an optional unwrap.
496+
class ForceOptional final : public ContextualMismatch {
497+
ForceOptional(ConstraintSystem &cs, Type fromType, Type toType,
498+
ConstraintLocator *locator)
499+
: ContextualMismatch(cs, FixKind::ForceOptional, fromType, toType,
500+
locator) {
501+
assert(fromType && "Base type must not be null");
502+
assert(fromType->getOptionalObjectType() &&
503+
"Unwrapped type must not be null");
504+
}
505+
506+
public:
507+
std::string getName() const override { return "force optional"; }
508+
509+
bool diagnose(bool asNote = false) const override;
510+
511+
static ForceOptional *create(ConstraintSystem &cs, Type fromType, Type toType,
512+
ConstraintLocator *locator);
513+
};
514+
517515
/// This is a contextual mismatch between throwing and non-throwing
518516
/// function types, repair it by dropping `throws` attribute.
519517
class DropThrowsAttribute final : public ContextualMismatch {

0 commit comments

Comments
 (0)