@@ -13,32 +13,41 @@ use rustc_hir::def_id::{DefId, LocalDefId};
1313use rustc_middle:: ty:: fast_reject:: { simplify_type, SimplifiedType , TreatParams } ;
1414use rustc_middle:: ty:: { self , CrateInherentImpls , Ty , TyCtxt } ;
1515use rustc_span:: symbol:: sym;
16+ use rustc_span:: ErrorGuaranteed ;
1617
1718use crate :: errors;
1819
1920/// On-demand query: yields a map containing all types mapped to their inherent impls.
20- pub fn crate_inherent_impls ( tcx : TyCtxt < ' _ > , ( ) : ( ) ) -> CrateInherentImpls {
21+ pub fn crate_inherent_impls (
22+ tcx : TyCtxt < ' _ > ,
23+ ( ) : ( ) ,
24+ ) -> Result < & ' _ CrateInherentImpls , ErrorGuaranteed > {
2125 let mut collect = InherentCollect { tcx, impls_map : Default :: default ( ) } ;
26+ let mut res = Ok ( ( ) ) ;
2227 for id in tcx. hir ( ) . items ( ) {
23- collect. check_item ( id) ;
28+ res = res . and ( collect. check_item ( id) ) ;
2429 }
25- collect. impls_map
30+ res?;
31+ Ok ( tcx. arena . alloc ( collect. impls_map ) )
2632}
2733
28- pub fn crate_incoherent_impls ( tcx : TyCtxt < ' _ > , simp : SimplifiedType ) -> & [ DefId ] {
29- let crate_map = tcx. crate_inherent_impls ( ( ) ) ;
30- tcx. arena . alloc_from_iter (
34+ pub fn crate_incoherent_impls (
35+ tcx : TyCtxt < ' _ > ,
36+ simp : SimplifiedType ,
37+ ) -> Result < & [ DefId ] , ErrorGuaranteed > {
38+ let crate_map = tcx. crate_inherent_impls ( ( ) ) ?;
39+ Ok ( tcx. arena . alloc_from_iter (
3140 crate_map. incoherent_impls . get ( & simp) . unwrap_or ( & Vec :: new ( ) ) . iter ( ) . map ( |d| d. to_def_id ( ) ) ,
32- )
41+ ) )
3342}
3443
3544/// On-demand query: yields a vector of the inherent impls for a specific type.
36- pub fn inherent_impls ( tcx : TyCtxt < ' _ > , ty_def_id : LocalDefId ) -> & [ DefId ] {
37- let crate_map = tcx. crate_inherent_impls ( ( ) ) ;
38- match crate_map. inherent_impls . get ( & ty_def_id) {
45+ pub fn inherent_impls ( tcx : TyCtxt < ' _ > , ty_def_id : LocalDefId ) -> Result < & [ DefId ] , ErrorGuaranteed > {
46+ let crate_map = tcx. crate_inherent_impls ( ( ) ) ? ;
47+ Ok ( match crate_map. inherent_impls . get ( & ty_def_id) {
3948 Some ( v) => & v[ ..] ,
4049 None => & [ ] ,
41- }
50+ } )
4251}
4352
4453struct InherentCollect < ' tcx > {
@@ -47,33 +56,36 @@ struct InherentCollect<'tcx> {
4756}
4857
4958impl < ' tcx > InherentCollect < ' tcx > {
50- fn check_def_id ( & mut self , impl_def_id : LocalDefId , self_ty : Ty < ' tcx > , ty_def_id : DefId ) {
59+ fn check_def_id (
60+ & mut self ,
61+ impl_def_id : LocalDefId ,
62+ self_ty : Ty < ' tcx > ,
63+ ty_def_id : DefId ,
64+ ) -> Result < ( ) , ErrorGuaranteed > {
5165 if let Some ( ty_def_id) = ty_def_id. as_local ( ) {
5266 // Add the implementation to the mapping from implementation to base
5367 // type def ID, if there is a base type for this implementation and
5468 // the implementation does not have any associated traits.
5569 let vec = self . impls_map . inherent_impls . entry ( ty_def_id) . or_default ( ) ;
5670 vec. push ( impl_def_id. to_def_id ( ) ) ;
57- return ;
71+ return Ok ( ( ) ) ;
5872 }
5973
6074 if self . tcx . features ( ) . rustc_attrs {
6175 let items = self . tcx . associated_item_def_ids ( impl_def_id) ;
6276
6377 if !self . tcx . has_attr ( ty_def_id, sym:: rustc_has_incoherent_inherent_impls) {
6478 let impl_span = self . tcx . def_span ( impl_def_id) ;
65- self . tcx . dcx ( ) . emit_err ( errors:: InherentTyOutside { span : impl_span } ) ;
66- return ;
79+ return Err ( self . tcx . dcx ( ) . emit_err ( errors:: InherentTyOutside { span : impl_span } ) ) ;
6780 }
6881
6982 for & impl_item in items {
7083 if !self . tcx . has_attr ( impl_item, sym:: rustc_allow_incoherent_impl) {
7184 let impl_span = self . tcx . def_span ( impl_def_id) ;
72- self . tcx . dcx ( ) . emit_err ( errors:: InherentTyOutsideRelevant {
85+ return Err ( self . tcx . dcx ( ) . emit_err ( errors:: InherentTyOutsideRelevant {
7386 span : impl_span,
7487 help_span : self . tcx . def_span ( impl_item) ,
75- } ) ;
76- return ;
88+ } ) ) ;
7789 }
7890 }
7991
@@ -82,24 +94,28 @@ impl<'tcx> InherentCollect<'tcx> {
8294 } else {
8395 bug ! ( "unexpected self type: {:?}" , self_ty) ;
8496 }
97+ Ok ( ( ) )
8598 } else {
8699 let impl_span = self . tcx . def_span ( impl_def_id) ;
87- self . tcx . dcx ( ) . emit_err ( errors:: InherentTyOutsideNew { span : impl_span } ) ;
100+ Err ( self . tcx . dcx ( ) . emit_err ( errors:: InherentTyOutsideNew { span : impl_span } ) )
88101 }
89102 }
90103
91- fn check_primitive_impl ( & mut self , impl_def_id : LocalDefId , ty : Ty < ' tcx > ) {
104+ fn check_primitive_impl (
105+ & mut self ,
106+ impl_def_id : LocalDefId ,
107+ ty : Ty < ' tcx > ,
108+ ) -> Result < ( ) , ErrorGuaranteed > {
92109 let items = self . tcx . associated_item_def_ids ( impl_def_id) ;
93110 if !self . tcx . hir ( ) . rustc_coherence_is_core ( ) {
94111 if self . tcx . features ( ) . rustc_attrs {
95112 for & impl_item in items {
96113 if !self . tcx . has_attr ( impl_item, sym:: rustc_allow_incoherent_impl) {
97114 let span = self . tcx . def_span ( impl_def_id) ;
98- self . tcx . dcx ( ) . emit_err ( errors:: InherentTyOutsidePrimitive {
115+ return Err ( self . tcx . dcx ( ) . emit_err ( errors:: InherentTyOutsidePrimitive {
99116 span,
100117 help_span : self . tcx . def_span ( impl_item) ,
101- } ) ;
102- return ;
118+ } ) ) ;
103119 }
104120 }
105121 } else {
@@ -108,8 +124,7 @@ impl<'tcx> InherentCollect<'tcx> {
108124 if let ty:: Ref ( _, subty, _) = ty. kind ( ) {
109125 note = Some ( errors:: InherentPrimitiveTyNote { subty : * subty } ) ;
110126 }
111- self . tcx . dcx ( ) . emit_err ( errors:: InherentPrimitiveTy { span, note } ) ;
112- return ;
127+ return Err ( self . tcx . dcx ( ) . emit_err ( errors:: InherentPrimitiveTy { span, note } ) ) ;
113128 }
114129 }
115130
@@ -118,11 +133,12 @@ impl<'tcx> InherentCollect<'tcx> {
118133 } else {
119134 bug ! ( "unexpected primitive type: {:?}" , ty) ;
120135 }
136+ Ok ( ( ) )
121137 }
122138
123- fn check_item ( & mut self , id : hir:: ItemId ) {
139+ fn check_item ( & mut self , id : hir:: ItemId ) -> Result < ( ) , ErrorGuaranteed > {
124140 if !matches ! ( self . tcx. def_kind( id. owner_id) , DefKind :: Impl { of_trait: false } ) {
125- return ;
141+ return Ok ( ( ) ) ;
126142 }
127143
128144 let id = id. owner_id . def_id ;
@@ -132,10 +148,10 @@ impl<'tcx> InherentCollect<'tcx> {
132148 ty:: Adt ( def, _) => self . check_def_id ( id, self_ty, def. did ( ) ) ,
133149 ty:: Foreign ( did) => self . check_def_id ( id, self_ty, did) ,
134150 ty:: Dynamic ( data, ..) if data. principal_def_id ( ) . is_some ( ) => {
135- self . check_def_id ( id, self_ty, data. principal_def_id ( ) . unwrap ( ) ) ;
151+ self . check_def_id ( id, self_ty, data. principal_def_id ( ) . unwrap ( ) )
136152 }
137153 ty:: Dynamic ( ..) => {
138- self . tcx . dcx ( ) . emit_err ( errors:: InherentDyn { span : item_span } ) ;
154+ Err ( self . tcx . dcx ( ) . emit_err ( errors:: InherentDyn { span : item_span } ) )
139155 }
140156 ty:: Bool
141157 | ty:: Char
@@ -151,7 +167,7 @@ impl<'tcx> InherentCollect<'tcx> {
151167 | ty:: FnPtr ( _)
152168 | ty:: Tuple ( ..) => self . check_primitive_impl ( id, self_ty) ,
153169 ty:: Alias ( ..) | ty:: Param ( _) => {
154- self . tcx . dcx ( ) . emit_err ( errors:: InherentNominal { span : item_span } ) ;
170+ Err ( self . tcx . dcx ( ) . emit_err ( errors:: InherentNominal { span : item_span } ) )
155171 }
156172 ty:: FnDef ( ..)
157173 | ty:: Closure ( ..)
@@ -162,7 +178,8 @@ impl<'tcx> InherentCollect<'tcx> {
162178 | ty:: Infer ( _) => {
163179 bug ! ( "unexpected impl self type of impl: {:?} {:?}" , id, self_ty) ;
164180 }
165- ty:: Error ( _) => { }
181+ // We could bail out here, but that will silence other useful errors.
182+ ty:: Error ( _) => Ok ( ( ) ) ,
166183 }
167184 }
168185}
0 commit comments