Skip to content

Commit 9fa77b3

Browse files
authored
Merge pull request #508 from madsmtm/fix-nsobjectprotocol
Fix protocol names
2 parents ed7eb95 + 1ad018a commit 9fa77b3

File tree

17 files changed

+165
-89
lines changed

17 files changed

+165
-89
lines changed

crates/header-translator/src/stmt.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ pub enum Stmt {
369369
/// extern_protocol!
370370
ProtocolDecl {
371371
id: ItemIdentifier,
372+
actual_name: Option<String>,
372373
availability: Availability,
373374
protocols: BTreeSet<ItemIdentifier>,
374375
methods: Vec<Method>,
@@ -797,9 +798,14 @@ impl Stmt {
797798
.collect()
798799
}
799800
EntityKind::ObjCProtocolDecl => {
800-
let id = ItemIdentifier::new(entity, context)
801-
.map_name(|name| context.replace_protocol_name(name));
802-
let data = context.protocol_data.get(&id.name);
801+
let actual_id = ItemIdentifier::new(entity, context);
802+
let data = context.protocol_data.get(&actual_id.name);
803+
let actual_name = data
804+
.map(|data| data.renamed.is_some())
805+
.unwrap_or_default()
806+
.then(|| actual_id.name.clone());
807+
808+
let id = actual_id.map_name(|name| context.replace_protocol_name(name));
803809

804810
if data.map(|data| data.skipped).unwrap_or_default() {
805811
return vec![];
@@ -830,6 +836,7 @@ impl Stmt {
830836

831837
vec![Self::ProtocolDecl {
832838
id,
839+
actual_name,
833840
availability,
834841
protocols,
835842
methods,
@@ -1616,6 +1623,7 @@ impl fmt::Display for Stmt {
16161623
}
16171624
Self::ProtocolDecl {
16181625
id,
1626+
actual_name,
16191627
availability,
16201628
protocols,
16211629
methods,
@@ -1683,7 +1691,13 @@ impl fmt::Display for Stmt {
16831691
}
16841692
writeln!(f, " }}")?;
16851693
writeln!(f)?;
1686-
writeln!(f, " unsafe impl ProtocolType for dyn {} {{}}", id.name)?;
1694+
writeln!(f, " unsafe impl ProtocolType for dyn {} {{", id.name)?;
1695+
if let Some(actual_name) = actual_name {
1696+
writeln!(f)?;
1697+
writeln!(f, " const NAME: &'static str = {actual_name:?};")?;
1698+
write!(f, " ")?;
1699+
}
1700+
writeln!(f, "}}")?;
16871701
writeln!(f, ");")?;
16881702
}
16891703
Self::StructDecl {

crates/icrate/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6565
to use in such hashing collections.
6666
* **BREAKING**: Added `HasStableHash` requirement on `NSDictionary` and
6767
`NSSet` creation methods, fixing a long-standing soundess issue.
68+
* Fixed the protocol names of `NSAccessibilityElementProtocol`,
69+
`NSTextAttachmentCellProtocol` and `NSFileProviderItemProtocol`.
6870

6971

7072
## icrate 0.0.4 - 2023-07-31

crates/icrate/examples/browser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ declare_class!(
7373
}
7474
}
7575

76+
unsafe impl NSObjectProtocol for Delegate {}
77+
7678
unsafe impl NSApplicationDelegate for Delegate {
7779
#[method(applicationDidFinishLaunching:)]
7880
#[allow(non_snake_case)]
@@ -293,8 +295,6 @@ impl Delegate {
293295
}
294296
}
295297

296-
unsafe impl NSObjectProtocol for Delegate {}
297-
298298
fn main() {
299299
let mtm = MainThreadMarker::new().unwrap();
300300
let app = NSApplication::sharedApplication(mtm);

crates/icrate/examples/delegate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ declare_class!(
5050
}
5151
}
5252

53+
unsafe impl NSObjectProtocol for AppDelegate {}
54+
5355
unsafe impl NSApplicationDelegate for AppDelegate {
5456
#[method(applicationDidFinishLaunching:)]
5557
fn did_finish_launching(&self, notification: &NSNotification) {
@@ -65,8 +67,6 @@ declare_class!(
6567
}
6668
);
6769

68-
unsafe impl NSObjectProtocol for AppDelegate {}
69-
7070
impl AppDelegate {
7171
pub fn new(ivar: u8, another_ivar: bool, mtm: MainThreadMarker) -> Id<Self> {
7272
unsafe { msg_send_id![mtm.alloc(), initWith: ivar, another: another_ivar] }

crates/icrate/examples/metal.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,21 @@ use objc2::{
3131
#[rustfmt::skip]
3232
const SHADERS: &str = r#"
3333
#include <metal_stdlib>
34-
34+
3535
struct SceneProperties {
3636
float time;
37-
};
38-
37+
};
38+
3939
struct VertexInput {
4040
metal::packed_float3 position;
4141
metal::packed_float3 color;
4242
};
43-
43+
4444
struct VertexOutput {
4545
metal::float4 position [[position]];
4646
metal::float4 color;
4747
};
48-
48+
4949
vertex VertexOutput vertex_main(
5050
device const SceneProperties& properties [[buffer(0)]],
5151
device const VertexInput* vertices [[buffer(1)]],
@@ -64,7 +64,7 @@ const SHADERS: &str = r#"
6464
out.color = metal::float4(in.color, 1);
6565
return out;
6666
}
67-
67+
6868
fragment metal::float4 fragment_main(VertexOutput in [[stage_in]]) {
6969
return in.color;
7070
}
@@ -155,6 +155,8 @@ declare_class!(
155155
}
156156
}
157157

158+
unsafe impl NSObjectProtocol for Delegate {}
159+
158160
// define the delegate methods for the `NSApplicationDelegate` protocol
159161
unsafe impl NSApplicationDelegate for Delegate {
160162
#[method(applicationDidFinishLaunching:)]
@@ -356,8 +358,6 @@ declare_class!(
356358
}
357359
);
358360

359-
unsafe impl NSObjectProtocol for Delegate {}
360-
361361
impl Delegate {
362362
pub fn new(mtm: MainThreadMarker) -> Id<Self> {
363363
unsafe { msg_send_id![mtm.alloc(), init] }

crates/icrate/src/fixes/Foundation/copying.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ extern_protocol!(
4141
Self: CounterpartOrSelf;
4242
}
4343

44-
unsafe impl ProtocolType for dyn NSCopying {
45-
const NAME: &'static str = "NSCopying";
46-
}
44+
unsafe impl ProtocolType for dyn NSCopying {}
4745
);
4846

4947
// FIXME: Remove this hack which makes NSMutableDictionary tests work
@@ -86,7 +84,5 @@ extern_protocol!(
8684
Self: CounterpartOrSelf;
8785
}
8886

89-
unsafe impl ProtocolType for dyn NSMutableCopying {
90-
const NAME: &'static str = "NSMutableCopying";
91-
}
87+
unsafe impl ProtocolType for dyn NSMutableCopying {}
9288
);

crates/icrate/src/generated

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![cfg(feature = "AppKit")]
2+
use icrate::AppKit::NSAccessibilityElementProtocol;
3+
use objc2::ProtocolType;
4+
5+
#[test]
6+
fn accessibility_element_protocol() {
7+
let actual: &str = <dyn NSAccessibilityElementProtocol>::NAME;
8+
assert_eq!(actual, "NSAccessibilityElement");
9+
}

crates/objc2/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4343
];
4444
```
4545

46+
### Fixed
47+
* Fixed the name of the protocol that `NSObjectProtocol` references.
48+
49+
### Removed
50+
* **BREAKING**: Removed `ProtocolType` implementation for `NSObject`.
51+
Use the more precise `NSObjectProtocol` trait instead!
52+
4653

4754
## 0.4.1 - 2023-07-31
4855

crates/objc2/src/__macro_helpers/mod.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -580,32 +580,56 @@ impl ClassProtocolMethodsBuilder<'_, '_> {
580580
}
581581
}
582582

583-
#[inline]
583+
#[cfg(all(debug_assertions, feature = "verify"))]
584584
pub fn __finish(self) {
585-
#[cfg(all(debug_assertions, feature = "verify"))]
585+
let superclass = self.builder.superclass();
586+
586587
if let Some(protocol) = self.protocol {
587588
for desc in &self.required_instance_methods {
588-
if !self.registered_instance_methods.contains(&desc.sel) {
589-
panic!(
590-
"must implement required protocol method -[{protocol} {}]",
591-
desc.sel
592-
)
589+
if self.registered_instance_methods.contains(&desc.sel) {
590+
continue;
591+
}
592+
593+
// TODO: Don't do this when `NS_PROTOCOL_REQUIRES_EXPLICIT_IMPLEMENTATION`
594+
if superclass
595+
.and_then(|superclass| superclass.instance_method(desc.sel))
596+
.is_some()
597+
{
598+
continue;
593599
}
600+
601+
panic!(
602+
"must implement required protocol method -[{protocol} {}]",
603+
desc.sel
604+
)
594605
}
595606
}
596607

597-
#[cfg(all(debug_assertions, feature = "verify"))]
598608
if let Some(protocol) = self.protocol {
599609
for desc in &self.required_class_methods {
600-
if !self.registered_class_methods.contains(&desc.sel) {
601-
panic!(
602-
"must implement required protocol method +[{protocol} {}]",
603-
desc.sel
604-
)
610+
if self.registered_class_methods.contains(&desc.sel) {
611+
continue;
612+
}
613+
614+
// TODO: Don't do this when `NS_PROTOCOL_REQUIRES_EXPLICIT_IMPLEMENTATION`
615+
if superclass
616+
.and_then(|superclass| superclass.class_method(desc.sel))
617+
.is_some()
618+
{
619+
continue;
605620
}
621+
622+
panic!(
623+
"must implement required protocol method +[{protocol} {}]",
624+
desc.sel
625+
);
606626
}
607627
}
608628
}
629+
630+
#[inline]
631+
#[cfg(not(all(debug_assertions, feature = "verify")))]
632+
pub fn __finish(self) {}
609633
}
610634

611635
#[cfg(test)]

0 commit comments

Comments
 (0)