2528 Commits

Author SHA1 Message Date
Samuel Tardieu
58a7b873cf
Rollup merge of #144890 - WaffleLapkin:project_fields, r=lcnr
Add `InterpCx::project_fields`

I was hoping for a much bigger improvement and this is lukewarm at best ^^'

Still, I think this makes sense.
2025-08-05 03:51:40 +02:00
Samuel Tardieu
a1e41a0227
Rollup merge of #144776 - nnethercote:Printer-cleanups, r=cjgillot
`Printer` cleanups

The trait `Printer` is implemented by six types, and the sub-trait `PrettyPrinter` is implemented by three of those types. The traits and the impls are complex and a bit of a mess. This PR starts to clean them up.

r? ``@davidtwco``
2025-08-05 03:51:34 +02:00
Waffle Lapkin
cf7b67420b
add project_fields helper function 2025-08-04 11:34:46 +02:00
Stuart Cook
0225f8b09c
Rollup merge of #144706 - zachs18:fix-144661, r=RalfJung
Do not give function allocations alignment in consteval and Miri.

We do not yet have a (clear and T-lang approved) design for how `#[align(N)]` on functions should affect function pointers' addresses on various platforms, so for now do not give function pointers alignment in consteval and Miri.

----

Old summary:

Not a full solution to <https://github.com/rust-lang/rust/issues/144661>, but fixes the immediate issue by making function allocations all have alignment 1 in consteval, ignoring `#[rustc_align(N)]`, so the compiler doesn't know if any offset other than 0 is non-null.

A more "principlied" solution would probably be to make function pointers to `#[instruction_set(arm::t32)]` functions be at offset 1 of an align-`max(2, align attribute)` allocation instead of at offset 0 of their allocation during consteval, and on wasm to either disallow `#[align(N)]` where N > 1, or to pad the function table such that the function pointer of a `#[align(N)]` function is a multiple of `N` at runtime.
2025-08-04 11:24:39 +10:00
Nicholas Nethercote
2434d8cecf Remove unused arg from path_append_impl.
None of the impls use it.
2025-08-03 19:58:59 +10:00
Nicholas Nethercote
1698c8e322 Rename Printer variables.
Currently they are mostly named `cx`, which is a terrible name for a
type that impls `Printer`/`PrettyPrinter`, and is easy to confuse with
other types like `TyCtxt`. This commit changes them to `p`. A couple of
existing `p` variables had to be renamed to make way.
2025-08-03 19:58:00 +10:00
Nicholas Nethercote
e7d6a0776b Remove type_name::AbsolutePathPrinter::comma_sep.
It's equivalent to the default `PrettyPrinter::comma_sep`.
2025-08-03 19:58:00 +10:00
Nicholas Nethercote
df524163be Mark Printer methods as unreachable where appropriate.
This helps me understand the structure of the code a lot.

If any of these are actually reachable, we can put the old code back,
add a new test case, and we will have improved our test coverage.
2025-08-03 19:58:00 +10:00
zachs18
fe720181b5
Update compiler/rustc_const_eval/src/interpret/memory.rs
Replace commented-out code with link to context for change.

Co-authored-by: Ralf Jung <post@ralfj.de>
2025-08-01 11:06:13 -05:00
Stypox
88c9a256a9
Add EnteredTraceSpan::or_if_tracing_disabled 2025-07-31 21:51:29 +02:00
Stypox
188f7367bf
Add tracing to more functions related to step.rs 2025-07-31 21:51:29 +02:00
Stypox
4e806c8a34
Add tracing calls to eval_statement/terminator 2025-07-31 21:51:29 +02:00
Zachary S
f554c79ef8 Do not give function allocations alignment in consteval or miri. 2025-07-31 12:50:40 -05:00
Jana Dönszelmann
26c28ee2ef
Rollup merge of #144726 - jdonszelmann:move-attr-data-structures, r=lcnr
merge rustc_attr_data_structures into rustc_hir

this move was discussed on zulip: [#t-compiler > attribute parsing rework @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/attribute.20parsing.20rework/near/528530091)

Many PRs in the attribute rework depend on this move.
2025-07-31 17:19:40 +02:00
Stypox
bb08a4dfc7
Make Miri's enter_trace_span! call const_eval's 2025-07-31 15:42:29 +02:00
Jana Dönszelmann
e1d3ad89c7
remove rustc_attr_data_structures 2025-07-31 14:19:27 +02:00
Stypox
e1f674cfa9
Use specific name for "frame" span field
Otherwise the field would be named "message" by default
2025-07-31 00:40:00 +02:00
Stypox
3b5fec08f1
Use new enter_trace_span! syntax for layout_of & friends 2025-07-31 00:40:00 +02:00
Stypox
8e786169e8
Uniform enter_trace_span! and add documentation
The macro was uniformed between rustc_const_eval and miri
2025-07-31 00:40:00 +02:00
Cameron Steffen
172af038a7 Rename trait_of_item -> trait_of_assoc 2025-07-28 09:53:50 -05:00
bors
d242a8bd5a Auto merge of #144469 - Kivooeo:chains-cleanup, r=SparrowLii
Some `let chains` clean-up

Not sure if this kind of clean-up is welcoming because of size, but I decided to try out one

r? compiler
2025-07-28 05:25:23 +00:00
Kivooeo
43725ed819 use let chains in ast, borrowck, codegen, const_eval 2025-07-28 06:08:48 +05:00
Ralf Jung
296586fb02 miri: for ABI mismatch errors, say which argument is the problem 2025-07-27 22:26:10 +02:00
Camille GILLOT
967ba2f93d Remove const deduplication from the interpreter. 2025-07-23 23:54:37 +00:00
Camille GILLOT
0460c92d52 Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
Camille GILLOT
9ff071219b Give an AllocId to ConstValue::Slice. 2025-07-23 23:54:37 +00:00
Matthias Krüger
16c10c9145
Rollup merge of #144256 - oli-obk:type-id-ice, r=RalfJung
Don't ICE on non-TypeId metadata within TypeId

fixes rust-lang/rust#144253

r? ``````````@RalfJung``````````
2025-07-23 15:59:30 +02:00
Oli Scherer
b2f8b40633 Don't ICE on non-TypeId metadata within TypeId 2025-07-21 14:23:20 +00:00
Ralf Jung
3f9be406a6 fix handling of base address for TypeId allocations 2025-07-20 22:14:27 +02:00
Ralf Jung
7672e4ed85 interpret: fix TypeId pointers being considered data pointers 2025-07-19 10:29:13 +02:00
Matthias Krüger
a59efc61f9
Rollup merge of #144123 - oli-obk:ctfe-unsize, r=RalfJung
Generalize `unsize` and `unsize_into` destinations

Just something that I noticed during other work. We do this for most such functions, so let's do it here, too.

r? ``@RalfJung``
2025-07-18 19:14:47 +02:00
Matthias Krüger
b3827e4f37
Rollup merge of #142673 - oli-obk:uninit-read-mem, r=RalfJung
Show the offset, length and memory of uninit read errors

r? ``@RalfJung``

I want to improve memory dumps in general. Not sure yet how to do so best within rust diagnostics, but in a perfect world I could generate a dummy in-memory file (that contains the rendered memory dump) that we then can then provide regular rustc `Span`s to. So we'd basically report normal diagnostics for them with squiggly lines and everything.
2025-07-18 19:14:43 +02:00
Oli Scherer
c65d3ce365 Generalize unsize and unsize_into destinations 2025-07-18 13:09:34 +00:00
Oli Scherer
652ba279ec Show the memory of uninit reads 2025-07-18 07:47:08 +00:00
Matthias Krüger
accf61dd42
Rollup merge of #143293 - folkertdev:naked-function-kcfi, r=compiler-errors
fix `-Zsanitizer=kcfi` on `#[naked]` functions

fixes https://github.com/rust-lang/rust/issues/143266

With `-Zsanitizer=kcfi`, indirect calls happen via generated intermediate shim that forwards the call. The generated shim preserves the attributes of the original, including `#[unsafe(naked)]`. The shim is not a naked function though, and violates its invariants (like having a body that consists of a single `naked_asm!` call).

My fix here is to match on the `InstanceKind`, and only use `codegen_naked_asm` when the instance is not a `ReifyShim`. That does beg the question whether there are other `InstanceKind`s that could come up. As far as I can tell the answer is no: calling via `dyn` seems to work find, and `#[track_caller]` is disallowed in combination with `#[naked]`.

r? codegen
````@rustbot```` label +A-naked
cc ````@maurer```` ````@rcvalle````
2025-07-18 04:27:51 +02:00
Oli Scherer
12eedafc38 Report the range of uninit bytes in CTFE errors 2025-07-17 20:00:51 +00:00
bors
9cd918bcbb Auto merge of #143879 - fee1-dead-contrib:push-lrlpoouyqqry, r=fmease
parse `const trait Trait`

r? oli-obk or anyone from project-const-traits

cc `@rust-lang/project-const-traits`
2025-07-17 15:54:33 +00:00
Deadbeef
69326878ee parse const trait Trait 2025-07-17 18:06:26 +08:00
Matthias Krüger
e7efa04450
Rollup merge of #143975 - RalfJung:type-id-eq, r=oli-obk
type_id_eq: check that the hash fully matches the type

The previous logic wouldn't always detect when the hash mismatches the provenance. Fix that by adding a new helper, `read_type_id`, that reads a single type ID while fully checking it for validity and consistency.

r? ``@oli-obk``
2025-07-17 10:41:48 +02:00
Matthias Krüger
4bf5787e2b
Rollup merge of #143955 - Stypox:tracing-frame-filter, r=RalfJung
Make frame spans appear on a separate trace line

This PR changes tracing_chrome's `tracing::Layer` so that if a span has the "tracing_separate_line" field as one of the span arguments, that span is put on a separate trace line. See https://github.com/rust-lang/miri/pull/4451 for an earlier attempt and for screenshots explaining better what I mean by "separate trace line".

This PR also makes the "frame" span use this feature (so it appears on a separate trace line, see https://github.com/rust-lang/miri/pull/4451 for motivation), but passes `tracing::field::Empty` as the span parameter value so it is ignored by other tracing layers (e.g. the logger):

```rust
info_span!("frame", tracing_separate_line = Empty, "{}", instance);
```

<details><summary>Also see the following discussion I had with ``@RalfJung</summary>``

> Is there no way to attach metadata we could use instead?

[These](https://docs.rs/tracing-core/0.1.34/src/tracing_core/metadata.rs.html#57) are the **static** metadata items we can control about a span. We can't add more metadata outside of them. The most relevant are:
- `name` (for the frame span it's currently "frame")
- `target` which acts as the category (for the frame span it's currently "rustc_const_eval::interpret::stack" by default)
- `fields` which contains a list of the *names* of each of the arguments passed to the `span!` macro (for the frame span it's currently ["message"], where "message" is the default identifier for data passed in the `format!` syntax)

When the tracing code is called at runtime, the **dynamic** values of the arguments are collected into a [`ValueSet`](https://docs.rs/tracing-core/0.1.34/src/tracing_core/field.rs.html#166). Each argument value stored there corresponds with one of the static names stored in `fields` (see above).

---

We have already determined that filtering out spans by `name` is not a good idea, and I would say the same goes for `target`. Both the `name` and the `target` fields are printed to stderr when `MIRI_LOG=` is enabled, so changing them to contain an identifier (e.g. "frame:tracing_separate_root" instead of "frame" as the name) would uselessly clutter the text logs (unless we add one more filter [there](https://github.com/rust-lang/rust/blob/master/compiler/rustc_log/src/lib.rs#L137), but then it gets even more complicated).

```rust
// examples of how the above (problematic) solutions would look like
info_span!("frame:tracing_separate_root", "{}", instance);
info_span!(target: "tracing_separate_root", "frame", "{}", instance);
```

---

So that leaves us with `fields` and their runtime values. Now, my initial thought (inspired by [this comment](https://github.com/rust-lang/miri/pull/4451#issuecomment-3068072303)) was to use a field with the static name "tracing_separate_root" and with a dynamic boolean value of "true". In `tracing_chrome.rs` we can easily check if this field is true and act accordingly. This would work but then again this field would also be picked up by the logger when `MIRI_LOG=` is enabled, and would uselessly clutter the text logs.

```rust
// example of how the above (problematic) solution would look like
info_span!("frame", tracing_separate_root = true, "{}", instance);
```

---

To avoid cluttering the text logs, we can instead set "tracing_separate_root" to the dynamic value of `tracing::field::Empty`. Citing from [here](https://docs.rs/tracing/0.1.41/tracing/field/struct.Empty.html), "when a field’s value is `Empty`, it will not be recorded".  "not being recorded" means that the field and its value won't be printed to stderr text logs, nor will it be printed by any other tracing layers that might be attached in the future. In `tracing_chrome.rs` we would still be able to check if "tracing_separate_root" is in the list of static `fields`, and act accordingly. So I believe this solution would effectively allow us to attach metadata to a span in a way that does not clutter logs and still allows being read in `tracing_chrome.rs`.

If we ever wanted to pass arbitrary metadata (i.e. not just a present/not present flag), it would be possible with a custom `Empty` that also holds data and implement `Value` without doing anything ([like `Empty` does](https://docs.rs/tracing-core/0.1.34/src/tracing_core/field.rs.html#775)).

```rust
// example of how the above solution would look like
info_span!("frame", tracing_separate_root = tracing::field::Empty, "{}", instance);
```
</details>
2025-07-17 10:41:48 +02:00
León Orell Valerian Liehr
83288db888
Rollup merge of #143595 - fee1-dead-contrib:push-sylpykzkmynr, r=RalfJung,fee1-dead
add `const_make_global`; err for `const_allocate` ptrs if didn't call

Implements as discussed on Zulip: [#t-compiler/const-eval > const heap](https://rust-lang.zulipchat.com/#narrow/channel/146212-t-compiler.2Fconst-eval/topic/const.20heap/with/527125421)

r? ```@rust-lang/wg-const-eval```

Fixes https://github.com/rust-lang/rust/issues/129233
2025-07-17 03:58:30 +02:00
Folkert de Vries
9c8ab89187
use codegen_instance_attrs where an instance is (easily) available 2025-07-16 23:24:32 +02:00
Samuel Tardieu
2a8f97910a
Rollup merge of #143968 - Stypox:tracing-FnAbiOf, r=RalfJung
Add tracing to `InterpCx::fn_abi_of_instance/fn_abi_of_fn_ptr`

This PR adds tracing to the `InterpCx::fn_abi_of_instance`/`::fn_abi_of_fn_ptr` functions by shadowing `FnAbiOf`'s trait methods with inherent methods on `InterpCx`, like done in rust-lang/rust#142721. The reason why I am targeting these two functions is because they are used for Miri interpretation, and they make a `layout_of` query down the line without passing through the `layout_of` that was traced in rust-lang/rust#142721.

There are other places where `layout_of` is called without being traced (see the analysis below), but that's because the `Machine` used there is not `MiriMachine` but rather `CompileTimeMachine` which does not implement `enter_trace_span()`. But after discussing with ```````@RalfJung``````` we agreed that the const-eval part should not be traced together with Miri, that's why I am ignoring the other places where `layout_of` is called.

r? ```````@RalfJung```````

<details><summary>Analysis of the places where <code>layout_of</code> is called</summary>

I did some analysis for https://github.com/rust-lang/rust/pull/142721#discussion_r2171494841, and these are all the places where the query `tcx.layout_of` is called (directly or indirectly) outside of a traced `InterpCx::layout_of` while a program is being interpreted by Miri:

```
adjust_for_rust_scalar  at ./compiler/rustc_ty_utils/src/abi.rs:302:35
{closure#2}             at ./compiler/rustc_ty_utils/src/abi.rs:522:25
eval_body_using_ecx<>   at ./compiler/rustc_const_eval/src/const_eval/eval_queries.rs:49:22
{closure#1}<>           at ./compiler/rustc_const_eval/src/interpret/operand.rs:851:76
{closure#0}<>           at ./compiler/rustc_const_eval/src/interpret/stack.rs:612:18
size_and_align          at ./compiler/rustc_middle/src/mir/interpret/mod.rs:387:38
```

I got these by:
- patching rustc with this patch that adds a span to the `layout_of` query which prints the backtrace:
[layout_of_other_places.diff.txt](https://github.com/user-attachments/files/21235523/layout_of_other_places.diff.txt)
- adding this to my bootstrap.toml to have debug symbols inside the Miri binary: `rust.debuginfo-level = "line-tables-only"` and also `build.tool.miri.features = ["tracing"]`
- obtaining a trace file with `MIRI_TRACING=1 ./x.py run miri --stage 1 --warnings warn --args src/tools/miri/tests/pass/hello.rs` (note: maybe using a file different than "src/tools/miri/tests/pass/hello.rs" would lead to more places where layout_of is called?)
-  running this query in Perfetto to select all `layout_of` spans that have as a direct parent a span named "frame" (as opposed to the parent being `InterpCx::layout_of`) and extract their backtrace: `select args.string_value from slice left join args on slice.arg_set_id = args.id where slice.name = "tcx.layout_of" and slice.parent_id in (select slice2.id from slice as slice2 where slice2.name = "frame") group by args.string_value`
- exporting the data as `.tsv` and processing that file through this Python script. It finds the first path in the backtraces where "layout" isn't mentioned, which imo is a good heuristic to not consider `layout_of` wrappers/friends as call places, but rather go down the backtrace until an actual call place is reached. [layout_of_other_places.py.txt](https://github.com/user-attachments/files/21235529/layout_of_other_places.py.txt)

</details>
2025-07-16 17:06:43 +02:00
Samuel Tardieu
7b5e08f8ef
Rollup merge of #143692 - RalfJung:miri-oob, r=oli-obk
miri: fix out-of-bounds error for ptrs with negative offsets

r? ```````@oli-obk```````
2025-07-16 17:06:39 +02:00
Ralf Jung
0bf0860a0e simplfy memory kind handling during interning 2025-07-16 15:29:52 +02:00
Ralf Jung
8f854d9cb2 const heap: fix ICE on forgotten make_global 2025-07-16 15:29:52 +02:00
Stypox
07a34c374b
Make frame spans appear on a separate trace line
This was done by making the tracing_chrome tracing layer check if "tracing_separate_line" was in the arguments of a span, and act accordingly.
2025-07-16 09:25:35 +02:00
Ralf Jung
42ec028027 type_id_eq: check that the hash fully matches the type 2025-07-16 08:02:47 +02:00
Deadbeef
fd48b7b8dd Comment more code and make tests clearer
Co-Authored-By: Ralf Jung <post@ralfj.de>
2025-07-16 00:50:20 +08:00
Deadbeef
44b38ca45e format pointer later instead of eagerly converting to string 2025-07-16 00:32:12 +08:00