Skip to content

Commit aadbeb4

Browse files
committed
[Import as Member] Error on convenience inits in extensions of CFTypes
Swift does not currently support user-defined factory inits. With import as member, we're seeing many C functions now imported as initializers, which gives users the false hope that they can define their own factory inits as conveinence inits in extensions of CF types. We issue an explicit error now, rather than crashing later.
1 parent 726b2c7 commit aadbeb4

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,6 +2034,9 @@ ERROR(enumstruct_convenience_init,none,
20342034
ERROR(nonclass_convenience_init,none,
20352035
"convenience initializer not allowed in non-class type %0",
20362036
(Type))
2037+
ERROR(cfclass_convenience_init,none,
2038+
"convenience initializers are not supported in extensions of CF types",
2039+
())
20372040

20382041
ERROR(dynamic_construct_class,none,
20392042
"constructing an object of class type %0 with a metatype value must use "

lib/Sema/TypeCheckDecl.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6337,8 +6337,16 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
63376337
// extensions thereof.
63386338
if (CD->isConvenienceInit()) {
63396339
if (auto extType = CD->getExtensionType()) {
6340-
if (!extType->getClassOrBoundGenericClass() &&
6341-
!extType->is<ErrorType>()) {
6340+
auto extClass = extType->getClassOrBoundGenericClass();
6341+
6342+
// Forbid convenience inits on Foreign CF types, as Swift does not yet
6343+
// support user-defined factory inits.
6344+
if (extClass &&
6345+
extClass->getForeignClassKind() == ClassDecl::ForeignKind::CFType) {
6346+
TC.diagnose(CD->getLoc(), diag::cfclass_convenience_init);
6347+
}
6348+
6349+
if (!extClass && !extType->is<ErrorType>()) {
63426350
auto ConvenienceLoc =
63436351
CD->getAttrs().getAttribute<ConvenienceAttr>()->getLocation();
63446352

test/decl/init/cf-types.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-parse-verify-swift
2+
// REQUIRES: OS=macosx
3+
4+
import CoreGraphics
5+
6+
extension CGMutablePath {
7+
public convenience init(p: Bool) { // expected-error{{convenience initializers are not supported in extensions of CF types}}
8+
self.init()
9+
}
10+
public convenience init?(maybe: Bool) { // expected-error{{convenience initializers are not supported in extensions of CF types}}
11+
self.init()
12+
}
13+
14+
public convenience init(toss: Bool) throws { // expected-error{{convenience initializers are not supported in extensions of CF types}}
15+
self.init()
16+
}
17+
}
18+
19+
public func useInit() {
20+
let _ = CGMutablePath(p: true)
21+
let _ = CGMutablePath(maybe: true)
22+
let _ = try! CGMutablePath(toss: true)
23+
}

0 commit comments

Comments
 (0)