Skip to content

Implement #[async_send] attribute for async fns #107056

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None);

let fn_def_id = self.local_def_id(fn_node_id);
let fn_local_id = self.lower_node_id(fn_node_id).local_id;

let opaque_ty_def_id =
self.create_def(fn_def_id, opaque_ty_node_id, DefPathData::ImplTrait, opaque_ty_span);
Expand Down Expand Up @@ -1967,6 +1968,31 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
));
debug!("lower_async_fn_ret_ty: generic_params={:#?}", generic_params);

let is_async_send = this.tcx.features().async_fn_in_trait
&& (true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The true definitely isn't! (Sorry, that was left over from by debugging attempts).

The async_fn_in_trait check is probably not needed either since you can't even type this attribute without it. I'll try and remove it.

|| this.attrs.get(&fn_local_id).map_or(false, |attrs| {
attrs.into_iter().any(|attr| attr.has_name(sym::async_send))
}));

// Add Send bound if `#[async_send]` attribute is present
let bounds = if is_async_send {
let send_bound = hir::GenericBound::LangItemTrait(
hir::LangItem::Send,
this.lower_span(span),
this.next_id(),
this.arena.alloc(hir::GenericArgs {
args: &[],
bindings: &[],
parenthesized: false,
span_ext: DUMMY_SP,
}),
);

arena_vec![this; future_bound, send_bound]
} else {
arena_vec![this; future_bound]
};

let opaque_ty_item = hir::OpaqueTy {
generics: this.arena.alloc(hir::Generics {
params: generic_params,
Expand All @@ -1975,7 +2001,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
where_clause_span: this.lower_span(span),
span: this.lower_span(span),
}),
bounds: arena_vec![this; future_bound],
bounds,
origin: hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
in_trait,
};
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
// RFC 2397
gated!(do_not_recommend, Normal, template!(Word), WarnFollowing, experimental!(do_not_recommend)),

gated!(async_send, Normal, template!(Word), ErrorFollowing, async_fn_in_trait,
"`async_send` is a temporary placeholder for marking async methods as returning a future \
that is also Send and may be removed or renamed in the future."),

// ==========================================================================
// Internal attributes: Stability, deprecation, and unsafe:
// ==========================================================================
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ language_item_table! {
FnOnceOutput, sym::fn_once_output, fn_once_output, Target::AssocTy, GenericRequirement::None;

Future, sym::future_trait, future_trait, Target::Trait, GenericRequirement::Exact(0);
Send, sym::send, send_trait, Target::Trait, GenericRequirement::Exact(0);
GeneratorState, sym::generator_state, gen_state, Target::Enum, GenericRequirement::None;
Generator, sym::generator, gen_trait, Target::Trait, GenericRequirement::Minimum(1);
Unpin, sym::unpin, unpin_trait, Target::Trait, GenericRequirement::None;
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ symbols! {
async_await,
async_closure,
async_fn_in_trait,
async_send,
atomic,
atomic_mod,
atomics,
Expand Down Expand Up @@ -1297,6 +1298,7 @@ symbols! {
self_in_typedefs,
self_struct_ctor,
semitransparent,
send,
shadow_call_stack,
shl,
shl_assign,
Expand Down
1 change: 1 addition & 0 deletions library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::hash::Hasher;
/// [ub]: ../../reference/behavior-considered-undefined.html
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "Send")]
#[cfg_attr(not(bootstrap), lang = "send")]
#[rustc_on_unimplemented(
message = "`{Self}` cannot be sent between threads safely",
label = "`{Self}` cannot be sent between threads safely"
Expand Down