@@ -925,22 +925,51 @@ bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) {
925925 if (expr->isPrefix () && isAddrOf ()) {
926926 auto ptr = ConvertFreshObject (sub_expr);
927927 auto name = NewTempName ();
928- StrCat (
929- std::format ( " {{ let {0} = {1}; {0}.with_mut(|__v| __v.{2}()); {0} }}" ,
930- name, ptr, method));
928+ StrCat (std::format (
929+ " {{ let {0} = {1}; {0}.with_mut(|__v| {{ __v.{2}(); }} ); {0} }}" , name ,
930+ ptr, method));
931931 MarkFresh ();
932932 return true ;
933933 }
934934
935+ bool is_prefix = expr->isPrefix ();
935936 auto str = ConvertLValue (sub_expr);
936937 if (!pending_deref_.empty ()) {
937- StrCat (pending_deref_.take (), " .with_mut(|__v| __v." , method, " ())" );
938+ bool fresh = pending_deref_.is_fresh ();
939+ auto ptr = pending_deref_.take ();
940+ if (!is_prefix) {
941+ StrCat (ptr, " .with_mut(|__v| __v." , method, " ())" );
942+ } else if (isVoid ()) {
943+ StrCat (ptr, " .with_mut(|__v| { __v." , method, " (); })" );
944+ } else if (isLValue ()) {
945+ auto name = NewTempName ();
946+ pending_deref_.set (std::format (" {{ let {0} = {1}{2}; {0}.with_mut(|__v| "
947+ " {{ __v.{3}(); }}); {0} }}" ,
948+ name, ptr, fresh ? " " : " .clone()" ,
949+ method),
950+ /* fresh=*/ true );
951+ return true ;
952+ } else {
953+ StrCat (ptr, " .with_mut(|__v| __v." , method, " ().clone())" );
954+ }
938955 } else if (!isVoid () &&
939956 clang::isa<clang::MemberExpr>(sub_expr->IgnoreParens ()) &&
940957 !IsUnboxedPlace (sub_expr)) {
941958 // The struct is borrowed in a block, which ends the borrow before the
942959 // result is used, e.g., to write through a pointer into the struct.
943- StrCat (std::format (" {{ {}.{}() }}" , str, method));
960+ if (!is_prefix) {
961+ StrCat (std::format (" {{ {}.{}() }}" , str, method));
962+ } else if (isLValue ()) {
963+ StrCat (std::format (" (*{}.{}())" , str, method));
964+ } else {
965+ StrCat (std::format (" {{ {}.{}().clone() }}" , str, method));
966+ }
967+ } else if (is_prefix && !isVoid ()) {
968+ StrCat (std::format (" (*{}.{}())" , str, method));
969+ if (!isLValue ()) {
970+ SetValueFreshness (expr->getType ());
971+ return true ;
972+ }
944973 } else {
945974 StrCat (str, ' .' , method, " ()" );
946975 }
0 commit comments