Commit Graph

233 Commits

Author SHA1 Message Date
Lucas Baumann bc883e24b8 replace SanitizerSet in CodegenFnAttrs by new type 2025-11-06 13:19:08 +01:00
Marijn Schouten 9e4ec2acc7 clippy fixes and code simplification 2025-11-02 08:16:38 +00:00
Matthias Krüger a5d38ede1d Rollup merge of #145724 - folkertdev:track-caller-drop-no-mangle, r=fee1-dead
the `#[track_caller]` shim should not inherit `#[no_mangle]`

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

builds on https://github.com/rust-lang/rust/pull/143293 which introduced a mechanism to strip attributes from shims.

cc `@Jules-Bertholet` `@workingjubilee` `@bjorn3`

---

Summary:

This PR fixes an interaction between `#[track_caller]`, `#[no_mangle]`, and casting to a function pointer.

A function annotated with `#[track_caller]` internally has a hidden extra argument for the panic location. The `#[track_caller]` attribute is only allowed on `extern "Rust"` functions. When a function is annotated with both `#[no_mangle]` and `#[track_caller]`, the exported symbol has the signature that includes the extra panic location argument. This works on stable rust today:

```rust
extern "Rust" {
    #[track_caller]
    fn rust_track_caller_ffi_test_tracked() -> &'static Location<'static>;
}

mod provides {
    use std::panic::Location;
    #[track_caller] // UB if we did not have this!
    #[no_mangle]
    fn rust_track_caller_ffi_test_tracked() -> &'static Location<'static> {
        Location::caller()
    }
}
```

When a `#[track_caller]` function is converted to a function pointer, a shim is added to drop the additional argument. So this is a valid program:

```rust
#[track_caller]
fn foo() {}

fn main() {
    let f = foo as fn();
    f();
}
```

The issue arises when `foo` is additionally annotated with `#[no_mangle]`, the generated shim currently inherits this attribute, also exporting a symbol named `foo`, but one without the hidden panic location argument. The linker rightfully complains about a duplicate symbol.

The solution of this PR is to have the generated shim drop the `#[no_mangle]` attribute.
2025-10-18 08:08:36 +02:00
beepster4096 033474bacf remove some deref_finder uses
elaborate drops and inline don't seem to actually need it
2025-10-10 20:30:19 -07:00
beepster4096 a0e9cb7cb4 erase coroutine shim dereftemps 2025-10-06 10:57:27 -07:00
Folkert de Vries 7d99979077 use codegen_instance_attrs in some additional places 2025-10-02 20:53:13 +02:00
dianqk 85b2f70693 mir-opt: Eliminate trivial unnecessary storage annotations 2025-10-02 14:55:51 +08:00
Jana Dönszelmann e1d3ad89c7 remove rustc_attr_data_structures 2025-07-31 14:19:27 +02:00
bors 837c5dd7de Auto merge of #142890 - kornelski:unused-var-debug, r=saethlin
MIR inliner maintains unused var_debug_info

Only `full` debuginfo level promises variable-level debug information, but the MIR inline pass needlessly preserved the local variable debug info for the `limited` level too.
2025-07-03 23:17:03 +00:00
dianqk 9f9cd5e283 mir: Add a new method to statement
Avoid introducing a large number of changes when adding optional initialization fields.
2025-06-29 20:13:36 +08:00
Kornel c9ef11695f Keep inlined var_debug_info only when full debug info is used 2025-06-28 12:22:49 +01:00
Camille GILLOT 2074013f44 Only store the LocalDefId instead of the whole instance. 2025-06-23 08:44:31 +00:00
Camille GILLOT 09aab29ebf Only compute recursive callees once. 2025-06-22 20:08:49 +00:00
mejrs 684b7b70f4 don't depend on rustc_attr_parsing if rustc_data_structures will do 2025-05-09 23:16:55 +02:00
Andrew Zhogin c366756a85 AsyncDrop implementation using shim codegen of async_drop_in_place::{closure}, scoped async drop added. 2025-04-28 16:23:13 +07:00
Chris Denton 5d2375f789 Rollup merge of #139042 - compiler-errors:do-not-optimize-switchint, r=saethlin
Do not remove trivial `SwitchInt` in analysis MIR

This PR ensures that we don't prematurely remove trivial `SwitchInt` terminators which affects both the borrow-checking and runtime semantics (i.e. UB) of the code. Previously the `SimplifyCfg` optimization was removing `SwitchInt` terminators when they was "trivial", i.e. when all arms branched to the same basic block, even if that `SwitchInt` terminator had the side-effect of reading an operand which (for example) may not be initialized or may point to an invalid place in memory.

This behavior is unlike all other optimizations, which are only applied after "analysis" (i.e. borrow-checking) is finished, and which Miri disables to make sure the compiler doesn't silently remove UB.

Fixing this code "breaks" (i.e. unmasks) code that used to borrow-check but no longer does, like:

```rust
fn foo() {
    let x;
    let (0 | _) = x;
}
```

This match expression should perform a read because `_` does not shadow the `0` literal pattern, and the compiler should have to read the match scrutinee to compare it to 0. I've checked that this behavior does not actually manifest in practice via a crater run which came back clean: https://github.com/rust-lang/rust/pull/139042#issuecomment-2767436367

As a side-note, it may be tempting to suggest that this is actually a good thing or that we should preserve this behavior. If we wanted to make this work (i.e. trivially optimize out reads from matches that are redundant like `0 | _`), then we should be enabling this behavior *after* fixing this. However, I think it's kinda unprincipled, and for example other variations of the code don't even work today, e.g.:

```rust
fn foo() {
    let x;
    let (0.. | _) = x;
}
```
2025-04-19 19:30:46 +00:00
Yotam Ofek 4b63362f3d Use newtype_index!-generated types more idiomatically 2025-04-14 16:17:06 +00:00
Michael Goulet 3ee62a906e Do not optimize out SwitchInt before borrowck, or if Zmir-preserve-ub 2025-04-08 21:05:20 +00:00
Scott McMurray 91af4aa2e2 Allow more top-down inlining for single-BB callees
This means that things like `<usize as Step>::forward_unchecked` and `<PartialOrd for f32>::le` will inline even if we've already done a bunch of inlining to find the calls to them.
2025-03-12 22:39:43 -07:00
Michael Goulet d33946c3ab Inline FnOnce once again 2025-03-03 23:30:18 +00:00
Michael Goulet e081b7b77e Better reasons for inline failure 2025-03-03 23:22:37 +00:00
Nicholas Nethercote fd7b4bf4e1 Move methods from Map to TyCtxt, part 2.
Continuing the work started in #136466.

Every method gains a `hir_` prefix, though for the ones that already
have a `par_` or `try_par_` prefix I added the `hir_` after that.
2025-02-18 10:17:44 +11:00
clubby789 2966256133 Make -O mean -C opt-level=3 2025-02-13 19:47:55 +00:00
Matthias Krüger d64bd3bedd Rollup merge of #136722 - kornelski:visit-spans, r=chenyukang
Visit all debug info in MIR Visitor

I've been experimenting with simplifying debug info in MIR inliner, and discovered that MIR Visitor doesn't reliably visit all spans. This PR adds the missing visitor calls.
2025-02-09 19:44:52 +01:00
bjorn3 1fcae03369 Rustfmt 2025-02-08 22:12:13 +00:00
Kornel 699567058f Visit SourceInfo of all Terminators 2025-02-08 00:23:06 +00:00
clubby789 2c35bd0499 #[optimize(none)] implies #[inline(never)] 2025-01-31 17:51:49 +00:00
clubby789 7a9661d768 Disable non-required MIR opts with optimize(none)
Co-authored-by: Waffle Lapkin <waffle.lapkin@gmail.com>
2025-01-23 17:40:41 +00:00
Michael Goulet b08f3d5bdb Consolidate ad-hoc MIR lints into real pass-manager-based MIR lints 2025-01-18 21:25:47 +00:00
Rémy Rakic a13354bea0 rename BitSet to DenseBitSet
This should make it clearer that this bitset is dense, with the
advantages and disadvantages that it entails.
2025-01-11 11:34:01 +00:00
David Wood cc9a9ecccb mir_build: check annotated functions w/out callers 2025-01-10 18:37:57 +00:00
David Wood dbec6bedf4 inline: move should inline check 2025-01-10 18:37:56 +00:00
David Wood 90066c0df3 inline: remove unnecessary promoted check 2025-01-10 18:37:55 +00:00
David Wood e4bae91be1 inline: re-introduce some callee body checks 2025-01-10 18:37:55 +00:00
David Wood 450793923e inline: force inlining shims 2025-01-10 18:37:55 +00:00
David Wood f86169a58f mir_transform: implement forced inlining
Adds `#[rustc_force_inline]` which is similar to always inlining but
reports an error if the inlining was not possible, and which always
attempts to inline annotated items, regardless of optimisation levels.
It can only be applied to free functions to guarantee that the MIR
inliner will be able to resolve calls.
2025-01-10 18:37:54 +00:00
Ralf Jung 3cd3649c6c rustc_intrinsic: support functions without body; they are implicitly marked as must-be-overridden 2025-01-04 11:41:51 +01:00
DianQK 93aea1d0fe mir: require is_cleanup when creating BasicBlockData 2024-12-18 20:43:54 +08:00
Jonathan Dönszelmann efb98b6552 rename rustc_attr to rustc_attr_parsing and create rustc_attr_data_structures 2024-12-16 19:08:19 +01:00
lcnr 8b90e70e06 mir validator: don't store mir phase 2024-12-02 13:38:18 +01:00
Camille GILLOT 7fa021ad86 Remove -Zfuel. 2024-11-26 10:45:21 +00:00
lcnr 948cec0fad move fn is_item_raw to TypingEnv 2024-11-19 18:06:20 +01:00
lcnr 9cba14b95b use TypingEnv when no infcx is available
the behavior of the type system not only depends on the current
assumptions, but also the currentnphase of the compiler. This is
mostly necessary as we need to decide whether and how to reveal
opaque types. We track this via the `TypingMode`.
2024-11-18 10:38:56 +01:00
Jubilee Young 843b6e0859 compiler: Directly use rustc_abi in mir_transform 2024-11-03 13:38:47 -08:00
lcnr 2cde638ac0 stop using ParamEnv::reveal while handling MIR 2024-10-31 14:55:53 +01:00
Deadbeef f6fea83342 Effects cleanup
- removed extra bits from predicates queries that are no longer needed in the new system
- removed the need for `non_erasable_generics` to take in tcx and DefId, removed unused arguments in callers
2024-10-26 10:19:07 +08:00
Michael Goulet c682aa162b Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
Michael Goulet 8f97231d34 Remove semi-nondeterminism of DefPathHash ordering from inliner 2024-09-16 21:41:15 -04:00
Nicholas Nethercote 8235af07d2 Improve comment formatting.
By reflowing comment lines that are too long, and a few that are very
short. Plus some other very minor formatting tweaks.
2024-09-10 08:42:30 +10:00
Nicholas Nethercote 7adde3f074 Make CallSite non-Copy.
It doesn't need to be, and it's 72 bytes on 64-bit platforms, which is
fairly large.
2024-09-09 15:15:43 +10:00