Skip to content

Commit 73652be

Browse files
committed
analyze: rewrite: clarify option downgrade no-op cases
1 parent af3980d commit 73652be

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::pointee_type::PointeeTypes;
1313
use crate::pointer_id::{PointerId, PointerTable};
1414
use crate::type_desc::{self, Ownership, Quantity, TypeDesc};
1515
use crate::util::{self, ty_callee, Callee};
16-
use log::trace;
16+
use log::{error, trace};
1717
use rustc_ast::Mutability;
1818
use rustc_middle::mir::{
1919
BasicBlock, Body, BorrowKind, Location, Operand, Place, PlaceElem, PlaceRef, Rvalue, Statement,
@@ -963,6 +963,8 @@ where
963963
// `to` is `&mut T`, we start by calling `p.as_deref_mut()`, which produces
964964
// `Option<&mut T>` without consuming `p`.
965965
if !from.own.is_copy() {
966+
// Note that all non-`Copy` ownership types are also safe. We don't reach this
967+
// code when `from.own` is `Raw` or `RawMut`.
966968
match to.own {
967969
Ownership::Raw | Ownership::Imm => {
968970
(self.emit)(RewriteKind::OptionDowngrade {
@@ -978,8 +980,17 @@ where
978980
});
979981
from.own = Ownership::Mut;
980982
}
983+
Ownership::Rc if from.own == Ownership::Rc => {
984+
// `p.clone()` allows using an `Option<Rc<T>>` without consuming the
985+
// original. However, `RewriteKind::Clone` is not yet implemented.
986+
error!("Option<Rc> -> Option<Rc> clone rewrite NYI");
987+
}
981988
_ => {
982-
// Remaining cases are unsupported.
989+
// Remaining cases don't have a valid downgrade operation. We leave them
990+
// as is, and the `unwrap`/`map` operations below will consume the original
991+
// value. Some cases are also impossible to implement, like casting from
992+
// `Rc` to `Box`, which will be caught when attempting the `qty`/`own`
993+
// casts below.
983994
}
984995
}
985996
}

0 commit comments

Comments
 (0)