@@ -90,7 +90,7 @@ use middle::mem_categorization as mc;
9090use middle:: mem_categorization:: McResult ;
9191use middle:: pat_util:: { self , pat_id_map} ;
9292use middle:: region:: CodeExtent ;
93- use middle:: subst:: { self , Subst , Substs , VecPerParamSpace , ParamSpace } ;
93+ use middle:: subst:: { self , Subst , Substs , VecPerParamSpace , ParamSpace , TypeSpace } ;
9494use middle:: traits;
9595use middle:: ty:: { FnSig , VariantInfo , TypeScheme } ;
9696use middle:: ty:: { Disr , ParamTy , ParameterEnvironment } ;
@@ -1947,6 +1947,43 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19471947 }
19481948 }
19491949
1950+ /// Returns the type that this AST path refers to. If the path has no type
1951+ /// parameters and the corresponding type has type parameters, fresh type
1952+ /// and/or region variables are substituted.
1953+ ///
1954+ /// This is used when checking the constructor in struct literals.
1955+ fn instantiate_struct_literal_ty ( & self ,
1956+ did : ast:: DefId ,
1957+ path : & ast:: Path )
1958+ -> TypeAndSubsts < ' tcx >
1959+ {
1960+ let tcx = self . tcx ( ) ;
1961+
1962+ let ty:: TypeScheme { generics, ty : decl_ty } = ty:: lookup_item_type ( tcx, did) ;
1963+
1964+ let wants_params =
1965+ generics. has_type_params ( TypeSpace ) || generics. has_region_params ( TypeSpace ) ;
1966+
1967+ let needs_defaults =
1968+ wants_params &&
1969+ path. segments . iter ( ) . all ( |s| s. parameters . is_empty ( ) ) ;
1970+
1971+ let substs = if needs_defaults {
1972+ let tps =
1973+ self . infcx ( ) . next_ty_vars ( generics. types . len ( TypeSpace ) ) ;
1974+ let rps =
1975+ self . infcx ( ) . region_vars_for_defs ( path. span ,
1976+ generics. regions . get_slice ( TypeSpace ) ) ;
1977+ Substs :: new_type ( tps, rps)
1978+ } else {
1979+ astconv:: ast_path_substs_for_ty ( self , self , & generics, path)
1980+ } ;
1981+
1982+ let ty = self . instantiate_type_scheme ( path. span , & substs, & decl_ty) ;
1983+
1984+ TypeAndSubsts { substs : substs, ty : ty }
1985+ }
1986+
19501987 pub fn write_nil ( & self , node_id : ast:: NodeId ) {
19511988 self . write_ty ( node_id, ty:: mk_nil ( self . tcx ( ) ) ) ;
19521989 }
@@ -3490,17 +3527,18 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
34903527 expected_field_type =
34913528 ty:: lookup_field_type (
34923529 tcx, class_id, field_id, substitutions) ;
3530+ expected_field_type =
3531+ fcx. normalize_associated_types_in (
3532+ field. span , & expected_field_type) ;
34933533 class_field_map. insert (
34943534 field. ident . node . name , ( field_id, true ) ) ;
34953535 fields_found += 1 ;
34963536 }
34973537 }
3538+
34983539 // Make sure to give a type to the field even if there's
34993540 // an error, so we can continue typechecking
3500- check_expr_coercable_to_type (
3501- fcx,
3502- & * field. expr ,
3503- expected_field_type) ;
3541+ check_expr_coercable_to_type ( fcx, & * field. expr , expected_field_type) ;
35043542 }
35053543
35063544 if error_happened {
@@ -4149,10 +4187,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
41494187 // parameters correctly.
41504188 let actual_structure_type = fcx. expr_ty ( & * expr) ;
41514189 if !ty:: type_is_error ( actual_structure_type) {
4152- let type_and_substs = astconv:: ast_path_to_ty_relaxed ( fcx,
4153- fcx,
4154- struct_id,
4155- path) ;
4190+ let type_and_substs = fcx. instantiate_struct_literal_ty ( struct_id, path) ;
41564191 match fcx. mk_subty ( false ,
41574192 infer:: Misc ( path. span ) ,
41584193 actual_structure_type,
0 commit comments