rust/src/tools
Matthias Krüger 9aaebd481a
Rollup merge of #129392 - compiler-errors:raw-ref-op-doesnt-diverge-but-more, r=lcnr
Do not consider match/let/ref of place that evaluates to `!` to diverge, disallow coercions from them too

Fixes #117288.

This PR implements a heuristic which disables two things that are currently being performed on the HIR when we have **expressions that involve place-like expressions that point to `!`**. Specifically, it will (in certain cases explained below):

### (1.) Disable the `NeverToAny` coercion we implicitly insert for `!`.

Which fixes this inadvertent, sneaky unsoundness:

```
unsafe {
    let x: *const ! = &0 as *const u8 as *const !;
    let _: () = *x;
}
```

which is UB because currently rust emits an *implicit* NeverToAny coercion even though we really shouldn't be, since there's no read of the value pointed by `x`.

### (2.) Disable the logic which considers expression which evaluate to `!` to diverge, which affects the type returned by the containing block.

Which fixes this unsoundness:

```
fn make_up_a_value<T>() -> T {
    unsafe {
        let x: *const ! = &0 as *const u8 as *const !;
        let _ = *x;
    }
}
```

We disable these two operations **if** the expression is a place-like expression (locals, statics, field projections, index operations, and deref operations), and if the parent expression is either:
(1.) the LHS of an assignment
(2.) AddrOf
(3.) A match or let **unless** all of the *patterns consitute a read*, which is explained below:

And finally, a pattern currently is considered to constitute a read **unless** it is a wildcard, or an OR pattern. An OR pattern is considered to constitute a read if all of its subpatterns constitute a read, to remain as conservative as possible in cases like `_ | subpat` or `subpat | _`.

All other patterns are considered currently to constitute a read. Specifically, because `NeverToAny` is a coercion performed on a *value* and not a *place*, `Struct { .. }` on a `!` type must be a coercion currently, and we currently rely on this behavior to allow us to perform coercions like `let _: i32 = x;` where `x: !`.

This is already considered UB by [miri](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=daf3a2246433fe43fdc07d1389c276c9), but also means it does not affect the preexisting UB in this case:

```
let Struct { .. } = *never_ptr;
```

Even though it's likely up for debate since we're not actually reading any data out of the struct, it almost certainly causes inference changes which I do *NOT* want to fix in this PR.
2024-10-06 11:06:57 +02:00
..
build-manifest Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
build_helper create CiEnv::is_rust_lang_managed_ci_job 2024-09-29 07:19:23 +03:00
bump-stage0 Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
cargo@ad074abe3a Update cargo 2024-10-04 16:37:09 -04:00
cargotest Reformat use declarations. 2024-07-29 08:26:52 +10:00
clippy Auto merge of #130540 - veera-sivarajan:fix-87525, r=estebank 2024-10-06 02:39:23 +00:00
collect-license-metadata Update generate-copyright 2024-08-06 11:04:55 +01:00
compiletest add has_enzyme/needs-enzyme to the test infra 2024-09-29 18:27:33 -04:00
coverage-dump Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
enzyme@2fe5164a24 Enzyme backend 2024-09-05 22:47:23 -04:00
error_index_generator Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
generate-copyright Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
generate-windows-sys Add windows-targets crate to std's sysroot 2024-08-09 10:43:43 +00:00
html-checker rustdoc: redesign toolbar and disclosure widgets 2024-09-10 17:56:05 -07:00
jsondocck Reformat use declarations. 2024-07-29 08:26:52 +10:00
jsondoclint Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
libcxx-version
linkchecker update the doc comment on lintchecker b/c it parses html now 2024-08-24 12:35:35 -04:00
lint-docs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
lld-wrapper Reformat use declarations. 2024-07-29 08:26:52 +10:00
llvm-bitcode-linker Reformat use declarations. 2024-07-29 08:26:52 +10:00
miri Fix up tests 2024-10-05 18:36:47 -04:00
miropt-test-tools
opt-dist Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
remote-test-client Reformat use declarations. 2024-07-29 08:26:52 +10:00
remote-test-server Reformat use declarations. 2024-07-29 08:26:52 +10:00
replace-version-placeholder Reformat use declarations. 2024-07-29 08:26:52 +10:00
rls Reformat use declarations. 2024-07-29 08:26:52 +10:00
run-make-support Fix run-make-support to respect per-stage cargo 2024-09-24 19:04:51 +08:00
rust-analyzer Add missing rustc_private 2024-09-25 10:56:37 +03:00
rust-installer Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustbook cargo update 2024-09-29 00:22:29 +00:00
rustc-perf@d5055e7804 bump up rustc-perf's version 2024-08-09 01:24:39 +09:00
rustc-perf-wrapper impl compare command and benchmark command to rustc-perf-wrapper 2024-08-09 20:03:14 +09:00
rustdoc Link std statically in rustc_driver 2024-08-11 04:16:53 +02:00
rustdoc-gui Enable --no-sandbox option by default for rustdoc GUI tests 2024-10-04 12:11:23 +02:00
rustdoc-gui-test Reformat use declarations. 2024-07-29 08:26:52 +10:00
rustdoc-js Separate core search logic with search ui 2024-08-29 00:26:16 +08:00
rustdoc-themes Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustfmt Merge commit 'b23b69900e' into sync-from-rustfmt 2024-09-19 21:46:44 -04:00
suggest-tests Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
tidy add unstable support for outputting file checksums for use in cargo 2024-10-01 21:23:20 -06:00
tier-check Add NuttX based targets for RISC-V and ARM 2024-07-19 22:00:42 +08:00
unicode-table-generator Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
unstable-book-gen Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
wasm-component-ld Update wasm-component-ld to 0.5.9 2024-09-30 16:56:40 -07:00
x Reformat use declarations. 2024-07-29 08:26:52 +10:00
cherry-pick.sh
publish_toolstate.py