Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2fc0231

Browse files
committed
Fix null check logic and make new tests pass
1 parent 9b56e20 commit 2fc0231

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

compiler/rustc_mir_transform/src/check_null.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ fn insert_null_check<'tcx>(
5353
user_ty: None,
5454
const_: Const::Val(ConstValue::Scalar(Scalar::from_target_usize(0, &tcx)), tcx.types.usize),
5555
}));
56-
let is_pointee_zst =
56+
let is_pointee_no_zst =
5757
local_decls.push(LocalDecl::with_source_info(tcx.types.bool, source_info)).into();
5858
stmts.push(Statement {
5959
source_info,
6060
kind: StatementKind::Assign(Box::new((
61-
is_pointee_zst,
61+
is_pointee_no_zst,
6262
Rvalue::BinaryOp(BinOp::Eq, Box::new((Operand::Copy(sizeof_pointee), zero.clone()))),
6363
))),
6464
});
@@ -69,7 +69,7 @@ fn insert_null_check<'tcx>(
6969
source_info,
7070
kind: StatementKind::Assign(Box::new((
7171
is_null,
72-
Rvalue::BinaryOp(BinOp::Ne, Box::new((Operand::Copy(addr), zero))),
72+
Rvalue::BinaryOp(BinOp::Eq, Box::new((Operand::Copy(addr), zero))),
7373
))),
7474
});
7575

@@ -79,10 +79,10 @@ fn insert_null_check<'tcx>(
7979
stmts.push(Statement {
8080
source_info,
8181
kind: StatementKind::Assign(Box::new((
82-
is_null,
82+
should_throw_exception,
8383
Rvalue::BinaryOp(
84-
BinOp::BitOr,
85-
Box::new((Operand::Copy(is_null), Operand::Copy(is_pointee_zst))),
84+
BinOp::BitAnd,
85+
Box::new((Operand::Copy(is_null), Operand::Copy(is_pointee_no_zst))),
8686
),
8787
))),
8888
});

tests/ui/mir/null/zero_sized_access.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//@ compile-flags: -C debug-assertions
33

44
fn main() {
5-
let ptr: *const () = std::ptr::null();
6-
let _ptr = unsafe { *ptr };
5+
let ptr: *mut () = std::ptr::null_mut();
6+
unsafe {
7+
*(ptr) = ();
8+
}
79
}

0 commit comments

Comments
 (0)