Error on incorrect item kind in async bound

This commit is contained in:
Michael Goulet 2024-01-26 17:33:42 +00:00
parent 54db272cc9
commit 3913c9a0ca
7 changed files with 88 additions and 9 deletions

View File

@ -11,6 +11,12 @@ ast_lowering_argument = argument
ast_lowering_assoc_ty_parentheses =
parenthesized generic arguments cannot be used in associated type constraints
ast_lowering_async_bound_not_on_trait =
`async` bound modifier only allowed on trait, not `{$descr}`
ast_lowering_async_bound_only_for_fn_traits =
`async` bound modifier only allowed on `Fn`/`FnMut`/`FnOnce` traits
ast_lowering_async_coroutines_not_supported =
`async` coroutines are not yet supported

View File

@ -395,3 +395,18 @@ pub(crate) struct GenericParamDefaultInBinder {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(ast_lowering_async_bound_not_on_trait)]
pub(crate) struct AsyncBoundNotOnTrait {
#[primary_span]
pub span: Span,
pub descr: &'static str,
}
#[derive(Diagnostic)]
#[diag(ast_lowering_async_bound_only_for_fn_traits)]
pub(crate) struct AsyncBoundOnlyForFnTraits {
#[primary_span]
pub span: Span,
}

View File

@ -1,6 +1,8 @@
use crate::ImplTraitPosition;
use super::errors::{GenericTypeWithParentheses, UseAngleBrackets};
use super::errors::{
AsyncBoundNotOnTrait, AsyncBoundOnlyForFnTraits, GenericTypeWithParentheses, UseAngleBrackets,
};
use super::ResolverAstLoweringExt;
use super::{GenericArgsCtor, LifetimeRes, ParenthesizedGenericArgs};
use super::{ImplTraitContext, LoweringContext, ParamMode};
@ -42,15 +44,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// When we have an `async` kw on a bound, map the trait it resolves to.
let mut bound_modifier_allowed_features = None;
if let Some(TraitBoundModifiers { asyncness: BoundAsyncness::Async(_), .. }) = modifiers {
if let Res::Def(DefKind::Trait, def_id) = res {
if let Some((async_def_id, features)) = self.map_trait_to_async_trait(def_id) {
res = Res::Def(DefKind::Trait, async_def_id);
bound_modifier_allowed_features = Some(features);
} else {
panic!();
match res {
Res::Def(DefKind::Trait, def_id) => {
if let Some((async_def_id, features)) = self.map_trait_to_async_trait(def_id) {
res = Res::Def(DefKind::Trait, async_def_id);
bound_modifier_allowed_features = Some(features);
} else {
self.dcx().emit_err(AsyncBoundOnlyForFnTraits { span: p.span });
}
}
Res::Err => {
// No additional error.
}
_ => {
// This error isn't actually emitted AFAICT, but it's best to keep
// it around in case the resolver doesn't always check the defkind
// of an item or something.
self.dcx().emit_err(AsyncBoundNotOnTrait { span: p.span, descr: res.descr() });
}
} else {
panic!();
}
}

View File

@ -0,0 +1,13 @@
// edition:2018
#![feature(async_closure)]
struct S;
fn test(x: impl async S) {}
//~^ ERROR expected trait, found struct `S`
fn missing(x: impl async Missing) {}
//~^ ERROR cannot find trait `Missing` in this scope
fn main() {}

View File

@ -0,0 +1,16 @@
error[E0404]: expected trait, found struct `S`
--> $DIR/not-a-trait.rs:7:23
|
LL | fn test(x: impl async S) {}
| ^ not a trait
error[E0405]: cannot find trait `Missing` in this scope
--> $DIR/not-a-trait.rs:10:26
|
LL | fn missing(x: impl async Missing) {}
| ^^^^^^^ not found in this scope
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0404, E0405.
For more information about an error, try `rustc --explain E0404`.

View File

@ -0,0 +1,10 @@
// edition:2018
#![feature(async_closure)]
trait Foo {}
fn test(x: impl async Foo) {}
//~^ ERROR `async` bound modifier only allowed on `Fn`/`FnMut`/`FnOnce` traits
fn main() {}

View File

@ -0,0 +1,8 @@
error: `async` bound modifier only allowed on `Fn`/`FnMut`/`FnOnce` traits
--> $DIR/wrong-trait.rs:7:23
|
LL | fn test(x: impl async Foo) {}
| ^^^
error: aborting due to 1 previous error