@@ -756,6 +756,7 @@ pub fn provide(providers: &mut Providers<'_>) {
756756 * providers = Providers {
757757 typeck_item_bodies,
758758 typeck_tables_of,
759+ diagnostic_only_typeck_tables_of,
759760 has_typeck_tables,
760761 adt_destructor,
761762 used_trait_imports,
@@ -941,7 +942,31 @@ where
941942 val. fold_with ( & mut FixupFolder { tcx } )
942943}
943944
944- fn typeck_tables_of ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> & ty:: TypeckTables < ' _ > {
945+ fn typeck_tables_of < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> & ty:: TypeckTables < ' tcx > {
946+ let fallback = move || tcx. type_of ( def_id) ;
947+ typeck_tables_of_with_fallback ( tcx, def_id, fallback)
948+ }
949+
950+ /// Used only to get `TypeckTables` for type inference during error recovery.
951+ /// Currently only used for type inference of `static`s and `const`s to avoid type cycle errors.
952+ fn diagnostic_only_typeck_tables_of < ' tcx > (
953+ tcx : TyCtxt < ' tcx > ,
954+ def_id : DefId ,
955+ ) -> & ty:: TypeckTables < ' tcx > {
956+ assert ! ( def_id. is_local( ) ) ;
957+ let fallback = move || {
958+ let span = tcx. hir ( ) . span ( tcx. hir ( ) . as_local_hir_id ( def_id) . unwrap ( ) ) ;
959+ tcx. sess . delay_span_bug ( span, "diagnostic only typeck table used" ) ;
960+ tcx. types . err
961+ } ;
962+ typeck_tables_of_with_fallback ( tcx, def_id, fallback)
963+ }
964+
965+ fn typeck_tables_of_with_fallback < ' tcx > (
966+ tcx : TyCtxt < ' tcx > ,
967+ def_id : DefId ,
968+ fallback : impl Fn ( ) -> Ty < ' tcx > + ' tcx ,
969+ ) -> & ' tcx ty:: TypeckTables < ' tcx > {
945970 // Closures' tables come from their outermost function,
946971 // as they are part of the same "inference environment".
947972 let outer_def_id = tcx. closure_base_def_id ( def_id) ;
@@ -963,7 +988,7 @@ fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> {
963988 let fcx = if let ( Some ( header) , Some ( decl) ) = ( fn_header, fn_decl) {
964989 let fn_sig = if crate :: collect:: get_infer_ret_ty ( & decl. output ) . is_some ( ) {
965990 let fcx = FnCtxt :: new ( & inh, param_env, body. value . hir_id ) ;
966- AstConv :: ty_of_fn ( & fcx, header. unsafety , header. abi , decl)
991+ AstConv :: ty_of_fn ( & fcx, header. unsafety , header. abi , decl, & [ ] , None )
967992 } else {
968993 tcx. fn_sig ( def_id)
969994 } ;
@@ -990,7 +1015,7 @@ fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> {
9901015 hir:: TyKind :: Infer => Some ( AstConv :: ast_ty_to_ty ( & fcx, ty) ) ,
9911016 _ => None ,
9921017 } )
993- . unwrap_or_else ( || tcx . type_of ( def_id ) ) ;
1018+ . unwrap_or_else ( fallback ) ;
9941019 let expected_type = fcx. normalize_associated_types_in ( body. value . span , & expected_type) ;
9951020 fcx. require_type_is_sized ( expected_type, body. value . span , traits:: ConstSized ) ;
9961021
@@ -1069,6 +1094,7 @@ fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> {
10691094 let ty = fcx. normalize_ty ( span, ty) ;
10701095 fcx. require_type_is_sized ( ty, span, code) ;
10711096 }
1097+
10721098 fcx. select_all_obligations_or_error ( ) ;
10731099
10741100 if fn_decl. is_some ( ) {
@@ -2563,6 +2589,10 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
25632589 Some ( self . next_region_var ( v) )
25642590 }
25652591
2592+ fn allow_ty_infer ( & self ) -> bool {
2593+ true
2594+ }
2595+
25662596 fn ty_infer ( & self , param : Option < & ty:: GenericParamDef > , span : Span ) -> Ty < ' tcx > {
25672597 if let Some ( param) = param {
25682598 if let GenericArgKind :: Type ( ty) = self . var_for_def ( span, param) . unpack ( ) {
0 commit comments