@@ -670,9 +670,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
670670 origin = updated. 1 ;
671671
672672 let ( place, capture_kind) = match capture_clause {
673- hir:: CaptureBy :: Value { .. } | hir:: CaptureBy :: Use { .. } => {
674- adjust_for_move_closure ( place, capture_kind)
675- }
673+ hir:: CaptureBy :: Value { .. } => adjust_for_move_closure ( place, capture_kind) ,
674+ hir:: CaptureBy :: Use { .. } => adjust_for_use_closure ( place, capture_kind) ,
676675 hir:: CaptureBy :: Ref => adjust_for_non_move_closure ( place, capture_kind) ,
677676 } ;
678677
@@ -1307,7 +1306,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13071306 for captured_place in root_var_min_capture_list. iter ( ) {
13081307 match captured_place. info . capture_kind {
13091308 // Only care about captures that are moved into the closure
1310- ty:: UpvarCapture :: ByValue => {
1309+ ty:: UpvarCapture :: ByValue | ty :: UpvarCapture :: ByUse => {
13111310 projections_list. push ( captured_place. place . projections . as_slice ( ) ) ;
13121311 diagnostics_info. insert ( UpvarMigrationInfo :: CapturingPrecise {
13131312 source_expr : captured_place. info . path_expr_id ,
@@ -1931,7 +1930,7 @@ fn apply_capture_kind_on_capture_ty<'tcx>(
19311930 region : ty:: Region < ' tcx > ,
19321931) -> Ty < ' tcx > {
19331932 match capture_kind {
1934- ty:: UpvarCapture :: ByValue => ty,
1933+ ty:: UpvarCapture :: ByValue | ty :: UpvarCapture :: ByUse => ty,
19351934 ty:: UpvarCapture :: ByRef ( kind) => Ty :: new_ref ( tcx, region, ty, kind. to_mutbl_lossy ( ) ) ,
19361935 }
19371936}
@@ -2168,6 +2167,20 @@ fn adjust_for_move_closure(
21682167 ( place, ty:: UpvarCapture :: ByValue )
21692168}
21702169
2170+ /// Truncate deref of any reference.
2171+ fn adjust_for_use_closure (
2172+ mut place : Place < ' _ > ,
2173+ mut kind : ty:: UpvarCapture ,
2174+ ) -> ( Place < ' _ > , ty:: UpvarCapture ) {
2175+ let first_deref = place. projections . iter ( ) . position ( |proj| proj. kind == ProjectionKind :: Deref ) ;
2176+
2177+ if let Some ( idx) = first_deref {
2178+ truncate_place_to_len_and_update_capture_kind ( & mut place, & mut kind, idx) ;
2179+ }
2180+
2181+ ( place, ty:: UpvarCapture :: ByUse )
2182+ }
2183+
21712184/// Adjust closure capture just that if taking ownership of data, only move data
21722185/// from enclosing stack frame.
21732186fn adjust_for_non_move_closure (
@@ -2178,7 +2191,7 @@ fn adjust_for_non_move_closure(
21782191 place. projections . iter ( ) . position ( |proj| proj. kind == ProjectionKind :: Deref ) ;
21792192
21802193 match kind {
2181- ty:: UpvarCapture :: ByValue => {
2194+ ty:: UpvarCapture :: ByValue | ty :: UpvarCapture :: ByUse => {
21822195 if let Some ( idx) = contains_deref {
21832196 truncate_place_to_len_and_update_capture_kind ( & mut place, & mut kind, idx) ;
21842197 }
@@ -2223,6 +2236,7 @@ fn construct_capture_kind_reason_string<'tcx>(
22232236
22242237 let capture_kind_str = match capture_info. capture_kind {
22252238 ty:: UpvarCapture :: ByValue => "ByValue" . into ( ) ,
2239+ ty:: UpvarCapture :: ByUse => "ByUse" . into ( ) ,
22262240 ty:: UpvarCapture :: ByRef ( kind) => format ! ( "{kind:?}" ) ,
22272241 } ;
22282242
@@ -2244,6 +2258,7 @@ fn construct_capture_info_string<'tcx>(
22442258
22452259 let capture_kind_str = match capture_info. capture_kind {
22462260 ty:: UpvarCapture :: ByValue => "ByValue" . into ( ) ,
2261+ ty:: UpvarCapture :: ByUse => "ByUse" . into ( ) ,
22472262 ty:: UpvarCapture :: ByRef ( kind) => format ! ( "{kind:?}" ) ,
22482263 } ;
22492264 format ! ( "{place_str} -> {capture_kind_str}" )
@@ -2339,8 +2354,11 @@ fn determine_capture_info(
23392354 // expressions.
23402355 let eq_capture_kind = match ( capture_info_a. capture_kind , capture_info_b. capture_kind ) {
23412356 ( ty:: UpvarCapture :: ByValue , ty:: UpvarCapture :: ByValue ) => true ,
2357+ ( ty:: UpvarCapture :: ByUse , ty:: UpvarCapture :: ByUse ) => true ,
23422358 ( ty:: UpvarCapture :: ByRef ( ref_a) , ty:: UpvarCapture :: ByRef ( ref_b) ) => ref_a == ref_b,
2343- ( ty:: UpvarCapture :: ByValue , _) | ( ty:: UpvarCapture :: ByRef ( _) , _) => false ,
2359+ ( ty:: UpvarCapture :: ByValue , _)
2360+ | ( ty:: UpvarCapture :: ByUse , _)
2361+ | ( ty:: UpvarCapture :: ByRef ( _) , _) => false ,
23442362 } ;
23452363
23462364 if eq_capture_kind {
@@ -2350,8 +2368,10 @@ fn determine_capture_info(
23502368 }
23512369 } else {
23522370 // We select the CaptureKind which ranks higher based the following priority order:
2353- // ByValue > MutBorrow > UniqueImmBorrow > ImmBorrow
2371+ // (ByUse | ByValue) > MutBorrow > UniqueImmBorrow > ImmBorrow
23542372 match ( capture_info_a. capture_kind , capture_info_b. capture_kind ) {
2373+ ( ty:: UpvarCapture :: ByUse , _) => capture_info_a,
2374+ ( _, ty:: UpvarCapture :: ByUse ) => capture_info_b,
23552375 ( ty:: UpvarCapture :: ByValue , _) => capture_info_a,
23562376 ( _, ty:: UpvarCapture :: ByValue ) => capture_info_b,
23572377 ( ty:: UpvarCapture :: ByRef ( ref_a) , ty:: UpvarCapture :: ByRef ( ref_b) ) => {
@@ -2405,7 +2425,7 @@ fn truncate_place_to_len_and_update_capture_kind<'tcx>(
24052425 }
24062426
24072427 ty:: UpvarCapture :: ByRef ( ..) => { }
2408- ty:: UpvarCapture :: ByValue => { }
2428+ ty:: UpvarCapture :: ByValue | ty :: UpvarCapture :: ByUse => { }
24092429 }
24102430
24112431 place. projections . truncate ( len) ;
0 commit comments