Skip to content

Commit 4e3e6db

Browse files
committed
Auto merge of rust-lang#84767 - scottmcm:try_trait_actual, r=lcnr
Implement the new desugaring from `try_trait_v2` ~~Currently blocked on rust-lang#84782, which has a PR in rust-lang#84811 Rebased atop that fix. `try_trait_v2` tracking issue: rust-lang#84277 Unfortunately this is already touching a ton of things, so if you have suggestions for good ways to split it up, I'd be happy to hear them. (The combination between the use in the library, the compiler changes, the corresponding diagnostic differences, even MIR tests mean that I don't really have a great plan for it other than trying to have decently-readable commits. r? `@ghost` ~~(This probably shouldn't go in during the last week before the fork anyway.)~~ Fork happened.
2 parents 491cf55 + e2edee4 commit 4e3e6db

File tree

91 files changed

+1176
-798
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+1176
-798
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+26-32
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
560560
)
561561
}
562562

563-
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_ok(<expr>) }`,
564-
/// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_ok(()) }`
563+
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_output(<expr>) }`,
564+
/// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_output(()) }`
565565
/// and save the block id to use it as a break target for desugaring of the `?` operator.
566566
fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind<'hir> {
567567
self.with_catch_scope(body.id, |this| {
@@ -590,9 +590,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
590590
let ok_wrapped_span =
591591
this.mark_span_with_reason(DesugaringKind::TryBlock, tail_expr.span, None);
592592

593-
// `::std::ops::Try::from_ok($tail_expr)`
593+
// `::std::ops::Try::from_output($tail_expr)`
594594
block.expr = Some(this.wrap_in_try_constructor(
595-
hir::LangItem::TryFromOk,
595+
hir::LangItem::TryTraitFromOutput,
596596
try_span,
597597
tail_expr,
598598
ok_wrapped_span,
@@ -1579,14 +1579,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
15791579
self.allow_try_trait.clone(),
15801580
);
15811581

1582-
// `Try::into_result(<expr>)`
1582+
// `Try::branch(<expr>)`
15831583
let scrutinee = {
15841584
// expand <expr>
15851585
let sub_expr = self.lower_expr_mut(sub_expr);
15861586

15871587
self.expr_call_lang_item_fn(
15881588
unstable_span,
1589-
hir::LangItem::TryIntoResult,
1589+
hir::LangItem::TryTraitBranch,
15901590
arena_vec![self; sub_expr],
15911591
)
15921592
};
@@ -1604,8 +1604,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
16041604
};
16051605
let attrs = vec![attr];
16061606

1607-
// `Ok(val) => #[allow(unreachable_code)] val,`
1608-
let ok_arm = {
1607+
// `ControlFlow::Continue(val) => #[allow(unreachable_code)] val,`
1608+
let continue_arm = {
16091609
let val_ident = Ident::with_dummy_span(sym::val);
16101610
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
16111611
let val_expr = self.arena.alloc(self.expr_ident_with_attrs(
@@ -1614,27 +1614,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
16141614
val_pat_nid,
16151615
ThinVec::from(attrs.clone()),
16161616
));
1617-
let ok_pat = self.pat_ok(span, val_pat);
1618-
self.arm(ok_pat, val_expr)
1617+
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
1618+
self.arm(continue_pat, val_expr)
16191619
};
16201620

1621-
// `Err(err) => #[allow(unreachable_code)]
1622-
// return Try::from_error(From::from(err)),`
1623-
let err_arm = {
1624-
let err_ident = Ident::with_dummy_span(sym::err);
1625-
let (err_local, err_local_nid) = self.pat_ident(try_span, err_ident);
1626-
let from_expr = {
1627-
let err_expr = self.expr_ident_mut(try_span, err_ident, err_local_nid);
1628-
self.expr_call_lang_item_fn(
1629-
try_span,
1630-
hir::LangItem::FromFrom,
1631-
arena_vec![self; err_expr],
1632-
)
1633-
};
1634-
let from_err_expr = self.wrap_in_try_constructor(
1635-
hir::LangItem::TryFromError,
1636-
unstable_span,
1637-
from_expr,
1621+
// `ControlFlow::Break(residual) =>
1622+
// #[allow(unreachable_code)]
1623+
// return Try::from_residual(residual),`
1624+
let break_arm = {
1625+
let residual_ident = Ident::with_dummy_span(sym::residual);
1626+
let (residual_local, residual_local_nid) = self.pat_ident(try_span, residual_ident);
1627+
let residual_expr = self.expr_ident_mut(try_span, residual_ident, residual_local_nid);
1628+
let from_residual_expr = self.wrap_in_try_constructor(
1629+
hir::LangItem::TryTraitFromResidual,
1630+
try_span,
1631+
self.arena.alloc(residual_expr),
16381632
unstable_span,
16391633
);
16401634
let thin_attrs = ThinVec::from(attrs);
@@ -1645,25 +1639,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
16451639
try_span,
16461640
hir::ExprKind::Break(
16471641
hir::Destination { label: None, target_id },
1648-
Some(from_err_expr),
1642+
Some(from_residual_expr),
16491643
),
16501644
thin_attrs,
16511645
))
16521646
} else {
16531647
self.arena.alloc(self.expr(
16541648
try_span,
1655-
hir::ExprKind::Ret(Some(from_err_expr)),
1649+
hir::ExprKind::Ret(Some(from_residual_expr)),
16561650
thin_attrs,
16571651
))
16581652
};
16591653

1660-
let err_pat = self.pat_err(try_span, err_local);
1661-
self.arm(err_pat, ret_expr)
1654+
let break_pat = self.pat_cf_break(try_span, residual_local);
1655+
self.arm(break_pat, ret_expr)
16621656
};
16631657

16641658
hir::ExprKind::Match(
16651659
scrutinee,
1666-
arena_vec![self; err_arm, ok_arm],
1660+
arena_vec![self; break_arm, continue_arm],
16671661
hir::MatchSource::TryDesugar,
16681662
)
16691663
}

compiler/rustc_ast_lowering/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ pub fn lower_crate<'a, 'hir>(
332332
lifetimes_to_define: Vec::new(),
333333
is_collecting_in_band_lifetimes: false,
334334
in_scope_lifetimes: Vec::new(),
335-
allow_try_trait: Some([sym::try_trait][..].into()),
335+
allow_try_trait: Some([sym::control_flow_enum, sym::try_trait_v2][..].into()),
336336
allow_gen_future: Some([sym::gen_future][..].into()),
337337
}
338338
.lower_crate(krate)
@@ -2490,14 +2490,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24902490
self.pat(span, hir::PatKind::Lit(expr))
24912491
}
24922492

2493-
fn pat_ok(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
2493+
fn pat_cf_continue(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
24942494
let field = self.single_pat_field(span, pat);
2495-
self.pat_lang_item_variant(span, hir::LangItem::ResultOk, field)
2495+
self.pat_lang_item_variant(span, hir::LangItem::ControlFlowContinue, field)
24962496
}
24972497

2498-
fn pat_err(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
2498+
fn pat_cf_break(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
24992499
let field = self.single_pat_field(span, pat);
2500-
self.pat_lang_item_variant(span, hir::LangItem::ResultErr, field)
2500+
self.pat_lang_item_variant(span, hir::LangItem::ControlFlowBreak, field)
25012501
}
25022502

25032503
fn pat_some(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {

compiler/rustc_hir/src/lang_items.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,12 @@ language_item_table! {
308308

309309
Termination, sym::termination, termination, Target::Trait;
310310

311-
Try, kw::Try, try_trait, Target::Trait;
311+
Try, sym::Try, try_trait, Target::Trait;
312312

313313
// Language items from AST lowering
314-
TryFromError, sym::from_error, from_error_fn, Target::Method(MethodKind::Trait { body: false });
315-
TryFromOk, sym::from_ok, from_ok_fn, Target::Method(MethodKind::Trait { body: false });
316-
TryIntoResult, sym::into_result, into_result_fn, Target::Method(MethodKind::Trait { body: false });
314+
TryTraitFromResidual, sym::from_residual, from_residual_fn, Target::Method(MethodKind::Trait { body: false });
315+
TryTraitFromOutput, sym::from_output, from_output_fn, Target::Method(MethodKind::Trait { body: false });
316+
TryTraitBranch, sym::branch, branch_fn, Target::Method(MethodKind::Trait { body: false });
317317

318318
PollReady, sym::Ready, poll_ready_variant, Target::Variant;
319319
PollPending, sym::Pending, poll_pending_variant, Target::Variant;
@@ -331,6 +331,9 @@ language_item_table! {
331331
ResultOk, sym::Ok, result_ok_variant, Target::Variant;
332332
ResultErr, sym::Err, result_err_variant, Target::Variant;
333333

334+
ControlFlowContinue, sym::Continue, cf_continue_variant, Target::Variant;
335+
ControlFlowBreak, sym::Break, cf_break_variant, Target::Variant;
336+
334337
IntoIterIntoIter, sym::into_iter, into_iter_fn, Target::Method(MethodKind::Trait { body: false });
335338
IteratorNext, sym::next, next_fn, Target::Method(MethodKind::Trait { body: false});
336339

compiler/rustc_span/src/symbol.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,12 @@ symbols! {
130130
BTreeSet,
131131
BinaryHeap,
132132
Borrow,
133+
Break,
133134
C,
134135
CString,
135136
Center,
136137
Clone,
138+
Continue,
137139
Copy,
138140
Count,
139141
Debug,
@@ -326,6 +328,7 @@ symbols! {
326328
box_patterns,
327329
box_syntax,
328330
braced_empty_structs,
331+
branch,
329332
breakpoint,
330333
bridge,
331334
bswap,
@@ -411,6 +414,7 @@ symbols! {
411414
constructor,
412415
contents,
413416
context,
417+
control_flow_enum,
414418
convert,
415419
copy,
416420
copy_closures,
@@ -511,7 +515,6 @@ symbols! {
511515
env,
512516
eq,
513517
ermsb_target_feature,
514-
err,
515518
exact_div,
516519
except,
517520
exchange_malloc,
@@ -581,10 +584,10 @@ symbols! {
581584
frem_fast,
582585
from,
583586
from_desugaring,
584-
from_error,
585587
from_generator,
586588
from_method,
587-
from_ok,
589+
from_output,
590+
from_residual,
588591
from_size_align_unchecked,
589592
from_trait,
590593
from_usize,
@@ -653,7 +656,6 @@ symbols! {
653656
instruction_set,
654657
intel,
655658
into_iter,
656-
into_result,
657659
into_trait,
658660
intra_doc_pointers,
659661
intrinsics,
@@ -965,6 +967,7 @@ symbols! {
965967
repr_packed,
966968
repr_simd,
967969
repr_transparent,
970+
residual,
968971
result,
969972
result_type,
970973
rhs,
@@ -1232,7 +1235,7 @@ symbols! {
12321235
try_blocks,
12331236
try_from_trait,
12341237
try_into_trait,
1235-
try_trait,
1238+
try_trait_v2,
12361239
tt,
12371240
tuple,
12381241
tuple_from_req,

library/alloc/src/collections/vec_deque/iter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<'a, T> Iterator for Iter<'a, T> {
6666
where
6767
Self: Sized,
6868
F: FnMut(B, Self::Item) -> R,
69-
R: Try<Ok = B>,
69+
R: Try<Output = B>,
7070
{
7171
let (mut iter, final_res);
7272
if self.tail <= self.head {
@@ -140,7 +140,7 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
140140
where
141141
Self: Sized,
142142
F: FnMut(B, Self::Item) -> R,
143-
R: Try<Ok = B>,
143+
R: Try<Output = B>,
144144
{
145145
let (mut iter, final_res);
146146
if self.tail <= self.head {

library/alloc/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@
140140
#![feature(maybe_uninit_extra, maybe_uninit_slice, maybe_uninit_uninit_array)]
141141
#![feature(alloc_layout_extra)]
142142
#![feature(trusted_random_access)]
143-
#![feature(try_trait)]
143+
#![cfg_attr(bootstrap, feature(try_trait))]
144+
#![cfg_attr(not(bootstrap), feature(try_trait_v2))]
144145
#![feature(min_type_alias_impl_trait)]
145146
#![feature(associated_type_bounds)]
146147
#![feature(slice_group_by)]

library/core/src/iter/adapters/chain.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ where
9898
where
9999
Self: Sized,
100100
F: FnMut(Acc, Self::Item) -> R,
101-
R: Try<Ok = Acc>,
101+
R: Try<Output = Acc>,
102102
{
103103
if let Some(ref mut a) = self.a {
104104
acc = a.try_fold(acc, &mut f)?;
@@ -281,7 +281,7 @@ where
281281
where
282282
Self: Sized,
283283
F: FnMut(Acc, Self::Item) -> R,
284-
R: Try<Ok = Acc>,
284+
R: Try<Output = Acc>,
285285
{
286286
if let Some(ref mut b) = self.b {
287287
acc = b.try_rfold(acc, &mut f)?;

library/core/src/iter/adapters/cloned.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ where
4646
where
4747
Self: Sized,
4848
F: FnMut(B, Self::Item) -> R,
49-
R: Try<Ok = B>,
49+
R: Try<Output = B>,
5050
{
5151
self.it.try_fold(init, clone_try_fold(f))
5252
}
@@ -82,7 +82,7 @@ where
8282
where
8383
Self: Sized,
8484
F: FnMut(B, Self::Item) -> R,
85-
R: Try<Ok = B>,
85+
R: Try<Output = B>,
8686
{
8787
self.it.try_rfold(init, clone_try_fold(f))
8888
}

library/core/src/iter/adapters/copied.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ where
5050
where
5151
Self: Sized,
5252
F: FnMut(B, Self::Item) -> R,
53-
R: Try<Ok = B>,
53+
R: Try<Output = B>,
5454
{
5555
self.it.try_fold(init, copy_try_fold(f))
5656
}
@@ -98,7 +98,7 @@ where
9898
where
9999
Self: Sized,
100100
F: FnMut(B, Self::Item) -> R,
101-
R: Try<Ok = B>,
101+
R: Try<Output = B>,
102102
{
103103
self.it.try_rfold(init, copy_try_fold(f))
104104
}

library/core/src/iter/adapters/cycle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ where
5353
fn try_fold<Acc, F, R>(&mut self, mut acc: Acc, mut f: F) -> R
5454
where
5555
F: FnMut(Acc, Self::Item) -> R,
56-
R: Try<Ok = Acc>,
56+
R: Try<Output = Acc>,
5757
{
5858
// fully iterate the current iterator. this is necessary because
5959
// `self.iter` may be empty even when `self.orig` isn't

library/core/src/iter/adapters/enumerate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ where
7171
where
7272
Self: Sized,
7373
Fold: FnMut(Acc, Self::Item) -> R,
74-
R: Try<Ok = Acc>,
74+
R: Try<Output = Acc>,
7575
{
7676
#[inline]
7777
fn enumerate<'a, T, Acc, R>(
@@ -150,7 +150,7 @@ where
150150
where
151151
Self: Sized,
152152
Fold: FnMut(Acc, Self::Item) -> R,
153-
R: Try<Ok = Acc>,
153+
R: Try<Output = Acc>,
154154
{
155155
// Can safely add and subtract the count, as `ExactSizeIterator` promises
156156
// that the number of elements fits into a `usize`.

library/core/src/iter/adapters/filter.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn filter_fold<T, Acc>(
3737
move |acc, item| if predicate(&item) { fold(acc, item) } else { acc }
3838
}
3939

40-
fn filter_try_fold<'a, T, Acc, R: Try<Ok = Acc>>(
40+
fn filter_try_fold<'a, T, Acc, R: Try<Output = Acc>>(
4141
predicate: &'a mut impl FnMut(&T) -> bool,
4242
mut fold: impl FnMut(Acc, T) -> R + 'a,
4343
) -> impl FnMut(Acc, T) -> R + 'a {
@@ -88,7 +88,7 @@ where
8888
where
8989
Self: Sized,
9090
Fold: FnMut(Acc, Self::Item) -> R,
91-
R: Try<Ok = Acc>,
91+
R: Try<Output = Acc>,
9292
{
9393
self.iter.try_fold(init, filter_try_fold(&mut self.predicate, fold))
9494
}
@@ -117,7 +117,7 @@ where
117117
where
118118
Self: Sized,
119119
Fold: FnMut(Acc, Self::Item) -> R,
120-
R: Try<Ok = Acc>,
120+
R: Try<Output = Acc>,
121121
{
122122
self.iter.try_rfold(init, filter_try_fold(&mut self.predicate, fold))
123123
}

0 commit comments

Comments
 (0)