Skip to content

Commit 0cd0b21

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 be46b18 commit 0cd0b21

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

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`

compiler/rustc_lint/src/async_fn_in_trait.rs

+66-5
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,87 @@ 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+
/// # #![deny(async_fn_in_trait)]
16+
/// pub trait Trait {
17+
/// async fn method(&self);
18+
/// }
19+
/// # fn main() {}
1420
/// ```
1521
///
1622
/// {{produces}}
1723
///
1824
/// ### Explanation
1925
///
20-
/// TODO
26+
/// When `async fn` is used in a trait definition, the trait does not
27+
/// promise that the opaque [`Future`] returned by the associated function
28+
/// or method will implement any [auto traits] such as [`Send`]. This may
29+
/// be surprising and may make the associated functions or methods on the
30+
/// trait less useful than intended. On traits exposed publicly from a
31+
/// crate, this may affect downstream crates whose authors cannot alter
32+
/// the trait definition.
33+
///
34+
/// For example, this code is invalid:
35+
///
36+
/// ```rust,compile_fail
37+
/// # #![feature(async_fn_in_trait)]
38+
/// pub trait Trait {
39+
/// async fn method(&self) {}
40+
/// }
41+
///
42+
/// fn test<T: Trait>(x: T) {
43+
/// fn is_send<T: Send>(_: T) {}
44+
/// is_send(x.method()); // Not OK.
45+
/// }
46+
/// ```
47+
///
48+
/// This lint exists to warn authors of publicly-reachable traits that
49+
/// they may want to consider desugaring the `async fn` to a normal `fn`
50+
/// that returns an opaque `impl Future<..> + Send` type.
51+
///
52+
/// For example, instead of:
53+
///
54+
/// ```rust
55+
/// # #![feature(async_fn_in_trait)]
56+
/// pub trait Trait {
57+
/// async fn method(&self) {}
58+
/// }
59+
/// ```
60+
///
61+
/// The author of the trait may want to write:
62+
///
63+
///
64+
/// ```rust
65+
/// # #![feature(return_position_impl_trait_in_trait)]
66+
/// use core::future::Future;
67+
/// pub trait Trait {
68+
/// fn method(&self) -> impl Future<Output = ()> + Send { async {} }
69+
/// }
70+
/// ```
71+
///
72+
/// Conversely, if the trait is used only locally, if only concrete types
73+
/// that implement the trait are used, or if the trait author otherwise
74+
/// does not care that the trait will not promise that the returned
75+
/// [`Future`] implements any [auto traits] such as [`Send`], then the
76+
/// lint may be suppressed.
77+
///
78+
/// [`Future`]: https://doc.rust-lang.org/core/future/trait.Future.html
79+
/// [`Send`]: https://doc.rust-lang.org/core/marker/trait.Send.html
80+
/// [auto traits]: https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits
2181
pub ASYNC_FN_IN_TRAIT,
2282
Warn,
23-
"TODO"
83+
"use of `async fn` in definition of a publicly-reachable trait"
2484
}
2585

2686
declare_lint_pass!(
27-
// TODO:
87+
/// Lint for use of `async fn` in the definition of a publicly-reachable
88+
/// trait.
2889
AsyncFnInTrait => [ASYNC_FN_IN_TRAIT]
2990
);
3091

tests/ui/async-await/in-trait/warn.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error: usage of `async fn` in trait is discouraged because they do not automatically have auto trait bounds
1+
error: use of `async fn` in public traits is discouraged as auto trait bounds cannot be specified
22
--> $DIR/warn.rs:7:5
33
|
44
LL | async fn not_send();
55
| ^^^^^
66
|
7-
= 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
7+
= 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`
88
note: the lint level is defined here
99
--> $DIR/warn.rs:4:9
1010
|
1111
LL | #![deny(async_fn_in_trait)]
1212
| ^^^^^^^^^^^^^^^^^
13-
help: you can alternatively desugar the `async fn` and any add additional traits such as `Send` to the signature
13+
help: you can alternatively desugar to a normal `fn` that returns `impl Future` and add any desired bounds such as `Send`
1414
|
1515
LL - async fn not_send();
1616
LL + fn not_send() -> impl std::future::Future<Output = ()> + Send;

0 commit comments

Comments
 (0)