Skip to content

DEMO: Implement the alternative try desugaring #98417

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
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
/// ControlFlow::Break(residual) =>
/// #[allow(unreachable_code)]
/// // If there is an enclosing `try {...}`:
/// break 'catch_target Try::from_residual(residual),
/// break 'catch_target Residual::into_try_type(residual),
/// // Otherwise:
/// return Try::from_residual(residual),
/// }
Expand Down Expand Up @@ -1571,7 +1571,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
let (residual_local, residual_local_nid) = self.pat_ident(try_span, residual_ident);
let residual_expr = self.expr_ident_mut(try_span, residual_ident, residual_local_nid);
let from_residual_expr = self.wrap_in_try_constructor(
hir::LangItem::TryTraitFromResidual,
if self.catch_scope.is_some() {
hir::LangItem::ResidualIntoTryType
} else {
hir::LangItem::TryTraitFromResidual
},
try_span,
self.arena.alloc(residual_expr),
unstable_span,
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 @@ -294,6 +294,7 @@ language_item_table! {
TryTraitFromOutput, sym::from_output, from_output_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
TryTraitBranch, sym::branch, branch_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
TryTraitFromYeet, sym::from_yeet, from_yeet_fn, Target::Fn, GenericRequirement::None;
ResidualIntoTryType, sym::into_try_type, into_try_type_fn, Target::Method(MethodKind::Trait { body: true }), GenericRequirement::None;

PollReady, sym::Ready, poll_ready_variant, Target::Variant, GenericRequirement::None;
PollPending, sym::Pending, poll_pending_variant, Target::Variant, GenericRequirement::None;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ symbols! {
intel,
into_future,
into_iter,
into_try_type,
intra_doc_pointers,
intrinsics,
irrefutable_let_patterns,
Expand Down
10 changes: 9 additions & 1 deletion library/core/src/ops/try_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,18 @@ where
/// and in the other direction,
/// `<Result<Infallible, E> as Residual<T>>::TryType = Result<T, E>`.
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
pub trait Residual<O> {
pub trait Residual<O>: Sized {
/// The "return" type of this meta-function.
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
type TryType: Try<Output = O, Residual = Self>;

/// Here for convenience in the `?` desugaring.
/// Probably should not be stabilized, as it should never be overridden.
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
#[cfg_attr(not(bootstrap), lang = "into_try_type")]
fn into_try_type(self) -> Self::TryType {
FromResidual::from_residual(self)
}
}

#[unstable(feature = "pub_crate_should_not_need_unstable_attr", issue = "none")]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/unix/kernel_copy/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn copy_specialization() -> Result<()> {
"inner Take allowed reading beyond end of file, some bytes should be left"
);

let mut sink = sink.into_inner()?;
let mut sink = sink.into_inner().map_err(io::Error::from)?;
sink.seek(SeekFrom::Start(0))?;
let mut copied = Vec::new();
sink.read_to_end(&mut copied)?;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/try-block/try-block-bad-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

pub fn main() {
let res: Result<u32, std::array::TryFromSliceError> = try {
Err("")?; //~ ERROR `?` couldn't convert the error
Err("")?; //~ ERROR mismatched types
5
};

Expand Down
13 changes: 6 additions & 7 deletions src/test/ui/try-block/try-block-bad-type.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
error[E0277]: `?` couldn't convert the error to `TryFromSliceError`
--> $DIR/try-block-bad-type.rs:7:16
error[E0308]: mismatched types
--> $DIR/try-block-bad-type.rs:7:9
|
LL | Err("")?;
| ^ the trait `From<&str>` is not implemented for `TryFromSliceError`
| ^^^^^^^^ expected struct `TryFromSliceError`, found `&str`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= help: the trait `From<Infallible>` is implemented for `TryFromSliceError`
= note: required because of the requirements on the impl of `FromResidual<Result<Infallible, &str>>` for `Result<u32, TryFromSliceError>`
= note: expected enum `Result<u32, TryFromSliceError>`
found enum `Result<_, &str>`

error[E0271]: type mismatch resolving `<Result<i32, i32> as Try>::Output == &str`
--> $DIR/try-block-bad-type.rs:12:9
Expand Down Expand Up @@ -38,5 +37,5 @@ LL | let res: i32 = try { 5 };

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0271, E0277.
Some errors have detailed explanations: E0271, E0277, E0308.
For more information about an error, try `rustc --explain E0271`.