Skip to content

Commit 59c4a92

Browse files
Resolve async fn signature even without body (in trait)
1 parent 6580010 commit 59c4a92

File tree

5 files changed

+98
-7
lines changed

5 files changed

+98
-7
lines changed

compiler/rustc_resolve/src/late.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,8 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
789789
let previous_value = self.diagnostic_metadata.current_function;
790790
match fn_kind {
791791
// Bail if the function is foreign, and thus cannot validly have
792-
// a body, or if there's no body for some other reason.
793-
FnKind::Fn(FnCtxt::Foreign, _, sig, _, generics, _)
794-
| FnKind::Fn(_, _, sig, _, generics, None) => {
792+
// a body.
793+
FnKind::Fn(FnCtxt::Foreign, _, sig, _, generics, _) => {
795794
self.visit_fn_header(&sig.header);
796795
self.visit_generics(generics);
797796
self.with_lifetime_rib(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// check-pass
2+
// edition:2021
3+
4+
#![feature(async_fn_in_trait)]
5+
#![allow(incomplete_features)]
6+
7+
use std::future::Future;
8+
9+
async fn yield_now() {}
10+
11+
trait AsyncIterator {
12+
type Item;
13+
async fn next(&mut self) -> Option<Self::Item>;
14+
}
15+
16+
struct YieldingRange {
17+
counter: u32,
18+
stop: u32,
19+
}
20+
21+
impl AsyncIterator for YieldingRange {
22+
type Item = u32;
23+
24+
async fn next(&mut self) -> Option<Self::Item> {
25+
if self.counter == self.stop {
26+
None
27+
} else {
28+
let c = self.counter;
29+
self.counter += 1;
30+
yield_now().await;
31+
Some(c)
32+
}
33+
}
34+
}
35+
36+
async fn async_main() {
37+
let mut x = YieldingRange { counter: 0, stop: 10 };
38+
39+
while let Some(v) = x.next().await {
40+
println!("Hi: {v}");
41+
}
42+
}
43+
44+
fn main() {
45+
let _ = async_main();
46+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// check-pass
2+
// edition:2021
3+
4+
#![feature(return_position_impl_trait_in_trait)]
5+
#![allow(incomplete_features)]
6+
7+
use std::future::Future;
8+
9+
async fn yield_now() {}
10+
11+
trait AsyncIterator {
12+
type Item;
13+
async fn next(&mut self) -> Option<Self::Item>;
14+
}
15+
16+
struct YieldingRange {
17+
counter: u32,
18+
stop: u32,
19+
}
20+
21+
impl AsyncIterator for YieldingRange {
22+
type Item = u32;
23+
24+
async fn next(&mut self) -> Option<Self::Item> {
25+
if self.counter == self.stop {
26+
None
27+
} else {
28+
let c = self.counter;
29+
self.counter += 1;
30+
yield_now().await;
31+
Some(c)
32+
}
33+
}
34+
}
35+
36+
async fn async_main() {
37+
let mut x = YieldingRange { counter: 0, stop: 10 };
38+
39+
while let Some(v) = x.next().await {
40+
println!("Hi: {v}");
41+
}
42+
}
43+
44+
fn main() {
45+
let _ = async_main();
46+
}

src/test/ui/resolve/issue-69401-trait-fn-no-body-ty-local.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ fn main() {}
22

33
trait Foo {
44
fn fn_with_type_named_same_as_local_in_param(b: b);
5-
//~^ ERROR cannot find type `b` in this scope [E0412]
5+
//~^ ERROR expected type, found local variable `b`
66
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0412]: cannot find type `b` in this scope
1+
error[E0573]: expected type, found local variable `b`
22
--> $DIR/issue-69401-trait-fn-no-body-ty-local.rs:4:53
33
|
44
LL | fn fn_with_type_named_same_as_local_in_param(b: b);
5-
| ^ not found in this scope
5+
| ^ not a type
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0412`.
9+
For more information about this error, try `rustc --explain E0573`.

0 commit comments

Comments
 (0)