From 0183677b4bc25fc762ec06a4665c4f384998fb50 Mon Sep 17 00:00:00 2001 From: Tyler K Date: Thu, 22 Aug 2024 15:43:21 -0700 Subject: [PATCH] Fix bug where result buffer is not generated if error type is ZST (#652) * Fix bug where result buffer is not generated if error type is blank Fixes yet another ICU4X demo_gen issue * Updating checking buffer logic --- tool/src/js/type_generation/converter.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tool/src/js/type_generation/converter.rs b/tool/src/js/type_generation/converter.rs index a315f8805..6f071e39e 100644 --- a/tool/src/js/type_generation/converter.rs +++ b/tool/src/js/type_generation/converter.rs @@ -394,7 +394,7 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> { // Result or Option ReturnType::Fallible(ref ok, _) | ReturnType::Nullable(ref ok) => { let (requires_buf, error_ret) = match return_type { - ReturnType::Fallible(_, Some(e)) => { + ReturnType::Fallible(s, Some(e)) => { let type_name = self.formatter.fmt_type_name(e.id().unwrap()); self.add_import(type_name.into()); @@ -404,6 +404,14 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> { _ => unreachable!(), }); + let is_out = matches!(s, SuccessType::OutType(..)); + + let success_empty = matches!(s, SuccessType::OutType(Type::Struct(s)) if match s.resolve(self.tcx) { + ReturnableStructDef::Struct(s) => s.fields.is_empty(), + ReturnableStructDef::OutStruct(s) => s.fields.is_empty(), + _ => unreachable!(), + }); + let receive_deref = self.gen_c_to_js_deref_for_type( e, match fields_empty { @@ -417,7 +425,8 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> { let type_name = self.formatter.fmt_type_name(e.id().unwrap()); let cause = self.gen_c_to_js_for_type(e, receive_deref, &method.lifetime_env); - (!fields_empty, format!( + // We still require an out buffer even if our error types is empty + (!fields_empty || (is_out && !success_empty), format!( "const cause = {cause};\n throw new globalThis.Error({message}, {{ cause }})", message = match e { Type::Enum(..) => format!("'{type_name}: ' + cause.value"),