@@ -1875,24 +1875,15 @@ fn check_variances_for_type_defn<'tcx>(
18751875 item : & ' tcx hir:: Item < ' tcx > ,
18761876 hir_generics : & hir:: Generics < ' tcx > ,
18771877) {
1878- let identity_args = ty:: GenericArgs :: identity_for_item ( tcx, item. owner_id ) ;
1879-
18801878 match item. kind {
18811879 ItemKind :: Enum ( ..) | ItemKind :: Struct ( ..) | ItemKind :: Union ( ..) => {
1882- for field in tcx. adt_def ( item. owner_id ) . all_fields ( ) {
1883- if field. ty ( tcx, identity_args) . references_error ( ) {
1884- return ;
1885- }
1886- }
1880+ // Ok
18871881 }
18881882 ItemKind :: TyAlias ( ..) => {
18891883 assert ! (
18901884 tcx. type_alias_is_lazy( item. owner_id) ,
18911885 "should not be computing variance of non-weak type alias"
18921886 ) ;
1893- if tcx. type_of ( item. owner_id ) . skip_binder ( ) . references_error ( ) {
1894- return ;
1895- }
18961887 }
18971888 kind => span_bug ! ( item. span, "cannot compute the variances of {kind:?}" ) ,
18981889 }
@@ -1955,6 +1946,15 @@ fn check_variances_for_type_defn<'tcx>(
19551946 continue ;
19561947 }
19571948
1949+ // Look for `ErrorGuaranteed` deeply within this type.
1950+ if let ControlFlow :: Break ( ErrorGuaranteed { .. } ) = tcx
1951+ . type_of ( item. owner_id )
1952+ . instantiate_identity ( )
1953+ . visit_with ( & mut HasErrorDeep { tcx, seen : Default :: default ( ) } )
1954+ {
1955+ continue ;
1956+ }
1957+
19581958 match hir_param. name {
19591959 hir:: ParamName :: Error => { }
19601960 _ => {
@@ -1965,6 +1965,46 @@ fn check_variances_for_type_defn<'tcx>(
19651965 }
19661966}
19671967
1968+ /// Look for `ErrorGuaranteed` deeply within structs' (unsubstituted) fields.
1969+ struct HasErrorDeep < ' tcx > {
1970+ tcx : TyCtxt < ' tcx > ,
1971+ seen : FxHashSet < DefId > ,
1972+ }
1973+ impl < ' tcx > TypeVisitor < TyCtxt < ' tcx > > for HasErrorDeep < ' tcx > {
1974+ type Result = ControlFlow < ErrorGuaranteed > ;
1975+
1976+ fn visit_ty ( & mut self , ty : Ty < ' tcx > ) -> Self :: Result {
1977+ match * ty. kind ( ) {
1978+ ty:: Adt ( def, _) => {
1979+ if self . seen . insert ( def. did ( ) ) {
1980+ for field in def. all_fields ( ) {
1981+ self . tcx . type_of ( field. did ) . instantiate_identity ( ) . visit_with ( self ) ?;
1982+ }
1983+ }
1984+ }
1985+ ty:: Error ( guar) => return ControlFlow :: Break ( guar) ,
1986+ _ => { }
1987+ }
1988+ ty. super_visit_with ( self )
1989+ }
1990+
1991+ fn visit_region ( & mut self , r : ty:: Region < ' tcx > ) -> Self :: Result {
1992+ if let Err ( guar) = r. error_reported ( ) {
1993+ ControlFlow :: Break ( guar)
1994+ } else {
1995+ ControlFlow :: Continue ( ( ) )
1996+ }
1997+ }
1998+
1999+ fn visit_const ( & mut self , c : ty:: Const < ' tcx > ) -> Self :: Result {
2000+ if let Err ( guar) = c. error_reported ( ) {
2001+ ControlFlow :: Break ( guar)
2002+ } else {
2003+ ControlFlow :: Continue ( ( ) )
2004+ }
2005+ }
2006+ }
2007+
19682008fn report_bivariance < ' tcx > (
19692009 tcx : TyCtxt < ' tcx > ,
19702010 param : & ' tcx hir:: GenericParam < ' tcx > ,
0 commit comments