@@ -53,18 +53,27 @@ pub fn translate_user_annotation<'tcx>(
5353 // Compute the inferred type of the place, and constraints needed to match it with the
5454 // user-provided type.
5555 let ( projected_tys, constraints) : ( Vec < ty:: Ty < ' tcx > > , QueryRegionConstraints < ' tcx > ) = {
56- let mut constraints = param_env
56+ let mut all_constraints: Vec < Option < & ' tcx QueryRegionConstraints < ' tcx > > > = Vec :: new ( ) ;
57+
58+ // `AscribeUserType` needs a normalized type.
59+ let normalized = param_env
60+ . and ( Normalize {
61+ value : ty:: set_opaques_to_non_rigid ( tcx, inferred_ty) ,
62+ } )
63+ . fully_perform ( & infcx, owner, span)
64+ . map_err ( |_| "could not normalize inferred annotation type" . to_owned ( ) ) ?;
65+ let inferred_ty = normalized. output ;
66+ all_constraints. push ( normalized. constraints ) ;
67+
68+ let ascribe_result = param_env
5769 . and ( AscribeUserType {
5870 mir_ty : inferred_ty,
5971 user_ty,
6072 } )
6173 . fully_perform ( & infcx, owner, span)
62- . map_err ( |_| "could not recover user type ascription" . to_owned ( ) ) ?
63- . constraints
64- . cloned ( )
65- . unwrap_or_default ( ) ;
66- // Add any normalization obligations coming from projections. In particular, projecting a field
67- // can expose an associated type with lifetime requirements.
74+ . map_err ( |_| "could not recover user type ascription" . to_owned ( ) ) ?;
75+ all_constraints. push ( ascribe_result. constraints ) ;
76+
6877 let mut projected_tys = Vec :: new ( ) ;
6978 for projections in projections {
7079 let mut projected_ty = mir:: PlaceTy :: from_ty ( inferred_ty) ;
@@ -77,14 +86,7 @@ pub fn translate_user_annotation<'tcx>(
7786 . fully_perform ( & infcx, owner, span)
7887 . map_err ( |_| "could not normalize user type projection" . to_owned ( ) ) ?;
7988 projected_ty. ty = resolved. output ;
80- if let Some ( extra) = resolved. constraints {
81- constraints
82- . constraints
83- . extend ( extra. constraints . iter ( ) . copied ( ) ) ;
84- constraints
85- . assumptions
86- . extend ( extra. assumptions . iter ( ) . copied ( ) ) ;
87- }
89+ all_constraints. push ( resolved. constraints ) ;
8890 let Some ( projection) = iter. next ( ) else { break } ;
8991 if matches ! (
9092 projection,
@@ -104,6 +106,18 @@ pub fn translate_user_annotation<'tcx>(
104106 }
105107 projected_tys. push ( projected_ty. ty ) ;
106108 }
109+
110+ // Merge the constraints we got from normalizing, relating and projecting.
111+ let mut constraints: QueryRegionConstraints < ' tcx > = Default :: default ( ) ;
112+ for c in all_constraints. into_iter ( ) . flatten ( ) {
113+ constraints
114+ . constraints
115+ . extend ( c. constraints . iter ( ) . copied ( ) ) ;
116+ constraints
117+ . assumptions
118+ . extend ( c. assumptions . iter ( ) . copied ( ) ) ;
119+ }
120+
107121 ( projected_tys, constraints)
108122 } ;
109123
0 commit comments