Skip to content

Commit 6faee94

Browse files
committed
Hackily fix missing availability annotation on new
1 parent 079308c commit 6faee94

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

crates/header-translator/src/availability.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,13 @@ impl Availability {
397397
}
398398
true
399399
}
400+
401+
pub fn method_update_new_from_init(&mut self, other: &Self) {
402+
if other.unavailable == Unavailable::ALL_UNAVAILABLE {
403+
self.unavailable = Unavailable::ALL_UNAVAILABLE;
404+
}
405+
// TODO: Other parameters?
406+
}
400407
}
401408

402409
impl fmt::Display for Availability {

crates/header-translator/src/global_analysis.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,51 @@ fn update_module(
217217

218218
module.stmts.extend(deprecated_fns);
219219

220+
// Propagate availability information of `init` to `new`.
221+
// NOTE: this only works within single files.
222+
let init_availability: BTreeMap<String, Availability> = module
223+
.stmts
224+
.iter()
225+
.filter_map(|stmt| match stmt {
226+
Stmt::ExternMethods {
227+
cls: id, methods, ..
228+
}
229+
| Stmt::ExternCategory {
230+
cls: id, methods, ..
231+
}
232+
| Stmt::ProtocolDecl { id, methods, .. } => Some(
233+
methods
234+
.iter()
235+
.filter(|method| method.selector == "init")
236+
.map(|method| (id.name.clone(), method.availability.clone())),
237+
),
238+
_ => None,
239+
})
240+
.flatten()
241+
.collect();
242+
for stmt in module.stmts.iter_mut() {
243+
if let Stmt::ExternMethods {
244+
cls: id, methods, ..
245+
}
246+
| Stmt::ExternCategory {
247+
cls: id, methods, ..
248+
}
249+
| Stmt::ProtocolDecl { id, methods, .. } = stmt
250+
{
251+
if let Some(init_availability) = init_availability.get(&id.name) {
252+
if let Some(method) = methods.iter_mut().find(|method| method.selector == "new") {
253+
method
254+
.availability
255+
.method_update_new_from_init(init_availability);
256+
// TODO(breaking): Remove unavailable instead of just marking them unsafe.
257+
if !method.availability.is_available() {
258+
method.safe = false;
259+
}
260+
}
261+
}
262+
}
263+
}
264+
220265
// disambiguate duplicate names
221266
// NOTE: this only works within single files
222267
let mut names = BTreeMap::<(String, String), &mut Method>::new();

crates/header-translator/src/method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ pub struct Method {
336336
memory_management: MemoryManagement,
337337
arguments: Vec<(String, Ty)>,
338338
result_type: Ty,
339-
safe: bool,
339+
pub safe: bool,
340340
is_pub: bool,
341341
// Thread-safe, even on main-thread only (@MainActor/@UIActor) classes
342342
non_isolated: bool,

framework-crates/objc2-foundation/src/tests/measurement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ use crate::{NSMeasurement, NSUnitMass, NSUnitPower};
44

55
#[test]
66
fn create() {
7-
let mass = NSMeasurement::<NSUnitMass>::new();
7+
let mass = unsafe { NSMeasurement::<NSUnitMass>::new() };
88
let _power = unsafe { mass.cast_unchecked::<NSUnitPower>() };
99
}

0 commit comments

Comments
 (0)