Skip to content

Commit eaabd24

Browse files
committed
[CSFix] Add a new fix to diagnose invalid static member refs on protocol metatype
1 parent 49cc219 commit eaabd24

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

include/swift/Sema/CSFix.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ enum class FixKind : uint8_t {
307307
/// convertible, but runtime does not support such convertions. e.g.
308308
/// function type casts.
309309
AllowUnsupportedRuntimeCheckedCast,
310+
311+
/// Allow reference to a static member on a protocol metatype
312+
/// even though result type of the reference doesn't conform
313+
/// to an expected protocol.
314+
AllowInvalidStaticMemberRefOnProtocolMetatype,
310315
};
311316

312317
class ConstraintFix {
@@ -2307,6 +2312,27 @@ class AllowUnsupportedRuntimeCheckedCast final
23072312
CheckedCastKind kind, ConstraintLocator *locator);
23082313
};
23092314

2315+
class AllowInvalidStaticMemberRefOnProtocolMetatype final
2316+
: public ConstraintFix {
2317+
Type BaseType;
2318+
2319+
AllowInvalidStaticMemberRefOnProtocolMetatype(ConstraintSystem &cs,
2320+
Type baseType,
2321+
ConstraintLocator *locator)
2322+
: ConstraintFix(cs,
2323+
FixKind::AllowInvalidStaticMemberRefOnProtocolMetatype,
2324+
locator),
2325+
BaseType(baseType) {}
2326+
2327+
public:
2328+
std::string getName() const override {
2329+
return "allow invalid static member reference on a protocol metatype";
2330+
}
2331+
2332+
static AllowInvalidStaticMemberRefOnProtocolMetatype *
2333+
create(ConstraintSystem &cs, Type baseType, ConstraintLocator *locator);
2334+
};
2335+
23102336
} // end namespace constraints
23112337
} // end namespace swift
23122338

lib/Sema/CSFix.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,3 +1938,23 @@ bool AllowUnsupportedRuntimeCheckedCast::diagnose(const Solution &solution,
19381938
solution, getFromType(), getToType(), CastKind, getLocator());
19391939
return failure.diagnose(asNote);
19401940
}
1941+
1942+
bool AllowInvalidStaticMemberRefOnProtocolMetatype::diagnose(
1943+
const Solution &solution, bool asNote) const {
1944+
auto *locator = getLocator();
1945+
auto overload = solution.getOverloadChoice(locator);
1946+
1947+
auto *member = overload.choice.getDeclOrNull();
1948+
assert(member);
1949+
1950+
AllowTypeOrInstanceMemberFailure failure(solution, BaseType, member,
1951+
member->createNameRef(), locator);
1952+
return failure.diagnose(asNote);
1953+
}
1954+
1955+
AllowInvalidStaticMemberRefOnProtocolMetatype *
1956+
AllowInvalidStaticMemberRefOnProtocolMetatype::create(
1957+
ConstraintSystem &cs, Type baseType, ConstraintLocator *locator) {
1958+
return new (cs.getAllocator())
1959+
AllowInvalidStaticMemberRefOnProtocolMetatype(cs, baseType, locator);
1960+
}

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10525,7 +10525,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1052510525
case FixKind::SpecifyBaseTypeForOptionalUnresolvedMember:
1052610526
case FixKind::AllowCheckedCastCoercibleOptionalType:
1052710527
case FixKind::AllowUnsupportedRuntimeCheckedCast:
10528-
case FixKind::AllowAlwaysSucceedCheckedCast: {
10528+
case FixKind::AllowAlwaysSucceedCheckedCast:
10529+
case FixKind::AllowInvalidStaticMemberRefOnProtocolMetatype: {
1052910530
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
1053010531
}
1053110532

0 commit comments

Comments
 (0)