Skip to content

Commit f53bcea

Browse files
committed
[CSFix] Add a tailored fix for invalid Swift -> C pointer conversions
This is a skeleton of a fix that would be used to diagnose situations when Swift -> C pointer conversion was attempted on a Swift function.
1 parent 5cb8604 commit f53bcea

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

include/swift/Sema/CSFix.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ enum class FixKind : uint8_t {
336336
/// resolved.
337337
SpecifyTypeForPlaceholder,
338338

339+
/// Allow Swift -> C pointer conversion in an argument position
340+
/// of a Swift function.
341+
AllowSwiftToCPointerConversion,
342+
339343
/// Allow `weak` declarations to be bound to a non-optional type.
340344
AllowNonOptionalWeak,
341345

@@ -2791,6 +2795,22 @@ class AllowNonOptionalWeak final : public ConstraintFix {
27912795
}
27922796
};
27932797

2798+
class AllowSwiftToCPointerConversion final : public ConstraintFix {
2799+
AllowSwiftToCPointerConversion(ConstraintSystem &cs,
2800+
ConstraintLocator *locator)
2801+
: ConstraintFix(cs, FixKind::AllowSwiftToCPointerConversion, locator) {}
2802+
2803+
public:
2804+
std::string getName() const override {
2805+
return "allow implicit Swift -> C pointer conversion";
2806+
}
2807+
2808+
bool diagnose(const Solution &solution, bool asNote = false) const override;
2809+
2810+
static AllowSwiftToCPointerConversion *create(ConstraintSystem &cs,
2811+
ConstraintLocator *locator);
2812+
};
2813+
27942814
} // end namespace constraints
27952815
} // end namespace swift
27962816

lib/Sema/CSFix.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,3 +2000,14 @@ AllowNonOptionalWeak *AllowNonOptionalWeak::create(ConstraintSystem &cs,
20002000
ConstraintLocator *locator) {
20012001
return new (cs.getAllocator()) AllowNonOptionalWeak(cs, locator);
20022002
}
2003+
2004+
bool AllowSwiftToCPointerConversion::diagnose(const Solution &solution,
2005+
bool asNote) const {
2006+
return false;
2007+
}
2008+
2009+
AllowSwiftToCPointerConversion *
2010+
AllowSwiftToCPointerConversion::create(ConstraintSystem &cs,
2011+
ConstraintLocator *locator) {
2012+
return new (cs.getAllocator()) AllowSwiftToCPointerConversion(cs, locator);
2013+
}

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11927,6 +11927,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1192711927
case FixKind::AddSendableAttribute:
1192811928
case FixKind::DropThrowsAttribute:
1192911929
case FixKind::DropAsyncAttribute:
11930+
case FixKind::AllowSwiftToCPointerConversion:
1193011931
llvm_unreachable("handled elsewhere");
1193111932
}
1193211933

0 commit comments

Comments
 (0)