Skip to content

Commit 7853457

Browse files
committed
Remove header-translator's ability to emit mutable classes/methods
Part of #563
1 parent 4a3d690 commit 7853457

File tree

6 files changed

+14
-103
lines changed

6 files changed

+14
-103
lines changed

crates/header-translator/src/config.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ pub struct MethodData {
245245
pub unsafe_: bool,
246246
#[serde(default = "skipped_default")]
247247
pub skipped: bool,
248-
pub mutating: Option<bool>,
249248
}
250249

251250
impl MethodData {
@@ -254,7 +253,6 @@ impl MethodData {
254253
// Only use `unsafe` from itself, never take if from the superclass
255254
unsafe_: self.unsafe_,
256255
skipped: self.skipped | superclass.skipped,
257-
mutating: self.mutating.or(superclass.mutating),
258256
}
259257
}
260258
}
@@ -295,7 +293,6 @@ impl Default for MethodData {
295293
Self {
296294
unsafe_: unsafe_default(),
297295
skipped: skipped_default(),
298-
mutating: None,
299296
}
300297
}
301298
}
@@ -348,24 +345,6 @@ impl<'de> Deserialize<'de> for Mutability {
348345
where
349346
E: de::Error,
350347
{
351-
if let Some(value) = value.strip_prefix("ImmutableWithMutableSubclass(") {
352-
let value = value
353-
.strip_suffix(')')
354-
.ok_or(de::Error::custom("end parenthesis"))?;
355-
let item =
356-
parse_itemidentifier(value).ok_or(de::Error::custom("requires ::"))?;
357-
return Ok(Mutability::ImmutableWithMutableSubclass(item));
358-
}
359-
360-
if let Some(value) = value.strip_prefix("MutableWithImmutableSuperclass(") {
361-
let value = value
362-
.strip_suffix(')')
363-
.ok_or(de::Error::custom("end parenthesis"))?;
364-
let item =
365-
parse_itemidentifier(value).ok_or(de::Error::custom("requires ::"))?;
366-
return Ok(Mutability::MutableWithImmutableSuperclass(item));
367-
}
368-
369348
if let Some(value) = value.strip_prefix("InteriorMutableWithSubclass(") {
370349
let value = value
371350
.strip_suffix(')')
@@ -385,8 +364,6 @@ impl<'de> Deserialize<'de> for Mutability {
385364
}
386365

387366
match value {
388-
"Immutable" => Ok(Mutability::Immutable),
389-
"Mutable" => Ok(Mutability::Mutable),
390367
"InteriorMutable" => Ok(Mutability::InteriorMutable),
391368
"MainThreadOnly" => Ok(Mutability::MainThreadOnly),
392369
value => Err(de::Error::custom(format!("unknown variant {value:?}"))),

crates/header-translator/src/method.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ pub struct Method {
254254
result_type: Ty,
255255
is_error: bool,
256256
safe: bool,
257-
mutating: bool,
258257
is_pub: bool,
259258
// Thread-safe, even on main-thread only (@MainActor/@UIActor) classes
260259
non_isolated: bool,
@@ -354,7 +353,6 @@ impl Method {
354353
pub(crate) fn parse_method(
355354
entity: Entity<'_>,
356355
data: MethodData,
357-
parent_is_mutable: bool,
358356
parent_is_mainthreadonly: bool,
359357
is_pub: bool,
360358
context: &Context<'_>,
@@ -504,10 +502,6 @@ impl Method {
504502
result_type,
505503
is_error,
506504
safe: !data.unsafe_,
507-
// Mutable if the parent is mutable is a reasonable default,
508-
// since immutable methods are usually either declared on an
509-
// immutable subclass, or as a property.
510-
mutating: data.mutating.unwrap_or(parent_is_mutable),
511505
is_pub,
512506
non_isolated: modifiers.non_isolated,
513507
mainthreadonly,
@@ -520,7 +514,6 @@ impl Method {
520514
property: PartialProperty<'_>,
521515
getter_data: MethodData,
522516
setter_data: Option<MethodData>,
523-
parent_is_mutable: bool,
524517
parent_is_mainthreadonly: bool,
525518
is_pub: bool,
526519
context: &Context<'_>,
@@ -581,9 +574,6 @@ impl Method {
581574
result_type: ty,
582575
is_error: false,
583576
safe: !getter_data.unsafe_,
584-
// Getters are usually not mutable, even if the class itself
585-
// is, so let's default to immutable.
586-
mutating: getter_data.mutating.unwrap_or(false),
587577
is_pub,
588578
non_isolated: modifiers.non_isolated,
589579
mainthreadonly,
@@ -626,8 +616,6 @@ impl Method {
626616
result_type,
627617
is_error: false,
628618
safe: !setter_data.unsafe_,
629-
// Setters are usually mutable if the class itself is.
630-
mutating: setter_data.mutating.unwrap_or(parent_is_mutable),
631619
is_pub,
632620
non_isolated: modifiers.non_isolated,
633621
mainthreadonly,
@@ -722,8 +710,6 @@ impl fmt::Display for Method {
722710
write!(f, "this: Allocated<Self>, ")?;
723711
} else if self.is_class {
724712
// Insert nothing; a class method is assumed
725-
} else if self.mutating {
726-
write!(f, "&mut self, ")?;
727713
} else {
728714
write!(f, "&self, ")?;
729715
}

crates/header-translator/src/stmt.rs

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ pub(crate) fn method_or_property_entities<'tu>(
217217
fn parse_methods(
218218
entity: &Entity<'_>,
219219
get_data: impl Fn(&str) -> MethodData,
220-
is_mutable: bool,
221220
thread_safety: &ThreadSafety,
222221
is_pub: bool,
223222
context: &Context<'_>,
@@ -235,7 +234,6 @@ fn parse_methods(
235234
if let Some((designated_initializer, method)) = Method::parse_method(
236235
entity,
237236
data,
238-
is_mutable,
239237
thread_safety.inferred_mainthreadonly(),
240238
is_pub,
241239
context,
@@ -262,7 +260,6 @@ fn parse_methods(
262260
partial,
263261
getter_data,
264262
setter_data,
265-
is_mutable,
266263
thread_safety.inferred_mainthreadonly(),
267264
is_pub,
268265
context,
@@ -298,10 +295,8 @@ pub(crate) fn items_required_by_decl(
298295
for (superclass, _, _) in parse_superclasses(entity, context) {
299296
items.push(superclass);
300297
}
301-
if let Some(
302-
Mutability::ImmutableWithMutableSubclass(subclass)
303-
| Mutability::InteriorMutableWithSubclass(subclass),
304-
) = data.map(|data| &data.mutability)
298+
if let Some(Mutability::InteriorMutableWithSubclass(subclass)) =
299+
data.map(|data| &data.mutability)
305300
{
306301
items.push(subclass.clone());
307302
}
@@ -388,10 +383,6 @@ fn verify_objc_decl(entity: &Entity<'_>, _context: &Context<'_>) {
388383

389384
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
390385
pub enum Mutability {
391-
Immutable,
392-
Mutable,
393-
ImmutableWithMutableSubclass(ItemIdentifier),
394-
MutableWithImmutableSuperclass(ItemIdentifier),
395386
#[default]
396387
InteriorMutable,
397388
InteriorMutableWithSubclass(ItemIdentifier),
@@ -400,33 +391,8 @@ pub enum Mutability {
400391
}
401392

402393
impl Mutability {
403-
pub fn is_mutable(&self) -> bool {
404-
matches!(
405-
self,
406-
Mutability::Mutable | Mutability::MutableWithImmutableSuperclass(_)
407-
)
408-
}
409-
410394
fn display<'a>(&'a self, other_generics: impl Display + 'a) -> impl Display + 'a {
411395
FormatterFn(move |f| match self {
412-
Self::Immutable => write!(f, "Immutable"),
413-
Self::Mutable => write!(f, "Mutable"),
414-
Self::ImmutableWithMutableSubclass(subclass) => {
415-
write!(
416-
f,
417-
"ImmutableWithMutableSubclass<{}{}>",
418-
subclass.path(),
419-
other_generics
420-
)
421-
}
422-
Self::MutableWithImmutableSuperclass(superclass) => {
423-
write!(
424-
f,
425-
"MutableWithImmutableSuperclass<{}{}>",
426-
superclass.path(),
427-
other_generics
428-
)
429-
}
430396
Self::InteriorMutable => write!(f, "InteriorMutable"),
431397
Self::InteriorMutableWithSubclass(subclass) => {
432398
write!(
@@ -679,8 +645,6 @@ impl Stmt {
679645
let (methods, designated_initializers) = parse_methods(
680646
entity,
681647
|name| ClassData::get_method_data(data, name),
682-
data.map(|data| data.mutability.is_mutable())
683-
.unwrap_or(false),
684648
&thread_safety,
685649
true,
686650
context,
@@ -724,9 +688,6 @@ impl Stmt {
724688
ClassData::get_method_data(superclass_data, name);
725689
data.merge_with_superclass(superclass_data)
726690
},
727-
data.map(|data| data.mutability.is_mutable())
728-
.or(superclass_data.map(|data| data.mutability.is_mutable()))
729-
.unwrap_or(false),
730691
&thread_safety,
731692
true,
732693
context,
@@ -876,8 +837,6 @@ impl Stmt {
876837
let (methods, designated_initializers) = parse_methods(
877838
entity,
878839
|name| ClassData::get_method_data(data, name),
879-
data.map(|data| data.mutability.is_mutable())
880-
.unwrap_or(false),
881840
&cls_thread_safety,
882841
true,
883842
context,
@@ -890,8 +849,7 @@ impl Stmt {
890849
);
891850
}
892851

893-
let extra_methods = if let Mutability::ImmutableWithMutableSubclass(subclass)
894-
| Mutability::InteriorMutableWithSubclass(subclass) =
852+
let extra_methods = if let Mutability::InteriorMutableWithSubclass(subclass) =
895853
data.map(|data| data.mutability.clone()).unwrap_or_default()
896854
{
897855
let subclass_data = context
@@ -907,9 +865,6 @@ impl Stmt {
907865
let subclass_data = ClassData::get_method_data(subclass_data, name);
908866
subclass_data.merge_with_superclass(data)
909867
},
910-
data.map(|data| data.mutability.is_mutable())
911-
.or(subclass_data.map(|data| data.mutability.is_mutable()))
912-
.unwrap_or(false),
913868
&cls_thread_safety,
914869
true,
915870
context,
@@ -982,7 +937,6 @@ impl Stmt {
982937
let (methods, designated_initializers) = parse_methods(
983938
entity,
984939
|name| ClassData::get_method_data(data, name),
985-
false,
986940
&cls_thread_safety,
987941
false,
988942
context,
@@ -1057,7 +1011,6 @@ impl Stmt {
10571011
.copied()
10581012
.unwrap_or_default()
10591013
},
1060-
false,
10611014
&thread_safety,
10621015
false,
10631016
context,

framework-crates/objc2-app-kit/translation-config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class.NSView.mutability = "MainThreadOnly"
199199
# in that case, they can't be!
200200
class.NSWindow.mutability = "MainThreadOnly"
201201

202-
# TODO: This should be one of MainThreadOnly or Immutable (+Send/Sync)
202+
# TODO: This should probably be MainThreadOnly, or maybe +Send/Sync?
203203
# class.NSAppearance.mutability = "MainThreadOnly"
204204

205205
# Documented Thread-Unsafe, but:

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ fn assert_auto_traits<T: Send + Sync + UnwindSafe + RefUnwindSafe>() {
3030
}
3131

3232
declare_class!(
33-
struct ImmutableSendSyncObject;
33+
struct SendSyncObject;
3434

35-
unsafe impl ClassType for ImmutableSendSyncObject {
35+
unsafe impl ClassType for SendSyncObject {
3636
type Super = NSObject;
37-
type Mutability = mutability::Immutable;
38-
const NAME: &'static str = "ImmutableSendSyncObject";
37+
type Mutability = mutability::InteriorMutable;
38+
const NAME: &'static str = "SendSyncObject";
3939
}
4040

41-
impl DeclaredClass for ImmutableSendSyncObject {}
41+
impl DeclaredClass for SendSyncObject {}
4242
);
4343

44-
unsafe impl Send for ImmutableSendSyncObject {}
45-
unsafe impl Sync for ImmutableSendSyncObject {}
44+
unsafe impl Send for SendSyncObject {}
45+
unsafe impl Sync for SendSyncObject {}
4646

4747
#[test]
4848
fn test_generic_auto_traits() {
@@ -60,9 +60,9 @@ fn test_generic_auto_traits() {
6060

6161
// Collections are not Send + Sync, since they are interior mutable, i.e.
6262
// mutable from `&self`.
63-
assert_not_impl_any!(NSArray<ImmutableSendSyncObject>: Send, Sync);
64-
assert_not_impl_any!(NSMutableArray<ImmutableSendSyncObject>: Send, Sync);
65-
assert_not_impl_any!(NSDictionary<ImmutableSendSyncObject, ImmutableSendSyncObject>: Send, Sync);
63+
assert_not_impl_any!(NSArray<SendSyncObject>: Send, Sync);
64+
assert_not_impl_any!(NSMutableArray<SendSyncObject>: Send, Sync);
65+
assert_not_impl_any!(NSDictionary<SendSyncObject, SendSyncObject>: Send, Sync);
6666

6767
// TODO: Make these UnwindSafe?
6868
assert_not_impl_any!(NSDictionary<NSProcessInfo, NSProcessInfo>: UnwindSafe, RefUnwindSafe);

framework-crates/objc2-quartz-core/translation-config.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ class.CAEAGLLayer.skipped-protocols = ["EAGLDrawable"]
6262
### Safety
6363
###
6464

65-
# We probably technically had the choice of making these classes mutating
66-
# methods require `&mut`, but as with so many things in Cocoa, that would
67-
# make it difficult to use in a larger context (e.g. even after assigning a
68-
# layer to a view you'd often still want to modify the layer).
69-
7065
fn.CACurrentMediaTime.unsafe = false
7166

7267
# SAFETY: Basic mathematical functions

0 commit comments

Comments
 (0)