@@ -1184,7 +1184,18 @@ impl FlowHandler {
1184
1184
let action = action. make_result_handler ( None ) ;
1185
1185
let stmt = make:: expr_stmt ( action) ;
1186
1186
let block = make:: block_expr ( iter:: once ( stmt. into ( ) ) , None ) ;
1187
- let condition = make:: condition ( call_expr, None ) ;
1187
+ let controlflow_break_path = make:: path_from_text ( "ControlFlow::Break" ) ;
1188
+ let tuple_pat = make:: tuple_pat ( iter:: empty ( ) ) ;
1189
+ let condition = make:: condition (
1190
+ call_expr,
1191
+ Some (
1192
+ make:: tuple_struct_pat (
1193
+ controlflow_break_path,
1194
+ iter:: once ( tuple_pat. into ( ) ) ,
1195
+ )
1196
+ . into ( ) ,
1197
+ ) ,
1198
+ ) ;
1188
1199
make:: expr_if ( condition, block, None )
1189
1200
}
1190
1201
FlowHandler :: IfOption { action } => {
@@ -1326,7 +1337,7 @@ impl Function {
1326
1337
. unwrap_or_else ( make:: ty_placeholder) ;
1327
1338
make:: ext:: ty_result ( fun_ty. make_ty ( ctx, module) , handler_ty)
1328
1339
}
1329
- FlowHandler :: If { .. } => make:: ext :: ty_bool ( ) ,
1340
+ FlowHandler :: If { .. } => make:: ty ( "ControlFlow<()>" ) ,
1330
1341
FlowHandler :: IfOption { action } => {
1331
1342
let handler_ty = action
1332
1343
. expr_ty ( ctx)
@@ -1461,8 +1472,11 @@ fn make_body(
1461
1472
} )
1462
1473
}
1463
1474
FlowHandler :: If { .. } => {
1464
- let lit_false = make:: expr_literal ( "false" ) ;
1465
- with_tail_expr ( block, lit_false. into ( ) )
1475
+ let controlflow_continue = make:: expr_call (
1476
+ make:: expr_path ( make:: path_from_text ( "ControlFlow::Continue" ) ) ,
1477
+ make:: arg_list ( iter:: once ( make:: expr_unit ( ) ) ) ,
1478
+ ) ;
1479
+ with_tail_expr ( block, controlflow_continue. into ( ) )
1466
1480
}
1467
1481
FlowHandler :: IfOption { .. } => {
1468
1482
let none = make:: expr_path ( make:: ext:: ident_path ( "None" ) ) ;
@@ -1638,7 +1652,10 @@ fn update_external_control_flow(handler: &FlowHandler, syntax: &SyntaxNode) {
1638
1652
fn make_rewritten_flow ( handler : & FlowHandler , arg_expr : Option < ast:: Expr > ) -> Option < ast:: Expr > {
1639
1653
let value = match handler {
1640
1654
FlowHandler :: None | FlowHandler :: Try { .. } => return None ,
1641
- FlowHandler :: If { .. } => make:: expr_literal ( "true" ) . into ( ) ,
1655
+ FlowHandler :: If { .. } => make:: expr_call (
1656
+ make:: expr_path ( make:: path_from_text ( "ControlFlow::Break" ) ) ,
1657
+ make:: arg_list ( iter:: once ( make:: expr_unit ( ) ) ) ,
1658
+ ) ,
1642
1659
FlowHandler :: IfOption { .. } => {
1643
1660
let expr = arg_expr. unwrap_or_else ( || make:: expr_tuple ( Vec :: new ( ) ) ) ;
1644
1661
let args = make:: arg_list ( iter:: once ( expr) ) ;
@@ -3284,18 +3301,18 @@ fn foo() {
3284
3301
fn foo() {
3285
3302
loop {
3286
3303
let mut n = 1;
3287
- if fun_name(&mut n) {
3304
+ if let ControlFlow::Break(()) = fun_name(&mut n) {
3288
3305
break;
3289
3306
}
3290
3307
let h = 1 + n;
3291
3308
}
3292
3309
}
3293
3310
3294
- fn $0fun_name(n: &mut i32) -> bool {
3311
+ fn $0fun_name(n: &mut i32) -> ControlFlow<()> {
3295
3312
let m = *n + 1;
3296
- return true ;
3313
+ return ControlFlow::Break(()) ;
3297
3314
*n += m;
3298
- false
3315
+ ControlFlow::Continue(())
3299
3316
}
3300
3317
"# ,
3301
3318
) ;
@@ -3321,19 +3338,19 @@ fn foo() {
3321
3338
fn foo() {
3322
3339
loop {
3323
3340
let mut n = 1;
3324
- if fun_name(n) {
3341
+ if let ControlFlow::Break(()) = fun_name(n) {
3325
3342
break;
3326
3343
}
3327
3344
let h = 1;
3328
3345
}
3329
3346
}
3330
3347
3331
- fn $0fun_name(n: i32) -> bool {
3348
+ fn $0fun_name(n: i32) -> ControlFlow<()> {
3332
3349
let m = n + 1;
3333
3350
if m == 42 {
3334
- return true ;
3351
+ return ControlFlow::Break(()) ;
3335
3352
}
3336
- false
3353
+ ControlFlow::Continue(())
3337
3354
}
3338
3355
"# ,
3339
3356
) ;
0 commit comments