Skip to content

Commit 8306724

Browse files
committed
[CSFix] Add a skeleton fix to require explicit existential coercion
1 parent 299d109 commit 8306724

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

include/swift/Sema/CSFix.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ enum class FixKind : uint8_t {
394394
/// Ignore a type mismatch while trying to infer generic parameter type
395395
/// from default expression.
396396
IgnoreDefaultExprTypeMismatch,
397+
398+
/// Coerce a result type of a call to a particular existential type
399+
/// by adding `as any <#Type#>`.
400+
AddExplicitExistentialCoercion,
397401
};
398402

399403
class ConstraintFix {
@@ -2981,6 +2985,26 @@ class IgnoreDefaultExprTypeMismatch : public AllowArgumentMismatch {
29812985
}
29822986
};
29832987

2988+
class AddExplicitExistentialCoercion final : public ConstraintFix {
2989+
Type ErasedResultType;
2990+
2991+
AddExplicitExistentialCoercion(ConstraintSystem &cs, Type erasedResultTy,
2992+
ConstraintLocator *locator)
2993+
: ConstraintFix(cs, FixKind::AddExplicitExistentialCoercion, locator),
2994+
ErasedResultType(erasedResultTy) {}
2995+
2996+
public:
2997+
std::string getName() const override {
2998+
return "add explicit existential type coercion";
2999+
}
3000+
3001+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3002+
3003+
static AddExplicitExistentialCoercion *create(ConstraintSystem &cs,
3004+
Type resultTy,
3005+
ConstraintLocator *locator);
3006+
};
3007+
29843008
} // end namespace constraints
29853009
} // end namespace swift
29863010

lib/Sema/CSFix.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,3 +2183,15 @@ IgnoreDefaultExprTypeMismatch::create(ConstraintSystem &cs, Type argType,
21832183
return new (cs.getAllocator())
21842184
IgnoreDefaultExprTypeMismatch(cs, argType, paramType, locator);
21852185
}
2186+
2187+
bool AddExplicitExistentialCoercion::diagnose(const Solution &solution,
2188+
bool asNote) const {
2189+
return false;
2190+
}
2191+
2192+
AddExplicitExistentialCoercion *
2193+
AddExplicitExistentialCoercion::create(ConstraintSystem &cs, Type resultTy,
2194+
ConstraintLocator *locator) {
2195+
return new (cs.getAllocator())
2196+
AddExplicitExistentialCoercion(cs, resultTy, locator);
2197+
}

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13078,6 +13078,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1307813078
case FixKind::DropAsyncAttribute:
1307913079
case FixKind::AllowSwiftToCPointerConversion:
1308013080
case FixKind::AllowTupleLabelMismatch:
13081+
case FixKind::AddExplicitExistentialCoercion:
1308113082
llvm_unreachable("handled elsewhere");
1308213083
}
1308313084

0 commit comments

Comments
 (0)