Add async_fn_in_trait lint
This commit is contained in:
parent
2e5a9dd6c9
commit
ec79720c1e
@ -5,6 +5,10 @@ lint_array_into_iter =
|
|||||||
.use_explicit_into_iter_suggestion =
|
.use_explicit_into_iter_suggestion =
|
||||||
or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value
|
or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value
|
||||||
|
|
||||||
|
lint_async_fn_in_trait = usage of `async fn` in trait is discouraged because they do not automatically have auto trait bounds
|
||||||
|
.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
|
||||||
|
.suggestion = you can alternatively desugar the `async fn` and any add additional traits such as `Send` to the signature
|
||||||
|
|
||||||
lint_atomic_ordering_fence = memory fences cannot have `Relaxed` ordering
|
lint_atomic_ordering_fence = memory fences cannot have `Relaxed` ordering
|
||||||
.help = consider using ordering modes `Acquire`, `Release`, `AcqRel` or `SeqCst`
|
.help = consider using ordering modes `Acquire`, `Release`, `AcqRel` or `SeqCst`
|
||||||
|
|
||||||
|
58
compiler/rustc_lint/src/async_fn_in_trait.rs
Normal file
58
compiler/rustc_lint/src/async_fn_in_trait.rs
Normal file
@ -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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -50,6 +50,7 @@ extern crate rustc_session;
|
|||||||
extern crate tracing;
|
extern crate tracing;
|
||||||
|
|
||||||
mod array_into_iter;
|
mod array_into_iter;
|
||||||
|
mod async_fn_in_trait;
|
||||||
pub mod builtin;
|
pub mod builtin;
|
||||||
mod context;
|
mod context;
|
||||||
mod deref_into_dyn_supertrait;
|
mod deref_into_dyn_supertrait;
|
||||||
@ -96,6 +97,7 @@ use rustc_session::lint::builtin::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use array_into_iter::ArrayIntoIter;
|
use array_into_iter::ArrayIntoIter;
|
||||||
|
use async_fn_in_trait::AsyncFnInTrait;
|
||||||
use builtin::*;
|
use builtin::*;
|
||||||
use deref_into_dyn_supertrait::*;
|
use deref_into_dyn_supertrait::*;
|
||||||
use drop_forget_useless::*;
|
use drop_forget_useless::*;
|
||||||
@ -234,6 +236,7 @@ late_lint_methods!(
|
|||||||
MapUnitFn: MapUnitFn,
|
MapUnitFn: MapUnitFn,
|
||||||
MissingDebugImplementations: MissingDebugImplementations,
|
MissingDebugImplementations: MissingDebugImplementations,
|
||||||
MissingDoc: MissingDoc,
|
MissingDoc: MissingDoc,
|
||||||
|
AsyncFnInTrait: AsyncFnInTrait,
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
@ -1818,3 +1818,24 @@ pub struct UnusedAllocationDiag;
|
|||||||
#[derive(LintDiagnostic)]
|
#[derive(LintDiagnostic)]
|
||||||
#[diag(lint_unused_allocation_mut)]
|
#[diag(lint_unused_allocation_mut)]
|
||||||
pub struct UnusedAllocationMutDiag;
|
pub struct UnusedAllocationMutDiag;
|
||||||
|
|
||||||
|
pub struct AsyncFnInTraitDiag {
|
||||||
|
pub sugg: Option<Vec<(Span, String)>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> DecorateLint<'a, ()> for AsyncFnInTraitDiag {
|
||||||
|
fn decorate_lint<'b>(
|
||||||
|
self,
|
||||||
|
diag: &'b mut rustc_errors::DiagnosticBuilder<'a, ()>,
|
||||||
|
) -> &'b mut rustc_errors::DiagnosticBuilder<'a, ()> {
|
||||||
|
diag.note(fluent::lint_note);
|
||||||
|
if let Some(sugg) = self.sugg {
|
||||||
|
diag.multipart_suggestion(fluent::lint_suggestion, sugg, Applicability::MaybeIncorrect);
|
||||||
|
}
|
||||||
|
diag
|
||||||
|
}
|
||||||
|
|
||||||
|
fn msg(&self) -> rustc_errors::DiagnosticMessage {
|
||||||
|
fluent::lint_async_fn_in_trait
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4000,14 +4000,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
|||||||
|
|
||||||
// ... whose signature is `async` (i.e. this is an AFIT)
|
// ... whose signature is `async` (i.e. this is an AFIT)
|
||||||
let (sig, body) = item.expect_fn();
|
let (sig, body) = item.expect_fn();
|
||||||
let hir::IsAsync::Async(async_span) = sig.header.asyncness else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Ok(async_span) =
|
|
||||||
self.tcx.sess.source_map().span_extend_while(async_span, |c| c.is_whitespace())
|
|
||||||
else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let hir::FnRetTy::Return(hir::Ty { kind: hir::TyKind::OpaqueDef(def, ..), .. }) =
|
let hir::FnRetTy::Return(hir::Ty { kind: hir::TyKind::OpaqueDef(def, ..), .. }) =
|
||||||
sig.decl.output
|
sig.decl.output
|
||||||
else {
|
else {
|
||||||
@ -4021,55 +4013,17 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let future = self.tcx.hir().item(*def).expect_opaque_ty();
|
let Some(sugg) = suggest_desugaring_async_fn_to_impl_future_in_trait(
|
||||||
let Some(hir::GenericBound::LangItemTrait(_, _, _, generics)) = future.bounds.get(0) else {
|
self.tcx,
|
||||||
// `async fn` should always lower to a lang item bound... but don't ICE.
|
*sig,
|
||||||
return;
|
*body,
|
||||||
};
|
opaque_def_id.expect_local(),
|
||||||
let Some(hir::TypeBindingKind::Equality { term: hir::Term::Ty(future_output_ty) }) =
|
&format!(" + {auto_trait}"),
|
||||||
generics.bindings.get(0).map(|binding| binding.kind)
|
) else {
|
||||||
else {
|
|
||||||
// Also should never happen.
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let function_name = self.tcx.def_path_str(fn_def_id);
|
let function_name = self.tcx.def_path_str(fn_def_id);
|
||||||
|
|
||||||
let mut sugg = if future_output_ty.span.is_empty() {
|
|
||||||
vec![
|
|
||||||
(async_span, String::new()),
|
|
||||||
(
|
|
||||||
future_output_ty.span,
|
|
||||||
format!(" -> impl std::future::Future<Output = ()> + {auto_trait}"),
|
|
||||||
),
|
|
||||||
]
|
|
||||||
} else {
|
|
||||||
vec![
|
|
||||||
(
|
|
||||||
future_output_ty.span.shrink_to_lo(),
|
|
||||||
"impl std::future::Future<Output = ".to_owned(),
|
|
||||||
),
|
|
||||||
(future_output_ty.span.shrink_to_hi(), format!("> + {auto_trait}")),
|
|
||||||
(async_span, String::new()),
|
|
||||||
]
|
|
||||||
};
|
|
||||||
|
|
||||||
// If there's a body, we also need to wrap it in `async {}`
|
|
||||||
if let hir::TraitFn::Provided(body) = body {
|
|
||||||
let body = self.tcx.hir().body(*body);
|
|
||||||
let body_span = body.value.span;
|
|
||||||
let body_span_without_braces =
|
|
||||||
body_span.with_lo(body_span.lo() + BytePos(1)).with_hi(body_span.hi() - BytePos(1));
|
|
||||||
if body_span_without_braces.is_empty() {
|
|
||||||
sugg.push((body_span_without_braces, " async {} ".to_owned()));
|
|
||||||
} else {
|
|
||||||
sugg.extend([
|
|
||||||
(body_span_without_braces.shrink_to_lo(), "async {".to_owned()),
|
|
||||||
(body_span_without_braces.shrink_to_hi(), "} ".to_owned()),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err.multipart_suggestion(
|
err.multipart_suggestion(
|
||||||
format!(
|
format!(
|
||||||
"`{auto_trait}` can be made part of the associated future's \
|
"`{auto_trait}` can be made part of the associated future's \
|
||||||
@ -4321,3 +4275,65 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceImplTraitFolder<'tcx> {
|
|||||||
self.tcx
|
self.tcx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
|
||||||
|
tcx: TyCtxt<'tcx>,
|
||||||
|
sig: hir::FnSig<'tcx>,
|
||||||
|
body: hir::TraitFn<'tcx>,
|
||||||
|
opaque_def_id: LocalDefId,
|
||||||
|
add_bounds: &str,
|
||||||
|
) -> Option<Vec<(Span, String)>> {
|
||||||
|
let hir::IsAsync::Async(async_span) = sig.header.asyncness else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let Ok(async_span) = tcx.sess.source_map().span_extend_while(async_span, |c| c.is_whitespace())
|
||||||
|
else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
let future = tcx.hir().get_by_def_id(opaque_def_id).expect_item().expect_opaque_ty();
|
||||||
|
let Some(hir::GenericBound::LangItemTrait(_, _, _, generics)) = future.bounds.get(0) else {
|
||||||
|
// `async fn` should always lower to a lang item bound... but don't ICE.
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
let Some(hir::TypeBindingKind::Equality { term: hir::Term::Ty(future_output_ty) }) =
|
||||||
|
generics.bindings.get(0).map(|binding| binding.kind)
|
||||||
|
else {
|
||||||
|
// Also should never happen.
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut sugg = if future_output_ty.span.is_empty() {
|
||||||
|
vec![
|
||||||
|
(async_span, String::new()),
|
||||||
|
(
|
||||||
|
future_output_ty.span,
|
||||||
|
format!(" -> impl std::future::Future<Output = ()>{add_bounds}"),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
vec![
|
||||||
|
(future_output_ty.span.shrink_to_lo(), "impl std::future::Future<Output = ".to_owned()),
|
||||||
|
(future_output_ty.span.shrink_to_hi(), format!(">{add_bounds}")),
|
||||||
|
(async_span, String::new()),
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
// If there's a body, we also need to wrap it in `async {}`
|
||||||
|
if let hir::TraitFn::Provided(body) = body {
|
||||||
|
let body = tcx.hir().body(body);
|
||||||
|
let body_span = body.value.span;
|
||||||
|
let body_span_without_braces =
|
||||||
|
body_span.with_lo(body_span.lo() + BytePos(1)).with_hi(body_span.hi() - BytePos(1));
|
||||||
|
if body_span_without_braces.is_empty() {
|
||||||
|
sugg.push((body_span_without_braces, " async {} ".to_owned()));
|
||||||
|
} else {
|
||||||
|
sugg.extend([
|
||||||
|
(body_span_without_braces.shrink_to_lo(), "async {".to_owned()),
|
||||||
|
(body_span_without_braces.shrink_to_hi(), "} ".to_owned()),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(sugg)
|
||||||
|
}
|
||||||
|
11
tests/ui/async-await/in-trait/warn.rs
Normal file
11
tests/ui/async-await/in-trait/warn.rs
Normal file
@ -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() {}
|
20
tests/ui/async-await/in-trait/warn.stderr
Normal file
20
tests/ui/async-await/in-trait/warn.stderr
Normal file
@ -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
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user