Skip to content

Commit c693c8d

Browse files
committed
Use raw borrows instead of normal borrows
1 parent 521948d commit c693c8d

File tree

4 files changed

+37
-58
lines changed

4 files changed

+37
-58
lines changed

c2rust-ast-builder/src/builder.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use syn::{__private::ToTokens, punctuated::Punctuated, *};
99

1010
pub mod properties {
1111
use proc_macro2::Span;
12-
use syn::{StaticMutability, Token};
12+
use syn::{PointerMutability, StaticMutability, Token};
1313

1414
pub trait ToToken {
1515
type Token;
@@ -33,6 +33,13 @@ pub mod properties {
3333
}
3434

3535
impl Mutability {
36+
pub fn to_pointer_mutability(&self, span: Span) -> PointerMutability {
37+
match self {
38+
Mutability::Mutable => PointerMutability::Mut(Token![mut](span)),
39+
Mutability::Immutable => PointerMutability::Const(Token![const](span)),
40+
}
41+
}
42+
3643
pub fn to_static_mutability(&self, span: Span) -> StaticMutability {
3744
match self {
3845
Mutability::Mutable => StaticMutability::Mut(Token![mut](span)),
@@ -953,7 +960,7 @@ impl Builder {
953960
self.path_expr(vec![name])
954961
}
955962

956-
pub fn addr_of_expr(self, e: Box<Expr>) -> Box<Expr> {
963+
pub fn borrow_expr(self, e: Box<Expr>) -> Box<Expr> {
957964
Box::new(parenthesize_if_necessary(Expr::Reference(ExprReference {
958965
attrs: self.attrs,
959966
and_token: Token![&](self.span),
@@ -962,6 +969,16 @@ impl Builder {
962969
})))
963970
}
964971

972+
pub fn raw_borrow_expr(self, e: Box<Expr>) -> Box<Expr> {
973+
Box::new(parenthesize_if_necessary(Expr::RawAddr(ExprRawAddr {
974+
attrs: self.attrs,
975+
and_token: Token![&](self.span),
976+
raw: Token![raw](self.span),
977+
mutability: self.mutbl.to_pointer_mutability(self.span),
978+
expr: e,
979+
})))
980+
}
981+
965982
pub fn mac_expr(self, mac: Macro) -> Box<Expr> {
966983
Box::new(Expr::Macro(ExprMacro {
967984
attrs: self.attrs,

c2rust-transpile/src/translator/assembly.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ impl<'c> Translation<'c> {
819819
// c2rust-ast-exporter added it (there's no gcc equivalent);
820820
// in this case, we need to do what clang does and pass in
821821
// the operand by-address instead of by-value
822-
out_expr = mk().mutbl().addr_of_expr(out_expr);
822+
out_expr = mk().mutbl().borrow_expr(out_expr);
823823
}
824824

825825
if let Some(_tied_operand) = tied_operands.get(&(output_idx, true)) {
@@ -836,7 +836,7 @@ impl<'c> Translation<'c> {
836836
let output_local = mk().local(
837837
mk().ident_pat(&output_name),
838838
None,
839-
Some(mk().mutbl().addr_of_expr(out_expr)),
839+
Some(mk().mutbl().borrow_expr(out_expr)),
840840
);
841841
stmts.push(mk().local_stmt(Box::new(output_local)));
842842

@@ -860,7 +860,7 @@ impl<'c> Translation<'c> {
860860
let mut in_expr = in_expr.into_value();
861861

862862
if operand.mem_only {
863-
in_expr = mk().addr_of_expr(in_expr);
863+
in_expr = mk().borrow_expr(in_expr);
864864
}
865865
if let Some(tied_operand) = tied_operands.get(&(input_idx, false)) {
866866
self.use_crate(ExternCrate::C2RustAsmCasts);

c2rust-transpile/src/translator/mod.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,12 +2979,8 @@ impl<'c> Translation<'c> {
29792979
}
29802980
}
29812981
_ => {
2982-
let addr_lhs = mk().set_mutbl(mutbl).addr_of_expr(lhs);
2983-
2984-
let lhs_type = self.convert_type(lhs_type.ctype)?;
2985-
let ty = mk().set_mutbl(mutbl).ptr_ty(lhs_type);
2986-
2987-
mk().cast_expr(addr_lhs, ty)
2982+
self.use_feature("raw_ref_op");
2983+
mk().set_mutbl(mutbl).raw_borrow_expr(lhs)
29882984
}
29892985
};
29902986
Ok(addr_lhs)
@@ -4512,25 +4508,19 @@ impl<'c> Translation<'c> {
45124508
{
45134509
Ok(val)
45144510
} else {
4515-
let method = if is_const || ctx.is_static {
4516-
"as_ptr"
4511+
let mutbl = if is_const {
4512+
Mutability::Immutable
45174513
} else {
4518-
"as_mut_ptr"
4514+
Mutability::Mutable
45194515
};
4520-
4521-
let call = val.map(|x| mk().method_call_expr(x, method, vec![]));
4522-
4523-
// Static arrays can now use as_ptr. Can also cast that const ptr to a
4524-
// mutable pointer as we do here:
4525-
if ctx.is_static && !is_const {
4526-
return Ok(call.map(|val| {
4527-
let inferred_type = mk().infer_ty();
4528-
let ptr_type = mk().mutbl().ptr_ty(inferred_type);
4529-
mk().cast_expr(val, ptr_type)
4530-
}));
4531-
}
4532-
4533-
Ok(call)
4516+
let target_ty = self.convert_type(ty.ctype)?;
4517+
4518+
Ok(val.map(|x| {
4519+
self.use_feature("raw_ref_op");
4520+
let borrow = mk().set_mutbl(mutbl).raw_borrow_expr(x);
4521+
// TODO: Change to call `ptr::as_[mut]_ptr` once that is available.
4522+
mk().cast_expr(borrow, target_ty)
4523+
}))
45344524
}
45354525
}
45364526
}

c2rust-transpile/src/translator/operators.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,6 @@ impl<'c> Translation<'c> {
901901
lrvalue: LRValue,
902902
) -> TranslationResult<WithStmts<Box<Expr>>> {
903903
let CQualTypeId { ctype, .. } = cqual_type;
904-
let ty = self.convert_type(ctype)?;
905904
let resolved_ctype = self.ast_context.resolve_type(ctype);
906905

907906
let mut unary = match name {
@@ -942,35 +941,8 @@ impl<'c> Translation<'c> {
942941
Mutability::Mutable
943942
};
944943

945-
arg.result_map(|a| {
946-
let mut addr_of_arg: Box<Expr>;
947-
948-
if ctx.is_static {
949-
// static variable initializers aren't able to use &mut,
950-
// so we work around that by using & and an extra cast
951-
// through & to *const to *mut
952-
addr_of_arg = mk().addr_of_expr(a);
953-
if let Mutability::Mutable = mutbl {
954-
let mut qtype = pointee_ty;
955-
qtype.qualifiers.is_const = true;
956-
let ty_ = self
957-
.type_converter
958-
.borrow_mut()
959-
.convert_pointer(&self.ast_context, qtype)?;
960-
addr_of_arg = mk().cast_expr(addr_of_arg, ty_);
961-
}
962-
} else {
963-
// Normal case is allowed to use &mut if needed
964-
addr_of_arg = mk().set_mutbl(mutbl).addr_of_expr(a);
965-
966-
// Avoid unnecessary reference to pointer decay in fn call args:
967-
if ctx.decay_ref.is_no() {
968-
return Ok(addr_of_arg);
969-
}
970-
}
971-
972-
Ok(mk().cast_expr(addr_of_arg, ty))
973-
})
944+
self.use_feature("raw_ref_op");
945+
Ok(arg.map(|a| mk().set_mutbl(mutbl).raw_borrow_expr(a)))
974946
}
975947
}
976948
c_ast::UnOp::PreIncrement => self.convert_pre_increment(ctx, cqual_type, true, arg),

0 commit comments

Comments
 (0)