@@ -1206,14 +1206,29 @@ void Converter::EmitFunctionPreamble(clang::FunctionDecl *decl) {
12061206 auto params = decl->getDefinition () ? decl->getDefinition ()->parameters ()
12071207 : decl->parameters ();
12081208 for (auto *param : params) {
1209- if (HasUsableDefaultArg (param)) {
1210- auto name = GetNamedDeclAsString (param);
1211- auto type = ToString (param->getType ());
1212- auto init = std::format (" {}.unwrap_or({})" , name,
1213- ToString (param->getDefaultArg ()));
1214- StrCat (std::format (" let mut {} : {} = {}" , name, type, init),
1209+ if (!HasUsableDefaultArg (param)) {
1210+ continue ;
1211+ }
1212+ auto name = GetNamedDeclAsString (param);
1213+ auto type = ToString (param->getType ());
1214+ std::string value;
1215+ if (auto *temp = GetDefaultArgTemporary (param)) {
1216+ auto storage = std::format (" __{}_default" , name);
1217+ StrCat (std::format (" let mut {} : Option<{}> = None" , storage,
1218+ ToString (temp->getType ().getUnqualifiedType ())),
12151219 token::kSemiColon );
1220+ value = std::format (" {}.insert({}) as {}" , storage,
1221+ ToString (temp->getSubExpr ()), type);
1222+ } else if (param->getType ()->isReferenceType ()) {
1223+ Buffer buf (*this );
1224+ ConvertVarInit (param->getType (), param->getDefaultArg ());
1225+ value = std::move (buf).str ();
1226+ } else {
1227+ value = ToString (param->getDefaultArg ());
12161228 }
1229+ StrCat (std::format (" let mut {} : {} = {}.unwrap_or_else(|| {} {{ {} }})" ,
1230+ name, type, name, keyword_unsafe_, value),
1231+ token::kSemiColon );
12171232 }
12181233}
12191234
0 commit comments