1
1
use crate :: ty:: print:: { with_forced_trimmed_paths, FmtPrinter , PrettyPrinter } ;
2
- use crate :: ty:: { self , BoundRegionKind , Region , Ty , TyCtxt } ;
2
+ use crate :: ty:: { self , Ty , TyCtxt } ;
3
+
3
4
use rustc_errors:: pluralize;
4
5
use rustc_hir as hir;
5
6
use rustc_hir:: def:: { CtorOf , DefKind } ;
6
- use rustc_hir:: def_id:: DefId ;
7
- use rustc_macros:: { TypeFoldable , TypeVisitable } ;
8
- use rustc_span:: symbol:: Symbol ;
9
- use rustc_target:: spec:: abi;
7
+ use rustc_macros:: extension;
8
+ pub use rustc_type_ir:: error:: ExpectedFound ;
9
+
10
10
use std:: borrow:: Cow ;
11
11
use std:: hash:: { DefaultHasher , Hash , Hasher } ;
12
12
use std:: path:: PathBuf ;
13
13
14
- #[ derive( Clone , Copy , Debug , PartialEq , Eq , TypeFoldable , TypeVisitable ) ]
15
- pub struct ExpectedFound < T > {
16
- pub expected : T ,
17
- pub found : T ,
18
- }
19
-
20
- impl < T > ExpectedFound < T > {
21
- pub fn new ( a_is_expected : bool , a : T , b : T ) -> Self {
22
- if a_is_expected {
23
- ExpectedFound { expected : a, found : b }
24
- } else {
25
- ExpectedFound { expected : b, found : a }
26
- }
27
- }
28
- }
29
-
30
- // Data structures used in type unification
31
- #[ derive( Copy , Clone , Debug , TypeVisitable , PartialEq , Eq ) ]
32
- #[ rustc_pass_by_value]
33
- pub enum TypeError < ' tcx > {
34
- Mismatch ,
35
- ConstnessMismatch ( ExpectedFound < ty:: BoundConstness > ) ,
36
- PolarityMismatch ( ExpectedFound < ty:: PredicatePolarity > ) ,
37
- SafetyMismatch ( ExpectedFound < hir:: Safety > ) ,
38
- AbiMismatch ( ExpectedFound < abi:: Abi > ) ,
39
- Mutability ,
40
- ArgumentMutability ( usize ) ,
41
- TupleSize ( ExpectedFound < usize > ) ,
42
- FixedArraySize ( ExpectedFound < u64 > ) ,
43
- ArgCount ,
44
- FieldMisMatch ( Symbol , Symbol ) ,
45
-
46
- RegionsDoesNotOutlive ( Region < ' tcx > , Region < ' tcx > ) ,
47
- RegionsInsufficientlyPolymorphic ( BoundRegionKind , Region < ' tcx > ) ,
48
- RegionsPlaceholderMismatch ,
49
-
50
- Sorts ( ExpectedFound < Ty < ' tcx > > ) ,
51
- ArgumentSorts ( ExpectedFound < Ty < ' tcx > > , usize ) ,
52
- IntMismatch ( ExpectedFound < ty:: IntVarValue > ) ,
53
- FloatMismatch ( ExpectedFound < ty:: FloatTy > ) ,
54
- Traits ( ExpectedFound < DefId > ) ,
55
- VariadicMismatch ( ExpectedFound < bool > ) ,
56
-
57
- /// Instantiating a type variable with the given type would have
58
- /// created a cycle (because it appears somewhere within that
59
- /// type).
60
- CyclicTy ( Ty < ' tcx > ) ,
61
- CyclicConst ( ty:: Const < ' tcx > ) ,
62
- ProjectionMismatched ( ExpectedFound < DefId > ) ,
63
- ExistentialMismatch ( ExpectedFound < & ' tcx ty:: List < ty:: PolyExistentialPredicate < ' tcx > > > ) ,
64
- ConstMismatch ( ExpectedFound < ty:: Const < ' tcx > > ) ,
65
-
66
- IntrinsicCast ,
67
- /// Safe `#[target_feature]` functions are not assignable to safe function pointers.
68
- TargetFeatureCast ( DefId ) ,
69
- }
70
-
71
- impl TypeError < ' _ > {
72
- pub fn involves_regions ( self ) -> bool {
73
- match self {
74
- TypeError :: RegionsDoesNotOutlive ( _, _)
75
- | TypeError :: RegionsInsufficientlyPolymorphic ( _, _)
76
- | TypeError :: RegionsPlaceholderMismatch => true ,
77
- _ => false ,
78
- }
79
- }
80
- }
14
+ pub type TypeError < ' tcx > = rustc_type_ir:: error:: TypeError < TyCtxt < ' tcx > > ;
81
15
82
- /// Explains the source of a type err in a short, human readable way. This is meant to be placed
83
- /// in parentheses after some larger message. You should also invoke `note_and_explain_type_err()`
84
- /// afterwards to present additional details, particularly when it comes to lifetime-related
85
- /// errors.
16
+ #[ extension( pub trait TypeErrorToStringExt <' tcx>) ]
17
+ /// Explains the source of a type err in a short, human readable way.
18
+ /// This is meant to be placed in parentheses after some larger message.
19
+ /// You should also invoke `note_and_explain_type_err()` afterwards
20
+ /// to present additional details, particularly when it comes to lifetime-
21
+ /// related errors.
86
22
impl < ' tcx > TypeError < ' tcx > {
87
- pub fn to_string ( self , tcx : TyCtxt < ' tcx > ) -> Cow < ' static , str > {
88
- use self :: TypeError :: * ;
23
+ fn to_string ( self , tcx : TyCtxt < ' tcx > ) -> Cow < ' static , str > {
89
24
fn report_maybe_different ( expected : & str , found : & str ) -> String {
90
25
// A naive approach to making sure that we're not reporting silly errors such as:
91
26
// (expected closure, found closure).
@@ -97,53 +32,56 @@ impl<'tcx> TypeError<'tcx> {
97
32
}
98
33
99
34
match self {
100
- CyclicTy ( _) => "cyclic type of infinite size" . into ( ) ,
101
- CyclicConst ( _) => "encountered a self-referencing constant" . into ( ) ,
102
- Mismatch => "types differ" . into ( ) ,
103
- ConstnessMismatch ( values) => {
35
+ TypeError :: CyclicTy ( _) => "cyclic type of infinite size" . into ( ) ,
36
+ TypeError :: CyclicConst ( _) => "encountered a self-referencing constant" . into ( ) ,
37
+ TypeError :: Mismatch => "types differ" . into ( ) ,
38
+ TypeError :: ConstnessMismatch ( values) => {
104
39
format ! ( "expected {} bound, found {} bound" , values. expected, values. found) . into ( )
105
40
}
106
- PolarityMismatch ( values) => {
41
+ TypeError :: PolarityMismatch ( values) => {
107
42
format ! ( "expected {} polarity, found {} polarity" , values. expected, values. found)
108
43
. into ( )
109
44
}
110
- SafetyMismatch ( values) => {
45
+ TypeError :: SafetyMismatch ( values) => {
111
46
format ! ( "expected {} fn, found {} fn" , values. expected, values. found) . into ( )
112
47
}
113
- AbiMismatch ( values) => {
48
+ TypeError :: AbiMismatch ( values) => {
114
49
format ! ( "expected {} fn, found {} fn" , values. expected, values. found) . into ( )
115
50
}
116
- ArgumentMutability ( _) | Mutability => "types differ in mutability" . into ( ) ,
117
- TupleSize ( values) => format ! (
51
+ TypeError :: ArgumentMutability ( _) | TypeError :: Mutability => {
52
+ "types differ in mutability" . into ( )
53
+ }
54
+ TypeError :: TupleSize ( values) => format ! (
118
55
"expected a tuple with {} element{}, found one with {} element{}" ,
119
56
values. expected,
120
57
pluralize!( values. expected) ,
121
58
values. found,
122
59
pluralize!( values. found)
123
60
)
124
61
. into ( ) ,
125
- FixedArraySize ( values) => format ! (
62
+ TypeError :: FixedArraySize ( values) => format ! (
126
63
"expected an array with a fixed size of {} element{}, found one with {} element{}" ,
127
64
values. expected,
128
65
pluralize!( values. expected) ,
129
66
values. found,
130
67
pluralize!( values. found)
131
68
)
132
69
. into ( ) ,
133
- ArgCount => "incorrect number of function parameters" . into ( ) ,
134
- FieldMisMatch ( adt, field) => format ! ( "field type mismatch: {adt}.{field}" ) . into ( ) ,
135
- RegionsDoesNotOutlive ( ..) => "lifetime mismatch" . into ( ) ,
70
+ TypeError :: ArgCount => "incorrect number of function parameters" . into ( ) ,
71
+ TypeError :: RegionsDoesNotOutlive ( ..) => "lifetime mismatch" . into ( ) ,
136
72
// Actually naming the region here is a bit confusing because context is lacking
137
- RegionsInsufficientlyPolymorphic ( ..) => {
73
+ TypeError :: RegionsInsufficientlyPolymorphic ( ..) => {
74
+ "one type is more general than the other" . into ( )
75
+ }
76
+ TypeError :: RegionsPlaceholderMismatch => {
138
77
"one type is more general than the other" . into ( )
139
78
}
140
- RegionsPlaceholderMismatch => "one type is more general than the other" . into ( ) ,
141
- ArgumentSorts ( values, _) | Sorts ( values) => {
79
+ TypeError :: ArgumentSorts ( values, _) | TypeError :: Sorts ( values) => {
142
80
let expected = values. expected . sort_string ( tcx) ;
143
81
let found = values. found . sort_string ( tcx) ;
144
82
report_maybe_different ( & expected, & found) . into ( )
145
83
}
146
- Traits ( values) => {
84
+ TypeError :: Traits ( values) => {
147
85
let ( mut expected, mut found) = with_forced_trimmed_paths ! ( (
148
86
tcx. def_path_str( values. expected) ,
149
87
tcx. def_path_str( values. found) ,
@@ -155,7 +93,7 @@ impl<'tcx> TypeError<'tcx> {
155
93
report_maybe_different ( & format ! ( "trait `{expected}`" ) , & format ! ( "trait `{found}`" ) )
156
94
. into ( )
157
95
}
158
- IntMismatch ( ref values) => {
96
+ TypeError :: IntMismatch ( ref values) => {
159
97
let expected = match values. expected {
160
98
ty:: IntVarValue :: IntType ( ty) => ty. name_str ( ) ,
161
99
ty:: IntVarValue :: UintType ( ty) => ty. name_str ( ) ,
@@ -166,66 +104,40 @@ impl<'tcx> TypeError<'tcx> {
166
104
} ;
167
105
format ! ( "expected `{expected}`, found `{found}`" ) . into ( )
168
106
}
169
- FloatMismatch ( ref values) => format ! (
107
+ TypeError :: FloatMismatch ( ref values) => format ! (
170
108
"expected `{}`, found `{}`" ,
171
109
values. expected. name_str( ) ,
172
110
values. found. name_str( )
173
111
)
174
112
. into ( ) ,
175
- VariadicMismatch ( ref values) => format ! (
113
+ TypeError :: VariadicMismatch ( ref values) => format ! (
176
114
"expected {} fn, found {} function" ,
177
115
if values. expected { "variadic" } else { "non-variadic" } ,
178
116
if values. found { "variadic" } else { "non-variadic" }
179
117
)
180
118
. into ( ) ,
181
- ProjectionMismatched ( ref values) => format ! (
119
+ TypeError :: ProjectionMismatched ( ref values) => format ! (
182
120
"expected `{}`, found `{}`" ,
183
121
tcx. def_path_str( values. expected) ,
184
122
tcx. def_path_str( values. found)
185
123
)
186
124
. into ( ) ,
187
- ExistentialMismatch ( ref values) => report_maybe_different (
125
+ TypeError :: ExistentialMismatch ( ref values) => report_maybe_different (
188
126
& format ! ( "trait `{}`" , values. expected) ,
189
127
& format ! ( "trait `{}`" , values. found) ,
190
128
)
191
129
. into ( ) ,
192
- ConstMismatch ( ref values) => {
130
+ TypeError :: ConstMismatch ( ref values) => {
193
131
format ! ( "expected `{}`, found `{}`" , values. expected, values. found) . into ( )
194
132
}
195
- IntrinsicCast => "cannot coerce intrinsics to function pointers" . into ( ) ,
196
- TargetFeatureCast ( _) => {
133
+ TypeError :: IntrinsicCast => "cannot coerce intrinsics to function pointers" . into ( ) ,
134
+ TypeError :: TargetFeatureCast ( _) => {
197
135
"cannot coerce functions with `#[target_feature]` to safe function pointers" . into ( )
198
136
}
199
137
}
200
138
}
201
139
}
202
140
203
- impl < ' tcx > TypeError < ' tcx > {
204
- pub fn must_include_note ( self ) -> bool {
205
- use self :: TypeError :: * ;
206
- match self {
207
- CyclicTy ( _) | CyclicConst ( _) | SafetyMismatch ( _) | ConstnessMismatch ( _)
208
- | PolarityMismatch ( _) | Mismatch | AbiMismatch ( _) | FixedArraySize ( _)
209
- | ArgumentSorts ( ..) | Sorts ( _) | IntMismatch ( _) | FloatMismatch ( _)
210
- | VariadicMismatch ( _) | TargetFeatureCast ( _) => false ,
211
-
212
- Mutability
213
- | ArgumentMutability ( _)
214
- | TupleSize ( _)
215
- | ArgCount
216
- | FieldMisMatch ( ..)
217
- | RegionsDoesNotOutlive ( ..)
218
- | RegionsInsufficientlyPolymorphic ( ..)
219
- | RegionsPlaceholderMismatch
220
- | Traits ( _)
221
- | ProjectionMismatched ( _)
222
- | ExistentialMismatch ( _)
223
- | ConstMismatch ( _)
224
- | IntrinsicCast => true ,
225
- }
226
- }
227
- }
228
-
229
141
impl < ' tcx > Ty < ' tcx > {
230
142
pub fn sort_string ( self , tcx : TyCtxt < ' tcx > ) -> Cow < ' static , str > {
231
143
match * self . kind ( ) {
0 commit comments