Skip to content

Commit

Permalink
Error on incorrect item kind in async bound
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jan 31, 2024
1 parent 54db272 commit 3913c9a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 9 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_ast_lowering/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_ast_lowering/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
29 changes: 20 additions & 9 deletions compiler/rustc_ast_lowering/src/path.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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!();
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/ui/async-await/async-fn/not-a-trait.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions tests/ui/async-await/async-fn/not-a-trait.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions tests/ui/async-await/async-fn/wrong-trait.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions tests/ui/async-await/async-fn/wrong-trait.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3913c9a

Please sign in to comment.