Error on incorrect item kind in async bound
This commit is contained in:
parent
54db272cc9
commit
3913c9a0ca
@ -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
|
||||
|
||||
|
@ -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,
|
||||
}
|
||||
|
@ -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!();
|
||||
}
|
||||
}
|
||||
|
||||
|
13
tests/ui/async-await/async-fn/not-a-trait.rs
Normal file
13
tests/ui/async-await/async-fn/not-a-trait.rs
Normal 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() {}
|
16
tests/ui/async-await/async-fn/not-a-trait.stderr
Normal file
16
tests/ui/async-await/async-fn/not-a-trait.stderr
Normal 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`.
|
10
tests/ui/async-await/async-fn/wrong-trait.rs
Normal file
10
tests/ui/async-await/async-fn/wrong-trait.rs
Normal 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() {}
|
8
tests/ui/async-await/async-fn/wrong-trait.stderr
Normal file
8
tests/ui/async-await/async-fn/wrong-trait.stderr
Normal 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user