@@ -34,67 +34,44 @@ pub fn lower_slice_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
34
34
}
35
35
}
36
36
37
- struct SliceLenPatchInformation < ' tcx > {
38
- add_statement : Statement < ' tcx > ,
39
- new_terminator_kind : TerminatorKind < ' tcx > ,
40
- }
41
-
42
37
fn lower_slice_len_call < ' tcx > (
43
38
tcx : TyCtxt < ' tcx > ,
44
39
block : & mut BasicBlockData < ' tcx > ,
45
40
local_decls : & IndexSlice < Local , LocalDecl < ' tcx > > ,
46
41
slice_len_fn_item_def_id : DefId ,
47
42
) {
48
- let mut patch_found: Option < SliceLenPatchInformation < ' _ > > = None ;
49
-
50
43
let terminator = block. terminator ( ) ;
51
- match & terminator. kind {
52
- TerminatorKind :: Call {
53
- func,
54
- args,
55
- destination,
56
- target : Some ( bb) ,
57
- call_source : CallSource :: Normal ,
58
- ..
59
- } => {
60
- // some heuristics for fast rejection
61
- if args. len ( ) != 1 {
62
- return ;
63
- }
64
- let Some ( arg) = args[ 0 ] . place ( ) else { return } ;
65
- let func_ty = func. ty ( local_decls, tcx) ;
66
- match func_ty. kind ( ) {
67
- ty:: FnDef ( fn_def_id, _) if fn_def_id == & slice_len_fn_item_def_id => {
68
- // perform modifications
69
- // from something like `_5 = core::slice::<impl [u8]>::len(move _6) -> bb1`
70
- // into:
71
- // ```
72
- // _5 = Len(*_6)
73
- // goto bb1
74
- // ```
44
+ if let TerminatorKind :: Call {
45
+ func,
46
+ args,
47
+ destination,
48
+ target : Some ( bb) ,
49
+ call_source : CallSource :: Normal ,
50
+ ..
51
+ } = & terminator. kind
52
+ // some heuristics for fast rejection
53
+ && let [ arg] = & args[ ..]
54
+ && let Some ( arg) = arg. place ( )
55
+ && let ty:: FnDef ( fn_def_id, _) = func. ty ( local_decls, tcx) . kind ( )
56
+ && * fn_def_id == slice_len_fn_item_def_id
57
+ {
58
+ // perform modifications from something like:
59
+ // _5 = core::slice::<impl [u8]>::len(move _6) -> bb1
60
+ // into:
61
+ // _5 = Len(*_6)
62
+ // goto bb1
75
63
76
- // make new RValue for Len
77
- let deref_arg = tcx. mk_place_deref ( arg) ;
78
- let r_value = Rvalue :: Len ( deref_arg) ;
79
- let len_statement_kind =
80
- StatementKind :: Assign ( Box :: new ( ( * destination, r_value) ) ) ;
81
- let add_statement =
82
- Statement { kind : len_statement_kind, source_info : terminator. source_info } ;
64
+ // make new RValue for Len
65
+ let deref_arg = tcx. mk_place_deref ( arg) ;
66
+ let r_value = Rvalue :: Len ( deref_arg) ;
67
+ let len_statement_kind =
68
+ StatementKind :: Assign ( Box :: new ( ( * destination, r_value) ) ) ;
69
+ let add_statement =
70
+ Statement { kind : len_statement_kind, source_info : terminator. source_info } ;
83
71
84
- // modify terminator into simple Goto
85
- let new_terminator_kind = TerminatorKind :: Goto { target : * bb } ;
86
-
87
- let patch = SliceLenPatchInformation { add_statement, new_terminator_kind } ;
88
-
89
- patch_found = Some ( patch) ;
90
- }
91
- _ => { }
92
- }
93
- }
94
- _ => { }
95
- }
72
+ // modify terminator into simple Goto
73
+ let new_terminator_kind = TerminatorKind :: Goto { target : * bb } ;
96
74
97
- if let Some ( SliceLenPatchInformation { add_statement, new_terminator_kind } ) = patch_found {
98
75
block. statements . push ( add_statement) ;
99
76
block. terminator_mut ( ) . kind = new_terminator_kind;
100
77
}
0 commit comments