Include enclosing item's context on const errors
On const errors, when they occur on an item that might be relevant, like in an associated const, render the line of the enclosing item in the error:
```
error[E0080]: evaluation panicked: not yet implemented
--> $DIR/ice-normalize-inherent-assoc-const.rs:10:28
|
LL | impl Foo {
LL | const ASSOC_C: usize = todo!();
| ^^^^^^^ evaluation of `Foo::ASSOC_C` failed here
```
```
error[E0080]: writing to ALLOC$ID which is read-only
--> $DIR/issue-100313.rs:18:5
|
LL | const _: () = {
LL | let x = T::<{ &true }>;
LL | x.set_false();
| ^^^^^^^^^^^^^ evaluation of `_` failed inside this call
|
note: inside `T::<&true>::set_false`
--> $DIR/issue-100313.rs:11:13
|
LL | impl<const B: &'static bool> T<B> {
LL | const fn set_false(&self) {
LL | unsafe {
LL | *(B as *const bool as *mut bool) = false;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
```
Only the `Goto` variant uses `loop_hint_attrs`, so other variants
shouldn't pay the cost. This shrinks `Terminator` and thus
`BasicBlockData` by 8 bytes, and also simplifies many users of
`Terminator`.
Every byte saved from `BasicBlockData` makes a big difference in
max-rss. For aws-sdk-ec2 full check, this saves 34.1 MiB (~0.62% of its
peak RSS)
Add a `TerminatorKind::goto` helper function for convenient construction
with a target and no hints. With that helper available, most of this
commit is simple, straightforward, mechanical translations of
construction or pattern matching. The only notable changes are:
- `compiler/rustc_codegen_ssa/src/mir/block.rs` actually uses the moved hints.
- `compiler/rustc_middle/src/mir/mod.rs` updates the size assertions.
- `compiler/rustc_mir_build/src/builder/cfg.rs` adds/updates helpers.
- `compiler/rustc_mir_build/src/builder/expr/into.rs` has the one place
that creates a `Goto` with non-empty hints.
reflection: fix ICE when TypeId::info called on ADT
We are refactoring from a large enum where the fields of the variants contain all the reflection info to a flat enum containing only the type kind (struct/bool/array etc). While we removed the fields of Struct, Enum and Union we forgot to take out the const eval code writing into those fields. That gave an ICE. This fixes that.
intrinsics: Rename `abort` to `abort_immediate`
The semantics of `intrinsics::abort()` are closer to what we have unstably as `abort_immediate()` than to `process::abort()` or `libc::abort()`. Rename it to make more clear that the intrinsic is more of an intentional crash with platform-specific behavior than what `libc::abort()` tries to be (i.e. raising `SIGABRT`).
If desired, a new intrinsic like `abort_gracefully` could be introduced that performs platform-specific behavior. This would more cleanly unblock https://github.com/rust-lang/rust/pull/149780.
See also discussion at the tracking issue for `immediate_abort` https://github.com/rust-lang/rust/issues/154601.
We are refactoring from a large enum where the fields of the variants
contain all the reflection info to a flat enum containing only the type
kind (struct/bool/array etc). While we removed the fields of Struct,
Enum and Union we forgot to take out the const eval code writing into
those fields. That gave an ICE. This fixes that.
The semantics of `intrinsics::abort()` are closer to what we have
unstably as `abort_immediate()` than to `process::abort()` or
`libc::abort()`. Rename it to make more clear that the intrinsic is more
of an intentional crash with platform-specific behavior than what
`libc::abort()` tries to be (i.e. raising `SIGABRT`).
Reorganise reflection intrinsics
Trivial refactor moving some things around.
We got a fair amount of intrinsics for reflection now with more on the way. At this point it's worth it to reorganize things a little. This prefixes them with `type_id_` (`TypeId` is the entry point for _nearly_ all of reflection) and moves their declaration into its own module (`core::intrinsics::reflection`).
Possible contentious is the move of the `type_id` intrinsic which has existed for a while and was previously at `core::intrinsics::type_id`. It is still unstable and as far as I am aware intrinsics should really not be relied upon by users.
I've decided to leave the `field_representing_field` (name, offset, actual_type_of) where they are since those where not created for reflection and will be changed in the future (see: https://github.com/rust-lang/rust/issues/162127).
r? oli-obk
core: Unstably constify `abort_immediate`
For const-only code there isn't much of a reason to use this over `panic!()`, but having `abort_immediate` const will make it possible to reuse more code meant for runtime at comptime. The semantics are well-defined so this is pretty straightforward.
Tracking issue: https://github.com/rust-lang/rust/issues/154601
use is_some_and / is_none_or in a few more places
IMO that makes the code more readable.
Clippy also lints this by default these days, but we don't run that lint on the compiler it seems.
It encodes the `emit` behaviour, one of:
- `BugAbort`: Abort as a bug.
- `FatalAbort`: Abort as a fatal error.
- `ErrorGuaranteed`: Return an `ErrorGuaranteed` (the default).
- `()`: return `()`.
This is useful but `Diag` is very widely used and it's arguably not
useful enough to be worth the generic parameter. This commit removes it.
Benefits:
- Many fewer generic parameters, including many functions where the
genericity is entirely uninteresting. This includes every
`Diagnostic::into_diag` and `Subdiagnostic::add_to_diag`. Removes the
need for the comment about `<G>` on `trait Diagnostic`.
- No `PhantomData` in `Diag`.
- Makes bootstrapping a bit faster.
The downside is there is no longer a single `emit` method.
- Getting a `!` return type requires calling `emit_bug` or `emit_fatal`.
This only affects a few call sites.
- Getting an `ErrorGuaranteed` return type requires calling `emit_err`.
This affects a lot of call sites. (It arguably makes call sites more
self-documenting.)
- Using the wrong `emit_*` on a diagnostic causes a runtime abort, which
previously couldn't occur. Seems unlikely, but it's not impossible.
- Note: If no particular return type is needed, `emit` can be used for
any diagnostic. (Bug/fatal diagnostics will still trigger abort.)
move bug and span_bug macros to rustc_span
This allows them to be used without depending on rustc_middle.
This is done by using a rustc_interface callback so that, if the callback is set, a internal compiler error diagnostic is printed through that. After that the panic is initiated.
r? @jdonszelmann @bjorn3
We only checked the provenance for a part of the allocation that will be
interned. Fixed by adding a method under `InterpCx` to check the whole
allocation.
Fix gap in const stability checks around intrinsics
@N1ark managed to find a gap in our const stability checks around intrinsics. This PR fixes that gap.
Sadly the standard library already relies on the gap, so for now I added a `rustc_allow_const_fn_unstable` but that attribute usually needs some process to be approved.
Also fixes https://github.com/rust-lang/rust/issues/150961 by properly supporting & testing `rustc_do_not_const_check` on intrinsics.
Simplify the `G` in `Diag<'a, G>`
The `EmissionGuarantee` trait isn't necessary, and just complicates things. Details in individual commits.
r? @oli-obk
It's a trait that is used to give `Diag::emit` different return types
depending on the `G` in `Diag<'_, G>`. It works, but it's overkill.
There are only four different `G` types in practice (`BugAbort`,
`FatalAbort`, `ErrorGuaranteed`, and `()`) and having inherent `emit`
methods for each of those four concrete `Diag<'_, G>` types is good
enough.
This commit makes that change. This removes `EmissionGuarantee`
trait bounds from many places, which is nice.
miri: enforce proper types for c-variadic arguments in shims
This adds proper type checks for c-variadic argument handling in Miri shims. So far we would have ICEd for many type mismatches since the innards of the interpreter generally assume that things are well-typed. The checks we are using are the same as for [`next_arg`](https://doc.rust-lang.org/nightly/std/ffi/struct.VaList.html#method.next_arg).
This uncovered some interesting points:
- We should accept `&mut` as equivalent to `*mut`. We don't allow `&mut` as a type in `next_arg` but we do allow it for c-variadic calls, so this change also affects a bit more code in const-eval (if the caller passes an `&mut` and the callee expects a `*mut`). Even CFI is okay with treating `&mut` and `*mut` as equivalent so this should be fine. Cc @folkertdev
- Some places in the standard library futex handling used types for their variadic arguments that are not quite correct according to these rules. I fixed those. Cc @m-ou-se
- One variadic function takes pointers to `libc::timespec`. A program with a libc dependency has two copies of libc (one in the sysroot, and the normal dependency). When Miri looks up the expected argument type, it picks one of them, leading to Miri seeing a type mismatch when there is a call using the other copy. C generally considers two copies of the same type to be identical, so I implemented logic that does the same: two `repr(C)` types are "identical" if the names of the types, and the names and types of all fields, are identical. I then also used that logic for the normal ABI checks, where it will also help since we had similar trouble there in the past (that we had to find other work-arounds for).
Reflection refactor ptrs
Tracking issue rust-lang/rust#146922
Part of the [Refactoring to many methods](https://rust-lang.zulipchat.com/#narrow/channel/572285-goals.2Freflection/topic/Refactoring.20to.20many.20methods/with/592226560) reflection subgoal. See that for the full context. In short for various reasons we are moving from an enum `TypeKind` where the variants hold the types information to one that just tells you the type kind. Separate methods on `TypeId` provide a way to get to the data that used to be in the variant.
Since function pointers have a lot of fields it felt better to keep the `FnPtr` struct we have now then add an intrinsic for each field.
r? @oli-obk
fix(reborrow): recursive implementation
If Reborrow finds '&'a mut T' fields where 'a is the Reborrowed type's first lifetime parameter (currently the only lifetime that is allowed to reborrow) then it inserts a Deref and borrow of the T, and likewise if it finds a 'T: Reborrow' field then the field type is recursed into.
This makes Reborrow always produce the correct borrow checking logic at the cost of most probably being inconsiderately expensive. The thinking is that performance will be a followup consideration.
r? @oli-obk