@@ -756,6 +756,7 @@ pub fn provide(providers: &mut Providers<'_>) {
756
756
* providers = Providers {
757
757
typeck_item_bodies,
758
758
typeck_tables_of,
759
+ diagnostic_only_typeck_tables_of,
759
760
has_typeck_tables,
760
761
adt_destructor,
761
762
used_trait_imports,
@@ -941,7 +942,31 @@ where
941
942
val. fold_with ( & mut FixupFolder { tcx } )
942
943
}
943
944
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 > {
945
970
// Closures' tables come from their outermost function,
946
971
// as they are part of the same "inference environment".
947
972
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<'_> {
963
988
let fcx = if let ( Some ( header) , Some ( decl) ) = ( fn_header, fn_decl) {
964
989
let fn_sig = if crate :: collect:: get_infer_ret_ty ( & decl. output ) . is_some ( ) {
965
990
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 )
967
992
} else {
968
993
tcx. fn_sig ( def_id)
969
994
} ;
@@ -990,7 +1015,7 @@ fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> {
990
1015
hir:: TyKind :: Infer => Some ( AstConv :: ast_ty_to_ty ( & fcx, ty) ) ,
991
1016
_ => None ,
992
1017
} )
993
- . unwrap_or_else ( || tcx . type_of ( def_id ) ) ;
1018
+ . unwrap_or_else ( fallback ) ;
994
1019
let expected_type = fcx. normalize_associated_types_in ( body. value . span , & expected_type) ;
995
1020
fcx. require_type_is_sized ( expected_type, body. value . span , traits:: ConstSized ) ;
996
1021
@@ -1069,6 +1094,7 @@ fn typeck_tables_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::TypeckTables<'_> {
1069
1094
let ty = fcx. normalize_ty ( span, ty) ;
1070
1095
fcx. require_type_is_sized ( ty, span, code) ;
1071
1096
}
1097
+
1072
1098
fcx. select_all_obligations_or_error ( ) ;
1073
1099
1074
1100
if fn_decl. is_some ( ) {
@@ -2563,6 +2589,10 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
2563
2589
Some ( self . next_region_var ( v) )
2564
2590
}
2565
2591
2592
+ fn allow_ty_infer ( & self ) -> bool {
2593
+ true
2594
+ }
2595
+
2566
2596
fn ty_infer ( & self , param : Option < & ty:: GenericParamDef > , span : Span ) -> Ty < ' tcx > {
2567
2597
if let Some ( param) = param {
2568
2598
if let GenericArgKind :: Type ( ty) = self . var_for_def ( span, param) . unpack ( ) {
0 commit comments