Skip to content

Commit fba984a

Browse files
committed
tests: Don't use std::mem::transmute to cast between pointers and Cell
Reduces the amount of `unsafe` in the code. Looks like in newer rustc versions, this causes crashes. Could be related to rust-lang/rust#121282
1 parent 16d16da commit fba984a

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/tests.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ b";
259259
assert_eq!(l, 1);
260260

261261
let after_a_call: *const ForthOperation = after_a_call;
262-
let after_a_call: Cell = unsafe { std::mem::transmute(after_a_call) };
262+
let after_a_call: Cell = after_a_call as Cell;
263263
assert_eq!(
264264
something_from_return_stack, after_a_call,
265265
"got from stack: ${:x} after_a_call: ${:x}",
@@ -449,10 +449,10 @@ test4
449449

450450
for number in [0, 1, -1, Cell::MAX / 2, Cell::MAX, Cell::MIN, Cell::MIN / 2] {
451451
{
452-
let cell_to_modify: Cell = 0;
452+
let mut cell_to_modify: Cell = 0;
453453
environment
454454
.data_stack
455-
.push(unsafe { std::mem::transmute(&cell_to_modify) })
455+
.push(&mut cell_to_modify as *mut Cell as Cell)
456456
.unwrap();
457457
environment.interpret_line(format!("{} swap !", number).as_bytes());
458458
assert_eq!(cell_to_modify, number);
@@ -462,7 +462,7 @@ test4
462462
let cell_to_read: Cell = number;
463463
environment
464464
.data_stack
465-
.push(unsafe { std::mem::transmute(&cell_to_read) })
465+
.push(&cell_to_read as *const Cell as Cell)
466466
.unwrap();
467467
environment.interpret_line("@".as_bytes());
468468
assert_eq!(cell_to_read, environment.data_stack.pop().unwrap());
@@ -471,10 +471,10 @@ test4
471471

472472
for number in [0, 1, 26, Byte::MAX, Byte::MAX / 2] {
473473
{
474-
let byte_to_modify: Byte = 0;
474+
let mut byte_to_modify: Byte = 0;
475475
environment
476476
.data_stack
477-
.push(unsafe { std::mem::transmute(&byte_to_modify) })
477+
.push(&mut byte_to_modify as *mut Byte as Cell)
478478
.unwrap();
479479
environment.interpret_line(format!("{} swap c!", number).as_bytes());
480480
assert_eq!(byte_to_modify, number);
@@ -484,7 +484,7 @@ test4
484484
let byte_to_read: Byte = number;
485485
environment
486486
.data_stack
487-
.push(unsafe { std::mem::transmute(&byte_to_read) })
487+
.push(&byte_to_read as *const Byte as Cell)
488488
.unwrap();
489489
environment.interpret_line("c@".as_bytes());
490490
assert_eq!(byte_to_read as Cell, environment.data_stack.pop().unwrap());
@@ -510,7 +510,7 @@ test4
510510

511511
let counted_string_address = *environment.data_stack.last().unwrap();
512512
let counted_string: &CountedString = unsafe {
513-
std::mem::transmute::<Cell, *const CountedString>(counted_string_address)
513+
(counted_string_address as *const CountedString)
514514
.as_ref()
515515
.unwrap()
516516
};

0 commit comments

Comments
 (0)