Skip to content

Commit d3eccc3

Browse files
committed
Ban @objc Methods that Introduce Constraints on Contextually Generic Methods
SE-0267 makes this legal in Swift, but these constraints are unrepresentable in Objective-C and often lead to blow-ups in SILGen when we construct invalid SIL signatures. rdar://71845752
1 parent 691ead3 commit d3eccc3

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Diff for: include/swift/AST/DiagnosticsSema.def

+3
Original file line numberDiff line numberDiff line change
@@ -4690,6 +4690,9 @@ ERROR(objc_invalid_on_static_subscript,none,
46904690
ERROR(objc_invalid_with_generic_params,none,
46914691
"%0 cannot be %" OBJC_ATTR_SELECT "1 because it has generic parameters",
46924692
(DescriptiveDeclKind, unsigned))
4693+
ERROR(objc_invalid_with_generic_requirements,none,
4694+
"%0 cannot be %" OBJC_ATTR_SELECT "1 because it has a 'where' clause",
4695+
(DescriptiveDeclKind, unsigned))
46934696
ERROR(objc_convention_invalid,none,
46944697
"%0 is not representable in Objective-C, so it cannot be used"
46954698
" with '@convention(%1)'", (Type, StringRef))

Diff for: lib/Sema/TypeCheckDeclObjC.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,17 @@ static bool checkObjCWithGenericParams(const ValueDecl *VD, ObjCReason Reason) {
327327
return true;
328328
}
329329

330+
if (GC->getTrailingWhereClause()) {
331+
// Diagnose this problem, if asked to.
332+
if (Diagnose) {
333+
VD->diagnose(diag::objc_invalid_with_generic_requirements,
334+
VD->getDescriptiveKind(), getObjCDiagnosticAttrKind(Reason));
335+
describeObjCReason(VD, Reason);
336+
}
337+
338+
return true;
339+
}
340+
330341
return false;
331342
}
332343

Diff for: test/attr/attr_objc.swift

+5
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ class GenericContext3<T> {
413413
}
414414
}
415415

416+
class GenericContext4<T> {
417+
@objc
418+
func foo() where T: Hashable { } // expected-error {{instance method cannot be marked @objc because it has a 'where' clause}}
419+
}
420+
416421
@objc // expected-error{{generic subclasses of '@objc' classes cannot have an explicit '@objc' because they are not directly visible from Objective-C}} {{1-7=}}
417422
class ConcreteSubclassOfGeneric : GenericContext3<Int> {}
418423

0 commit comments

Comments
 (0)