77//! `tcx.inherent_impls(def_id)`). That value, however,
88//! is computed by selecting an idea from this table.
99
10- use rustc_errors:: struct_span_err;
1110use rustc_hir as hir;
1211use rustc_hir:: def:: DefKind ;
1312use rustc_hir:: def_id:: { DefId , LocalDefId } ;
1413use rustc_middle:: ty:: fast_reject:: { simplify_type, SimplifiedType , TreatParams } ;
1514use rustc_middle:: ty:: { self , CrateInherentImpls , Ty , TyCtxt } ;
1615use rustc_span:: symbol:: sym;
1716
17+ use crate :: errors;
18+
1819/// On-demand query: yields a map containing all types mapped to their inherent impls.
1920pub fn crate_inherent_impls ( tcx : TyCtxt < ' _ > , ( ) : ( ) ) -> CrateInherentImpls {
2021 let mut collect = InherentCollect { tcx, impls_map : Default :: default ( ) } ;
@@ -45,14 +46,6 @@ struct InherentCollect<'tcx> {
4546 impls_map : CrateInherentImpls ,
4647}
4748
48- const INTO_CORE : & str = "consider moving this inherent impl into `core` if possible" ;
49- const INTO_DEFINING_CRATE : & str =
50- "consider moving this inherent impl into the crate defining the type if possible" ;
51- const ADD_ATTR_TO_TY : & str = "alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type \
52- and `#[rustc_allow_incoherent_impl]` to the relevant impl items";
53- const ADD_ATTR : & str =
54- "alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items" ;
55-
5649impl < ' tcx > InherentCollect < ' tcx > {
5750 fn check_def_id ( & mut self , impl_def_id : LocalDefId , self_ty : Ty < ' tcx > , ty_def_id : DefId ) {
5851 if let Some ( ty_def_id) = ty_def_id. as_local ( ) {
@@ -69,30 +62,17 @@ impl<'tcx> InherentCollect<'tcx> {
6962
7063 if !self . tcx . has_attr ( ty_def_id, sym:: rustc_has_incoherent_inherent_impls) {
7164 let impl_span = self . tcx . def_span ( impl_def_id) ;
72- struct_span_err ! (
73- self . tcx. sess,
74- impl_span,
75- E0390 ,
76- "cannot define inherent `impl` for a type outside of the crate where the type is defined" ,
77- )
78- . help ( INTO_DEFINING_CRATE )
79- . span_help ( impl_span, ADD_ATTR_TO_TY )
80- . emit ( ) ;
65+ self . tcx . sess . emit_err ( errors:: InherentTyOutside { span : impl_span } ) ;
8166 return ;
8267 }
8368
8469 for & impl_item in items {
8570 if !self . tcx . has_attr ( impl_item, sym:: rustc_allow_incoherent_impl) {
8671 let impl_span = self . tcx . def_span ( impl_def_id) ;
87- struct_span_err ! (
88- self . tcx. sess,
89- impl_span,
90- E0390 ,
91- "cannot define inherent `impl` for a type outside of the crate where the type is defined" ,
92- )
93- . help ( INTO_DEFINING_CRATE )
94- . span_help ( self . tcx . def_span ( impl_item) , ADD_ATTR )
95- . emit ( ) ;
72+ self . tcx . sess . emit_err ( errors:: InherentTyOutsideRelevant {
73+ span : impl_span,
74+ help_span : self . tcx . def_span ( impl_item) ,
75+ } ) ;
9676 return ;
9777 }
9878 }
@@ -104,16 +84,7 @@ impl<'tcx> InherentCollect<'tcx> {
10484 }
10585 } else {
10686 let impl_span = self . tcx . def_span ( impl_def_id) ;
107- struct_span_err ! (
108- self . tcx. sess,
109- impl_span,
110- E0116 ,
111- "cannot define inherent `impl` for a type outside of the crate \
112- where the type is defined"
113- )
114- . span_label ( impl_span, "impl for type defined outside of crate." )
115- . note ( "define and implement a trait or new type instead" )
116- . emit ( ) ;
87+ self . tcx . sess . emit_err ( errors:: InherentTyOutsideNew { span : impl_span } ) ;
11788 }
11889 }
11990
@@ -124,34 +95,20 @@ impl<'tcx> InherentCollect<'tcx> {
12495 for & impl_item in items {
12596 if !self . tcx . has_attr ( impl_item, sym:: rustc_allow_incoherent_impl) {
12697 let span = self . tcx . def_span ( impl_def_id) ;
127- struct_span_err ! (
128- self . tcx. sess,
98+ self . tcx . sess . emit_err ( errors:: InherentTyOutsidePrimitive {
12999 span,
130- E0390 ,
131- "cannot define inherent `impl` for primitive types outside of `core`" ,
132- )
133- . help ( INTO_CORE )
134- . span_help ( self . tcx . def_span ( impl_item) , ADD_ATTR )
135- . emit ( ) ;
100+ help_span : self . tcx . def_span ( impl_item) ,
101+ } ) ;
136102 return ;
137103 }
138104 }
139105 } else {
140106 let span = self . tcx . def_span ( impl_def_id) ;
141- let mut err = struct_span_err ! (
142- self . tcx. sess,
143- span,
144- E0390 ,
145- "cannot define inherent `impl` for primitive types" ,
146- ) ;
147- err. help ( "consider using an extension trait instead" ) ;
107+ let mut note = None ;
148108 if let ty:: Ref ( _, subty, _) = ty. kind ( ) {
149- err. note ( format ! (
150- "you could also try moving the reference to \
151- uses of `{subty}` (such as `self`) within the implementation"
152- ) ) ;
109+ note = Some ( errors:: InherentPrimitiveTyNote { subty : * subty } ) ;
153110 }
154- err . emit ( ) ;
111+ self . tcx . sess . emit_err ( errors :: InherentPrimitiveTy { span , note } ) ;
155112 return ;
156113 }
157114 }
@@ -178,15 +135,7 @@ impl<'tcx> InherentCollect<'tcx> {
178135 self . check_def_id ( id, self_ty, data. principal_def_id ( ) . unwrap ( ) ) ;
179136 }
180137 ty:: Dynamic ( ..) => {
181- struct_span_err ! (
182- self . tcx. sess,
183- item_span,
184- E0785 ,
185- "cannot define inherent `impl` for a dyn auto trait"
186- )
187- . span_label ( item_span, "impl requires at least one non-auto trait" )
188- . note ( "define and implement a new trait or type instead" )
189- . emit ( ) ;
138+ self . tcx . sess . emit_err ( errors:: InherentDyn { span : item_span } ) ;
190139 }
191140 ty:: Bool
192141 | ty:: Char
@@ -202,17 +151,7 @@ impl<'tcx> InherentCollect<'tcx> {
202151 | ty:: FnPtr ( _)
203152 | ty:: Tuple ( ..) => self . check_primitive_impl ( id, self_ty) ,
204153 ty:: Alias ( ..) | ty:: Param ( _) => {
205- let mut err = struct_span_err ! (
206- self . tcx. sess,
207- item_span,
208- E0118 ,
209- "no nominal type found for inherent implementation"
210- ) ;
211-
212- err. span_label ( item_span, "impl requires a nominal type" )
213- . note ( "either implement a trait on it or create a newtype to wrap it instead" ) ;
214-
215- err. emit ( ) ;
154+ self . tcx . sess . emit_err ( errors:: InherentNominal { span : item_span } ) ;
216155 }
217156 ty:: FnDef ( ..)
218157 | ty:: Closure ( ..)
0 commit comments