Optimize unnecessary check in Vec::retain
The function `vec::Vec::retain` only have two stages:
1. Nothing was deleted.
2. Some elements were deleted.
Here is an unnecessary check `if g.deleted_cnt > 0` in the loop, and it's difficult for compiler to optimize it. I split the loop into two stages manully and keep the code clean using const generics.
I write a special but common bench case for this optimization. I call retain on vec but keep all elements.
Before and after this optimization:
```
test vec::bench_retain_whole_100000 ... bench: 84,803 ns/iter (+/- 17,314)
```
```
test vec::bench_retain_whole_100000 ... bench: 42,638 ns/iter (+/- 16,910)
```
The result is expected, there are two `if`s before the optimization and one `if` after.
Make diangostic item naming consistent
Right now there is about a 50/50 split of naming diagnostic items as `vec_type` vs `Vec`. So it is hard to guess a diagnostic item name with confidence. I know it's not great to change these retroactively, but I think it will be much easier to maintain consistency after consistency is established.
Make *const (), *mut () okay for FFI
Pointer-to-() is used occasionally in the standard library to mean "pointer to none-of-your-business". Examples:
- `RawWakerVTable::new` https://doc.rust-lang.org/1.51.0/std/task/struct.RawWakerVTable.html#method.new
- `<*const T>::to_raw_parts` https://doc.rust-lang.org/nightly/std/primitive.pointer.html#method.to_raw_parts
I believe it's useful for the same purpose in FFI signatures, even while `()` itself is not FFI safe. The following should be allowed:
```rust
extern "C" {
fn demo(pc: *const (), pm: *mut ());
}
```
Prior to this PR, those pointers were not considered okay for an extern signature.
```console
warning: `extern` block uses type `()`, which is not FFI-safe
--> src/main.rs:2:17
|
2 | fn demo(pc: *const (), pm: *mut ());
| ^^^^^^^^^ not FFI-safe
|
= note: `#[warn(improper_ctypes)]` on by default
= help: consider using a struct instead
= note: tuples have unspecified layout
warning: `extern` block uses type `()`, which is not FFI-safe
--> src/main.rs:2:32
|
2 | fn demo(pc: *const (), pm: *mut ());
| ^^^^^^^ not FFI-safe
|
= help: consider using a struct instead
= note: tuples have unspecified layout
```
Deriving: Include bound generic params in type parameters for where clause
Fixes#89188.
The `derive` macro ignored the `for<'s>` needed with the `Fn` trait in that code example.
edit: I'm unsure if this might cause regressions. I'm not an experienced compiler developer so I'm not used to thinking about unwanted side effects code changes like this might have.
resolve: Cache module loading for all foreign modules
It was previously cached for modules loaded from `fn get_module`, but not for modules loaded from `fn build_reduced_graph_for_external_crate_res`.
This also makes all foreign modules use their real parent, span and expansion instead of possibly a parent/span/expansion of their reexport.
Modules are also often compared using referential equality (`ptr::eq`), this change makes such comparisons correct in all cases.
An ICE happening on attempt to decode expansions for foreign enums and traits is avoided.
Also local enums and traits are now added to the module map.
Follow up to https://github.com/rust-lang/rust/pull/88872.
r? `@cjgillot`
It was previously cached for modules loaded from `fn get_module`, but not for modules loaded from `fn build_reduced_graph_for_external_crate_res`.
This also makes all foreign modules use their real parent, span and expansion instead of possibly a parent/span/expansion of their reexport.
An ICE happening on attempt to decode expansions for foreign enums and traits is avoided.
Also local enums and traits are now added to the module map.
Avoid nondeterminism in trimmed_def_paths
Previously this query depended on the global interning order of Symbols, which
meant that irrelevant changes could influence the query and cause
recompilations. This commit ensures that the return set is stable and will not
be affected by the global order by deterministically (in lexicographic order)
choosing a name to use if there are multiple names for a single DefId.
This should fix the cause of the [regressions] in #83343.
[regressions]: https://perf.rust-lang.org/compare.html?start=9620f3a84b079decfdc2e557be007580b097fe43&end=addb4da686a97da46159f0123cb6cdc2ce3d7fdb
Fix clippy lints
I'm currently working on allowing clippy to run on librustdoc after a discussion I had with `@Mark-Simulacrum.` So in the meantime, I fixed a few lints on the compiler crates.
Rollup of 7 pull requests
Successful merges:
- #85223 (rustdoc: Clarified the attribute which prompts the warning)
- #88847 (platform-support.md: correct ARMv7+MUSL platform triple notes)
- #88963 (Coerce const FnDefs to implement const Fn traits )
- #89376 (Fix use after drop in self-profile with llvm events)
- #89422 (Replace whitespaces in doctests' name with dashes)
- #89440 (Clarify a sentence in the documentation of Vec (#84488))
- #89441 (Normalize after substituting via `field.ty()`)
Failed merges:
r? `@ghost`
`@rustbot` modify labels: rollup
Normalize after substituting via `field.ty()`
Back in https://github.com/rust-lang/rust/issues/72476 I hadn't understood where the problem was coming from, and only worked around the issue. What happens is that calling `field.ty()` on a field of a generic struct substitutes the appropriate generics but doesn't normalize the resulting type.
As a consumer of types I'm surprised that one would substitute without normalizing, feels like a footgun, so I added a comment.
Fixes https://github.com/rust-lang/rust/issues/89393.
Clarify a sentence in the documentation of Vec (#84488)
I literally changed a single word, but this should clear up the confusion of #84488. Can probably be `rollup`ed :) (My first PR, hope I'm doing this right)
fixes#84488
r? `@steveklabnik`
Replace whitespaces in doctests' name with dashes
Fixes#88263.
Instead of handling white spaces when we filter tests (which would be quite complicated since we split on them!), I propose to instead replace them with dashes.
So for example, this:
```console
test foo.rs - Iter2<T, P>::len (line 13) ... ok
test foo.rs - Iter<T, P>::len (line 4) ... ok
```
becomes:
```console
test foo.rs - Iter<T,-P>::len (line 4) ... ok
test foo.rs - Iter2<T,-P>::len (line 13) ... ok
```
r? `@jyn514`
Fix use after drop in self-profile with llvm events
self-profile with `-Z self-profile-events=llvm` have failed with a segmentation fault due to this use after drop.
this type of events can be more useful now that the new passmanager is the default.
Coerce const FnDefs to implement const Fn traits
You can now pass a FnDef to a function expecting `F` where `F: ~const FnTrait`.
r? ``@oli-obk``
``@rustbot`` label T-compiler F-const_trait_impl
platform-support.md: correct ARMv7+MUSL platform triple notes
This PR fixes two minor inconsistencies in the platform support list.
- use "with MUSL" suffix for "armv7-unknown-linux-musleabi"
- add "hardfloat" suffix for "armv7-unknown-linux-musleabihf"
r? `@steveklabnik`
rustdoc: Clarified the attribute which prompts the warning
The example call was lacking clarification of the `#![warn(rustdoc::invalid_codeblock_attributes)]` attribute which generates the specified warning.
Rework HIR API to make invocations of the hir_crate query harder.
`hir_crate` forces the recomputation of queries that depend on it.
This PR aims at avoiding useless invocations of `hir_crate` by making dependent code go through `tcx.hir()`.
Correct caller/callsite confusion in inliner message
`callee_body` is the MIR `Body` for the `callsite.callee` so this message basically says `"Inline {bar span} into bar"` when it should say `"Inline bar into foo"`.
Extracted out of #82280
Improve error message for `printf`-style format strings
Fixes#89173. The following is actually supported today:
```rust
fn main() {
let num = 5;
let width = 20;
print!("%*2$x", num, width);
}
```
```
error: multiple unused formatting arguments
--> src/main.rs:4:21
|
4 | print!("%*2$x", num, width);
| ------- ^^^ ^^^^^ argument never used
| || |
| || argument never used
| |help: format specifiers use curly braces: `{:1$x}`
| multiple missing formatting specifiers
|
= note: printf formatting not supported; see the documentation for `std::fmt`
```
However, as noted in #89173, something like
```rust
print!("%0*x", width, num);
```
does not give a helpful suggestion. I think this is partly intended, because there actually _is_ no Rust equivalent to this; you always have to use a positional or named argument to specify the width (instead of just using the "next" argument, as `printf` or even `.*` as a precision specifier in Rust would). Therefore, I have added a note:
```
[...]
note: format specifiers use curly braces, and you have to use a positional or named parameter for the width
--> t2.rs:4:13
|
4 | print!("%0*x", width, num);
| ^^^^
= note: printf formatting not supported; see the documentation for `std::fmt`
```
This is not perfect, but it should at least point the user in the right direction, instead of issuing no explanation at all.
cc ```@lcnr```
Added -Z randomize-layout flag
An implementation of #77316, it currently randomly shuffles the fields of `repr(rust)` types based on their `DefPathHash`
r? ``@eddyb``
Rollup of 7 pull requests
Successful merges:
- #88838 (Do not suggest importing inaccessible items)
- #89251 (Detect when negative literal indices are used and suggest appropriate code)
- #89321 (Rebase resume argument projections during state transform)
- #89327 (Pick one possible lifetime in case there are multiple choices)
- #89344 (Cleanup lower_generics_mut and make span be the bound itself)
- #89397 (Update `llvm` submodule to fix function name mangling on x86 Windows)
- #89412 (Add regression test for issues #88969 and #89119 )
Failed merges:
r? `@ghost`
`@rustbot` modify labels: rollup
Cleanup lower_generics_mut and make span be the bound itself
Closes#86298 (supersedes those changes)
r? `@cjgillot` since you reviewed the other PR
(Used wrong branch for #89338)