Skip to content

Commit 3001237

Browse files
traviscrosscompiler-errors
authored andcommitted
Fill in prose to describe the async_fn_in_trait lint
We're stabilizing `async fn` in trait (AFIT), but we have some reservations about how people might use this in the definitions of publicly-visible traits, so we're going to lint about that. This is a bit of an odd lint for `rustc`. We normally don't lint just to have people confirm that they understand how Rust works. But in this one exceptional case, this seems like the right thing to do as compared to the other plausible alternatives. In this commit, we describe the nature of this odd lint.
1 parent b251f2c commit 3001237

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

Diff for: compiler/rustc_lint/messages.ftl

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ lint_array_into_iter =
55
.use_explicit_into_iter_suggestion =
66
or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value
77
8-
lint_async_fn_in_trait = usage of `async fn` in trait is discouraged because they do not automatically have auto trait bounds
9-
.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
10-
.suggestion = you can alternatively desugar the `async fn` and any add additional traits such as `Send` to the signature
8+
lint_async_fn_in_trait = use of `async fn` in public traits is discouraged as auto trait bounds cannot be specified
9+
.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`
10+
.suggestion = you can alternatively desugar to a normal `fn` that returns `impl Future` and add any desired bounds such as `Send`
1111
1212
lint_atomic_ordering_fence = memory fences cannot have `Relaxed` ordering
1313
.help = consider using ordering modes `Acquire`, `Release`, `AcqRel` or `SeqCst`

Diff for: compiler/rustc_lint/src/async_fn_in_trait.rs

+64-5
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,85 @@ use rustc_hir as hir;
55
use rustc_trait_selection::traits::error_reporting::suggestions::suggest_desugaring_async_fn_to_impl_future_in_trait;
66

77
declare_lint! {
8-
/// TODO
8+
/// The `async_fn_in_trait` lint detects use of `async fn` in the
9+
/// definition of a publicly-reachable trait.
910
///
1011
/// ### Example
1112
///
1213
/// ```rust
13-
/// fn foo<T: Drop>() {}
14+
/// # #![feature(async_fn_in_trait)]
15+
/// pub trait Trait {
16+
/// async fn method(&self);
17+
/// }
1418
/// ```
1519
///
1620
/// {{produces}}
1721
///
1822
/// ### Explanation
1923
///
20-
/// TODO
24+
/// When `async fn` is used in a trait definition, the trait does not
25+
/// promise that the opaque [`Future`] returned by the associated function
26+
/// or method will implement any [auto traits] such as [`Send`]. This may
27+
/// be surprising and may make the associated functions or methods on the
28+
/// trait less useful than intended. On traits exposed publicly from a
29+
/// crate, this may affect downstream crates whose authors cannot alter
30+
/// the trait definition.
31+
///
32+
/// For example, this code is invalid:
33+
///
34+
/// ```rust,compile_fail
35+
/// # #![feature(async_fn_in_trait)]
36+
/// pub trait Trait {
37+
/// async fn method(&self) {}
38+
/// }
39+
///
40+
/// fn test<T: Trait>(x: T) {
41+
/// fn is_send<T: Send>(_: T) {}
42+
/// is_send(x.method()); // Not OK.
43+
/// }
44+
/// ```
45+
///
46+
/// This lint exists to warn authors of publicly-reachable traits that
47+
/// they may want to consider desugaring the `async fn` to a normal `fn`
48+
/// that returns an opaque `impl Future<..> + Send` type.
49+
///
50+
/// For example, instead of:
51+
///
52+
/// ```rust
53+
/// # #![feature(async_fn_in_trait)]
54+
/// pub trait Trait {
55+
/// async fn method(&self) {}
56+
/// }
57+
/// ```
58+
///
59+
/// The author of the trait may want to write:
60+
///
61+
///
62+
/// ```rust
63+
/// # #![feature(return_position_impl_trait_in_trait)]
64+
/// use core::future::Future;
65+
/// pub trait Trait {
66+
/// fn method(&self) -> impl Future<Output = ()> + Send { async {} }
67+
/// }
68+
/// ```
69+
///
70+
/// Conversely, if the trait is used only locally, if only concrete types
71+
/// that implement the trait are used, or if the trait author otherwise
72+
/// does not care that the trait will not promise that the returned
73+
/// [`Future`] implements any [auto traits] such as [`Send`], then the
74+
/// lint may be suppressed.
75+
///
76+
/// [`Future`]: https://doc.rust-lang.org/core/future/trait.Future.html
77+
/// [`Send`]: https://doc.rust-lang.org/core/marker/trait.Send.html
78+
/// [auto traits]: https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits
2179
pub ASYNC_FN_IN_TRAIT,
2280
Warn,
23-
"TODO"
81+
"use of `async fn` in definition of a publicly-reachable trait"
2482
}
2583

2684
declare_lint_pass!(
27-
// TODO:
85+
/// Lint for use of `async fn` in the definition of a publicly-reachable
86+
/// trait.
2887
AsyncFnInTrait => [ASYNC_FN_IN_TRAIT]
2988
);
3089

0 commit comments

Comments
 (0)