Skip to content

Commit 6de384c

Browse files
committed
analyze: rewrite::mir_op: wrap &e in Some(_) if result type is nullable
1 parent 1419204 commit 6de384c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

c2rust-analyze/src/rewrite/expr/mir_op.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,14 @@ impl<'a, 'tcx> ExprRewriteVisitor<'a, 'tcx> {
564564
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => false,
565565
};
566566
self.enter_rvalue_place(0, |v| v.visit_place(pl, mutbl));
567+
568+
if let Some(expect_ty) = expect_ty {
569+
if self.is_nullable(expect_ty.label) {
570+
// Nullable (`Option`) output is expected, but `Ref` always produces a
571+
// `NON_NULL` pointer. Cast rvalue from `&T` to `Option<&T>` or similar.
572+
self.emit(RewriteKind::OptionSome);
573+
}
574+
}
567575
}
568576
Rvalue::ThreadLocalRef(_def_id) => {
569577
// TODO
@@ -585,6 +593,9 @@ impl<'a, 'tcx> ExprRewriteVisitor<'a, 'tcx> {
585593
}),
586594
_ => (),
587595
}
596+
if desc.option {
597+
self.emit(RewriteKind::OptionSome);
598+
}
588599
}
589600
}
590601
Rvalue::Len(pl) => {

0 commit comments

Comments
 (0)