Skip to content

Commit dbddcb8

Browse files
authored
Merge pull request #150 from madsmtm/msg-send-receiver-soundness
Change how mutability in `msg_send!` is done
2 parents 5bd6979 + 2beef61 commit dbddcb8

29 files changed

+321
-138
lines changed

objc2-foundation/examples/custom_class.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ fn main() {
7070

7171
obj.set_number(7);
7272
println!("Number: {}", unsafe {
73-
let number: u32 = msg_send![obj, number];
73+
let number: u32 = msg_send![&obj, number];
7474
number
7575
});
7676

7777
unsafe {
78-
let _: () = msg_send![obj, setNumber: 12u32];
78+
let _: () = msg_send![&mut obj, setNumber: 12u32];
7979
}
8080
println!("Number: {}", obj.number());
8181
}

objc2-foundation/src/array.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,18 @@ impl<T: Message, O: Ownership> NSMutableArray<T, O> {
272272
obj
273273
}
274274

275+
fn remove_last(&mut self) {
276+
unsafe { msg_send![self, removeLastObject] }
277+
}
278+
275279
#[doc(alias = "removeLastObject")]
276280
pub fn pop(&mut self) -> Option<Id<T, O>> {
277-
self.last().map(|obj| {
278-
let obj = unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() };
279-
unsafe {
280-
let _: () = msg_send![self, removeLastObject];
281-
}
282-
obj
283-
})
281+
self.last()
282+
.map(|obj| unsafe { Id::retain(obj as *const T as *mut T).unwrap_unchecked() })
283+
.map(|obj| {
284+
self.remove_last();
285+
obj
286+
})
284287
}
285288

286289
#[doc(alias = "removeAllObjects")]

objc2-foundation/src/attributed_string.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,14 @@ mod tests {
153153
let s2 = s1.copy();
154154
// NSAttributedString performs this optimization in GNUStep's runtime,
155155
// but not in Apple's; so we don't test for it!
156-
// assert_eq!(s1.as_ptr(), s2.as_ptr());
156+
// assert_eq!(Id::as_ptr(&s1), Id::as_ptr(&s2));
157157
assert!(s2.is_kind_of(NSAttributedString::class()));
158158

159159
let s3 = s1.mutable_copy();
160-
assert_ne!(s1.as_ptr(), s3.as_ptr() as *mut NSAttributedString);
160+
assert_ne!(
161+
Id::as_ptr(&s1),
162+
Id::as_ptr(&s3) as *const NSAttributedString
163+
);
161164
assert!(s3.is_kind_of(NSMutableAttributedString::class()));
162165
}
163166
}

objc2-foundation/src/data.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,14 @@ impl NSMutableData {
154154

155155
/// Mutation methods
156156
impl NSMutableData {
157+
// Helps with reborrowing issue
158+
fn raw_bytes_mut(&mut self) -> *mut c_void {
159+
unsafe { msg_send![self, mutableBytes] }
160+
}
161+
157162
#[doc(alias = "mutableBytes")]
158163
pub fn bytes_mut(&mut self) -> &mut [u8] {
159-
let ptr: *mut c_void = unsafe { msg_send![self, mutableBytes] };
164+
let ptr = self.raw_bytes_mut();
160165
// The bytes pointer may be null for length zero
161166
if ptr.is_null() {
162167
&mut []

objc2-foundation/src/dictionary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<K: Message, V: Message> NSDictionary<K, V> {
129129

130130
pub fn into_values_array(dict: Id<Self, Owned>) -> Id<NSArray<V, Owned>, Shared> {
131131
unsafe {
132-
let vals = msg_send![dict, allValues];
132+
let vals = msg_send![&dict, allValues];
133133
Id::retain_autoreleased(vals).unwrap()
134134
}
135135
}

objc2-foundation/src/enumerator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<'a, T: Message> Iterator for NSEnumerator<'a, T> {
3333
type Item = &'a T;
3434

3535
fn next(&mut self) -> Option<&'a T> {
36-
unsafe { msg_send![self.id, nextObject] }
36+
unsafe { msg_send![&mut self.id, nextObject] }
3737
}
3838
}
3939

objc2-foundation/src/mutable_attributed_string.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,14 @@ mod tests {
9090
fn test_copy() {
9191
let s1 = NSMutableAttributedString::from_nsstring(&NSString::from_str("abc"));
9292
let s2 = s1.copy();
93-
assert_ne!(s1.as_ptr() as *const NSAttributedString, s2.as_ptr());
93+
assert_ne!(
94+
Id::as_ptr(&s1) as *const NSAttributedString,
95+
Id::as_ptr(&s2)
96+
);
9497
assert!(s2.is_kind_of(NSAttributedString::class()));
9598

9699
let s3 = s1.mutable_copy();
97-
assert_ne!(s1.as_ptr(), s3.as_ptr());
100+
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3));
98101
assert!(s3.is_kind_of(NSMutableAttributedString::class()));
99102
}
100103
}

objc2-foundation/src/mutable_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@ mod tests {
200200
fn test_copy() {
201201
let s1 = NSMutableString::from_str("abc");
202202
let s2 = s1.copy();
203-
assert_ne!(s1.as_ptr(), s2.as_ptr() as *mut NSMutableString);
203+
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s2) as *const NSMutableString);
204204
assert!(s2.is_kind_of(NSString::class()));
205205

206206
let s3 = s1.mutable_copy();
207-
assert_ne!(s1.as_ptr(), s3.as_ptr());
207+
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3));
208208
assert!(s3.is_kind_of(NSMutableString::class()));
209209
}
210210
}

objc2-foundation/src/string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,11 @@ mod tests {
346346
let s1 = NSString::from_str("abc");
347347
let s2 = s1.copy();
348348
// An optimization that NSString makes, since it is immutable
349-
assert_eq!(s1.as_ptr(), s2.as_ptr());
349+
assert_eq!(Id::as_ptr(&s1), Id::as_ptr(&s2));
350350
assert!(s2.is_kind_of(NSString::class()));
351351

352352
let s3 = s1.mutable_copy();
353-
assert_ne!(s1.as_ptr(), s3.as_ptr() as *mut NSString);
353+
assert_ne!(Id::as_ptr(&s1), Id::as_ptr(&s3) as *const NSString);
354354
assert!(s3.is_kind_of(NSMutableString::class()));
355355
}
356356

objc2-foundation/src/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ mod tests {
177177
fn test_value_nsrange() {
178178
let val = NSValue::new(NSRange::from(1..2));
179179
assert!(NSRange::ENCODING.equivalent_to_str(val.encoding().unwrap()));
180-
let range: NSRange = unsafe { objc2::msg_send![val, rangeValue] };
180+
let range: NSRange = unsafe { objc2::msg_send![&val, rangeValue] };
181181
assert_eq!(range, NSRange::from(1..2));
182182
// NSValue -getValue is broken on GNUStep for some types
183183
#[cfg(not(feature = "gnustep-1-7"))]

0 commit comments

Comments
 (0)