1- use std:: mem;
1+ use std:: { fmt , mem} ;
22
3- use rustc_errors:: { Diag , DiagArgName , DiagArgValue , DiagMessage , IntoDiagArg } ;
3+ use rustc_errors:: Diag ;
44use rustc_middle:: mir:: AssertKind ;
5- use rustc_middle:: mir:: interpret:: { AllocId , Provenance , ReportedErrorInfo , UndefinedBehaviorInfo } ;
5+ use rustc_middle:: mir:: interpret:: {
6+ AllocId , Provenance , ReportedErrorInfo , UndefinedBehaviorInfo , UnsupportedOpInfo ,
7+ } ;
68use rustc_middle:: query:: TyCtxtAt ;
79use rustc_middle:: ty:: ConstInt ;
810use rustc_middle:: ty:: layout:: LayoutError ;
911use rustc_span:: { Span , Symbol } ;
1012
1113use super :: CompileTimeMachine ;
12- use crate :: errors:: { self , FrameNote , ReportErrorExt } ;
14+ use crate :: errors:: { self , FrameNote } ;
1315use crate :: interpret:: {
1416 CtfeProvenance , ErrorHandled , Frame , InterpCx , InterpErrorInfo , InterpErrorKind ,
1517 MachineStopType , Pointer , err_inval, err_machine_stop,
@@ -40,65 +42,49 @@ pub enum ConstEvalErrKind {
4042 ConstMakeGlobalWithOffset ( Pointer < Option < CtfeProvenance > > ) ,
4143}
4244
43- impl MachineStopType for ConstEvalErrKind {
44- fn diagnostic_message ( & self ) -> DiagMessage {
45+ impl fmt :: Display for ConstEvalErrKind {
46+ fn fmt ( & self , f : & mut fmt :: Formatter < ' _ > ) -> fmt :: Result {
4547 use ConstEvalErrKind :: * ;
46- use rustc_errors:: msg;
47-
4848 match self {
49- ConstAccessesMutGlobal => "constant accesses mutable global memory" . into ( ) ,
49+ ConstAccessesMutGlobal => write ! ( f , "constant accesses mutable global memory" ) ,
5050 ModifiedGlobal => {
51- "modifying a static's initial value from another static's initializer" . into ( )
51+ write ! ( f , "modifying a static's initial value from another static's initializer" )
5252 }
53- Panic { .. } => msg ! ( "evaluation panicked: {$ msg}" ) ,
53+ Panic { msg , .. } => write ! ( f , "evaluation panicked: {msg}" ) ,
5454 RecursiveStatic => {
55- "encountered static that tried to access itself during initialization" . into ( )
55+ write ! ( f , "encountered static that tried to access itself during initialization" )
5656 }
57- AssertFailure ( x) => x . diagnostic_message ( ) ,
57+ AssertFailure ( x) => write ! ( f , "{x}" ) ,
5858 WriteThroughImmutablePointer => {
59- msg ! (
59+ write ! (
60+ f,
6061 "writing through a pointer that was derived from a shared (immutable) reference"
6162 )
6263 }
63- ConstMakeGlobalPtrAlreadyMadeGlobal { .. } => {
64- msg ! ( "attempting to call `const_make_global` twice on the same allocation {$alloc}" )
65- }
66- ConstMakeGlobalPtrIsNonHeap ( _) => {
67- msg ! (
68- "pointer passed to `const_make_global` does not point to a heap allocation: {$ptr}"
64+ ConstMakeGlobalPtrAlreadyMadeGlobal ( alloc) => {
65+ write ! (
66+ f,
67+ "attempting to call `const_make_global` twice on the same allocation {alloc}"
6968 )
7069 }
71- ConstMakeGlobalWithDanglingPtr ( _) => {
72- msg ! ( "pointer passed to `const_make_global` is dangling: {$ptr}" )
73- }
74- ConstMakeGlobalWithOffset ( _) => {
75- msg ! ( "making {$ptr} global which does not point to the beginning of an object" )
76- }
77- }
78- }
79- fn add_args ( self : Box < Self > , adder : & mut dyn FnMut ( DiagArgName , DiagArgValue ) ) {
80- use ConstEvalErrKind :: * ;
81- match * self {
82- RecursiveStatic
83- | ConstAccessesMutGlobal
84- | ModifiedGlobal
85- | WriteThroughImmutablePointer => { }
86- AssertFailure ( kind) => kind. add_args ( adder) ,
87- Panic { msg, .. } => {
88- adder ( "msg" . into ( ) , msg. into_diag_arg ( & mut None ) ) ;
70+ ConstMakeGlobalPtrIsNonHeap ( ptr) => {
71+ write ! (
72+ f,
73+ "pointer passed to `const_make_global` does not point to a heap allocation: {ptr}"
74+ )
8975 }
90- ConstMakeGlobalPtrIsNonHeap ( ptr)
91- | ConstMakeGlobalWithOffset ( ptr)
92- | ConstMakeGlobalWithDanglingPtr ( ptr) => {
93- adder ( "ptr" . into ( ) , format ! ( "{ptr:?}" ) . into_diag_arg ( & mut None ) ) ;
76+ ConstMakeGlobalWithDanglingPtr ( ptr) => {
77+ write ! ( f, "pointer passed to `const_make_global` is dangling: {ptr}" )
9478 }
95- ConstMakeGlobalPtrAlreadyMadeGlobal ( alloc ) => {
96- adder ( "alloc" . into ( ) , alloc . into_diag_arg ( & mut None ) ) ;
79+ ConstMakeGlobalWithOffset ( ptr ) => {
80+ write ! ( f , "making {ptr} global which does not point to the beginning of an object" )
9781 }
9882 }
9983 }
10084}
10185
86+ impl MachineStopType for ConstEvalErrKind { }
87+
10288/// The errors become [`InterpErrorKind::MachineStop`] when being raised.
10389impl < ' tcx > Into < InterpErrorInfo < ' tcx > > for ConstEvalErrKind {
10490 fn into ( self ) -> InterpErrorInfo < ' tcx > {
@@ -207,14 +193,20 @@ where
207193 _ => {
208194 let ( our_span, frames) = get_span_and_frames ( ) ;
209195 let span = span. substitute_dummy ( our_span) ;
210- let mut err = tcx. dcx ( ) . struct_span_err ( our_span, error. diagnostic_message ( ) ) ;
211- // We allow invalid programs in infallible promoteds since invalid layouts can occur
212- // anyway (e.g. due to size overflow). And we allow OOM as that can happen any time.
213- let allowed_in_infallible = matches ! (
196+ let mut err = tcx. dcx ( ) . struct_span_err ( our_span, error. to_string ( ) ) ;
197+ if matches ! (
214198 error,
215- InterpErrorKind :: ResourceExhaustion ( _) | InterpErrorKind :: InvalidProgram ( _)
216- ) ;
217-
199+ InterpErrorKind :: UndefinedBehavior ( UndefinedBehaviorInfo :: ValidationError {
200+ ptr_bytes_warning: true ,
201+ ..
202+ } ) | InterpErrorKind :: Unsupported (
203+ UnsupportedOpInfo :: ReadPointerAsInt ( ..)
204+ | UnsupportedOpInfo :: ReadPartialPointer ( ..)
205+ )
206+ ) {
207+ err. help ( "this code performed an operation that depends on the underlying bytes representing a pointer" ) ;
208+ err. help ( "the absolute address of a pointer is not known at compile-time, so such operations are not supported" ) ;
209+ }
218210 if let InterpErrorKind :: UndefinedBehavior ( UndefinedBehaviorInfo :: InvalidUninitBytes (
219211 Some ( ( alloc_id, _access) ) ,
220212 ) ) = error
@@ -229,7 +221,12 @@ where
229221 err. subdiagnostic ( raw_bytes) ;
230222 }
231223
232- error. add_args ( & mut err) ;
224+ // We allow invalid programs in infallible promoteds since invalid layouts can occur
225+ // anyway (e.g. due to size overflow). And we allow OOM as that can happen any time.
226+ let allowed_in_infallible = matches ! (
227+ error,
228+ InterpErrorKind :: ResourceExhaustion ( _) | InterpErrorKind :: InvalidProgram ( _)
229+ ) ;
233230
234231 mk ( & mut err, span, frames) ;
235232 let g = err. emit ( ) ;
0 commit comments