Commit Graph

266574 Commits

Author SHA1 Message Date
Matthias Krüger
729aa49d0f
Rollup merge of #130381 - workingjubilee:sometimes-code-really-is-self-descriptive, r=Noratrieb
library: Compute Rust exception class from its string repr

Noticed this awkwardness while scanning through the code. I think we can do better than that.
2024-09-15 11:55:47 +02:00
Matthias Krüger
9ed667f8ed
Rollup merge of #130371 - saethlin:transmutability-enum-ice, r=compiler-errors
Correctly account for niche-optimized tags in rustc_transmute

This is a bit hacky, but it fixes the ICE and makes it possible to run the safe transmute check on every `mem::transmute` check we instantiate. I want to write a lint that needs to do that, but this stands well on its own.

cc `@jswrenn` here's the fix I alluded to yesterday :)

Fixes #123693
2024-09-15 11:55:47 +02:00
Matthias Krüger
3225bd5f96
Rollup merge of #130295 - chrisnc:armv8r-feature-fix, r=workingjubilee
Fix target-cpu fpu features on Armv8-R.

This is a follow-up to #123159, but applied to Armv8-R.

This required https://github.com/llvm/llvm-project/pull/88287 to work properly. Now that this change exists in rustc's llvm, we can fix Armv8-R's default fpu features. In Armv8-R's case, the default features from LLVM for floating-point are sufficient, because there is no integer-only variant of this architecture.
2024-09-15 11:55:46 +02:00
Matthias Krüger
e267534b07
Rollup merge of #130118 - RalfJung:unwrap_unchecked, r=Noratrieb
move Option::unwrap_unchecked into const_option feature gate

That's where `unwrap` and `expect` are so IMO it makes more sense to group them together.

Part of #91930, #67441
2024-09-15 11:55:46 +02:00
Matthias Krüger
011289c9d4
Rollup merge of #129195 - RalfJung:const-mut-refs, r=fee1-dead
Stabilize `&mut` (and `*mut`) as well as `&Cell` (and `*const Cell`) in const

This stabilizes `const_mut_refs` and `const_refs_to_cell`. That allows a bunch of new things in const contexts:
- Mentioning `&mut` types
- Creating `&mut` and `*mut` values
- Creating `&T` and `*const T` values where `T` contains interior mutability
- Dereferencing `&mut` and `*mut` values (both for reads and writes)

The same rules as at runtime apply: mutating immutable data is UB. This includes mutation through pointers derived from shared references; the following is diagnosed with a hard error:
```rust
#[allow(invalid_reference_casting)]
const _: () = {
    let mut val = 15;
    let ptr = &val as *const i32 as *mut i32;
    unsafe { *ptr = 16; }
};
```

The main limitation that is enforced is that the final value of a const (or non-`mut` static) may not contain `&mut` values nor interior mutable `&` values. This is necessary because the memory those references point to becomes *read-only* when the constant is done computing, so (interior) mutable references to such memory would be pretty dangerous. We take a multi-layered approach here to ensuring no mutable references escape the initializer expression:
- A static analysis rejects (interior) mutable references when the referee looks like it may outlive the current MIR body.
- To be extra sure, this static check is complemented by a "safety net" of dynamic checks. ("Dynamic" in the sense of "running during/after const-evaluation, e.g. at runtime of this code" -- in contrast to "static" which works entirely by looking at the MIR without evaluating it.)
  - After the final value is computed, we do a type-driven traversal of the entire value, and if we find any `&mut` or interior-mutable `&` we error out.
  - However, the type-driven traversal cannot traverse `union` or raw pointers, so there is a second dynamic check where if the final value of the const contains any pointer that was not derived from a shared reference, we complain. This is currently a future-compat lint, but will become an ICE in #128543. On the off-chance that it's actually possible to trigger this lint on stable, I'd prefer if we could make it an ICE before stabilizing const_mut_refs, but it's not a hard blocker. This part of the "safety net" is only active for mutable references since with shared references, it has false positives.

Altogether this should prevent people from leaking (interior) mutable references out of the const initializer.

While updating the tests I learned that surprisingly, this code gets rejected:
```rust
const _: Vec<i32> = {
    let mut x = Vec::<i32>::new(); //~ ERROR destructor of `Vec<i32>` cannot be evaluated at compile-time
    let r = &mut x;
    let y = x;
    y
};
```
The analysis that rejects destructors in `const` is very conservative when it sees an `&mut` being created to `x`, and then considers `x` to be always live. See [here](https://github.com/rust-lang/rust/issues/65394#issuecomment-541499219) for a longer explanation. `const_precise_live_drops` will solve this, so I consider this problem to be tracked by https://github.com/rust-lang/rust/issues/73255.

Cc `@rust-lang/wg-const-eval` `@rust-lang/lang`
Cc https://github.com/rust-lang/rust/issues/57349
Cc https://github.com/rust-lang/rust/issues/80384
2024-09-15 11:55:45 +02:00
onur-ozkan
13542cdb80 add change entry for dist.vendor
Signed-off-by: onur-ozkan <work@onurozkan.dev>
2024-09-15 12:24:07 +03:00
onur-ozkan
9778f25ea5 document dist.vendor in config.example.toml
Signed-off-by: onur-ozkan <work@onurozkan.dev>
2024-09-15 12:23:05 +03:00
onur-ozkan
741b3164d8 make dist vendoring configurable
Signed-off-by: onur-ozkan <work@onurozkan.dev>
2024-09-15 12:23:02 +03:00
bors
1ae268816c Auto merge of #130148 - RalfJung:interpret-get_ptr_alloc_mut, r=saethlin
interpret: get_ptr_alloc_mut: lookup allocation only once

I don't think this will make a big perf difference, but it makes this function symmetric with `get_ptr_alloc` -- and it's always nice to successfully solve a borrow checker puzzle like this. ;)
2024-09-15 08:59:26 +00:00
Ralf Jung
efdc01bfee unix/sync: cleanup 2024-09-15 10:55:03 +02:00
Ralf Jung
f16f09db63 interpret: get_ptr_alloc_mut: lookup allocation only once 2024-09-15 10:21:19 +02:00
Ralf Jung
49316f871c also stabilize const_refs_to_cell 2024-09-15 10:20:47 +02:00
Ralf Jung
544a6a7df3 const_refs_to_cell: dont let mutable references sneak past the interior mutability check 2024-09-15 09:51:34 +02:00
Ralf Jung
9ad5728593 clean up const checking of mutable references 2024-09-15 09:51:34 +02:00
Ralf Jung
3175cc2814 stabilize const_mut_refs 2024-09-15 09:51:32 +02:00
Ralf Jung
268f6cf558 also use compute_size_in_bytes for relevant multiplications in Miri 2024-09-15 09:21:43 +02:00
Jubilee Young
7b02be8abc compiler: Document AbiAndPrefAlign 2024-09-14 22:45:25 -07:00
bors
bc486f31a6 Auto merge of #130379 - Zalathar:rollup-wpmcnql, r=Zalathar
Rollup of 6 pull requests

Successful merges:

 - #130042 (properly handle EOF in BufReader::peek)
 - #130061 (Add `NonNull` convenience methods to `Box` and `Vec`)
 - #130202 (set `download-ci-llvm = true` by default on "library" and "tools" profiles)
 - #130214 (MaybeUninit::zeroed: mention that padding is not zeroed)
 - #130353 (Make some lint doctests compatible with `--stage=0`)
 - #130370 (unstable-book: `trait_upcasting` example should not have `#![allow(incomplete_features)]`)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-09-15 05:15:25 +00:00
Chris Copeland
1a0ba01177
Fix target-cpu fpu features on Armv8-R.
This is a follow-up to #123159, but applied to Armv8-R.

This required https://github.com/llvm/llvm-project/pull/88287 to work
properly. Now that this change exists in rustc's llvm, we can fix
Armv8-R's default fpu features. In Armv8-R's case, the default features
from LLVM for floating-point are sufficient, because there is no
integer-only variant of this architecture.
2024-09-14 21:39:23 -07:00
Jubilee Young
fef7373d13 library: Compute Rust exception class from its string repr 2024-09-14 20:26:37 -07:00
Zalathar
2a3e17c6d5 coverage: Remove unnecessary bcb_successors
Given that we directly access the graph predecessors/successors in so many
other places, and sometimes must do so to satisfy the borrow checker, there is
little value in having this trivial helper method.
2024-09-15 12:51:57 +10:00
Zalathar
669327f575 coverage: Replace bcb_has_multiple_in_edges with sole_predecessor
This does a better job of expressing the special cases that occur when a node
in the coverage graph has exactly one in-edge.
2024-09-15 12:51:57 +10:00
Zalathar
e24310b07c coverage: Simplify choosing an out-edge to be given a counter expression
By building the list of candidate edges up-front, and making the
candidate-selection method fallible, we can remove a few pieces of awkward
code.
2024-09-15 12:51:57 +10:00
Zalathar
771659d264 coverage: Track whether a node's count is the sum of its out-edges 2024-09-15 12:51:57 +10:00
Zalathar
6251d16e4c coverage: Streamline creation of physical edge counters 2024-09-15 12:51:57 +10:00
Zalathar
7cb85862b9 coverage: Streamline creation of physical node counters
- Look up the node's predecessors only once
- Get rid of some overly verbose logging
- Explain why some nodes need physical counters
- Extract a helper method to create and set a physical node counter
2024-09-15 12:51:56 +10:00
Zalathar
13b814fc17 coverage: Tweak comments in graph 2024-09-15 12:25:12 +10:00
Stuart Cook
4b6f838e56
Rollup merge of #130370 - kpreid:patch-2, r=compiler-errors
unstable-book: `trait_upcasting` example should not have `#![allow(incomplete_features)]`

Tracking issue: #65991

`trait_upcasting` is not currently an incomplete feature; therefore examples of its use do not require `#![allow(incomplete_features)]`.
2024-09-15 12:14:58 +10:00
Stuart Cook
12fb8e45c2
Rollup merge of #130353 - Zalathar:lint-zero, r=jieyouxu
Make some lint doctests compatible with `--stage=0`

Currently, running `x test compiler --stage=0` (with `rust.parallel-compiler=false` to avoid other problems) results in two failures, because these lint doctests aren't compatible with the current stage0 compiler.

In theory, the more “correct” solution would be to wrap the opening triple-backtick line in  `#[cfg_attr(not(bootstrap), doc = "..."]`. However, that causes a few practical problems:
- `tidy` doesn't understand that syntax, and miscounts the number of backticks in the comment block.
- `lint-docs` doesn't understand that syntax, and thinks it's trying to declare the lint name.
- Working around the above problems would cause more work and more confusion for whoever does the next bootstrap beta bump.

So instead this PR adds some bootstrap gates inside the individual doctests, which end up producing the desired behaviour, and are straightforward to remove.
2024-09-15 12:14:57 +10:00
Stuart Cook
0648987532
Rollup merge of #130214 - RalfJung:zeroed, r=Mark-Simulacrum
MaybeUninit::zeroed: mention that padding is not zeroed

That should clarify cases like [this](https://github.com/rust-lang/rust/pull/129778#issuecomment-2342542847).
2024-09-15 12:14:56 +10:00
Stuart Cook
36ee8520e8
Rollup merge of #130202 - onur-ozkan:force-ci-llvm-on-default-profiles, r=Mark-Simulacrum
set `download-ci-llvm = true` by default on "library" and "tools" profiles

It's very rare for developers to need to modify LLVM, so "if-unchanged" isn't a good default for "tools" and "library" profiles since it fetches the LLVM submodule to track changes.
2024-09-15 12:14:56 +10:00
Stuart Cook
c11505f218
Rollup merge of #130061 - theemathas:box_vec_non_null, r=MarkSimulacrum,workingjubilee
Add `NonNull` convenience methods to `Box` and `Vec`

Implements the ACP: https://github.com/rust-lang/libs-team/issues/418.

The docs for the added methods are mostly copied from the existing methods that use raw pointers instead of `NonNull`.

I'm new to this "contributing to rustc" thing, so I'm sorry if I did something wrong. In particular, I don't know what the process is for creating a new unstable feature. Please advise me if I should do something. Thank you.
2024-09-15 12:14:55 +10:00
Stuart Cook
e02e6bf0e9
Rollup merge of #130042 - lolbinarycat:bufreaker_peek_eof, r=Amanieu
properly handle EOF in BufReader::peek

previously this would cause an infinite loop due to it being unable to read `n` bytes.
2024-09-15 12:14:55 +10:00
bors
4f1be92153 Auto merge of #129753 - folkertdev:stabilize-const-extern-fn, r=RalfJung
stabilize `const_extern_fn`

closes https://github.com/rust-lang/rust/issues/64926

tracking issue: https://github.com/rust-lang/rust/issues/64926
reference PR: https://github.com/rust-lang/reference/pull/1596

## Stabilizaton Report

### Summary

Using `const extern "Rust"` and `const extern "C"` was already stabilized (since version 1.62.0, see https://github.com/rust-lang/rust/pull/95346). This PR stabilizes the other calling conventions: it is now possible to write  `const unsafe extern "calling-convention" fn` and `const extern "calling-convention" fn` for any supported calling convention:

```rust
const extern "C-unwind" fn foo1(val: u8) -> u8 { val + 1}
const extern "stdcall" fn foo2(val: u8) -> u8 { val + 1}
const unsafe extern "C-unwind" fn bar1(val: bool) -> bool { !val }
const unsafe extern "stdcall" fn bar2(val: bool) -> bool { !val }
```

This can be used to const-ify an `extern fn`, or conversely, to make a `const fn` callable from external code.

r? T-lang

cc `@RalfJung`
2024-09-14 23:47:59 +00:00
5d8f15cb7a
Update to latest rustc 2024-09-14 18:13:10 -05:00
Ben Kimock
2ac554b73a Correctly account for niche-optimized tags 2024-09-14 17:52:03 -04:00
Kevin Reid
29ccc0d325
trait_upcasting is not currently an incomplete feature. 2024-09-14 14:46:29 -07:00
Ben Kimock
c547f51de1 Add a test 2024-09-14 17:30:07 -04:00
bors
9b72238eb8 Auto merge of #128543 - RalfJung:const-interior-mut, r=fee1-dead
const-eval interning: accept interior mutable pointers in final value

…but keep rejecting mutable references

This fixes https://github.com/rust-lang/rust/issues/121610 by no longer firing the lint when there is a pointer with interior mutability in the final value of the constant. On stable, such pointers can be created with code like:
```rust
pub enum JsValue {
    Undefined,
    Object(Cell<bool>),
}
impl Drop for JsValue {
    fn drop(&mut self) {}
}
// This does *not* get promoted since `JsValue` has a destructor.
// However, the outer scope rule applies, still giving this 'static lifetime.
const UNDEFINED: &JsValue = &JsValue::Undefined;
```
It's not great to accept such values since people *might* think that it is legal to mutate them with unsafe code. (This is related to how "infectious" `UnsafeCell` is, which is a [wide open question](https://github.com/rust-lang/unsafe-code-guidelines/issues/236).) However, we [explicitly document](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) that things created by `const` are immutable. Furthermore, we also accept the following even more questionable code without any lint today:
```rust
let x: &'static Option<Cell<i32>> = &None;
```
This is even more questionable since it does *not* involve a `const`, and yet still puts the data into immutable memory. We could view this as promotion [potentially introducing UB](https://github.com/rust-lang/unsafe-code-guidelines/issues/493). However, we've accepted this since ~forever and it's [too late to reject this now](https://github.com/rust-lang/rust/pull/122789); the pattern is just too useful.

So basically, if you think that `UnsafeCell` should be tracked fully precisely, then you should want the lint we currently emit to be removed, which this PR does. If you think `UnsafeCell` should "infect" surrounding `enum`s, the big problem is really https://github.com/rust-lang/unsafe-code-guidelines/issues/493 which does not trigger the lint -- the cases the lint triggers on are actually the "harmless" ones as there is an explicit surrounding `const` explaining why things end up being immutable.

What all this goes to show is that the hard error added in https://github.com/rust-lang/rust/pull/118324 (later turned into the future-compat lint that I am now suggesting we remove) was based on some wrong assumptions, at least insofar as it concerns shared references. Furthermore, that lint does not help at all for the most problematic case here where the potential UB is completely implicit. (In fact, the lint is actively in the way of [my preferred long-term strategy](https://github.com/rust-lang/unsafe-code-guidelines/issues/493#issuecomment-2028674105) for dealing with this UB.) So I think we should go back to square one and remove that error/lint for shared references. For mutable references, it does seem to work as intended, so we can keep it. Here it serves as a safety net in case the static checks that try to contain mutable references to the inside of a const initializer are not working as intended; I therefore made the check ICE to encourage users to tell us if that safety net is triggered.

Closes https://github.com/rust-lang/rust/issues/122153 by removing the lint.

Cc `@rust-lang/opsem` `@rust-lang/lang`
2024-09-14 21:11:04 +00:00
Michael Goulet
63405fc2b3 Consider synthetic closure bodies to be typeck children 2024-09-14 16:33:25 -04:00
bors
5e3ede22ef Auto merge of #129974 - cuviper:ci-llvm-19, r=Mark-Simulacrum
ci: add a runner for vanilla LLVM 19

Ubuntu 24.10 has `llvm-19` packages that we can start testing with.
The `Dockerfile` is otherwise the same as the `llvm-18` runner.
2024-09-14 18:44:23 +00:00
Tim (Theemathas) Chirananthavat
811ee38ff0 Add tracking issue number for box_vec_non_null 2024-09-15 01:11:18 +07:00
bjorn3
6fbe4d90b8 Rename remove_dir_if_exists to ensure_empty_dir and create the dir in this function
This avoids removing the directory, which may conflict with sandbox
systems like Landlock.
2024-09-14 17:34:24 +00:00
bors
8c4f0552fc Auto merge of #3884 - Mandragorian:detect_cond_move, r=RalfJung
detect when pthread_cond_t is moved

Closes #3749
2024-09-14 17:03:40 +00:00
Konstantinos Andrikopoulos
b6dea9ebdd detect when pthread_cond_t is moved
Closes #3749
2024-09-14 18:53:21 +02:00
bors
5fe0e40e05 Auto merge of #130357 - fmease:rollup-j3ej4q0, r=fmease
Rollup of 6 pull requests

Successful merges:

 - #130017 (coverage: Extract `executor::block_on` from several async coverage tests)
 - #130268 (simd_shuffle: require index argument to be a vector)
 - #130290 (Stabilize entry_insert)
 - #130294 (Lifetime cleanups)
 - #130343 (docs: Enable required feature for 'closure_returning_async_block' lint)
 - #130349 (Fix `Parser::break_up_float`'s right span)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-09-14 16:18:12 +00:00
León Orell Valerian Liehr
2b40fdbb28
Rollup merge of #130349 - ShE3py:break_up_float, r=fmease
Fix `Parser::break_up_float`'s right span

```rs
use std::mem::offset_of;

fn main() {
    offset_of!((u8,), 0.0);
}
```
Before:
```
error[E0609]: no field `0` on type `u8`
    --> ./main.rs:4:25
     |
4    |       offset_of!((u8,), 0.0);
     |  _____--------------------^-
     | |     |
     | |     in this macro invocation
5    | | }
...    |
     |
     = note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error
```
After:
```
error[E0609]: no field `0` on type `u8`
 --> ./main.rs:4:25
  |
4 |     offset_of!((u8,), 0.0);
  |                         ^

error: aborting due to 1 previous error
```

---
`@rustbot` label +A-parser +D-imprecise-spans
2024-09-14 18:12:14 +02:00
León Orell Valerian Liehr
d1701a5334
Rollup merge of #130343 - Fayti1703:patch-correct-async-block-lint-doc, r=compiler-errors
docs: Enable required feature for 'closure_returning_async_block' lint

Failing to do this results in the lint example output complaining about the lint not existing instead of the thing the lint is supposed to be complaining about.

See <https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#closure-returning-async-block>:
![image](https://github.com/user-attachments/assets/78bae16f-3fb6-4d6d-b8aa-768b477cd187)
2024-09-14 18:12:13 +02:00
León Orell Valerian Liehr
03e8b6bbfa
Rollup merge of #130294 - nnethercote:more-lifetimes, r=lcnr
Lifetime cleanups

The last commit is very opinionated, let's see how we go.

r? `@oli-obk`
2024-09-14 18:12:13 +02:00
León Orell Valerian Liehr
f873367243
Rollup merge of #130290 - passcod:stabilise-entry-insert, r=ChrisDenton
Stabilize entry_insert

This stabilises `HashMap::Entry::insert_entry`, following the FCP in tracking issue #65225.

This was implemented in #64656 five years ago.
2024-09-14 18:12:12 +02:00