Skip to content

Commit b3aa8e7

Browse files
committed
Auto merge of #115864 - compiler-errors:rpitit-sugg, r=estebank
Suggest desugaring to return-position `impl Future` when an `async fn` in trait fails an auto trait bound First commit allows us to store the span of the `async` keyword in HIR. Second commit implements a suggestion to desugar an `async fn` to a return-position `impl Future` in trait to slightly improve the `Send` situation being discussed in #115822. This suggestion is only made when `#![feature(return_type_notation)]` is not enabled -- if it is, we should instead suggest an appropriate where-clause bound.
2 parents f73d376 + 9072415 commit b3aa8e7

File tree

31 files changed

+348
-54
lines changed

31 files changed

+348
-54
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13081308

13091309
fn lower_asyncness(&mut self, a: Async) -> hir::IsAsync {
13101310
match a {
1311-
Async::Yes { .. } => hir::IsAsync::Async,
1311+
Async::Yes { span, .. } => hir::IsAsync::Async(span),
13121312
Async::No => hir::IsAsync::NotAsync,
13131313
}
13141314
}

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
302302
if free_region.bound_region.is_named() {
303303
// A named region that is actually named.
304304
Some(RegionName { name, source: RegionNameSource::NamedFreeRegion(span) })
305-
} else if let hir::IsAsync::Async = tcx.asyncness(self.mir_hir_id().owner) {
305+
} else if tcx.asyncness(self.mir_hir_id().owner).is_async() {
306306
// If we spuriously thought that the region is named, we should let the
307307
// system generate a true name for error messages. Currently this can
308308
// happen if we have an elided name in an async fn for example: the

compiler/rustc_hir/src/hir.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,13 +2853,13 @@ impl ImplicitSelfKind {
28532853
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug)]
28542854
#[derive(HashStable_Generic)]
28552855
pub enum IsAsync {
2856-
Async,
2856+
Async(Span),
28572857
NotAsync,
28582858
}
28592859

28602860
impl IsAsync {
28612861
pub fn is_async(self) -> bool {
2862-
self == IsAsync::Async
2862+
matches!(self, IsAsync::Async(_))
28632863
}
28642864
}
28652865

@@ -3296,7 +3296,7 @@ pub struct FnHeader {
32963296

32973297
impl FnHeader {
32983298
pub fn is_async(&self) -> bool {
3299-
matches!(&self.asyncness, IsAsync::Async)
3299+
matches!(&self.asyncness, IsAsync::Async(_))
33003300
}
33013301

33023302
pub fn is_const(&self) -> bool {
@@ -4091,10 +4091,10 @@ mod size_asserts {
40914091
static_assert_size!(GenericBound<'_>, 48);
40924092
static_assert_size!(Generics<'_>, 56);
40934093
static_assert_size!(Impl<'_>, 80);
4094-
static_assert_size!(ImplItem<'_>, 80);
4095-
static_assert_size!(ImplItemKind<'_>, 32);
4096-
static_assert_size!(Item<'_>, 80);
4097-
static_assert_size!(ItemKind<'_>, 48);
4094+
static_assert_size!(ImplItem<'_>, 88);
4095+
static_assert_size!(ImplItemKind<'_>, 40);
4096+
static_assert_size!(Item<'_>, 88);
4097+
static_assert_size!(ItemKind<'_>, 56);
40984098
static_assert_size!(Local<'_>, 64);
40994099
static_assert_size!(Param<'_>, 32);
41004100
static_assert_size!(Pat<'_>, 72);
@@ -4105,8 +4105,8 @@ mod size_asserts {
41054105
static_assert_size!(Res, 12);
41064106
static_assert_size!(Stmt<'_>, 32);
41074107
static_assert_size!(StmtKind<'_>, 16);
4108-
static_assert_size!(TraitItem<'_>, 80);
4109-
static_assert_size!(TraitItemKind<'_>, 40);
4108+
static_assert_size!(TraitItem<'_>, 88);
4109+
static_assert_size!(TraitItemKind<'_>, 48);
41104110
static_assert_size!(Ty<'_>, 48);
41114111
static_assert_size!(TyKind<'_>, 32);
41124112
// tidy-alphabetical-end

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ fn compare_asyncness<'tcx>(
595595
trait_m: ty::AssocItem,
596596
delay: bool,
597597
) -> Result<(), ErrorGuaranteed> {
598-
if tcx.asyncness(trait_m.def_id) == hir::IsAsync::Async {
598+
if tcx.asyncness(trait_m.def_id).is_async() {
599599
match tcx.fn_sig(impl_m.def_id).skip_binder().skip_binder().output().kind() {
600600
ty::Alias(ty::Opaque, ..) => {
601601
// allow both `async fn foo()` and `fn foo() -> impl Future`

compiler/rustc_hir_analysis/src/check/entry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
112112
}
113113

114114
let main_asyncness = tcx.asyncness(main_def_id);
115-
if let hir::IsAsync::Async = main_asyncness {
115+
if main_asyncness.is_async() {
116116
let asyncness_span = main_fn_asyncness_span(tcx, main_def_id);
117117
tcx.sess.emit_err(errors::MainFunctionAsync { span: main_span, asyncness: asyncness_span });
118118
error = true;
@@ -212,7 +212,7 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
212212
});
213213
error = true;
214214
}
215-
if let hir::IsAsync::Async = sig.header.asyncness {
215+
if sig.header.asyncness.is_async() {
216216
let span = tcx.def_span(it.owner_id);
217217
tcx.sess.emit_err(errors::StartAsync { span: span });
218218
error = true;

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
12131213
&& let Some(generics) = self.tcx.hir().get_generics(self.tcx.local_parent(param_id))
12141214
&& let Some(param) = generics.params.iter().find(|p| p.def_id == param_id)
12151215
&& param.is_elided_lifetime()
1216-
&& let hir::IsAsync::NotAsync = self.tcx.asyncness(lifetime_ref.hir_id.owner.def_id)
1216+
&& !self.tcx.asyncness(lifetime_ref.hir_id.owner.def_id).is_async()
12171217
&& !self.tcx.features().anonymous_lifetime_in_impl_trait
12181218
{
12191219
let mut diag = rustc_session::parse::feature_err(

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ impl<'a> State<'a> {
23042304

23052305
match header.asyncness {
23062306
hir::IsAsync::NotAsync => {}
2307-
hir::IsAsync::Async => self.word_nbsp("async"),
2307+
hir::IsAsync::Async(_) => self.word_nbsp("async"),
23082308
}
23092309

23102310
self.print_unsafety(header.unsafety);

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,10 +987,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
987987
let bound_vars = self.tcx.late_bound_vars(fn_id);
988988
let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars));
989989
let ty = match self.tcx.asyncness(fn_id.owner) {
990-
hir::IsAsync::Async => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
990+
ty::Asyncness::Yes => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
991991
span_bug!(fn_decl.output.span(), "failed to get output type of async function")
992992
}),
993-
hir::IsAsync::NotAsync => ty,
993+
ty::Asyncness::No => ty,
994994
};
995995
let ty = self.normalize(expr.span, ty);
996996
if self.can_coerce(found, ty) {

compiler/rustc_lint/src/builtin.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use crate::{
4141
},
4242
EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext,
4343
};
44-
use hir::IsAsync;
4544
use rustc_ast::attr;
4645
use rustc_ast::tokenstream::{TokenStream, TokenTree};
4746
use rustc_ast::visit::{FnCtxt, FnKind};
@@ -1294,7 +1293,7 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
12941293
span: Span,
12951294
def_id: LocalDefId,
12961295
) {
1297-
if fn_kind.asyncness() == IsAsync::Async
1296+
if fn_kind.asyncness().is_async()
12981297
&& !cx.tcx.features().async_fn_track_caller
12991298
// Now, check if the function has the `#[track_caller]` attribute
13001299
&& let Some(attr) = cx.tcx.get_attr(def_id, sym::track_caller)

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ define_tables! {
439439
coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
440440
mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
441441
rendered_const: Table<DefIndex, LazyValue<String>>,
442-
asyncness: Table<DefIndex, hir::IsAsync>,
442+
asyncness: Table<DefIndex, ty::Asyncness>,
443443
fn_arg_names: Table<DefIndex, LazyArray<Ident>>,
444444
generator_kind: Table<DefIndex, LazyValue<hir::GeneratorKind>>,
445445
trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,

0 commit comments

Comments
 (0)