Skip to content

Commit afea0b4

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 28d58f6 commit afea0b4

File tree

3 files changed

+71
-11
lines changed

3 files changed

+71
-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

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

2685
declare_lint_pass!(
27-
// TODO:
86+
/// Lint for use of `async fn` in the definition of a publicly-reachable
87+
/// trait.
2888
AsyncFnInTrait => [ASYNC_FN_IN_TRAIT]
2989
);
3090

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)