Skip to content
Open
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
5 changes: 0 additions & 5 deletions compiler/rustc_ast_lowering/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::sync::Arc;

use rustc_attr_ir::lang_items::LangItem;
use rustc_attr_ir::target::Target;
use rustc_span::sym;
use thin_vec::thin_vec;

use crate::LoweringContext;

Expand Down Expand Up @@ -351,9 +349,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
),
));

let attrs: rustc_ast::AttrVec = thin_vec![self.unreachable_code_attr(span)];
self.lower_attrs(contract_check.hir_id, &attrs, span, Target::Expression);

let ret_block = self.block_all(span, arena_vec![self; ret_stmt], Some(contract_check));
self.arena.alloc(self.expr_block(self.arena.alloc(ret_block)))
}
Expand Down
35 changes: 9 additions & 26 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_session::diagnostics::report_lit_error;
use rustc_span::{
ByteSymbol, DUMMY_SP, DesugaringKind, Ident, Span, Spanned, Symbol, respan, span_bug, sym,
};
use thin_vec::{ThinVec, thin_vec};
use thin_vec::ThinVec;
mod closure;

use crate::diagnostics::{
Expand Down Expand Up @@ -1933,13 +1933,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
/// Desugar `ExprKind::Try` from: `<expr>?` into:
/// ```ignore (pseudo-rust)
/// match Try::branch(<expr>) {
/// ControlFlow::Continue(val) => #[allow(unreachable_code)] val,,
/// ControlFlow::Continue(val) => val,
/// ControlFlow::Break(residual) =>
/// #[allow(unreachable_code)]
/// // If there is an enclosing `try {...}`:
/// break 'catch_target Residual::into_try_type(residual),
/// // If there is an enclosing homogeneous `try {...}`:
/// break 'catch_target residual_into_try_type(residual),
/// // If there is an enclosing heterogeneous`try {...}`:
/// break 'catch_target FromResidual::from_residual(residual),
/// // Otherwise:
/// return Try::from_residual(residual),
/// return FromResidual::from_residual(residual),
/// }
/// ```
fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind<'hir> {
Expand Down Expand Up @@ -1967,21 +1968,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
)
};

let attrs: AttrVec = thin_vec![self.unreachable_code_attr(try_span)];

// `ControlFlow::Continue(val) => #[allow(unreachable_code)] val,`
// `ControlFlow::Continue(val) => val,`
let continue_arm = {
let val_ident = Ident::with_dummy_span(sym::val);
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
self.lower_attrs(val_expr.hir_id, &attrs, span, Target::Expression);
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
self.arm(continue_pat, val_expr, try_span)
};

// `ControlFlow::Break(residual) =>
// #[allow(unreachable_code)]
// return Try::from_residual(residual),`
// `ControlFlow::Break(residual) => <return or break> <constructor>(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);
Expand Down Expand Up @@ -2016,7 +2012,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
let ret_expr = self.checked_return(Some(from_residual_expr));
self.arena.alloc(self.expr(try_span, ret_expr))
};
self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression);

let break_pat = self.pat_cf_break(try_span, residual_local);
self.arm(break_pat, ret_expr, try_span)
Expand Down Expand Up @@ -2357,18 +2352,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
body: expr,
}
}

/// `#[allow(unreachable_code)]`
pub(super) fn unreachable_code_attr(&mut self, span: Span) -> Attribute {
let attr = attr::mk_attr_nested_word(
&self.tcx.sess.psess.attr_id_generator,
AttrStyle::Outer,
sym::allow,
sym::unreachable_code,
span,
);
attr
}
}

/// Used by [`LoweringContext::make_lowered_await`] to customize the desugaring based on what kind
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// `.await` operator.
Some(DesugaringKind::Await) => return,

// Don't lint *within* contract attrs
Some(DesugaringKind::Contract) => return,
_ => {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,53 +61,33 @@
+ let mut _56: bool;
scope 1 {
debug residual => _9;
scope 2 {
}
}
scope 3 {
scope 2 {
debug val => _12;
scope 4 {
}
}
scope 5 {
scope 3 {
debug residual => _19;
scope 6 {
}
}
scope 7 {
scope 4 {
debug val => _22;
scope 8 {
}
}
scope 9 {
scope 5 {
debug residual => _29;
scope 10 {
}
}
scope 11 {
scope 6 {
debug val => _32;
scope 12 {
}
}
scope 13 {
scope 7 {
debug residual => _39;
scope 14 {
}
}
scope 15 {
scope 8 {
debug val => _42;
scope 16 {
}
}
scope 17 {
scope 9 {
debug residual => _49;
scope 18 {
}
}
scope 19 {
scope 10 {
debug val => _52;
scope 20 {
}
}

bb0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ fn test() -> Option<Vec<u32>> {
let _11: u32;
scope 1 {
debug residual => _8;
scope 2 {
}
}
scope 3 {
scope 2 {
debug val => _11;
scope 4 {
}
}

bb0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ fn test() -> Option<Vec<u32>> {
let _11: u32;
scope 1 {
debug residual => _8;
scope 2 {
}
}
scope 3 {
scope 2 {
debug val => _11;
scope 4 {
}
}

bb0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,28 @@
let _9: i32;
scope 1 {
debug residual => _6;
scope 2 {
scope 8 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<!, i32>>>::from_residual) {
let mut _14: isize;
let _15: i32;
let mut _16: i32;
let mut _17: bool;
scope 9 {
scope 10 (inlined <i32 as From<i32>>::from) {
}
scope 6 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<!, i32>>>::from_residual) {
let mut _14: isize;
let _15: i32;
let mut _16: i32;
let mut _17: bool;
scope 7 {
scope 8 (inlined <i32 as From<i32>>::from) {
}
}
}
}
scope 3 {
scope 2 {
debug val => _9;
scope 4 {
}
}
scope 5 (inlined <Result<i32, i32> as Try>::branch) {
scope 3 (inlined <Result<i32, i32> as Try>::branch) {
let mut _10: isize;
let _11: i32;
let _12: i32;
let mut _13: std::result::Result<!, i32>;
scope 6 {
scope 4 {
}
scope 7 {
scope 5 {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,28 @@
let _9: i32;
scope 1 {
debug residual => _6;
scope 2 {
scope 8 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<!, i32>>>::from_residual) {
let mut _14: isize;
let _15: i32;
let mut _16: i32;
let mut _17: bool;
scope 9 {
scope 10 (inlined <i32 as From<i32>>::from) {
}
scope 6 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<!, i32>>>::from_residual) {
let mut _14: isize;
let _15: i32;
let mut _16: i32;
let mut _17: bool;
scope 7 {
scope 8 (inlined <i32 as From<i32>>::from) {
}
}
}
}
scope 3 {
scope 2 {
debug val => _9;
scope 4 {
}
}
scope 5 (inlined <Result<i32, i32> as Try>::branch) {
scope 3 (inlined <Result<i32, i32> as Try>::branch) {
let mut _10: isize;
let _11: i32;
let _12: i32;
let mut _13: std::result::Result<!, i32>;
scope 6 {
scope 4 {
}
scope 7 {
scope 5 {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ fn option_traits(_1: Option<u32>) -> Option<u32> {
let mut _5: u32;
scope 1 {
debug residual => const Option::<!>::None;
scope 2 {
}
}
scope 3 {
scope 2 {
debug val => _4;
scope 4 {
}
}

bb0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ fn option_traits(_1: Option<u32>) -> Option<u32> {
let mut _5: u32;
scope 1 {
debug residual => const Option::<!>::None;
scope 2 {
}
}
scope 3 {
scope 2 {
debug val => _4;
scope 4 {
}
}

bb0: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@ fn map_via_question_mark(_1: Option<i32>) -> Option<i32> {
let mut _9: i32;
scope 1 {
debug residual => const Option::<!>::None;
scope 2 {
scope 7 (inlined <Option<i32> as FromResidual<Option<!>>>::from_residual) {
let mut _3: isize;
let mut _5: bool;
}
scope 5 (inlined <Option<i32> as FromResidual<Option<!>>>::from_residual) {
let mut _3: isize;
let mut _5: bool;
}
}
scope 3 {
scope 2 {
debug val => _8;
scope 4 {
}
}
scope 5 (inlined <Option<i32> as Try>::branch) {
scope 3 (inlined <Option<i32> as Try>::branch) {
let mut _2: isize;
let _6: i32;
scope 6 {
scope 4 {
}
}

Expand Down
24 changes: 10 additions & 14 deletions tests/mir-opt/separate_const_switch.identity.JumpThreading.diff
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,27 @@
let _5: i32;
scope 1 {
debug residual => _4;
scope 2 {
scope 8 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<!, i32>>>::from_residual) {
let mut _10: isize;
let _11: i32;
let mut _12: bool;
scope 9 {
scope 10 (inlined <i32 as From<i32>>::from) {
}
scope 6 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<!, i32>>>::from_residual) {
let mut _10: isize;
let _11: i32;
let mut _12: bool;
scope 7 {
scope 8 (inlined <i32 as From<i32>>::from) {
}
}
}
}
scope 3 {
scope 2 {
debug val => _5;
scope 4 {
}
}
scope 5 (inlined <Result<i32, i32> as Try>::branch) {
scope 3 (inlined <Result<i32, i32> as Try>::branch) {
let mut _6: isize;
let _7: i32;
let _8: i32;
let mut _9: std::result::Result<!, i32>;
scope 6 {
scope 4 {
}
scope 7 {
scope 5 {
}
}

Expand Down
Loading
Loading