7
7
//! `tcx.inherent_impls(def_id)`). That value, however,
8
8
//! is computed by selecting an idea from this table.
9
9
10
- use rustc_errors:: struct_span_err;
11
10
use rustc_hir as hir;
12
11
use rustc_hir:: def:: DefKind ;
13
12
use rustc_hir:: def_id:: { DefId , LocalDefId } ;
14
13
use rustc_middle:: ty:: fast_reject:: { simplify_type, SimplifiedType , TreatParams } ;
15
14
use rustc_middle:: ty:: { self , CrateInherentImpls , Ty , TyCtxt } ;
16
15
use rustc_span:: symbol:: sym;
17
16
17
+ use crate :: errors;
18
+
18
19
/// On-demand query: yields a map containing all types mapped to their inherent impls.
19
20
pub fn crate_inherent_impls ( tcx : TyCtxt < ' _ > , ( ) : ( ) ) -> CrateInherentImpls {
20
21
let mut collect = InherentCollect { tcx, impls_map : Default :: default ( ) } ;
@@ -45,14 +46,6 @@ struct InherentCollect<'tcx> {
45
46
impls_map : CrateInherentImpls ,
46
47
}
47
48
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
-
56
49
impl < ' tcx > InherentCollect < ' tcx > {
57
50
fn check_def_id ( & mut self , impl_def_id : LocalDefId , self_ty : Ty < ' tcx > , ty_def_id : DefId ) {
58
51
if let Some ( ty_def_id) = ty_def_id. as_local ( ) {
@@ -69,30 +62,17 @@ impl<'tcx> InherentCollect<'tcx> {
69
62
70
63
if !self . tcx . has_attr ( ty_def_id, sym:: rustc_has_incoherent_inherent_impls) {
71
64
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 } ) ;
81
66
return ;
82
67
}
83
68
84
69
for & impl_item in items {
85
70
if !self . tcx . has_attr ( impl_item, sym:: rustc_allow_incoherent_impl) {
86
71
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
+ } ) ;
96
76
return ;
97
77
}
98
78
}
@@ -104,16 +84,7 @@ impl<'tcx> InherentCollect<'tcx> {
104
84
}
105
85
} else {
106
86
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 } ) ;
117
88
}
118
89
}
119
90
@@ -124,34 +95,20 @@ impl<'tcx> InherentCollect<'tcx> {
124
95
for & impl_item in items {
125
96
if !self . tcx . has_attr ( impl_item, sym:: rustc_allow_incoherent_impl) {
126
97
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 {
129
99
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
+ } ) ;
136
102
return ;
137
103
}
138
104
}
139
105
} else {
140
106
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 ;
148
108
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 } ) ;
153
110
}
154
- err . emit ( ) ;
111
+ self . tcx . sess . emit_err ( errors :: InherentPrimitiveTy { span , note } ) ;
155
112
return ;
156
113
}
157
114
}
@@ -178,15 +135,7 @@ impl<'tcx> InherentCollect<'tcx> {
178
135
self . check_def_id ( id, self_ty, data. principal_def_id ( ) . unwrap ( ) ) ;
179
136
}
180
137
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 } ) ;
190
139
}
191
140
ty:: Bool
192
141
| ty:: Char
@@ -202,17 +151,7 @@ impl<'tcx> InherentCollect<'tcx> {
202
151
| ty:: FnPtr ( _)
203
152
| ty:: Tuple ( ..) => self . check_primitive_impl ( id, self_ty) ,
204
153
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 } ) ;
216
155
}
217
156
ty:: FnDef ( ..)
218
157
| ty:: Closure ( ..)
0 commit comments