Skip to content

Commit 1ae1388

Browse files
authored
Rollup merge of rust-lang#125733 - compiler-errors:async-fn-assoc-item, r=fmease
Add lang items for `AsyncFn*`, `Future`, `AsyncFnKindHelper`'s associated types Adds lang items for `AsyncFnOnce::Output`, `AsyncFnOnce::CallOnceFuture`, `AsyncFnMut::CallRefFuture`, and uses them in the new solver. I'm mostly interested in doing this to help accelerate uplifting the new trait solver into a separate crate. The old solver is kind of spaghetti, so I haven't moved that to use these lang items (i.e. it still uses `item_name`-based comparisons). update: Also adds lang items for `Future::Output` and `AsyncFnKindHelper::Upvars`. cc ``@lcnr``
2 parents a34d0f1 + a03ba7f commit 1ae1388

File tree

8 files changed

+40
-62
lines changed

8 files changed

+40
-62
lines changed

compiler/rustc_hir/src/lang_items.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,18 @@ language_item_table! {
228228
AsyncFn, sym::async_fn, async_fn_trait, Target::Trait, GenericRequirement::Exact(1);
229229
AsyncFnMut, sym::async_fn_mut, async_fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);
230230
AsyncFnOnce, sym::async_fn_once, async_fn_once_trait, Target::Trait, GenericRequirement::Exact(1);
231-
AsyncFnKindHelper, sym::async_fn_kind_helper,async_fn_kind_helper, Target::Trait, GenericRequirement::Exact(1);
231+
AsyncFnOnceOutput, sym::async_fn_once_output, async_fn_once_output, Target::AssocTy, GenericRequirement::Exact(1);
232+
CallOnceFuture, sym::call_once_future, call_once_future, Target::AssocTy, GenericRequirement::Exact(1);
233+
CallRefFuture, sym::call_ref_future, call_ref_future, Target::AssocTy, GenericRequirement::Exact(2);
234+
AsyncFnKindHelper, sym::async_fn_kind_helper, async_fn_kind_helper, Target::Trait, GenericRequirement::Exact(1);
235+
AsyncFnKindUpvars, sym::async_fn_kind_upvars, async_fn_kind_upvars, Target::AssocTy, GenericRequirement::Exact(5);
232236

233237
FnOnceOutput, sym::fn_once_output, fn_once_output, Target::AssocTy, GenericRequirement::None;
234238

235239
Iterator, sym::iterator, iterator_trait, Target::Trait, GenericRequirement::Exact(0);
236240
FusedIterator, sym::fused_iterator, fused_iterator_trait, Target::Trait, GenericRequirement::Exact(0);
237241
Future, sym::future_trait, future_trait, Target::Trait, GenericRequirement::Exact(0);
242+
FutureOutput, sym::future_output, future_output, Target::AssocTy, GenericRequirement::Exact(0);
238243
AsyncIterator, sym::async_iterator, async_iterator_trait, Target::Trait, GenericRequirement::Exact(0);
239244

240245
CoroutineState, sym::coroutine_state, coroutine_state, Target::Enum, GenericRequirement::None;

compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ pub fn suggest_impl_trait<'tcx>(
14781478
),
14791479
(
14801480
infcx.tcx.lang_items().future_trait(),
1481-
infcx.tcx.get_diagnostic_item(sym::FutureOutput),
1481+
infcx.tcx.lang_items().future_output(),
14821482
format_as_assoc,
14831483
),
14841484
(

compiler/rustc_span/src/symbol.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ symbols! {
210210
FsPermissions,
211211
FusedIterator,
212212
Future,
213-
FutureOutput,
214213
GlobalAlloc,
215214
Hash,
216215
HashMap,
@@ -439,8 +438,10 @@ symbols! {
439438
async_fn,
440439
async_fn_in_trait,
441440
async_fn_kind_helper,
441+
async_fn_kind_upvars,
442442
async_fn_mut,
443443
async_fn_once,
444+
async_fn_once_output,
444445
async_fn_track_caller,
445446
async_fn_traits,
446447
async_for_loop,
@@ -498,6 +499,8 @@ symbols! {
498499
call,
499500
call_mut,
500501
call_once,
502+
call_once_future,
503+
call_ref_future,
501504
caller_location,
502505
capture_disjoint_fields,
503506
catch_unwind,
@@ -911,6 +914,7 @@ symbols! {
911914
fundamental,
912915
fused_iterator,
913916
future,
917+
future_output,
914918
future_trait,
915919
gdb_script_file,
916920
ge,

compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_macros::{TypeFoldable, TypeVisitable};
99
use rustc_middle::bug;
1010
use rustc_middle::traits::solve::Goal;
1111
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, Upcast};
12-
use rustc_span::sym;
1312

1413
use crate::solve::EvalCtxt;
1514

@@ -454,12 +453,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable<'tc
454453
.rebind(ty::TraitRef::new(tcx, future_trait_def_id, [sig.output()]))
455454
.upcast(tcx),
456455
];
457-
let future_output_def_id = tcx
458-
.associated_items(future_trait_def_id)
459-
.filter_by_name_unhygienic(sym::Output)
460-
.next()
461-
.unwrap()
462-
.def_id;
456+
let future_output_def_id = tcx.require_lang_item(LangItem::FutureOutput, None);
463457
let future_output_ty = Ty::new_projection(tcx, future_output_def_id, [sig.output()]);
464458
Ok((
465459
bound_sig.rebind(AsyncCallableRelevantTypes {
@@ -510,12 +504,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable<'tc
510504
);
511505
}
512506

513-
let future_output_def_id = tcx
514-
.associated_items(future_trait_def_id)
515-
.filter_by_name_unhygienic(sym::Output)
516-
.next()
517-
.unwrap()
518-
.def_id;
507+
let future_output_def_id = tcx.require_lang_item(LangItem::FutureOutput, None);
519508
let future_output_ty = Ty::new_projection(tcx, future_output_def_id, [sig.output()]);
520509
Ok((
521510
bound_sig.rebind(AsyncCallableRelevantTypes {
@@ -592,13 +581,7 @@ fn coroutine_closure_to_ambiguous_coroutine<'tcx>(
592581
args: ty::CoroutineClosureArgs<'tcx>,
593582
sig: ty::CoroutineClosureSignature<'tcx>,
594583
) -> Ty<'tcx> {
595-
let async_fn_kind_trait_def_id = tcx.require_lang_item(LangItem::AsyncFnKindHelper, None);
596-
let upvars_projection_def_id = tcx
597-
.associated_items(async_fn_kind_trait_def_id)
598-
.filter_by_name_unhygienic(sym::Upvars)
599-
.next()
600-
.unwrap()
601-
.def_id;
584+
let upvars_projection_def_id = tcx.require_lang_item(LangItem::AsyncFnKindUpvars, None);
602585
let tupled_upvars_ty = Ty::new_projection(
603586
tcx,
604587
upvars_projection_def_id,

compiler/rustc_trait_selection/src/solve/normalizes_to/mod.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -407,16 +407,20 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
407407
output_coroutine_ty,
408408
coroutine_return_ty,
409409
}| {
410-
let (projection_term, term) = match tcx.item_name(goal.predicate.def_id()) {
411-
sym::CallOnceFuture => (
410+
let lang_items = tcx.lang_items();
411+
let (projection_term, term) = if Some(goal.predicate.def_id())
412+
== lang_items.call_once_future()
413+
{
414+
(
412415
ty::AliasTerm::new(
413416
tcx,
414417
goal.predicate.def_id(),
415418
[goal.predicate.self_ty(), tupled_inputs_ty],
416419
),
417420
output_coroutine_ty.into(),
418-
),
419-
sym::CallRefFuture => (
421+
)
422+
} else if Some(goal.predicate.def_id()) == lang_items.call_ref_future() {
423+
(
420424
ty::AliasTerm::new(
421425
tcx,
422426
goal.predicate.def_id(),
@@ -427,8 +431,9 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
427431
],
428432
),
429433
output_coroutine_ty.into(),
430-
),
431-
sym::Output => (
434+
)
435+
} else if Some(goal.predicate.def_id()) == lang_items.async_fn_once_output() {
436+
(
432437
ty::AliasTerm::new(
433438
tcx,
434439
goal.predicate.def_id(),
@@ -438,8 +443,9 @@ impl<'tcx> assembly::GoalKind<'tcx> for NormalizesTo<'tcx> {
438443
],
439444
),
440445
coroutine_return_ty.into(),
441-
),
442-
name => bug!("no such associated type: {name}"),
446+
)
447+
} else {
448+
bug!("no such associated type in `AsyncFn*`: {:?}", goal.predicate.def_id())
443449
};
444450
ty::ProjectionPredicate { projection_term, term }
445451
},

compiler/rustc_trait_selection/src/traits/project.rs

+6-30
Original file line numberDiff line numberDiff line change
@@ -1680,14 +1680,8 @@ fn confirm_closure_candidate<'cx, 'tcx>(
16801680
args.coroutine_captures_by_ref_ty(),
16811681
)
16821682
} else {
1683-
let async_fn_kind_trait_def_id =
1684-
tcx.require_lang_item(LangItem::AsyncFnKindHelper, None);
1685-
let upvars_projection_def_id = tcx
1686-
.associated_items(async_fn_kind_trait_def_id)
1687-
.filter_by_name_unhygienic(sym::Upvars)
1688-
.next()
1689-
.unwrap()
1690-
.def_id;
1683+
let upvars_projection_def_id =
1684+
tcx.require_lang_item(LangItem::AsyncFnKindUpvars, None);
16911685
let tupled_upvars_ty = Ty::new_projection(
16921686
tcx,
16931687
upvars_projection_def_id,
@@ -1816,14 +1810,8 @@ fn confirm_async_closure_candidate<'cx, 'tcx>(
18161810
args.coroutine_captures_by_ref_ty(),
18171811
)
18181812
} else {
1819-
let async_fn_kind_trait_def_id =
1820-
tcx.require_lang_item(LangItem::AsyncFnKindHelper, None);
1821-
let upvars_projection_def_id = tcx
1822-
.associated_items(async_fn_kind_trait_def_id)
1823-
.filter_by_name_unhygienic(sym::Upvars)
1824-
.next()
1825-
.unwrap()
1826-
.def_id;
1813+
let upvars_projection_def_id =
1814+
tcx.require_lang_item(LangItem::AsyncFnKindUpvars, None);
18271815
// When we don't know the closure kind (and therefore also the closure's upvars,
18281816
// which are computed at the same time), we must delay the computation of the
18291817
// generator's upvars. We do this using the `AsyncFnKindHelper`, which as a trait
@@ -1880,13 +1868,7 @@ fn confirm_async_closure_candidate<'cx, 'tcx>(
18801868
let term = match item_name {
18811869
sym::CallOnceFuture | sym::CallRefFuture => sig.output(),
18821870
sym::Output => {
1883-
let future_trait_def_id = tcx.require_lang_item(LangItem::Future, None);
1884-
let future_output_def_id = tcx
1885-
.associated_items(future_trait_def_id)
1886-
.filter_by_name_unhygienic(sym::Output)
1887-
.next()
1888-
.unwrap()
1889-
.def_id;
1871+
let future_output_def_id = tcx.require_lang_item(LangItem::FutureOutput, None);
18901872
Ty::new_projection(tcx, future_output_def_id, [sig.output()])
18911873
}
18921874
name => bug!("no such associated type: {name}"),
@@ -1919,13 +1901,7 @@ fn confirm_async_closure_candidate<'cx, 'tcx>(
19191901
let term = match item_name {
19201902
sym::CallOnceFuture | sym::CallRefFuture => sig.output(),
19211903
sym::Output => {
1922-
let future_trait_def_id = tcx.require_lang_item(LangItem::Future, None);
1923-
let future_output_def_id = tcx
1924-
.associated_items(future_trait_def_id)
1925-
.filter_by_name_unhygienic(sym::Output)
1926-
.next()
1927-
.unwrap()
1928-
.def_id;
1904+
let future_output_def_id = tcx.require_lang_item(LangItem::FutureOutput, None);
19291905
Ty::new_projection(tcx, future_output_def_id, [sig.output()])
19301906
}
19311907
name => bug!("no such associated type: {name}"),

library/core/src/future/future.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::task::{Context, Poll};
3535
pub trait Future {
3636
/// The type of value produced on completion.
3737
#[stable(feature = "futures_api", since = "1.36.0")]
38-
#[rustc_diagnostic_item = "FutureOutput"]
38+
#[cfg_attr(not(bootstrap), lang = "future_output")]
3939
type Output;
4040

4141
/// Attempt to resolve the future to a final value, registering

library/core/src/ops/async_function.rs

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub trait AsyncFn<Args: Tuple>: AsyncFnMut<Args> {
2626
pub trait AsyncFnMut<Args: Tuple>: AsyncFnOnce<Args> {
2727
/// Future returned by [`AsyncFnMut::async_call_mut`] and [`AsyncFn::async_call`].
2828
#[unstable(feature = "async_fn_traits", issue = "none")]
29+
#[cfg_attr(not(bootstrap), lang = "call_ref_future")]
2930
type CallRefFuture<'a>: Future<Output = Self::Output>
3031
where
3132
Self: 'a;
@@ -46,10 +47,12 @@ pub trait AsyncFnMut<Args: Tuple>: AsyncFnOnce<Args> {
4647
pub trait AsyncFnOnce<Args: Tuple> {
4748
/// Future returned by [`AsyncFnOnce::async_call_once`].
4849
#[unstable(feature = "async_fn_traits", issue = "none")]
50+
#[cfg_attr(not(bootstrap), lang = "call_once_future")]
4951
type CallOnceFuture: Future<Output = Self::Output>;
5052

5153
/// Output type of the called closure's future.
5254
#[unstable(feature = "async_fn_traits", issue = "none")]
55+
#[cfg_attr(not(bootstrap), lang = "async_fn_once_output")]
5356
type Output;
5457

5558
/// Call the [`AsyncFnOnce`], returning a future which may move out of the called closure.
@@ -143,6 +146,7 @@ mod internal_implementation_detail {
143146
// `for<'env> fn() -> (&'env T, ...)`. This allows us to represent the binder
144147
// of the closure's self-capture, and these upvar types will be instantiated with
145148
// the `'closure_env` region provided to the associated type.
149+
#[cfg_attr(not(bootstrap), lang = "async_fn_kind_upvars")]
146150
type Upvars<'closure_env, Inputs, Upvars, BorrowedUpvarsAsFnPtr>;
147151
}
148152
}

0 commit comments

Comments
 (0)