@@ -2,6 +2,7 @@ use crate::middle::resolve_bound_vars as rbv;
22use crate :: mir:: interpret:: LitToConstInput ;
33use crate :: ty:: { self , InternalSubsts , ParamEnv , ParamEnvAnd , Ty , TyCtxt } ;
44use rustc_data_structures:: intern:: Interned ;
5+ use rustc_error_messages:: MultiSpan ;
56use rustc_hir as hir;
67use rustc_hir:: def:: { DefKind , Res } ;
78use rustc_hir:: def_id:: LocalDefId ;
@@ -13,6 +14,7 @@ mod valtree;
1314
1415pub use int:: * ;
1516pub use kind:: * ;
17+ use rustc_span:: DUMMY_SP ;
1618pub use valtree:: * ;
1719
1820/// Use this rather than `ConstData`, whenever possible.
@@ -104,6 +106,34 @@ impl<'tcx> Const<'tcx> {
104106 Const :: new ( tcx, ty:: ConstKind :: Expr ( expr) , ty)
105107 }
106108
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+
107137 /// Literals and const generic parameters are eagerly converted to a constant, everything else
108138 /// becomes `Unevaluated`.
109139 #[ instrument( skip( tcx) , level = "debug" ) ]
@@ -200,7 +230,9 @@ impl<'tcx> Const<'tcx> {
200230 param_ty,
201231 ) )
202232 }
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+ }
204236 arg => bug ! ( "unexpected bound var resolution for {:?}: {arg:?}" , expr. hir_id) ,
205237 }
206238 }
@@ -285,7 +317,7 @@ impl<'tcx> Const<'tcx> {
285317 if let Some ( val) = self . kind ( ) . try_eval_for_typeck ( tcx, param_env) {
286318 match val {
287319 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 ( ) ) ,
289321 }
290322 } else {
291323 // Either the constant isn't evaluatable or ValTree creation failed.
0 commit comments