Skip to content
Draft
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
50 changes: 24 additions & 26 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
/// // If there is an enclosing `try {...}`:
/// break 'catch_target Residual::into_try_type(residual),
/// // Otherwise:
/// return Try::from_residual(residual),
/// return TryFromBreak::from_break(residual),
/// }
/// ```
fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind<'hir> {
Expand All @@ -1962,7 +1962,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

self.expr_call_lang_item_fn(
unstable_span,
LangItem::TryTraitBranch,
LangItem::TryOperatorBranch,
arena_vec![self; sub_expr],
)
};
Expand All @@ -1981,40 +1981,38 @@ impl<'hir> LoweringContext<'_, 'hir> {

// `ControlFlow::Break(residual) =>
// #[allow(unreachable_code)]
// return Try::from_residual(residual),`
// return TryFromBreak::from_break(residual),`
let break_arm = {
let residual_ident = Ident::with_dummy_span(sym::residual);
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 (constructor_item, target_id) = match self.try_block_scope {
TryBlockScope::Function => {
(LangItem::TryTraitFromResidual, Err(hir::LoopIdError::OutsideLoopScope))
}
TryBlockScope::Homogeneous(block_id) => {
(LangItem::ResidualIntoTryType, Ok(block_id))
}
TryBlockScope::Heterogeneous(block_id) => {
(LangItem::TryTraitFromResidual, Ok(block_id))
let constructor_item = match self.try_block_scope {
TryBlockScope::Function | TryBlockScope::Heterogeneous(_) => {
LangItem::IntoTryHeterogeneous
}
TryBlockScope::Homogeneous(_) => LangItem::IntoTryHomogeneous,
};
let from_residual_expr = self.wrap_in_try_constructor(
constructor_item,
try_span,
self.arena.alloc(residual_expr),
unstable_span,
);
let ret_expr = if target_id.is_ok() {
self.arena.alloc(self.expr(
try_span,
hir::ExprKind::Break(
hir::Destination { label: None, target_id },
Some(from_residual_expr),
),
))
} else {
let ret_expr = self.checked_return(Some(from_residual_expr));
self.arena.alloc(self.expr(try_span, ret_expr))
let ret_expr = match self.try_block_scope {
TryBlockScope::Function => {
let ret_expr = self.checked_return(Some(from_residual_expr));
self.arena.alloc(self.expr(try_span, ret_expr))
}
TryBlockScope::Homogeneous(block_id) | TryBlockScope::Heterogeneous(block_id) => {
self.arena.alloc(self.expr(
try_span,
hir::ExprKind::Break(
hir::Destination { label: None, target_id: Ok(block_id) },
Some(from_residual_expr),
),
))
}
};
self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression);

Expand All @@ -2032,12 +2030,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
/// Desugar `ExprKind::Yeet` from: `do yeet <expr>` into:
/// ```ignore(illustrative)
/// // If there is an enclosing `try {...}`:
/// break 'catch_target FromResidual::from_residual(Yeet(residual));
/// break 'catch_target TryFromBreak::from_break(Yeet(residual));
/// // Otherwise:
/// return FromResidual::from_residual(Yeet(residual));
/// return TryFromBreak::from_break(Yeet(residual));
/// ```
/// But to simplify this, there's a `from_yeet` lang item function which
/// handles the combined `FromResidual::from_residual(Yeet(residual))`.
/// handles the combined `TryFromBreak::from_break(Yeet(residual))`.
fn lower_expr_yeet(&mut self, span: Span, sub_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
// The expression (if present) or `()` otherwise.
let (yeeted_span, yeeted_expr) = if let Some(sub_expr) = sub_expr {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ macro_rules! allow {

allow! {
ALLOW_CONTRACTS: [sym::contracts_internals];
ALLOW_TRY_TRAIT: [sym::try_trait_v2, sym::try_trait_v2_residual, sym::yeet_desugar_details];
ALLOW_TRY_TRAIT: [sym::try_internals, sym::try_trait_v2, sym::yeet_desugar_details];
ALLOW_PATTERN_TYPE: [sym::pattern_types, sym::pattern_type_range_trait];
ALLOW_GEN_FUTURE: [sym::gen_future];
ALLOW_GEN_FUTURE_WITH_ASYNC_FN_TRACK_CALLER: [sym::gen_future, sym::closure_track_caller];
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_attr_ir/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ impl FormatString {
/// on(all(from_desugaring = "QuestionMark"),
/// message = "the `?` operator can only be used in {ItemContext} \
/// that returns `Result` or `Option` \
/// (or another type that implements `{FromResidual}`)",
/// (or another type that implements `{TryFromBreak}`)",
/// label = "cannot use the `?` operator in {ItemContext} that returns `{Self}`",
/// parent_label = "this function should return `Result` or `Option` to accept `?`"
/// ),
/// )]
/// pub trait FromResidual<R = <Self as Try>::Residual> {
/// pub trait TryFromBreak<R = <Self as Try>::Break> {
/// ...
/// }
///
Expand All @@ -203,8 +203,8 @@ impl FormatString {
///
/// ```rust,ignore (just an example)
/// FormatArgs {
/// this: "FromResidual",
/// this_resolved: "FromResidual<Option<!>>",
/// this: "TryFromBreak",
/// this_resolved: "TryFromBreak<Option<!>>",
/// item_context: "an async function",
/// generic_args: [("Self", "u32"), ("R", "Option<!>")],
/// }
Expand Down Expand Up @@ -419,12 +419,12 @@ pub enum LitOrArg {
/// on(all(from_desugaring = "QuestionMark"),
/// message = "the `?` operator can only be used in {ItemContext} \
/// that returns `Result` or `Option` \
/// (or another type that implements `{FromResidual}`)",
/// (or another type that implements `{TryFromBreak}`)",
/// label = "cannot use the `?` operator in {ItemContext} that returns `{Self}`",
/// parent_label = "this function should return `Result` or `Option` to accept `?`"
/// ),
/// )]
/// pub trait FromResidual<R = <Self as Try>::Residual> {
/// pub trait TryFromBreak<R = <Self as Try>::Break> {
/// ...
/// }
///
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_attr_ir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,11 @@ language_item_table! {
SliceLen, sym::slice_len_fn, slice_len_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None;

// Language items from AST lowering
TryTraitFromResidual, sym::from_residual, from_residual_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
IntoTryHeterogeneous, sym::into_try_heterogeneous, into_try_heterogeneous_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None;
IntoTryHomogeneous, sym::into_try_homogeneous, into_try_homogeneous_fn, Target::Method(MethodKind::Inherent), GenericRequirement::None;
TryOperatorBranch, sym::try_operator_branch, try_operator_branch_fn, Target::Fn, GenericRequirement::None;
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::Fn, GenericRequirement::None;

CoercePointeeValidated, sym::coerce_pointee_validated, coerce_pointee_validated_trait, Target::Trait, GenericRequirement::Exact(0);

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,8 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
}
UseSpans::PatUse(span)
| UseSpans::OtherUse(span)
| UseSpans::FnSelfUse { var_span: span, .. } => {
| UseSpans::FnSelfUse { var_span: span, .. }
| UseSpans::QuestionMarkUse(span) => {
let block = &self.body.basic_blocks[location.block];

let kind = if let Some(&Statement {
Expand Down
Loading
Loading