@@ -2,6 +2,7 @@ use crate::middle::resolve_bound_vars as rbv;
2
2
use crate :: mir:: interpret:: LitToConstInput ;
3
3
use crate :: ty:: { self , InternalSubsts , ParamEnv , ParamEnvAnd , Ty , TyCtxt } ;
4
4
use rustc_data_structures:: intern:: Interned ;
5
+ use rustc_error_messages:: MultiSpan ;
5
6
use rustc_hir as hir;
6
7
use rustc_hir:: def:: { DefKind , Res } ;
7
8
use rustc_hir:: def_id:: LocalDefId ;
@@ -13,6 +14,7 @@ mod valtree;
13
14
14
15
pub use int:: * ;
15
16
pub use kind:: * ;
17
+ use rustc_span:: DUMMY_SP ;
16
18
pub use valtree:: * ;
17
19
18
20
/// Use this rather than `ConstData`, whenever possible.
@@ -104,6 +106,34 @@ impl<'tcx> Const<'tcx> {
104
106
Const :: new ( tcx, ty:: ConstKind :: Expr ( expr) , ty)
105
107
}
106
108
109
+ #[ inline]
110
+ pub fn new_error ( tcx : TyCtxt < ' tcx > , e : ty:: ErrorGuaranteed , ty : Ty < ' tcx > ) -> Const < ' tcx > {
111
+ Const :: new ( tcx, ty:: ConstKind :: Error ( e) , ty)
112
+ }
113
+
114
+ /// Like [TyCtxt::ty_error] but for constants.
115
+ #[ track_caller]
116
+ pub fn new_misc_error ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> Const < ' tcx > {
117
+ Const :: new_error_with_message (
118
+ tcx,
119
+ ty,
120
+ DUMMY_SP ,
121
+ "ty::ConstKind::Error constructed but no error reported" ,
122
+ )
123
+ }
124
+
125
+ /// Like [TyCtxt::ty_error_with_message] but for constants.
126
+ #[ track_caller]
127
+ pub fn new_error_with_message < S : Into < MultiSpan > > (
128
+ tcx : TyCtxt < ' tcx > ,
129
+ ty : Ty < ' tcx > ,
130
+ span : S ,
131
+ msg : & ' static str ,
132
+ ) -> Const < ' tcx > {
133
+ let reported = tcx. sess . delay_span_bug ( span, msg) ;
134
+ Const :: new_error ( tcx, reported, ty)
135
+ }
136
+
107
137
/// Literals and const generic parameters are eagerly converted to a constant, everything else
108
138
/// becomes `Unevaluated`.
109
139
#[ instrument( skip( tcx) , level = "debug" ) ]
@@ -200,7 +230,9 @@ impl<'tcx> Const<'tcx> {
200
230
param_ty,
201
231
) )
202
232
}
203
- Some ( rbv:: ResolvedArg :: Error ( guar) ) => Some ( tcx. const_error ( param_ty, guar) ) ,
233
+ Some ( rbv:: ResolvedArg :: Error ( guar) ) => {
234
+ Some ( ty:: Const :: new_error ( tcx, guar, param_ty) )
235
+ }
204
236
arg => bug ! ( "unexpected bound var resolution for {:?}: {arg:?}" , expr. hir_id) ,
205
237
}
206
238
}
@@ -285,7 +317,7 @@ impl<'tcx> Const<'tcx> {
285
317
if let Some ( val) = self . kind ( ) . try_eval_for_typeck ( tcx, param_env) {
286
318
match val {
287
319
Ok ( val) => ty:: Const :: new_value ( tcx, val, self . ty ( ) ) ,
288
- Err ( guar) => tcx . const_error ( self . ty ( ) , guar ) ,
320
+ Err ( guar) => ty :: Const :: new_error ( tcx , guar , self . ty ( ) ) ,
289
321
}
290
322
} else {
291
323
// Either the constant isn't evaluatable or ValTree creation failed.
0 commit comments