-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add async_fn_in_trait
lint
#116184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add async_fn_in_trait
lint
#116184
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ec79720
Add async_fn_in_trait lint
compiler-errors 28d58f6
Bless tests
compiler-errors afea0b4
Fill in prose to describe the `async_fn_in_trait` lint
traviscross 90dfa24
Only reachable traits
compiler-errors c373d20
Address review nits
compiler-errors 2f52490
Apply suggestions from code review
tmandry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,123 @@ | ||
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! { | ||
/// The `async_fn_in_trait` lint detects use of `async fn` in the | ||
/// definition of a publicly-reachable trait. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// # #![feature(async_fn_in_trait)] | ||
/// pub trait Trait { | ||
/// async fn method(&self); | ||
/// } | ||
/// # fn main() {} | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// When `async fn` is used in a trait definition, the trait does not | ||
/// promise that the opaque [`Future`] returned by the associated function | ||
/// or method will implement any [auto traits] such as [`Send`]. This may | ||
/// be surprising and may make the associated functions or methods on the | ||
/// trait less useful than intended. On traits exposed publicly from a | ||
/// crate, this may affect downstream crates whose authors cannot alter | ||
/// the trait definition. | ||
/// | ||
/// For example, this code is invalid: | ||
/// | ||
/// ```rust,compile_fail | ||
/// # #![feature(async_fn_in_trait)] | ||
/// pub trait Trait { | ||
/// async fn method(&self) {} | ||
/// } | ||
/// | ||
/// fn test<T: Trait>(x: T) { | ||
/// fn spawn<T: Send>(_: T) {} | ||
/// spawn(x.method()); // Not OK. | ||
/// } | ||
/// ``` | ||
/// | ||
/// This lint exists to warn authors of publicly-reachable traits that | ||
/// they may want to consider desugaring the `async fn` to a normal `fn` | ||
/// that returns an opaque `impl Future<..> + Send` type. | ||
/// | ||
/// For example, instead of: | ||
/// | ||
/// ```rust | ||
/// # #![feature(async_fn_in_trait)] | ||
/// pub trait Trait { | ||
/// async fn method(&self) {} | ||
/// } | ||
/// ``` | ||
/// | ||
/// The author of the trait may want to write: | ||
/// | ||
/// | ||
/// ```rust | ||
/// # #![feature(return_position_impl_trait_in_trait)] | ||
/// use core::future::Future; | ||
/// pub trait Trait { | ||
/// fn method(&self) -> impl Future<Output = ()> + Send { async {} } | ||
/// } | ||
/// ``` | ||
/// | ||
tmandry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Conversely, if the trait is used only locally, if it is never used in | ||
/// generic functions, or if it is only used in single-threaded contexts | ||
/// that do not care whether the returned [`Future`] implements [auto traits] | ||
/// such as [`Send`], then the lint may be suppressed. | ||
tmandry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// [`Future`]: https://doc.rust-lang.org/core/future/trait.Future.html | ||
/// [`Send`]: https://doc.rust-lang.org/core/marker/trait.Send.html | ||
/// [auto traits]: https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits | ||
pub ASYNC_FN_IN_TRAIT, | ||
Warn, | ||
"use of `async fn` in definition of a publicly-reachable trait" | ||
} | ||
|
||
declare_lint_pass!( | ||
/// Lint for use of `async fn` in the definition of a publicly-reachable | ||
/// trait. | ||
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 | ||
{ | ||
// RTN can be used to bound `async fn` in traits in a better way than "always" | ||
if cx.tcx.features().return_type_notation { | ||
return; | ||
} | ||
|
||
// Only need to think about library implications of reachable traits | ||
if !cx.tcx.effective_visibilities(()).is_reachable(item.owner_id.def_id) { | ||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use std::future::Future; | ||
|
||
trait MyTrait { | ||
#[allow(async_fn_in_trait)] | ||
async fn foo(&self) -> i32; | ||
} | ||
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.