-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e5a9dd
commit ec79720
Showing
7 changed files
with
186 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::lints::AsyncFnInTraitDiag; | ||
use crate::LateContext; | ||
use crate::LateLintPass; | ||
use rustc_hir as hir; | ||
use rustc_trait_selection::traits::error_reporting::suggestions::suggest_desugaring_async_fn_to_impl_future_in_trait; | ||
|
||
declare_lint! { | ||
/// TODO | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// fn foo<T: Drop>() {} | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// TODO | ||
pub ASYNC_FN_IN_TRAIT, | ||
Warn, | ||
"TODO" | ||
} | ||
|
||
declare_lint_pass!( | ||
// TODO: | ||
AsyncFnInTrait => [ASYNC_FN_IN_TRAIT] | ||
); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for AsyncFnInTrait { | ||
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'tcx>) { | ||
if let hir::TraitItemKind::Fn(sig, body) = item.kind | ||
&& let hir::IsAsync::Async(async_span) = sig.header.asyncness | ||
{ | ||
if cx.tcx.features().return_type_notation { | ||
return; | ||
} | ||
|
||
let hir::FnRetTy::Return(hir::Ty { kind: hir::TyKind::OpaqueDef(def, ..), .. }) = | ||
sig.decl.output | ||
else { | ||
// This should never happen, but let's not ICE. | ||
return; | ||
}; | ||
let sugg = suggest_desugaring_async_fn_to_impl_future_in_trait( | ||
cx.tcx, | ||
sig, | ||
body, | ||
def.owner_id.def_id, | ||
" + Send", | ||
); | ||
cx.tcx.emit_spanned_lint(ASYNC_FN_IN_TRAIT, item.hir_id(), async_span, AsyncFnInTraitDiag { | ||
sugg | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// edition: 2021 | ||
|
||
#![feature(async_fn_in_trait)] | ||
#![deny(async_fn_in_trait)] | ||
|
||
trait Foo { | ||
async fn not_send(); | ||
//~^ ERROR | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: usage of `async fn` in trait is discouraged because they do not automatically have auto trait bounds | ||
--> $DIR/warn.rs:7:5 | ||
| | ||
LL | async fn not_send(); | ||
| ^^^^^ | ||
| | ||
= note: you can suppress this lint if you plan to use the trait locally, for concrete types, or do not care about auto traits like `Send` on the future | ||
note: the lint level is defined here | ||
--> $DIR/warn.rs:4:9 | ||
| | ||
LL | #![deny(async_fn_in_trait)] | ||
| ^^^^^^^^^^^^^^^^^ | ||
help: you can alternatively desugar the `async fn` and any add additional traits such as `Send` to the signature | ||
| | ||
LL - async fn not_send(); | ||
LL + fn not_send() -> impl std::future::Future<Output = ()> + Send; | ||
| | ||
|
||
error: aborting due to previous error | ||
|