Skip to content

Commit e81101f

Browse files
committed
fix: 2 async bugs
1. Async function with return stmt in loop does not work as expected. 2. Async function is missing the return check
1 parent 5136218 commit e81101f

File tree

6 files changed

+133
-15
lines changed

6 files changed

+133
-15
lines changed

src/ast/expects/test_diag.pi.expect

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,75 @@
310310
},
311311
},
312312
},
313+
PLDiag {
314+
raw: PLDiagRaw {
315+
code: Err(
316+
RETURN_TYPE_MISMATCH,
317+
),
318+
help: None,
319+
labels: [
320+
PLLabel {
321+
file: u!(""),
322+
txt: Some(
323+
(
324+
u!("expected type {}"),
325+
[
326+
u!("()"),
327+
],
328+
),
329+
),
330+
range: Range {
331+
start: Pos {
332+
line: 190,
333+
column: 5,
334+
offset: 2129,
335+
},
336+
end: Pos {
337+
line: 190,
338+
column: 5,
339+
offset: 2129,
340+
},
341+
},
342+
},
343+
PLLabel {
344+
file: u!(""),
345+
txt: Some(
346+
(
347+
u!("found type {}"),
348+
[
349+
u!("i64"),
350+
],
351+
),
352+
),
353+
range: Range {
354+
start: Pos {
355+
line: 190,
356+
column: 12,
357+
offset: 2136,
358+
},
359+
end: Pos {
360+
line: 190,
361+
column: 12,
362+
offset: 2136,
363+
},
364+
},
365+
},
366+
],
367+
source: None,
368+
range: Range {
369+
start: Pos {
370+
line: 190,
371+
column: 5,
372+
offset: 2129,
373+
},
374+
end: Pos {
375+
line: 190,
376+
column: 14,
377+
offset: 2138,
378+
},
379+
},
380+
},
381+
},
313382
PLDiag {
314383
raw: PLDiagRaw {
315384
code: Err(
@@ -401,6 +470,50 @@
401470
},
402471
},
403472
},
473+
PLDiag {
474+
raw: PLDiagRaw {
475+
code: Err(
476+
FUNCTION_MUST_HAVE_RETURN,
477+
),
478+
help: None,
479+
labels: [],
480+
source: None,
481+
range: Range {
482+
start: Pos {
483+
line: 150,
484+
column: 2,
485+
offset: 1597,
486+
},
487+
end: Pos {
488+
line: 150,
489+
column: 2,
490+
offset: 1597,
491+
},
492+
},
493+
},
494+
},
495+
PLDiag {
496+
raw: PLDiagRaw {
497+
code: Err(
498+
FUNCTION_MUST_HAVE_RETURN,
499+
),
500+
help: None,
501+
labels: [],
502+
source: None,
503+
range: Range {
504+
start: Pos {
505+
line: 191,
506+
column: 2,
507+
offset: 2140,
508+
},
509+
end: Pos {
510+
line: 191,
511+
column: 2,
512+
offset: 2140,
513+
},
514+
},
515+
},
516+
},
404517
PLDiag {
405518
raw: PLDiagRaw {
406519
code: Err(

src/ast/node/control.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,25 +327,22 @@ impl Node for WhileNode {
327327
// builder.place_safepoint(ctx);
328328
// let terminator = self.body.emit_child(ctx, builder)?.get_term();
329329

330-
let mut terminator = TerminatorEnum::None;
331330
// emit the code inside a child context because it belongs to a sub-block
332331
let mut child = ctx.new_child(self.body.range().start, builder);
333332
if !skip_body || matches!(builder, BuilderEnum::NoOp(_)) {
334333
if let Some(mut def) = gen_def {
335334
def.emit(&mut child, builder)?;
336335
}
337-
terminator = self.body.emit(&mut child, builder)?.get_term();
336+
let terminator = self.body.emit(&mut child, builder)?.get_term();
337+
if !terminator.is_return() {
338+
builder.build_unconditional_branch(cond_block);
339+
}
338340
}
339341
builder.build_dbg_location(start);
340-
builder.build_unconditional_branch(cond_block);
341342
ctx.position_at_end(after_block, builder);
342343
ctx.emit_comment_highlight(&self.comments[0]);
343344
NodeOutput::default()
344-
.with_term(if terminator.is_return() {
345-
terminator
346-
} else {
347-
TerminatorEnum::None
348-
})
345+
.with_term(TerminatorEnum::None)
349346
.to_result()
350347
}
351348
}
@@ -434,16 +431,12 @@ impl Node for ForNode {
434431
builder.build_unconditional_branch(cond_block);
435432
ctx.position_at_end(body_block, builder);
436433
builder.place_safepoint(ctx);
437-
let terminator = self.body.emit_child(ctx, builder)?.get_term();
434+
_ = self.body.emit_child(ctx, builder);
438435
builder.build_unconditional_branch(opt_block);
439436
ctx.position_at_end(after_block, builder);
440437
ctx.emit_comment_highlight(&self.comments[0]);
441438
NodeOutput::default()
442-
.with_term(if terminator == TerminatorEnum::Return {
443-
terminator
444-
} else {
445-
TerminatorEnum::None
446-
})
439+
.with_term(TerminatorEnum::None)
447440
.to_result()
448441
}
449442
}

src/ast/node/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ impl FuncDefNode {
10121012
.emit(child, builder)?
10131013
.get_term())
10141014
})?;
1015-
if !terminator.is_return() && self.generator_ty.is_none() {
1015+
if !terminator.is_return() && !self.generator_ty.is_iter() {
10161016
return Err(child.add_diag(
10171017
self.range
10181018
.end

test/lsp_diag/test_diag.pi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,7 @@ fn test_if_let_diag() void {
185185
struct test_invalid_field {
186186
f
187187
}
188+
189+
async fn test_no_return() Task<()> {
190+
return 1;
191+
}

test/main.pi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ async fn main() Task<()> {
8080
await std_test::test_nested_async_closure_in_normal_f2();
8181
await std_test::test_delay();
8282
await std_test::test_http();
83+
await std_test::test_async_loop();
8384
// await std_test::test_dns();
8485

8586
return ();

test/test/std_test.pi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ pub async fn test_dns() Task<()> {
260260
}
261261

262262

263+
pub async fn test_async_loop() Task<()> {
264+
while true {
265+
return ();
266+
}
267+
return ();
268+
}
269+
263270
pub async fn test_udp() Task<()> {
264271
let udp_t = udp::new_udp_socket();
265272
udp_t.bind("127.0.0.1", 8080 as i32);

0 commit comments

Comments
 (0)