Skip to content

Commit 4baf635

Browse files
committed
Mark NSObject/NSCoding/... as unsafe in argument position
NSObject and certain protocols do not provide enough information for us to automatically mark their methods as safe.
1 parent a60aa49 commit 4baf635

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

crates/header-translator/src/rust_type.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,9 @@ impl PointeeTy {
10651065
"must implement {}",
10661066
separate_with_comma_and(protocols.iter().map(|(p, _)| &p.id.name))
10671067
)));
1068+
} else if id.name == "NSObject" {
1069+
// `NSObject` has similar safety to `AnyObject`.
1070+
safety = TypeSafety::unknown_in_argument("should be of the correct type");
10681071
}
10691072

10701073
if id.name.contains("Mutable") {
@@ -1084,16 +1087,32 @@ impl PointeeTy {
10841087

10851088
safety
10861089
}
1087-
Self::AnyObject { protocols } => match protocols.len() {
1090+
Self::AnyObject { protocols } => match &**protocols {
10881091
// Make `AnyObject` conservatively disallowed as argument,
10891092
// see https://github.com/madsmtm/objc2/issues/562.
10901093
//
10911094
// Returning it is fine though, since if you actually
10921095
// want to do anything with the type, you have to downcast it,
10931096
// and `objc2` checks that at runtime.
1094-
0 => TypeSafety::unknown_in_argument("should be of the correct type"),
1095-
// `ProtocolObject<dyn MyProtocol>` is well-typed.
1096-
1 => TypeSafety::SAFE,
1097+
[] => TypeSafety::unknown_in_argument("should be of the correct type"),
1098+
// Certain protocols are not descriptive enough, and often
1099+
// essentially amount to `AnyObject`.
1100+
[(protocol, _)]
1101+
if matches!(
1102+
&*protocol.id.name,
1103+
"NSObjectProtocol"
1104+
| "NSCoding"
1105+
| "NSSecureCoding"
1106+
| "NSCopying"
1107+
| "NSMutableCopying"
1108+
| "NSFastEnumeration"
1109+
) =>
1110+
{
1111+
TypeSafety::unknown_in_argument("should be of the correct type")
1112+
}
1113+
// Other `ProtocolObject<dyn MyProtocol>`s are treated as
1114+
// proper types. (An example here is delegate protocols).
1115+
[_] => TypeSafety::SAFE,
10971116
// FIXME: Only a single protocol is properly supported,
10981117
// multiple protocol restrictions are currently `AnyObject`.
10991118
_ => TypeSafety::unknown_in_argument("should be of the correct type"),

0 commit comments

Comments
 (0)