@@ -104,24 +104,53 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
104104 pub ( in super :: super ) fn check_repeat_exprs ( & self ) {
105105 let mut deferred_repeat_expr_checks = self . deferred_repeat_expr_checks . borrow_mut ( ) ;
106106 debug ! ( "FnCtxt::check_repeat_exprs: {} deferred checks" , deferred_repeat_expr_checks. len( ) ) ;
107- for ( element, element_ty, count) in deferred_repeat_expr_checks. drain ( ..) {
108- // We want to emit an error if the const is not structurally resolveable as otherwise
109- // we can find up conservatively proving `Copy` which may infer the repeat expr count
110- // to something that never required `Copy` in the first place.
111- let count =
112- self . structurally_resolve_const ( element. span , self . normalize ( element. span , count) ) ;
113-
114- // Avoid run on "`NotCopy: Copy` is not implemented" errors when the repeat expr count
115- // is erroneous/unknown. The user might wind up specifying a repeat count of 0/1.
116- if count. references_error ( ) {
117- continue ;
118- }
119107
120- // If the length is 0, we don't create any elements, so we don't copy any.
121- // If the length is 1, we don't copy that one element, we move it. Only check
122- // for `Copy` if the length is larger.
123- if count. try_to_target_usize ( self . tcx ) . is_none_or ( |x| x > 1 ) {
124- self . enforce_repeat_element_needs_copy_bound ( element, element_ty) ;
108+ let deferred_repeat_expr_checks = deferred_repeat_expr_checks
109+ . drain ( ..)
110+ . flat_map ( |( element, element_ty, count) | {
111+ // We want to emit an error if the const is not structurally resolveable as otherwise
112+ // we can find up conservatively proving `Copy` which may infer the repeat expr count
113+ // to something that never required `Copy` in the first place.
114+ let count = self
115+ . structurally_resolve_const ( element. span , self . normalize ( element. span , count) ) ;
116+
117+ // Avoid run on "`NotCopy: Copy` is not implemented" errors when the repeat expr count
118+ // is erroneous/unknown. The user might wind up specifying a repeat count of 0/1.
119+ if count. references_error ( ) {
120+ return None ;
121+ }
122+
123+ Some ( ( element, element_ty, count) )
124+ } )
125+ // We collect to force the side effects of structurally resolving the repeat count to happen in one
126+ // go, to avoid side effects from proving `Copy` affecting whether repeat counts are known or not.
127+ // If we did not do this we would get results that depend on the order that we evaluate each repeat
128+ // expr's `Copy` check.
129+ . collect :: < Vec < _ > > ( ) ;
130+
131+ for ( element, element_ty, count) in deferred_repeat_expr_checks {
132+ match count. kind ( ) {
133+ ty:: ConstKind :: Value ( val)
134+ if val. try_to_target_usize ( self . tcx ) . is_none_or ( |count| count > 1 ) =>
135+ {
136+ self . enforce_repeat_element_needs_copy_bound ( element, element_ty)
137+ }
138+ // If the length is 0 or 1 we don't actually copy the element, we either don't create it
139+ // or we just use the one value.
140+ ty:: ConstKind :: Value ( _) => ( ) ,
141+
142+ // If the length is a generic parameter or some rigid alias then conservatively
143+ // require `element_ty: Copy` as it may wind up being `>1` after monomorphization.
144+ ty:: ConstKind :: Param ( _)
145+ | ty:: ConstKind :: Expr ( _)
146+ | ty:: ConstKind :: Placeholder ( _)
147+ | ty:: ConstKind :: Unevaluated ( _) => {
148+ self . enforce_repeat_element_needs_copy_bound ( element, element_ty)
149+ }
150+
151+ ty:: ConstKind :: Bound ( _, _) | ty:: ConstKind :: Infer ( _) | ty:: ConstKind :: Error ( _) => {
152+ unreachable ! ( )
153+ }
125154 }
126155 }
127156 }
0 commit comments