Skip to content

Commit 902fc7d

Browse files
committed
Properly propagate NS_UNAVAILABLE
This was not properly parsed, since `entity.get_availabilty()` returns the availability for the current target, and not the attribute if it's applied to the entire item. This allows us to more correctly mark methods as (un)safe.
1 parent 8e9a9fb commit 902fc7d

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

crates/header-translator/src/availability.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ use std::{
33
fmt::{self, Display},
44
};
55

6-
use clang::{Entity, PlatformAvailability, Version};
6+
use clang::{Entity, EntityKind, PlatformAvailability, Version};
77

8-
use crate::{context::Context, display_helper::FormatterFn};
8+
use crate::{
9+
context::Context, display_helper::FormatterFn, immediate_children,
10+
unexposed_attr::UnexposedAttr,
11+
};
912

1013
#[derive(Debug, Clone, PartialEq, Default)]
1114
struct Unavailable {
@@ -162,7 +165,7 @@ fn version_cmp(left: Version, right: Version) -> Ordering {
162165
}
163166

164167
impl Availability {
165-
pub fn parse(entity: &Entity<'_>, _context: &Context<'_>) -> Self {
168+
pub fn parse(entity: &Entity<'_>, context: &Context<'_>) -> Self {
166169
let availabilities = entity
167170
.get_platform_availability()
168171
.expect("platform availability");
@@ -250,10 +253,16 @@ impl Availability {
250253
}
251254
}
252255

256+
// This is an unconditional attribute that overrides the platform
257+
// availability for the current platform.
258+
//
259+
// Unfortunately, it is partially merged into the platform
260+
// availability above, so it's kinda hard to know when it applies.
253261
let current_target_availability = entity.get_availability();
254262
match current_target_availability {
255263
clang::Availability::Available => {}
256264
clang::Availability::Deprecated => {
265+
// TODO: Handle this better?
257266
if deprecated == Versions::default() {
258267
deprecated = Versions::MIN;
259268
}
@@ -262,12 +271,19 @@ impl Availability {
262271
error!(?entity, "cannot handle 'Inaccessible' availability")
263272
}
264273
clang::Availability::Unavailable => {
265-
if unavailable == Unavailable::default() {
266-
// TODO: Set per-target.
274+
// Handled below.
275+
}
276+
}
277+
278+
immediate_children(entity, |entity, _span| {
279+
if let EntityKind::UnexposedAttr = entity.get_kind() {
280+
if let Some(UnexposedAttr::FullyUnavailable) =
281+
UnexposedAttr::parse(&entity, context)
282+
{
267283
unavailable = Unavailable::ALL_UNAVAILABLE;
268284
}
269285
}
270-
}
286+
});
271287

272288
Self {
273289
unavailable,

crates/header-translator/src/method.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ impl MethodModifiers {
9090
UnexposedAttr::NoThrow => {
9191
// TODO: Use this somehow?
9292
}
93+
UnexposedAttr::FullyUnavailable => {
94+
// Handled in Availability::parse.
95+
}
9396
attr => error!(?attr, "unknown attribute on method"),
9497
}
9598
}

crates/header-translator/src/rust_type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,9 @@ impl Ty {
14081408
Some(UnexposedAttr::NoEscape) => {
14091409
no_escape = true;
14101410
}
1411+
Some(UnexposedAttr::FullyUnavailable) => {
1412+
// Irrelevant on types.
1413+
}
14111414
Some(attr) => error!(?attr, "unknown attribute on type"),
14121415
None => {}
14131416
}

crates/header-translator/src/unexposed_attr.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub enum UnexposedAttr {
3333

3434
NoEscape,
3535
NoThrow,
36+
37+
FullyUnavailable,
3638
}
3739

3840
impl UnexposedAttr {
@@ -153,6 +155,18 @@ impl UnexposedAttr {
153155
"NS_REQUIRES_PROPERTY_DEFINITIONS" => None,
154156
// Weak specifiers - would be interesting if Rust supported weak statics
155157
"GK_EXTERN_WEAK" | "MC_EXTERN_WEAK" | "weak_import" => None,
158+
// Some availability attributes are not properly exposed.
159+
"NS_UNAVAILABLE"
160+
| "UNAVAILABLE_ATTRIBUTE"
161+
| "DISPATCH_UNAVAILABLE"
162+
| "AV_INIT_UNAVAILABLE"
163+
| "AVKIT_INIT_UNAVAILABLE"
164+
| "MP_INIT_UNAVAILABLE"
165+
| "VS_INIT_UNAVAILABLE" => Some(Self::FullyUnavailable),
166+
// TODO: Add this to the above?
167+
"CF_AUTOMATED_REFCOUNT_UNAVAILABLE"
168+
| "NS_AUTOMATED_REFCOUNT_UNAVAILABLE"
169+
| "NS_AUTOMATED_REFCOUNT_WEAK_UNAVAILABLE" => None,
156170
// Availability attributes - their data is already exposed.
157171
"__API_AVAILABLE"
158172
| "__API_DEPRECATED"
@@ -333,14 +347,12 @@ impl UnexposedAttr {
333347
| "__WATCHOS_UNAVAILABLE"
334348
| "__UNAVAILABLE_PUBLIC_IOS"
335349
| "APPKIT_API_UNAVAILABLE_BEGIN_MACCATALYST"
336-
| "AVKIT_INIT_UNAVAILABLE"
337350
| "BROWSERENGINE_TEXTINPUT_AVAILABILITY"
338351
| "BROWSERENGINE_ACCESSIBILITY_AVAILABILITY"
339352
| "BROWSERENGINE_ACCESSIBILITY_MARKER_AVAILABILITY"
340353
| "BROWSERENGINE_ACCESSIBILITY_REMOTE_AVAILABILITY"
341354
| "CA_CANONICAL_DEPRECATED"
342355
| "CB_CM_API_AVAILABLE"
343-
| "CF_AUTOMATED_REFCOUNT_UNAVAILABLE"
344356
| "CG_OBSOLETE"
345357
| "CK_SHARE_ACCESS_REQUESTER_AVAILABILITY"
346358
| "CK_SHARE_BLOCKED_IDENTITY_AVAILABILITY"
@@ -353,7 +365,6 @@ impl UnexposedAttr {
353365
| "DEPRECATED_ATTRIBUTE"
354366
| "DEPRECATED_ATTRIBUTE_EXCLUDE_PUBLIC_IOS"
355367
| "DEPRECATED_MSG_ATTRIBUTE"
356-
| "DISPATCH_UNAVAILABLE"
357368
| "EN_API_AVAILABLE"
358369
| "EN_API_AVAILABLE_V2"
359370
| "EN_API_AVAILABLE_V3"
@@ -379,17 +390,11 @@ impl UnexposedAttr {
379390
| "MIDICI1_1"
380391
| "MIDICI1_2"
381392
| "MIDINETWORKSESSION_AVAILABLE"
382-
| "MP_INIT_UNAVAILABLE"
383-
| "NS_AUTOMATED_REFCOUNT_UNAVAILABLE"
384-
| "NS_AUTOMATED_REFCOUNT_WEAK_UNAVAILABLE"
385393
| "NS_CLASS_AVAILABLE"
386-
| "NS_UNAVAILABLE"
387-
| "UNAVAILABLE_ATTRIBUTE"
388394
| "UT_AVAILABLE_BEGIN"
389395
| "MP_DEPRECATED_BEGIN"
390396
| "SEC_ASN1_API_DEPRECATED"
391397
| "SECUREDOWNLOAD_API_DEPRECATED"
392-
| "VS_INIT_UNAVAILABLE"
393398
| "XCT_METRIC_API_AVAILABLE"
394399
| "XCUI_PROTECTED_RESOURCES_RESET_API_AVAILABLE" => None,
395400
s if s.starts_with("AVAILABLE_MAC_OS_X_VERSION_") => None,
@@ -435,8 +440,6 @@ impl UnexposedAttr {
435440
None
436441
}
437442
"objc_non_runtime_protocol" => None,
438-
// Emits unavailability attributes on `new` and `init` methods
439-
"AV_INIT_UNAVAILABLE" => None,
440443
// Helper used to easy declare @interface in CompositorServices.
441444
"CP_OBJECT_DECL" => {
442445
let _ = get_arguments();
@@ -446,6 +449,7 @@ impl UnexposedAttr {
446449
"XCT_WEAK_EXPORT" => None,
447450
// Irrelevant, we don't emit dispatch_object_t anyhow.
448451
"DISPATCH_TRANSPARENT_UNION" => None,
452+
"NS_NO_TAIL_CALL" => None,
449453
_ => return Err(()),
450454
})
451455
}

generated

Submodule generated updated 147 files

0 commit comments

Comments
 (0)