Skip to content

Commit 823142f

Browse files
committed
Auto merge of #164122 - mejrs:no-new_ast_lowering-attrs, r=hanna-kruppe
Don't emit allow attributes in try/contract desugaring
2 parents 12433c3 + e1663b6 commit 823142f

14 files changed

Lines changed: 68 additions & 152 deletions

‎compiler/rustc_ast_lowering/src/contract.rs‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use std::sync::Arc;
22

33
use rustc_attr_ir::lang_items::LangItem;
4-
use rustc_attr_ir::target::Target;
54
use rustc_span::sym;
6-
use thin_vec::thin_vec;
75

86
use crate::LoweringContext;
97

@@ -351,9 +349,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
351349
),
352350
));
353351

354-
let attrs: rustc_ast::AttrVec = thin_vec![self.unreachable_code_attr(span)];
355-
self.lower_attrs(contract_check.hir_id, &attrs, span, Target::Expression);
356-
357352
let ret_block = self.block_all(span, arena_vec![self; ret_stmt], Some(contract_check));
358353
self.arena.alloc(self.expr_block(self.arena.alloc(ret_block)))
359354
}

‎compiler/rustc_ast_lowering/src/expr.rs‎

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_session::diagnostics::report_lit_error;
1616
use rustc_span::{
1717
ByteSymbol, DUMMY_SP, DesugaringKind, Ident, Span, Spanned, Symbol, respan, span_bug, sym,
1818
};
19-
use thin_vec::{ThinVec, thin_vec};
19+
use thin_vec::ThinVec;
2020
mod closure;
2121

2222
use crate::diagnostics::{
@@ -1933,13 +1933,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
19331933
/// Desugar `ExprKind::Try` from: `<expr>?` into:
19341934
/// ```ignore (pseudo-rust)
19351935
/// match Try::branch(<expr>) {
1936-
/// ControlFlow::Continue(val) => #[allow(unreachable_code)] val,,
1936+
/// ControlFlow::Continue(val) => val,
19371937
/// ControlFlow::Break(residual) =>
1938-
/// #[allow(unreachable_code)]
1939-
/// // If there is an enclosing `try {...}`:
1940-
/// break 'catch_target Residual::into_try_type(residual),
1938+
/// // If there is an enclosing homogeneous `try {...}`:
1939+
/// break 'catch_target residual_into_try_type(residual),
1940+
/// // If there is an enclosing heterogeneous`try {...}`:
1941+
/// break 'catch_target FromResidual::from_residual(residual),
19411942
/// // Otherwise:
1942-
/// return Try::from_residual(residual),
1943+
/// return FromResidual::from_residual(residual),
19431944
/// }
19441945
/// ```
19451946
fn lower_expr_try(&mut self, span: Span, sub_expr: &Expr) -> hir::ExprKind<'hir> {
@@ -1967,21 +1968,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
19671968
)
19681969
};
19691970

1970-
let attrs: AttrVec = thin_vec![self.unreachable_code_attr(try_span)];
1971-
1972-
// `ControlFlow::Continue(val) => #[allow(unreachable_code)] val,`
1971+
// `ControlFlow::Continue(val) => val,`
19731972
let continue_arm = {
19741973
let val_ident = Ident::with_dummy_span(sym::val);
19751974
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
19761975
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
1977-
self.lower_attrs(val_expr.hir_id, &attrs, span, Target::Expression);
19781976
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
19791977
self.arm(continue_pat, val_expr, try_span)
19801978
};
19811979

1982-
// `ControlFlow::Break(residual) =>
1983-
// #[allow(unreachable_code)]
1984-
// return Try::from_residual(residual),`
1980+
// `ControlFlow::Break(residual) => <return or break> <constructor>(residual),`
19851981
let break_arm = {
19861982
let residual_ident = Ident::with_dummy_span(sym::residual);
19871983
let (residual_local, residual_local_nid) = self.pat_ident(try_span, residual_ident);
@@ -2016,7 +2012,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
20162012
let ret_expr = self.checked_return(Some(from_residual_expr));
20172013
self.arena.alloc(self.expr(try_span, ret_expr))
20182014
};
2019-
self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression);
20202015

20212016
let break_pat = self.pat_cf_break(try_span, residual_local);
20222017
self.arm(break_pat, ret_expr, try_span)
@@ -2357,18 +2352,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
23572352
body: expr,
23582353
}
23592354
}
2360-
2361-
/// `#[allow(unreachable_code)]`
2362-
pub(super) fn unreachable_code_attr(&mut self, span: Span) -> Attribute {
2363-
let attr = attr::mk_attr_nested_word(
2364-
&self.tcx.sess.psess.attr_id_generator,
2365-
AttrStyle::Outer,
2366-
sym::allow,
2367-
sym::unreachable_code,
2368-
span,
2369-
);
2370-
attr
2371-
}
23722355
}
23732356

23742357
/// Used by [`LoweringContext::make_lowered_await`] to customize the desugaring based on what kind

‎compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8484
// `.await` operator.
8585
Some(DesugaringKind::Await) => return,
8686

87+
// Don't lint *within* contract attrs
88+
Some(DesugaringKind::Contract) => return,
8789
_ => {}
8890
}
8991

‎tests/mir-opt/building/fallible_struct_drop.build.ElaborateDrops.diff‎

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -61,53 +61,33 @@
6161
+ let mut _56: bool;
6262
scope 1 {
6363
debug residual => _9;
64-
scope 2 {
65-
}
6664
}
67-
scope 3 {
65+
scope 2 {
6866
debug val => _12;
69-
scope 4 {
70-
}
7167
}
72-
scope 5 {
68+
scope 3 {
7369
debug residual => _19;
74-
scope 6 {
75-
}
7670
}
77-
scope 7 {
71+
scope 4 {
7872
debug val => _22;
79-
scope 8 {
80-
}
8173
}
82-
scope 9 {
74+
scope 5 {
8375
debug residual => _29;
84-
scope 10 {
85-
}
8676
}
87-
scope 11 {
77+
scope 6 {
8878
debug val => _32;
89-
scope 12 {
90-
}
9179
}
92-
scope 13 {
80+
scope 7 {
9381
debug residual => _39;
94-
scope 14 {
95-
}
9682
}
97-
scope 15 {
83+
scope 8 {
9884
debug val => _42;
99-
scope 16 {
100-
}
10185
}
102-
scope 17 {
86+
scope 9 {
10387
debug residual => _49;
104-
scope 18 {
105-
}
10688
}
107-
scope 19 {
89+
scope 10 {
10890
debug val => _52;
109-
scope 20 {
110-
}
11191
}
11292

11393
bb0: {

‎tests/mir-opt/issue_62289.test.ElaborateDrops.after.panic-abort.mir‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@ fn test() -> Option<Vec<u32>> {
1515
let _11: u32;
1616
scope 1 {
1717
debug residual => _8;
18-
scope 2 {
19-
}
2018
}
21-
scope 3 {
19+
scope 2 {
2220
debug val => _11;
23-
scope 4 {
24-
}
2521
}
2622

2723
bb0: {

‎tests/mir-opt/issue_62289.test.ElaborateDrops.after.panic-unwind.mir‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@ fn test() -> Option<Vec<u32>> {
1515
let _11: u32;
1616
scope 1 {
1717
debug residual => _8;
18-
scope 2 {
19-
}
2018
}
21-
scope 3 {
19+
scope 2 {
2220
debug val => _11;
23-
scope 4 {
24-
}
2521
}
2622

2723
bb0: {

‎tests/mir-opt/jump_threading.identity.JumpThreading.panic-abort.diff‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,28 @@
1414
let _9: i32;
1515
scope 1 {
1616
debug residual => _6;
17-
scope 2 {
18-
scope 8 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<!, i32>>>::from_residual) {
19-
let mut _14: isize;
20-
let _15: i32;
21-
let mut _16: i32;
22-
let mut _17: bool;
23-
scope 9 {
24-
scope 10 (inlined <i32 as From<i32>>::from) {
25-
}
17+
scope 6 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<!, i32>>>::from_residual) {
18+
let mut _14: isize;
19+
let _15: i32;
20+
let mut _16: i32;
21+
let mut _17: bool;
22+
scope 7 {
23+
scope 8 (inlined <i32 as From<i32>>::from) {
2624
}
2725
}
2826
}
2927
}
30-
scope 3 {
28+
scope 2 {
3129
debug val => _9;
32-
scope 4 {
33-
}
3430
}
35-
scope 5 (inlined <Result<i32, i32> as Try>::branch) {
31+
scope 3 (inlined <Result<i32, i32> as Try>::branch) {
3632
let mut _10: isize;
3733
let _11: i32;
3834
let _12: i32;
3935
let mut _13: std::result::Result<!, i32>;
40-
scope 6 {
36+
scope 4 {
4137
}
42-
scope 7 {
38+
scope 5 {
4339
}
4440
}
4541

‎tests/mir-opt/jump_threading.identity.JumpThreading.panic-unwind.diff‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,28 @@
1414
let _9: i32;
1515
scope 1 {
1616
debug residual => _6;
17-
scope 2 {
18-
scope 8 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<!, i32>>>::from_residual) {
19-
let mut _14: isize;
20-
let _15: i32;
21-
let mut _16: i32;
22-
let mut _17: bool;
23-
scope 9 {
24-
scope 10 (inlined <i32 as From<i32>>::from) {
25-
}
17+
scope 6 (inlined #[track_caller] <Result<i32, i32> as FromResidual<Result<!, i32>>>::from_residual) {
18+
let mut _14: isize;
19+
let _15: i32;
20+
let mut _16: i32;
21+
let mut _17: bool;
22+
scope 7 {
23+
scope 8 (inlined <i32 as From<i32>>::from) {
2624
}
2725
}
2826
}
2927
}
30-
scope 3 {
28+
scope 2 {
3129
debug val => _9;
32-
scope 4 {
33-
}
3430
}
35-
scope 5 (inlined <Result<i32, i32> as Try>::branch) {
31+
scope 3 (inlined <Result<i32, i32> as Try>::branch) {
3632
let mut _10: isize;
3733
let _11: i32;
3834
let _12: i32;
3935
let mut _13: std::result::Result<!, i32>;
40-
scope 6 {
36+
scope 4 {
4137
}
42-
scope 7 {
38+
scope 5 {
4339
}
4440
}
4541

‎tests/mir-opt/pre-codegen/option_bubble_debug.option_traits.runtime-optimized.after.panic-abort.mir‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ fn option_traits(_1: Option<u32>) -> Option<u32> {
99
let mut _5: u32;
1010
scope 1 {
1111
debug residual => const Option::<!>::None;
12-
scope 2 {
13-
}
1412
}
15-
scope 3 {
13+
scope 2 {
1614
debug val => _4;
17-
scope 4 {
18-
}
1915
}
2016

2117
bb0: {

‎tests/mir-opt/pre-codegen/option_bubble_debug.option_traits.runtime-optimized.after.panic-unwind.mir‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ fn option_traits(_1: Option<u32>) -> Option<u32> {
99
let mut _5: u32;
1010
scope 1 {
1111
debug residual => const Option::<!>::None;
12-
scope 2 {
13-
}
1412
}
15-
scope 3 {
13+
scope 2 {
1614
debug val => _4;
17-
scope 4 {
18-
}
1915
}
2016

2117
bb0: {

0 commit comments

Comments
 (0)