Skip to content

Commit 792e3b5

Browse files
Error on incorrect item kind in async bound
1 parent 97337db commit 792e3b5

File tree

7 files changed

+88
-9
lines changed

7 files changed

+88
-9
lines changed

compiler/rustc_ast_lowering/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ ast_lowering_argument = argument
1111
ast_lowering_assoc_ty_parentheses =
1212
parenthesized generic arguments cannot be used in associated type constraints
1313
14+
ast_lowering_async_bound_not_on_trait =
15+
`async` bound modifier only allowed on trait, not `{$descr}`
16+
17+
ast_lowering_async_bound_only_for_fn_traits =
18+
`async` bound modifier only allowed on `Fn`/`FnMut`/`FnOnce` traits
19+
1420
ast_lowering_async_coroutines_not_supported =
1521
`async` coroutines are not yet supported
1622

compiler/rustc_ast_lowering/src/errors.rs

+15
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,18 @@ pub(crate) struct GenericParamDefaultInBinder {
395395
#[primary_span]
396396
pub span: Span,
397397
}
398+
399+
#[derive(Diagnostic)]
400+
#[diag(ast_lowering_async_bound_not_on_trait)]
401+
pub(crate) struct AsyncBoundNotOnTrait {
402+
#[primary_span]
403+
pub span: Span,
404+
pub descr: &'static str,
405+
}
406+
407+
#[derive(Diagnostic)]
408+
#[diag(ast_lowering_async_bound_only_for_fn_traits)]
409+
pub(crate) struct AsyncBoundOnlyForFnTraits {
410+
#[primary_span]
411+
pub span: Span,
412+
}

compiler/rustc_ast_lowering/src/path.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::ImplTraitPosition;
22

3-
use super::errors::{GenericTypeWithParentheses, UseAngleBrackets};
3+
use super::errors::{
4+
AsyncBoundNotOnTrait, AsyncBoundOnlyForFnTraits, GenericTypeWithParentheses, UseAngleBrackets,
5+
};
46
use super::ResolverAstLoweringExt;
57
use super::{GenericArgsCtor, LifetimeRes, ParenthesizedGenericArgs};
68
use super::{ImplTraitContext, LoweringContext, ParamMode};
@@ -42,15 +44,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
4244
// When we have an `async` kw on a bound, map the trait it resolves to.
4345
let mut bound_modifier_allowed_features = None;
4446
if let Some(TraitBoundModifiers { asyncness: BoundAsyncness::Async(_), .. }) = modifiers {
45-
if let Res::Def(DefKind::Trait, def_id) = res {
46-
if let Some((async_def_id, features)) = self.map_trait_to_async_trait(def_id) {
47-
res = Res::Def(DefKind::Trait, async_def_id);
48-
bound_modifier_allowed_features = Some(features);
49-
} else {
50-
panic!();
47+
match res {
48+
Res::Def(DefKind::Trait, def_id) => {
49+
if let Some((async_def_id, features)) = self.map_trait_to_async_trait(def_id) {
50+
res = Res::Def(DefKind::Trait, async_def_id);
51+
bound_modifier_allowed_features = Some(features);
52+
} else {
53+
self.dcx().emit_err(AsyncBoundOnlyForFnTraits { span: p.span });
54+
}
55+
}
56+
Res::Err => {
57+
// No additional error.
58+
}
59+
_ => {
60+
// This error isn't actually emitted AFAICT, but it's best to keep
61+
// it around in case the resolver doesn't always check the defkind
62+
// of an item or something.
63+
self.dcx().emit_err(AsyncBoundNotOnTrait { span: p.span, descr: res.descr() });
5164
}
52-
} else {
53-
panic!();
5465
}
5566
}
5667

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// edition:2018
2+
3+
#![feature(async_closure)]
4+
5+
struct S;
6+
7+
fn test(x: impl async S) {}
8+
//~^ ERROR expected trait, found struct `S`
9+
10+
fn missing(x: impl async Missing) {}
11+
//~^ ERROR cannot find trait `Missing` in this scope
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0404]: expected trait, found struct `S`
2+
--> $DIR/not-a-trait.rs:7:23
3+
|
4+
LL | fn test(x: impl async S) {}
5+
| ^ not a trait
6+
7+
error[E0405]: cannot find trait `Missing` in this scope
8+
--> $DIR/not-a-trait.rs:10:26
9+
|
10+
LL | fn missing(x: impl async Missing) {}
11+
| ^^^^^^^ not found in this scope
12+
13+
error: aborting due to 2 previous errors
14+
15+
Some errors have detailed explanations: E0404, E0405.
16+
For more information about an error, try `rustc --explain E0404`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// edition:2018
2+
3+
#![feature(async_closure)]
4+
5+
trait Foo {}
6+
7+
fn test(x: impl async Foo) {}
8+
//~^ ERROR `async` bound modifier only allowed on `Fn`/`FnMut`/`FnOnce` traits
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: `async` bound modifier only allowed on `Fn`/`FnMut`/`FnOnce` traits
2+
--> $DIR/wrong-trait.rs:7:23
3+
|
4+
LL | fn test(x: impl async Foo) {}
5+
| ^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)