frontend 0.4.0

rustc's frontend with no LLVM and no std: parsing through MIR, as a library
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
// `#![no_std]`: these arrive with the standard prelude and name no path, so a `std::`
// search cannot see them - and a `#[derive]` can use them without the name appearing
// in this file at all, which is why they are not trimmed by inspection.
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;

use core::any::Any;
use alloc::borrow::Cow;
use core::fmt;

use either::Either;
use crate::rustc_abi::{Align, Size, VariantIdx};
use crate::rustc_errors::{DiagArgValue, ErrorGuaranteed, IntoDiagArg};
use rustc_macros::{StableHash, TyDecodable, TyEncodable};
use crate::rustc_span::def_id::DefId;
use crate::rustc_span::{DUMMY_SP, Span, Symbol};

use super::{AllocId, AllocRange, ConstAllocation, Pointer, Scalar};
use crate::rustc_middle::diagnostics;
use crate::rustc_middle::mir::interpret::CtfeProvenance;
use crate::rustc_middle::mir::{ConstAlloc, ConstValue};
use crate::rustc_middle::ty::{self, Ty, TyCtxt, ValTree, layout};

#[derive(Debug, Copy, Clone, PartialEq, Eq, StableHash, TyEncodable, TyDecodable)]
pub enum ErrorHandled {
    /// Already reported an error for this evaluation, and the compilation is
    /// *guaranteed* to fail. Warnings/lints *must not* produce `Reported`.
    Reported(ReportedErrorInfo, Span),
    /// Don't emit an error, the evaluation failed because the MIR was generic
    /// and the args didn't fully monomorphize it.
    TooGeneric(Span),
}

impl From<ReportedErrorInfo> for ErrorHandled {
    #[inline]
    fn from(error: ReportedErrorInfo) -> ErrorHandled {
        ErrorHandled::Reported(error, DUMMY_SP)
    }
}

impl ErrorHandled {
    pub(crate) fn with_span(self, span: Span) -> Self {
        match self {
            ErrorHandled::Reported(err, _span) => ErrorHandled::Reported(err, span),
            ErrorHandled::TooGeneric(_span) => ErrorHandled::TooGeneric(span),
        }
    }

    pub fn emit_note(&self, tcx: TyCtxt<'_>) {
        match self {
            &ErrorHandled::Reported(err, span) => {
                if !err.allowed_in_infallible && !span.is_dummy() {
                    tcx.dcx().emit_note(diagnostics::ErroneousConstant { span });
                }
            }
            &ErrorHandled::TooGeneric(_) => {}
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, StableHash, TyEncodable, TyDecodable)]
pub struct ReportedErrorInfo {
    error: ErrorGuaranteed,
    /// Whether this error is allowed to show up even in otherwise "infallible" promoteds.
    /// This is for things like overflows during size computation or resource exhaustion.
    allowed_in_infallible: bool,
}

impl ReportedErrorInfo {
    #[inline]
    pub fn const_eval_error(error: ErrorGuaranteed) -> ReportedErrorInfo {
        ReportedErrorInfo { allowed_in_infallible: false, error }
    }

    /// Use this when the error that led to this is *not* a const-eval error
    /// (e.g., a layout or type checking error).
    #[inline]
    pub fn non_const_eval_error(error: ErrorGuaranteed) -> ReportedErrorInfo {
        ReportedErrorInfo { allowed_in_infallible: true, error }
    }

    /// Use this when the error that led to this *is* a const-eval error, but
    /// we do allow it to occur in infallible constants (e.g., resource exhaustion).
    #[inline]
    pub fn allowed_in_infallible(error: ErrorGuaranteed) -> ReportedErrorInfo {
        ReportedErrorInfo { allowed_in_infallible: true, error }
    }

    pub fn is_allowed_in_infallible(&self) -> bool {
        self.allowed_in_infallible
    }
}

impl From<ReportedErrorInfo> for ErrorGuaranteed {
    #[inline]
    fn from(val: ReportedErrorInfo) -> Self {
        val.error
    }
}

/// An error type for the `const_to_valtree` query. Some error should be reported with a "use-site span",
/// which means the query cannot emit the error, so those errors are represented as dedicated variants here.
#[derive(Debug, Copy, Clone, PartialEq, Eq, StableHash, TyEncodable, TyDecodable)]
pub enum ValTreeCreationError<'tcx> {
    /// The constant is too big to be valtree'd.
    NodesOverflow,
    /// The constant references mutable or external memory, so it cannot be valtree'd.
    InvalidConst,
    /// Values of this type, or this particular value, are not supported as valtrees.
    NonSupportedType(Ty<'tcx>),
    /// Trying to valtree this constant would cause the valtree to have cycles.
    CyclicConst,
    /// The error has already been handled by const evaluation.
    ErrorHandled(ErrorHandled),
}

impl<'tcx> From<ErrorHandled> for ValTreeCreationError<'tcx> {
    fn from(err: ErrorHandled) -> Self {
        ValTreeCreationError::ErrorHandled(err)
    }
}

impl<'tcx> From<InterpErrorInfo<'tcx>> for ValTreeCreationError<'tcx> {
    fn from(err: InterpErrorInfo<'tcx>) -> Self {
        // An error occurred outside the const-eval query, as part of constructing the valtree. We
        // don't currently preserve the details of this error, since `InterpErrorInfo` cannot be put
        // into a query result and it can only be access of some mutable or external memory.
        let (_kind, backtrace) = err.into_parts();
        backtrace.print_backtrace();
        ValTreeCreationError::InvalidConst
    }
}

impl<'tcx> ValTreeCreationError<'tcx> {
    pub(crate) fn with_span(self, span: Span) -> Self {
        use ValTreeCreationError::*;
        match self {
            ErrorHandled(handled) => ErrorHandled(handled.with_span(span)),
            other => other,
        }
    }
}

pub type EvalToAllocationRawResult<'tcx> = Result<ConstAlloc<'tcx>, ErrorHandled>;
pub type EvalStaticInitializerRawResult<'tcx> = Result<ConstAllocation<'tcx>, ErrorHandled>;
pub type EvalToConstValueResult<'tcx> = Result<ConstValue, ErrorHandled>;
pub type EvalToValTreeResult<'tcx> = Result<ValTree<'tcx>, ValTreeCreationError<'tcx>>;

#[cfg(target_pointer_width = "64")]
crate::static_assert_size!(InterpErrorInfo<'_>, 8);

/// Packages the kind of error we got from the const code interpreter
/// up with a Rust-level backtrace of where the error occurred.
/// These should always be constructed by calling `.into()` on
/// an `InterpError`. In `rustc_mir::interpret`, we have `throw_err_*`
/// macros for this.
///
/// Interpreter errors must *not* be silently discarded (that will lead to a panic). Instead,
/// explicitly call `discard_err` if this is really the right thing to do. Note that if
/// this happens during const-eval or in Miri, it could lead to a UB error being lost!
#[derive(Debug)]
pub struct InterpErrorInfo<'tcx>(Box<InterpErrorInfoInner<'tcx>>);

#[derive(Debug)]
struct InterpErrorInfoInner<'tcx> {
    kind: InterpErrorKind<'tcx>,
    backtrace: InterpErrorBacktrace,
}

/// Was a captured `std::backtrace::Backtrace`. There is no backtrace without `std`, so the
/// capture is gone and this carries nothing; the type is kept because `into_parts` /
/// `from_parts` are part of the interpreter's public shape.
///
/// BEHAVIOUR CHANGE: `RUSTC_CTFE_BACKTRACE` (`CtfeBacktrace::Capture` / `::Immediate`) no longer
/// does anything - a CTFE error now prints its message with no rustc-internal backtrace under it.
#[derive(Debug)]
pub struct InterpErrorBacktrace;

impl InterpErrorBacktrace {
    pub fn new() -> InterpErrorBacktrace {
        InterpErrorBacktrace
    }

    pub fn print_backtrace(&self) {}
}

impl<'tcx> InterpErrorInfo<'tcx> {
    pub fn into_parts(self) -> (InterpErrorKind<'tcx>, InterpErrorBacktrace) {
        let InterpErrorInfo(inner) = self;
        let InterpErrorInfoInner { kind, backtrace } = *inner;
        (kind, backtrace)
    }

    pub fn into_kind(self) -> InterpErrorKind<'tcx> {
        self.0.kind
    }

    pub fn from_parts(kind: InterpErrorKind<'tcx>, backtrace: InterpErrorBacktrace) -> Self {
        Self(Box::new(InterpErrorInfoInner { kind, backtrace }))
    }

    #[inline]
    pub fn kind(&self) -> &InterpErrorKind<'tcx> {
        &self.0.kind
    }

    /// Turn the given error into a human-readable string. Expects the string to be printed, so if
    /// `RUSTC_CTFE_BACKTRACE` is set this will show a backtrace of the rustc internals that
    /// triggered the error.
    ///
    /// This is NOT the preferred way to render an error; use `report` from `const_eval` instead.
    /// However, this is useful when error messages appear in ICEs.
    pub fn to_string(&self) -> String {
        self.0.backtrace.print_backtrace();
        self.0.kind.to_string()
    }
}

impl From<ErrorHandled> for InterpErrorInfo<'_> {
    fn from(err: ErrorHandled) -> Self {
        InterpErrorKind::InvalidProgram(match err {
            ErrorHandled::Reported(r, _span) => InvalidProgramInfo::AlreadyReported(r),
            ErrorHandled::TooGeneric(_span) => InvalidProgramInfo::TooGeneric,
        })
        .into()
    }
}

impl<'tcx> From<InterpErrorKind<'tcx>> for InterpErrorInfo<'tcx> {
    fn from(kind: InterpErrorKind<'tcx>) -> Self {
        InterpErrorInfo(Box::new(InterpErrorInfoInner {
            kind,
            backtrace: InterpErrorBacktrace::new(),
        }))
    }
}

/// Details of why a pointer had to be in-bounds.
#[derive(Debug, Copy, Clone)]
pub enum CheckInAllocMsg {
    /// We are accessing memory.
    MemoryAccess,
    /// We are doing pointer arithmetic.
    InboundsPointerArithmetic,
    /// None of the above -- generic/unspecific inbounds test.
    /// The string is the subject of the test, e.g. "pointer".
    Dereferenceable(&'static str),
}

impl fmt::Display for CheckInAllocMsg {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use CheckInAllocMsg::*;
        match self {
            MemoryAccess => write!(f, "memory access failed"),
            InboundsPointerArithmetic => write!(f, "in-bounds pointer arithmetic failed"),
            Dereferenceable(what) => write!(f, "{what} not dereferenceable"),
        }
    }
}

/// Details of which pointer is not aligned.
#[derive(Debug, Copy, Clone)]
pub enum CheckAlignMsg {
    /// The accessed pointer did not have proper alignment.
    AccessedPtr,
    /// The access occurred with a place that was based on a misaligned pointer.
    BasedOn,
}

#[derive(Debug, Copy, Clone)]
pub enum InvalidMetaKind {
    /// Size of a `[T]` is too big
    SliceTooBig,
    /// Size of a DST is too big
    TooBig,
}

impl IntoDiagArg for InvalidMetaKind {
    fn into_diag_arg(self, _: &mut crate::rustc_errors::LongTyPath) -> DiagArgValue {
        DiagArgValue::Str(Cow::Borrowed(match self {
            InvalidMetaKind::SliceTooBig => "slice_too_big",
            InvalidMetaKind::TooBig => "too_big",
        }))
    }
}

/// Details of an access to uninitialized bytes / bad pointer bytes where it is not allowed.
#[derive(Debug, Clone, Copy)]
pub struct BadBytesAccess {
    /// Range of the original memory access.
    pub access: AllocRange,
    /// Range of the bad memory that was encountered. (Might not be maximal.)
    pub bad: AllocRange,
}

/// Information about a misaligned pointer.
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
pub struct Misalignment {
    pub has: Align,
    pub required: Align,
}

/// Error information for when the program caused Undefined Behavior.
#[derive(Debug)]
pub enum UndefinedBehaviorInfo<'tcx> {
    /// Free-form case. Only for errors that are never caught! Used by miri
    Ub(String),
    /// Validation error.
    ValidationError {
        orig_ty: Ty<'tcx>,
        path: Option<String>,
        msg: String,
        ptr_bytes_warning: bool,
    },

    /// Unreachable code was executed.
    Unreachable,
    /// A slice/array index projection went out-of-bounds.
    BoundsCheckFailed { len: u64, index: u64 },
    /// Something was divided by 0 (x / 0).
    DivisionByZero,
    /// Something was "remainded" by 0 (x % 0).
    RemainderByZero,
    /// Signed division overflowed (INT_MIN / -1).
    DivisionOverflow,
    /// Signed remainder overflowed (INT_MIN % -1).
    RemainderOverflow,
    /// Overflowing inbounds pointer arithmetic.
    PointerArithOverflow,
    /// Overflow in arithmetic that may not overflow.
    ArithOverflow { intrinsic: Symbol },
    /// Shift by too much.
    ShiftOverflow { intrinsic: Symbol, shift_amount: Either<u128, i128> },
    /// Invalid metadata in a wide pointer
    InvalidMeta(InvalidMetaKind),
    /// Reading a C string that does not end within its allocation.
    UnterminatedCString(Pointer<AllocId>),
    /// Using a pointer after it got freed.
    PointerUseAfterFree(AllocId, CheckInAllocMsg),
    /// Used a pointer outside the bounds it is valid for.
    PointerOutOfBounds {
        alloc_id: AllocId,
        alloc_size: Size,
        ptr_offset: i64,
        /// The size of the memory range that was expected to be in-bounds.
        inbounds_size: i64,
        msg: CheckInAllocMsg,
    },
    /// Using an integer as a pointer in the wrong way.
    DanglingIntPointer {
        addr: u64,
        /// The size of the memory range that was expected to be in-bounds (or 0 if we need an
        /// allocation but not any actual memory there, e.g. for function pointers).
        inbounds_size: i64,
        msg: CheckInAllocMsg,
    },
    /// Used a pointer with bad alignment.
    AlignmentCheckFailed(Misalignment, CheckAlignMsg),
    /// Writing to read-only memory.
    WriteToReadOnly(AllocId),
    /// Trying to access the data behind a function pointer.
    DerefFunctionPointer(AllocId),
    /// Trying to access the data behind a vtable pointer.
    DerefVTablePointer(AllocId),
    /// Trying to access the data behind a va_list pointer.
    DerefVaListPointer(AllocId),
    /// Trying to access the actual type id.
    DerefTypeIdPointer(AllocId),
    /// Using a non-boolean `u8` as bool.
    InvalidBool(u8),
    /// Using a non-character `u32` as character.
    InvalidChar(u32),
    /// The tag of an enum does not encode an actual discriminant.
    InvalidTag(Scalar<AllocId>),
    /// Using a pointer-not-to-a-function as function pointer.
    InvalidFunctionPointer(Pointer<AllocId>),
    /// Using a pointer-not-to-a-va-list as variable argument list pointer.
    InvalidVaListPointer(Pointer<AllocId>),
    /// Using a pointer-not-to-a-vtable as vtable pointer.
    InvalidVTablePointer(Pointer<Option<AllocId>>),
    /// Using a vtable for the wrong trait.
    InvalidVTableTrait {
        /// The vtable that was actually referenced by the wide pointer metadata.
        vtable_dyn_type: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
        /// The vtable that was expected at the point in MIR that it was accessed.
        expected_dyn_type: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
    },
    /// Using a string that is not valid UTF-8,
    InvalidStr(core::str::Utf8Error),
    /// Using uninitialized data where it is not allowed.
    InvalidUninitBytes(Option<(AllocId, BadBytesAccess)>),
    /// Working with a local that is not currently live.
    DeadLocal,
    /// A discriminant of an uninhabited enum variant is written.
    UninhabitedEnumVariantWritten(VariantIdx),
    /// An uninhabited enum variant is projected.
    UninhabitedEnumVariantRead(Option<VariantIdx>),
    /// Trying to set discriminant to the niched variant, but the value does not match.
    InvalidNichedEnumVariantWritten { enum_ty: Ty<'tcx> },
    /// ABI-incompatible argument types.
    AbiMismatchArgument {
        /// The index of the argument whose type is wrong.
        arg_idx: usize,
        caller_ty: Ty<'tcx>,
        callee_ty: Ty<'tcx>,
    },
    /// ABI-incompatible return types.
    AbiMismatchReturn { caller_ty: Ty<'tcx>, callee_ty: Ty<'tcx> },
    /// `va_arg` was called on an exhausted `VaList`.
    VaArgOutOfBounds,
    /// The caller and callee disagree on whether they are c-variadic or not.
    CVariadicMismatch { caller_is_c_variadic: bool, callee_is_c_variadic: bool },
    /// The caller and callee disagree on the number of fixed (i.e. non-c-variadic) arguments.
    CVariadicFixedCountMismatch { caller: u32, callee: u32 },
}

impl<'tcx> fmt::Display for UndefinedBehaviorInfo<'tcx> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use UndefinedBehaviorInfo::*;

        fn fmt_in_alloc_attempt(
            f: &mut fmt::Formatter<'_>,
            msg: CheckInAllocMsg,
            inbounds_size: i64,
        ) -> fmt::Result {
            let inbounds_size_fmt = if inbounds_size == 1 {
                format_args!("1 byte")
            } else {
                format_args!("{inbounds_size} bytes")
            };
            write!(f, "{msg}: ")?;
            match msg {
                CheckInAllocMsg::MemoryAccess => {
                    write!(f, "attempting to access {inbounds_size_fmt}")
                }
                CheckInAllocMsg::InboundsPointerArithmetic => {
                    write!(f, "attempting to offset pointer by {inbounds_size_fmt}")
                }
                CheckInAllocMsg::Dereferenceable(what) if inbounds_size == 0 => {
                    write!(f, "{what} must point to some allocation")
                }
                CheckInAllocMsg::Dereferenceable(what) => {
                    write!(f, "{what} must be dereferenceable for {inbounds_size_fmt}")
                }
            }
        }

        match self {
            Ub(msg) => write!(f, "{msg}"),

            ValidationError { orig_ty, path: None, msg, .. } => {
                write!(f, "constructing invalid value of type {orig_ty}: {msg}")
            }
            ValidationError { orig_ty, path: Some(path), msg, .. } => {
                write!(f, "constructing invalid value of type {orig_ty}: at {path}, {msg}")
            }

            Unreachable => write!(f, "entering unreachable code"),
            BoundsCheckFailed { len, index } => {
                write!(f, "indexing out of bounds: the len is {len} but the index is {index}")
            }
            DivisionByZero => write!(f, "dividing by zero"),
            RemainderByZero => write!(f, "calculating the remainder with a divisor of zero"),
            DivisionOverflow => write!(f, "overflow in signed division (dividing MIN by -1)"),
            RemainderOverflow => write!(f, "overflow in signed remainder (dividing MIN by -1)"),
            PointerArithOverflow => write!(
                f,
                "overflowing pointer arithmetic: the total offset in bytes does not fit in an `isize`"
            ),
            ArithOverflow { intrinsic } => write!(f, "arithmetic overflow in `{intrinsic}`"),
            ShiftOverflow { shift_amount, intrinsic } => {
                write!(f, "overflowing shift by {shift_amount} in `{intrinsic}`")
            }
            InvalidMeta(InvalidMetaKind::SliceTooBig) => write!(
                f,
                "invalid metadata in wide pointer: slice is bigger than largest supported object"
            ),
            InvalidMeta(InvalidMetaKind::TooBig) => write!(
                f,
                "invalid metadata in wide pointer: total size is bigger than largest supported object"
            ),
            UnterminatedCString(ptr) => write!(
                f,
                "reading a null-terminated string starting at {ptr} with no null found before end of allocation"
            ),
            PointerUseAfterFree(alloc_id, msg) => {
                write!(f, "{msg}: {alloc_id} has been freed, so this pointer is dangling")
            }
            &PointerOutOfBounds { alloc_id, alloc_size, ptr_offset, inbounds_size, msg } => {
                fmt_in_alloc_attempt(f, msg, inbounds_size)?;
                write!(f, ", but got ")?;
                // Write pointer. Offset might be negative so we cannot use the normal `impl Display
                // for Pointer`.
                write!(f, "{}", alloc_id)?;
                if ptr_offset > 0 {
                    write!(f, "+{:#x}", ptr_offset)?;
                } else if ptr_offset < 0 {
                    write!(f, "-{:#x}", ptr_offset.unsigned_abs())?;
                }
                // Write why it is invalid.
                write!(f, " which ")?;
                if ptr_offset < 0 {
                    write!(f, "points to before the beginning of the allocation")
                } else if inbounds_size < 0 {
                    // We expected the ptr to have memory to its left, but it does not.
                    if ptr_offset == 0 {
                        write!(f, "is at the beginning of the allocation")
                    } else {
                        write!(f, "is only {ptr_offset} bytes from the beginning of the allocation")
                    }
                } else {
                    let ptr_offset = ptr_offset as u64;
                    let alloc_size = alloc_size.bytes();
                    if ptr_offset >= alloc_size {
                        let size = if alloc_size == 1 {
                            format_args!("1 byte")
                        } else {
                            format_args!("{alloc_size} bytes")
                        };
                        write!(f, "is at or beyond the end of the allocation of size {size}",)
                    } else {
                        let dist_to_end = alloc_size - ptr_offset;
                        let dist = if dist_to_end == 1 {
                            format_args!("1 byte")
                        } else {
                            format_args!("{dist_to_end} bytes")
                        };
                        write!(f, "is only {dist} from the end of the allocation",)
                    }
                }
            }
            &DanglingIntPointer { addr: 0, inbounds_size, msg } => {
                fmt_in_alloc_attempt(f, msg, inbounds_size)?;
                write!(f, ", but got null pointer")
            }
            &DanglingIntPointer { addr, inbounds_size, msg } => {
                fmt_in_alloc_attempt(f, msg, inbounds_size)?;
                write!(
                    f,
                    ", but got {ptr} which is a dangling pointer (it has no provenance)",
                    ptr = Pointer::<Option<CtfeProvenance>>::without_provenance(addr),
                )
            }
            AlignmentCheckFailed(misalign, msg) => {
                write!(
                    f,
                    "{acc} with alignment {has}, but alignment {required} is required",
                    acc = match msg {
                        CheckAlignMsg::AccessedPtr => "accessing memory",
                        CheckAlignMsg::BasedOn => "accessing memory based on pointer",
                    },
                    has = misalign.has.bytes(),
                    required = misalign.required.bytes(),
                )
            }
            WriteToReadOnly(alloc) => write!(f, "writing to {alloc} which is read-only"),
            DerefFunctionPointer(alloc) => {
                write!(f, "accessing {alloc} which contains a function")
            }
            DerefVTablePointer(alloc) => write!(f, "accessing {alloc} which contains a vtable"),
            DerefVaListPointer(alloc) => {
                write!(f, "accessing {alloc} which contains a variable argument list")
            }
            DerefTypeIdPointer(alloc) => write!(f, "accessing {alloc} which contains a `TypeId`"),
            InvalidBool(value) => {
                write!(f, "interpreting an invalid 8-bit value as a bool: 0x{value:02x}")
            }
            InvalidChar(value) => {
                write!(f, "interpreting an invalid 32-bit value as a char: 0x{value:08x}")
            }
            InvalidTag(tag) => write!(f, "enum value has invalid tag: {tag:x}"),
            InvalidFunctionPointer(ptr) => {
                write!(f, "using {ptr} as function pointer but it does not point to a function")
            }
            InvalidVaListPointer(ptr) => write!(
                f,
                "using {ptr} as variable argument list pointer but it does not point to a variable argument list"
            ),
            InvalidVTablePointer(ptr) => {
                write!(f, "using {ptr} as vtable pointer but it does not point to a vtable")
            }
            InvalidVTableTrait { vtable_dyn_type, expected_dyn_type } => write!(
                f,
                "using vtable for `{vtable_dyn_type}` but `{expected_dyn_type}` was expected"
            ),
            InvalidStr(err) => write!(f, "this string is not valid UTF-8: {err}"),
            InvalidUninitBytes(None) => {
                write!(
                    f,
                    "using uninitialized data, but this operation requires initialized memory"
                )
            }
            InvalidUninitBytes(Some((alloc, info))) => write!(
                f,
                "reading memory at {alloc}{access}, but memory is uninitialized at {uninit}, and this operation requires initialized memory",
                access = info.access,
                uninit = info.bad,
            ),
            DeadLocal => write!(f, "accessing a dead local variable"),
            UninhabitedEnumVariantWritten(_) => {
                write!(f, "writing discriminant of an uninhabited enum variant")
            }
            UninhabitedEnumVariantRead(_) => {
                write!(f, "read discriminant of an uninhabited enum variant")
            }
            InvalidNichedEnumVariantWritten { enum_ty } => {
                write!(
                    f,
                    "trying to set discriminant of a {enum_ty} to the niched variant, but the value does not match"
                )
            }
            AbiMismatchArgument { arg_idx, caller_ty, callee_ty } => write!(
                f,
                "calling a function whose parameter #{arg_idx} has type {callee_ty} passing argument of type {caller_ty}",
                arg_idx = arg_idx + 1, // adjust for 1-indexed lists in output
            ),
            AbiMismatchReturn { caller_ty, callee_ty } => write!(
                f,
                "calling a function with return type {callee_ty} passing return place of type {caller_ty}"
            ),
            VaArgOutOfBounds => write!(f, "more C-variadic arguments read than were passed"),
            CVariadicMismatch { .. } => write!(
                f,
                "calling a function where the caller and callee disagree on whether the function is C-variadic"
            ),
            CVariadicFixedCountMismatch { caller, callee } => write!(
                f,
                "calling a C-variadic function with {caller} fixed arguments, but the function expects {callee}"
            ),
        }
    }
}

/// Error information for when the program we executed turned out not to actually be a valid
/// program. This cannot happen in stand-alone Miri (except for layout errors that are only detect
/// during monomorphization), but it can happen during CTFE/ConstProp where we work on generic code
/// or execution does not have all information available.
#[derive(Debug)]
pub enum InvalidProgramInfo<'tcx> {
    /// Resolution can fail if we are in a too generic context.
    TooGeneric,
    /// Abort in case errors are already reported.
    AlreadyReported(ReportedErrorInfo),
    /// An error occurred during layout computation.
    Layout(layout::LayoutError<'tcx>),
}

impl<'tcx> fmt::Display for InvalidProgramInfo<'tcx> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use InvalidProgramInfo::*;
        match self {
            TooGeneric => write!(f, "encountered overly generic constant"),
            AlreadyReported(_) => {
                write!(
                    f,
                    "an error has already been reported elsewhere (this should not usually be printed)"
                )
            }
            Layout(e) => write!(f, "{e}"),
        }
    }
}

/// Error information for when the program did something that might (or might not) be correct
/// to do according to the Rust spec, but due to limitations in the interpreter, the
/// operation could not be carried out. These limitations can differ between CTFE and the
/// Miri engine, e.g., CTFE does not support dereferencing pointers at integral addresses.
#[derive(Debug)]
pub enum UnsupportedOpInfo {
    /// Free-form case. Only for errors that are never caught! Used by Miri.
    // FIXME still use translatable diagnostics
    Unsupported(String),
    /// Unsized local variables.
    UnsizedLocal,
    /// Extern type field with an indeterminate offset.
    ExternTypeField,
    //
    // The variants below are only reachable from CTFE/const prop, miri will never emit them.
    //
    /// Attempting to read or copy parts of a pointer to somewhere else; without knowing absolute
    /// addresses, the resulting state cannot be represented by the CTFE interpreter.
    ReadPartialPointer(Pointer<AllocId>),
    /// Encountered a pointer where we needed an integer.
    ReadPointerAsInt(Option<(AllocId, BadBytesAccess)>),
    /// Accessing thread local statics
    ThreadLocalStatic(DefId),
    /// Accessing an unsupported extern static.
    ExternStatic(DefId),
}

impl fmt::Display for UnsupportedOpInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use UnsupportedOpInfo::*;
        match self {
            Unsupported(s) => write!(f, "{s}"),
            ExternTypeField => {
                write!(f, "`extern type` field does not have a known offset")
            }
            UnsizedLocal => write!(f, "unsized locals are not supported"),
            ReadPartialPointer(ptr) => {
                write!(f, "unable to read parts of a pointer from memory at {ptr}")
            }
            ReadPointerAsInt(_) => write!(f, "unable to turn pointer into integer"),
            &ThreadLocalStatic(did) => {
                write!(
                    f,
                    "cannot access thread local static `{did}`",
                    did = ty::tls::with(|tcx| tcx.def_path_str(did))
                )
            }
            &ExternStatic(did) => {
                write!(
                    f,
                    "cannot access extern static `{did}`",
                    did = ty::tls::with(|tcx| tcx.def_path_str(did))
                )
            }
        }
    }
}

/// Error information for when the program exhausted the resources granted to it
/// by the interpreter.
#[derive(Debug)]
pub enum ResourceExhaustionInfo {
    /// The stack grew too big.
    StackFrameLimitReached,
    /// There is not enough memory (on the host) to perform an allocation.
    MemoryExhausted,
    /// The address space (of the target) is full.
    AddressSpaceFull,
    /// The compiler got an interrupt signal (a user ran out of patience).
    Interrupted,
}

impl fmt::Display for ResourceExhaustionInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use ResourceExhaustionInfo::*;
        match self {
            StackFrameLimitReached => {
                write!(f, "reached the configured maximum number of stack frames")
            }
            MemoryExhausted => {
                write!(f, "tried to allocate more memory than available to compiler")
            }
            AddressSpaceFull => {
                write!(f, "there are no more free addresses in the address space")
            }
            Interrupted => write!(f, "compilation was interrupted"),
        }
    }
}

/// A trait for machine-specific errors (or other "machine stop" conditions).
pub trait MachineStopType: Any + fmt::Display + fmt::Debug + Send {
    /// This error occurred during validation, inside a value at the given path.
    fn with_validation_path(&mut self, _path: String) {}
}

impl dyn MachineStopType {
    #[inline(always)]
    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
        let x: &dyn Any = self;
        x.downcast_ref()
    }
}

#[derive(Debug)]
pub enum InterpErrorKind<'tcx> {
    /// The program caused undefined behavior.
    UndefinedBehavior(UndefinedBehaviorInfo<'tcx>),
    /// The program was invalid (ill-typed, bad MIR, not sufficiently monomorphized, ...).
    InvalidProgram(InvalidProgramInfo<'tcx>),
    /// The program did something the interpreter does not support (some of these *might* be UB
    /// but the interpreter is not sure).
    Unsupported(UnsupportedOpInfo),
    /// The program exhausted the interpreter's resources (stack/heap too big,
    /// execution takes too long, ...).
    ResourceExhaustion(ResourceExhaustionInfo),
    /// Stop execution for a machine-controlled reason. This is never raised by
    /// the core engine itself.
    MachineStop(Box<dyn MachineStopType>),
}

impl<'tcx> fmt::Display for InterpErrorKind<'tcx> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use InterpErrorKind::*;
        match self {
            Unsupported(msg) => write!(f, "{msg}"),
            InvalidProgram(msg) => write!(f, "{msg}"),
            UndefinedBehavior(msg) => write!(f, "{msg}"),
            ResourceExhaustion(msg) => write!(f, "{msg}"),
            MachineStop(msg) => write!(f, "{msg}"),
        }
    }
}

impl InterpErrorKind<'_> {
    /// Some errors do string formatting even if the error is never printed.
    /// To avoid performance issues, there are places where we want to be sure to never raise these formatting errors,
    /// so this method lets us detect them and `bug!` on unexpected errors.
    pub fn formatted_string(&self) -> bool {
        matches!(
            self,
            InterpErrorKind::Unsupported(UnsupportedOpInfo::Unsupported(_))
                | InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::ValidationError { .. })
                | InterpErrorKind::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_))
        )
    }
}

// Macros for constructing / throwing `InterpErrorKind`
#[macro_export]
macro_rules! err_unsup {
    ($($tt:tt)*) => {
        $crate::rustc_middle::mir::interpret::InterpErrorKind::Unsupported(
            $crate::rustc_middle::mir::interpret::UnsupportedOpInfo::$($tt)*
        )
    };
}

#[macro_export]
macro_rules! err_unsup_format {
    ($($tt:tt)*) => { $crate::err_unsup!(Unsupported(format!($($tt)*))) };
}

#[macro_export]
macro_rules! err_inval {
    ($($tt:tt)*) => {
        $crate::rustc_middle::mir::interpret::InterpErrorKind::InvalidProgram(
            $crate::rustc_middle::mir::interpret::InvalidProgramInfo::$($tt)*
        )
    };
}

#[macro_export]
macro_rules! err_ub {
    ($($tt:tt)*) => {
        $crate::rustc_middle::mir::interpret::InterpErrorKind::UndefinedBehavior(
            $crate::rustc_middle::mir::interpret::UndefinedBehaviorInfo::$($tt)*
        )
    };
}

#[macro_export]
macro_rules! err_ub_format {
    ($($tt:tt)*) => { $crate::err_ub!(Ub(format!($($tt)*))) };
}

#[macro_export]
macro_rules! err_exhaust {
    ($($tt:tt)*) => {
        $crate::rustc_middle::mir::interpret::InterpErrorKind::ResourceExhaustion(
            $crate::rustc_middle::mir::interpret::ResourceExhaustionInfo::$($tt)*
        )
    };
}

#[macro_export]
macro_rules! err_machine_stop {
    ($($tt:tt)*) => {
        $crate::rustc_middle::mir::interpret::InterpErrorKind::MachineStop(Box::new($($tt)*))
    };
}

// The `throw_*` macros return from the enclosing fn or closure. Upstream used `do yeet` so
// they could also exit a `try {}` block; neither exists on stable, and every former `try {}`
// in this tree is now an immediately called closure, so `return` exits the same scope.
// `InterpErrorInfo::from` rather than `.into()` keeps inference working in closures whose
// return type is not annotated.
#[macro_export]
macro_rules! throw_unsup {
    ($($tt:tt)*) => {
        return ::core::result::Result::Err(
            $crate::rustc_middle::mir::interpret::InterpErrorInfo::from($crate::err_unsup!($($tt)*)),
        )
    };
}

#[macro_export]
macro_rules! throw_unsup_format {
    ($($tt:tt)*) => {
        return ::core::result::Result::Err(
            $crate::rustc_middle::mir::interpret::InterpErrorInfo::from(
                $crate::err_unsup_format!($($tt)*),
            ),
        )
    };
}

#[macro_export]
macro_rules! throw_inval {
    ($($tt:tt)*) => {
        return ::core::result::Result::Err(
            $crate::rustc_middle::mir::interpret::InterpErrorInfo::from($crate::err_inval!($($tt)*)),
        )
    };
}

#[macro_export]
macro_rules! throw_ub {
    ($($tt:tt)*) => {
        return ::core::result::Result::Err(
            $crate::rustc_middle::mir::interpret::InterpErrorInfo::from($crate::err_ub!($($tt)*)),
        )
    };
}

#[macro_export]
macro_rules! throw_ub_format {
    ($($tt:tt)*) => {
        return ::core::result::Result::Err(
            $crate::rustc_middle::mir::interpret::InterpErrorInfo::from(
                $crate::err_ub_format!($($tt)*),
            ),
        )
    };
}

#[macro_export]
macro_rules! throw_exhaust {
    ($($tt:tt)*) => {
        return ::core::result::Result::Err(
            $crate::rustc_middle::mir::interpret::InterpErrorInfo::from($crate::err_exhaust!($($tt)*)),
        )
    };
}

#[macro_export]
macro_rules! throw_machine_stop {
    ($($tt:tt)*) => {
        return ::core::result::Result::Err(
            $crate::rustc_middle::mir::interpret::InterpErrorInfo::from(
                $crate::err_machine_stop!($($tt)*),
            ),
        )
    };
}

/// The result type used by the interpreter.
///
/// Upstream this is a newtype around `Result` that implements the unstable `Try` trait, hides
/// `ok()`, and panics if an error is dropped unhandled. Stable Rust cannot implement `Try`, and
/// `?` is used on this type thousands of times, so it is a plain `Result` here. What is lost is
/// only the debugging guard: a discarded error no longer panics on drop (`Result` is still
/// `#[must_use]`). `?` on a `Result<_, E>` with `E: Into<InterpErrorInfo>` keeps working because
/// every such `E` has a `From` impl on `InterpErrorInfo`.
pub type InterpResult<'tcx, T = ()> = Result<T, InterpErrorInfo<'tcx>>;

/// The upstream newtype's extra methods, as an extension trait because a type alias of a
/// foreign type cannot have inherent methods. Import it (`use ...::InterpResultExt as _;`)
/// wherever these are called.
pub trait InterpResultExt<'tcx, T>: Sized {
    /// Discard the error information in this result. Only use this if ignoring Undefined Behavior is okay!
    fn discard_err(self) -> Option<T>;

    /// Look at the `Result` wrapped inside of this.
    /// Must only be used to report the error!
    fn report_err(self) -> Result<T, InterpErrorInfo<'tcx>>;

    fn map_err_kind(
        self,
        f: impl FnOnce(InterpErrorKind<'tcx>) -> InterpErrorKind<'tcx>,
    ) -> InterpResult<'tcx, T>;

    fn inspect_err_info(self, f: impl FnOnce(&InterpErrorInfo<'tcx>)) -> InterpResult<'tcx, T>;

    /// Returns success if both `self` and `other` succeed, while ensuring we don't
    /// accidentally drop an error.
    ///
    /// If both are an error, `self` will be reported.
    ///
    /// Named `interp_and` rather than upstream's `and` because `Result::and` is an inherent
    /// method with different semantics and would silently win method resolution.
    fn interp_and<U>(self, other: InterpResult<'tcx, U>) -> InterpResult<'tcx, (T, U)>;
}

impl<'tcx, T> InterpResultExt<'tcx, T> for InterpResult<'tcx, T> {
    #[inline]
    fn discard_err(self) -> Option<T> {
        self.ok()
    }

    #[inline]
    fn report_err(self) -> Result<T, InterpErrorInfo<'tcx>> {
        self
    }

    #[inline]
    fn map_err_kind(
        self,
        f: impl FnOnce(InterpErrorKind<'tcx>) -> InterpErrorKind<'tcx>,
    ) -> InterpResult<'tcx, T> {
        self.map_err(|mut e| {
            e.0.kind = f(e.0.kind);
            e
        })
    }

    #[inline]
    fn inspect_err_info(self, f: impl FnOnce(&InterpErrorInfo<'tcx>)) -> InterpResult<'tcx, T> {
        self.inspect_err(f)
    }

    #[inline]
    fn interp_and<U>(self, other: InterpResult<'tcx, U>) -> InterpResult<'tcx, (T, U)> {
        match self {
            Ok(t) => interp_ok((t, other?)),
            Err(e) => {
                // Discard the other error.
                drop(other);
                // Return `self`.
                Err(e)
            }
        }
    }
}

#[inline(always)]
pub fn interp_ok<'tcx, T>(x: T) -> InterpResult<'tcx, T> {
    Ok(x)
}