Skip to content

Commit edadca1

Browse files
kkysenRua
andcommitted
transpile: change negated: bool to is_null: bool in fn convert_pointer_is_null
Co-authored-by: Rua <ruawhitepaw@gmail.com>
1 parent dfc52ab commit edadca1

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

c2rust-transpile/src/translator/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,39 +2715,39 @@ impl<'c> Translation<'c> {
27152715
.ok_or_else(|| format_err!("bad condition type"))?;
27162716

27172717
let null_pointer_case =
2718-
|negated: bool, ptr: CExprId| -> TranslationResult<WithStmts<Box<Expr>>> {
2718+
|is_null: bool, ptr: CExprId| -> TranslationResult<WithStmts<Box<Expr>>> {
27192719
let val = self.convert_expr(ctx.used().decay_ref(), ptr, None)?;
27202720
let ptr_type = self.ast_context[ptr]
27212721
.kind
27222722
.get_type()
27232723
.ok_or_else(|| format_err!("bad pointer type for condition"))?;
27242724

2725-
val.result_map(|val| self.convert_pointer_is_null(ctx, ptr_type, val, negated))
2725+
val.result_map(|val| self.convert_pointer_is_null(ctx, ptr_type, val, is_null))
27262726
};
27272727

27282728
match self.ast_context[cond_id].kind {
27292729
CExprKind::Binary(_, c_ast::BinOp::EqualEqual, null_expr, ptr, _, _)
27302730
if self.ast_context.is_null_expr(null_expr) =>
27312731
{
2732-
null_pointer_case(!target, ptr)
2732+
null_pointer_case(target, ptr)
27332733
}
27342734

27352735
CExprKind::Binary(_, c_ast::BinOp::EqualEqual, ptr, null_expr, _, _)
27362736
if self.ast_context.is_null_expr(null_expr) =>
27372737
{
2738-
null_pointer_case(!target, ptr)
2738+
null_pointer_case(target, ptr)
27392739
}
27402740

27412741
CExprKind::Binary(_, c_ast::BinOp::NotEqual, null_expr, ptr, _, _)
27422742
if self.ast_context.is_null_expr(null_expr) =>
27432743
{
2744-
null_pointer_case(target, ptr)
2744+
null_pointer_case(!target, ptr)
27452745
}
27462746

27472747
CExprKind::Binary(_, c_ast::BinOp::NotEqual, ptr, null_expr, _, _)
27482748
if self.ast_context.is_null_expr(null_expr) =>
27492749
{
2750-
null_pointer_case(target, ptr)
2750+
null_pointer_case(!target, ptr)
27512751
}
27522752

27532753
CExprKind::Unary(_, c_ast::UnOp::Not, subexpr_id, _) => {
@@ -4829,7 +4829,7 @@ impl<'c> Translation<'c> {
48294829
let ty = &self.ast_context.resolve_type(ty_id).kind;
48304830

48314831
Ok(if ty.is_pointer() {
4832-
self.convert_pointer_is_null(ctx, ty_id, val, target)?
4832+
self.convert_pointer_is_null(ctx, ty_id, val, !target)?
48334833
} else if ty.is_bool() {
48344834
if target {
48354835
val

c2rust-transpile/src/translator/operators.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,17 +620,17 @@ impl<'c> Translation<'c> {
620620
// Using `.is_none()` and `.is_some()` for null comparison means
621621
// we don't have to rely on `trait PartialEq` as much
622622
// and it is also more idiomatic.
623-
let (negated, bin_op) = match op {
624-
c_ast::BinOp::EqualEqual => (false, BinOp::Eq(Default::default())),
625-
c_ast::BinOp::NotEqual => (true, BinOp::Ne(Default::default())),
623+
let (is_null, bin_op) = match op {
624+
c_ast::BinOp::EqualEqual => (true, BinOp::Eq(Default::default())),
625+
c_ast::BinOp::NotEqual => (false, BinOp::Ne(Default::default())),
626626
_ => unreachable!(),
627627
};
628628
let expr = match lhs_rhs_ids {
629629
Some((lhs_expr_id, _)) if self.ast_context.is_null_expr(lhs_expr_id) => {
630-
self.convert_pointer_is_null(ctx, rhs_type.ctype, rhs, negated)?
630+
self.convert_pointer_is_null(ctx, rhs_type.ctype, rhs, is_null)?
631631
}
632632
Some((_, rhs_expr_id)) if self.ast_context.is_null_expr(rhs_expr_id) => {
633-
self.convert_pointer_is_null(ctx, lhs_type.ctype, lhs, negated)?
633+
self.convert_pointer_is_null(ctx, lhs_type.ctype, lhs, is_null)?
634634
}
635635
_ => mk().binary_expr(bin_op, lhs, rhs),
636636
};

c2rust-transpile/src/translator/pointers.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,10 @@ impl<'c> Translation<'c> {
570570
ctx: ExprContext,
571571
ptr_type: CTypeId,
572572
e: Box<Expr>,
573-
negated: bool,
573+
is_null: bool,
574574
) -> TranslationResult<Box<Expr>> {
575575
Ok(if self.ast_context.is_function_pointer(ptr_type) {
576-
let method = if negated { "is_some" } else { "is_none" };
576+
let method = if is_null { "is_none" } else { "is_some" };
577577
mk().method_call_expr(e, method, vec![])
578578
} else {
579579
// TODO: `pointer::is_null` becomes stably const in Rust 1.84.
@@ -583,11 +583,11 @@ impl<'c> Translation<'c> {
583583
"cannot check nullity of pointer in `const` context",
584584
));
585585
}
586-
let is_null = mk().method_call_expr(e, "is_null", vec![]);
587-
if negated {
588-
mk().unary_expr(UnOp::Not(Default::default()), is_null)
586+
let is_null_val = mk().method_call_expr(e, "is_null", vec![]);
587+
if !is_null {
588+
mk().unary_expr(UnOp::Not(Default::default()), is_null_val)
589589
} else {
590-
is_null
590+
is_null_val
591591
}
592592
})
593593
}

0 commit comments

Comments
 (0)