@@ -63,7 +63,7 @@ struct CSig {
63
63
enum Scalar {
64
64
Void ,
65
65
OptionBool ( Type ) ,
66
- ResultBool ( bool ) ,
66
+ ResultBool ( Option < Type > , Option < Type > ) ,
67
67
Type ( Type ) ,
68
68
}
69
69
@@ -448,15 +448,13 @@ impl Return {
448
448
// Unpack a result as a boolean return type, with two
449
449
// return pointers for ok and err values
450
450
TypeDefKind :: Result ( r) => {
451
- let mut has_ok_type = false ;
452
451
if let Some ( ok) = r. ok {
453
- has_ok_type = true ;
454
452
self . retptrs . push ( ok) ;
455
453
}
456
454
if let Some ( err) = r. err {
457
455
self . retptrs . push ( err) ;
458
456
}
459
- self . scalar = Some ( Scalar :: ResultBool ( has_ok_type ) ) ;
457
+ self . scalar = Some ( Scalar :: ResultBool ( r . ok , r . err ) ) ;
460
458
return ;
461
459
}
462
460
@@ -956,9 +954,9 @@ impl InterfaceGenerator<'_> {
956
954
match & ret. scalar {
957
955
None | Some ( Scalar :: Void ) => self . src . h_fns ( "void" ) ,
958
956
Some ( Scalar :: OptionBool ( _id) ) => self . src . h_fns ( "bool" ) ,
959
- Some ( Scalar :: ResultBool ( has_ok_type ) ) => {
957
+ Some ( Scalar :: ResultBool ( ok , _err ) ) => {
960
958
result_rets = true ;
961
- result_rets_has_ok_type = * has_ok_type ;
959
+ result_rets_has_ok_type = ok . is_some ( ) ;
962
960
self . src . h_fns ( "bool" ) ;
963
961
}
964
962
Some ( Scalar :: Type ( ty) ) => self . print_ty ( SourceType :: HFns , ty) ,
@@ -1593,10 +1591,6 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
1593
1591
) ;
1594
1592
self . ret_store_cnt = self . ret_store_cnt + 1 ;
1595
1593
}
1596
-
1597
- fn check_all_retptrs_written ( & self ) {
1598
- assert ! ( self . ret_store_cnt == self . sig. retptrs. len( ) ) ;
1599
- }
1600
1594
}
1601
1595
1602
1596
impl Bindgen for FunctionBindgen < ' _ , ' _ > {
@@ -2277,28 +2271,41 @@ impl Bindgen for FunctionBindgen<'_, '_> {
2277
2271
. gen
2278
2272
. type_string ( func. results . iter_types ( ) . next ( ) . unwrap ( ) ) ;
2279
2273
let option_ret = self . locals . tmp ( "ret" ) ;
2280
- uwrite ! (
2281
- self . src,
2282
- "
2283
- {ty} {ret};
2284
- {ret}.is_some = {tag};
2285
- {ret}.val = {val};
2286
- " ,
2287
- ty = option_ty,
2288
- ret = option_ret,
2289
- tag = ret,
2290
- val = val,
2291
- ) ;
2274
+ if !self . gen . is_empty_type ( ty) {
2275
+ uwrite ! (
2276
+ self . src,
2277
+ "
2278
+ {ty} {ret};
2279
+ {ret}.is_some = {tag};
2280
+ {ret}.val = {val};
2281
+ " ,
2282
+ ty = option_ty,
2283
+ ret = option_ret,
2284
+ tag = ret,
2285
+ val = val,
2286
+ ) ;
2287
+ } else {
2288
+ uwrite ! (
2289
+ self . src,
2290
+ "
2291
+ {ty} {ret};
2292
+ {ret}.is_some = {tag};
2293
+ " ,
2294
+ ty = option_ty,
2295
+ ret = option_ret,
2296
+ tag = ret,
2297
+ ) ;
2298
+ }
2292
2299
results. push ( option_ret) ;
2293
2300
}
2294
- Some ( Scalar :: ResultBool ( has_ok_type ) ) => {
2301
+ Some ( Scalar :: ResultBool ( ok , err ) ) => {
2295
2302
let result_ty = self
2296
2303
. gen
2297
2304
. type_string ( func. results . iter_types ( ) . next ( ) . unwrap ( ) ) ;
2298
2305
let ret = self . locals . tmp ( "ret" ) ;
2299
2306
let mut ret_iter = self . sig . ret . retptrs . iter ( ) ;
2300
2307
uwriteln ! ( self . src, "{result_ty} {ret};" ) ;
2301
- let ok_name = if * has_ok_type {
2308
+ let ok_name = if ok . is_some ( ) {
2302
2309
if let Some ( ty) = ret_iter. next ( ) {
2303
2310
let val = self . locals . tmp ( "ok" ) ;
2304
2311
if args. len ( ) > 0 {
@@ -2329,82 +2336,88 @@ impl Bindgen for FunctionBindgen<'_, '_> {
2329
2336
assert ! ( ret_iter. next( ) . is_none( ) ) ;
2330
2337
uwrite ! ( self . src, "" ) ;
2331
2338
uwriteln ! ( self . src, "{ret}.is_err = !{}({args});" , self . sig. name) ;
2332
-
2333
- if let Some ( err_name) = err_name {
2334
- uwriteln ! (
2335
- self . src,
2336
- "if ({ret}.is_err) {{
2337
- {ret}.val.err = {err_name};
2338
- }}" ,
2339
- ) ;
2339
+ if self . gen . get_nonempty_type ( err. as_ref ( ) ) . is_some ( ) {
2340
+ if let Some ( err_name) = err_name {
2341
+ uwriteln ! (
2342
+ self . src,
2343
+ "if ({ret}.is_err) {{
2344
+ {ret}.val.err = {err_name};
2345
+ }}" ,
2346
+ ) ;
2347
+ }
2340
2348
}
2341
- if let Some ( ok_name) = ok_name {
2342
- uwriteln ! (
2343
- self . src,
2344
- "if (!{ret}.is_err) {{
2345
- {ret}.val.ok = {ok_name};
2346
- }}"
2347
- ) ;
2348
- } else {
2349
- uwrite ! ( self . src, "\n " ) ;
2349
+ if self . gen . get_nonempty_type ( ok. as_ref ( ) ) . is_some ( ) {
2350
+ if let Some ( ok_name) = ok_name {
2351
+ uwriteln ! (
2352
+ self . src,
2353
+ "if (!{ret}.is_err) {{
2354
+ {ret}.val.ok = {ok_name};
2355
+ }}"
2356
+ ) ;
2357
+ } else {
2358
+ uwrite ! ( self . src, "\n " ) ;
2359
+ }
2350
2360
}
2351
2361
results. push ( ret) ;
2352
2362
}
2353
2363
}
2354
2364
}
2355
- Instruction :: Return { .. } if self . gen . in_import => {
2356
- match self . sig . ret . scalar {
2357
- None => {
2358
- for op in operands. iter ( ) {
2359
- self . store_in_retptr ( op) ;
2360
- }
2365
+ Instruction :: Return { .. } if self . gen . in_import => match self . sig . ret . scalar {
2366
+ None => {
2367
+ for op in operands. iter ( ) {
2368
+ self . store_in_retptr ( op) ;
2361
2369
}
2362
- Some ( Scalar :: Void ) => {
2363
- assert ! ( operands. is_empty( ) ) ;
2364
- }
2365
- Some ( Scalar :: Type ( _) ) => {
2366
- assert_eq ! ( operands. len( ) , 1 ) ;
2367
- self . src . push_str ( "return " ) ;
2368
- self . src . push_str ( & operands[ 0 ] ) ;
2369
- self . src . push_str ( ";\n " ) ;
2370
- }
2371
- Some ( Scalar :: OptionBool ( _) ) => {
2372
- assert_eq ! ( operands. len( ) , 1 ) ;
2373
- let variant = & operands[ 0 ] ;
2370
+ }
2371
+ Some ( Scalar :: Void ) => {
2372
+ assert ! ( operands. is_empty( ) ) ;
2373
+ }
2374
+ Some ( Scalar :: Type ( _) ) => {
2375
+ assert_eq ! ( operands. len( ) , 1 ) ;
2376
+ self . src . push_str ( "return " ) ;
2377
+ self . src . push_str ( & operands[ 0 ] ) ;
2378
+ self . src . push_str ( ";\n " ) ;
2379
+ }
2380
+ Some ( Scalar :: OptionBool ( o) ) => {
2381
+ assert_eq ! ( operands. len( ) , 1 ) ;
2382
+ let variant = & operands[ 0 ] ;
2383
+ if !self . gen . is_empty_type ( & o) {
2374
2384
self . store_in_retptr ( & format ! ( "{}.val" , variant) ) ;
2375
- self . src . push_str ( "return " ) ;
2376
- self . src . push_str ( & variant) ;
2377
- self . src . push_str ( ".is_some;\n " ) ;
2378
2385
}
2379
- Some ( Scalar :: ResultBool ( has_ok_type) ) => {
2380
- assert_eq ! ( operands. len( ) , 1 ) ;
2381
- let variant = & operands[ 0 ] ;
2382
- assert ! ( self . sig. retptrs. len( ) <= 2 ) ;
2383
- uwriteln ! ( self . src, "if (!{}.is_err) {{" , variant) ;
2386
+ self . src . push_str ( "return " ) ;
2387
+ self . src . push_str ( & variant) ;
2388
+ self . src . push_str ( ".is_some;\n " ) ;
2389
+ }
2390
+ Some ( Scalar :: ResultBool ( ok, err) ) => {
2391
+ assert_eq ! ( operands. len( ) , 1 ) ;
2392
+ let variant = & operands[ 0 ] ;
2393
+ assert ! ( self . sig. retptrs. len( ) <= 2 ) ;
2394
+ uwriteln ! ( self . src, "if (!{}.is_err) {{" , variant) ;
2395
+ if let Some ( _) = self . gen . get_nonempty_type ( ok. as_ref ( ) ) {
2384
2396
if self . sig . retptrs . len ( ) == 2 {
2385
2397
self . store_in_retptr ( & format ! ( "{}.val.ok" , variant) ) ;
2386
- } else if self . sig . retptrs . len ( ) == 1 && has_ok_type {
2398
+ } else if self . sig . retptrs . len ( ) == 1 && ok . is_some ( ) {
2387
2399
self . store_in_retptr ( & format ! ( "{}.val.ok" , variant) ) ;
2388
2400
}
2389
- uwriteln ! (
2390
- self . src,
2391
- " return 1;
2401
+ }
2402
+ uwriteln ! (
2403
+ self . src,
2404
+ " return 1;
2392
2405
}} else {{"
2393
- ) ;
2406
+ ) ;
2407
+ if let Some ( _) = self . gen . get_nonempty_type ( err. as_ref ( ) ) {
2394
2408
if self . sig . retptrs . len ( ) == 2 {
2395
2409
self . store_in_retptr ( & format ! ( "{}.val.err" , variant) ) ;
2396
- } else if self . sig . retptrs . len ( ) == 1 && !has_ok_type {
2410
+ } else if self . sig . retptrs . len ( ) == 1 && !ok . is_some ( ) {
2397
2411
self . store_in_retptr ( & format ! ( "{}.val.err" , variant) ) ;
2398
2412
}
2399
- uwriteln ! (
2400
- self . src,
2401
- " return 0;
2402
- }}"
2403
- ) ;
2404
2413
}
2414
+ uwriteln ! (
2415
+ self . src,
2416
+ " return 0;
2417
+ }}"
2418
+ ) ;
2405
2419
}
2406
- self . check_all_retptrs_written ( ) ;
2407
- }
2420
+ } ,
2408
2421
Instruction :: Return { amt, .. } => {
2409
2422
assert ! ( * amt <= 1 ) ;
2410
2423
if * amt == 1 {
0 commit comments