Pass the unremapped path to the `rustc` invocation for doctests
Turns out that `--remap-path-scope` is only half working with doctests, this is because `rustdoc` use a secret environment variable to pass the real filename of the doctest.
However we were only passing a remapped path. That means that doctests source path were getting remapped, but only for one scope: `documentation`. It didn't respect `macro` for `file!` for example.
To fix this, we have to:
1. pass the local path to the `rustc` invocation
2. have `rustc` treat that path as a real filename and apply remapping per scope to it
- I had to add a new `Input` variant, otherwise I can't differentiate normal input `-` from doctests `-` source
4. we have to pass `--remap-path-{prefix,scope}` to `rustc` so the path actually gets remap properly
Deny partial `-Z stack-protector` by default in all editions
Deny it only as a warn-by-default future-incompatibility warning.
This enables RFC 3855 for stack-protector. With this PR, uses of stack-protector that only have it enabled for a subset of the crates within a process need to pass `-Z allow-partial-mitigations=stack-protector` as well.
This was not done in rust-lang/rust#149357 to allow for a smooth transition period.
cc the stack-protector tracking issue at rust-lang/rust#114903.
r? @rcvalle (do we need any special protocol for the breaking change?)
This enables RFC 3855 for stack-protector. With this PR, uses of stack-protector
that only have it enabled for a subset of the crates within a process need to pass
`-Z allow-partial-mitigations=stack-protector` as well.
This uses a future-incompat warning to avoid breaking people.
This was not done in issue 149357 to allow for a smooth transition period.
cc the stack-protector tracking issue at issue 114903.
Parser: Refactor & better document `should_continue_as_assoc_expr` & `can_continue_expr_unambiguously`
Whenever I stumbled across `should_continue_as_assoc_expr` I had to very slowly reread its impl because the contained `match` was written in an incredibly convoluted fashion rendering it illegible. Moreover, I always had a hard time figuring out what part of the code was recovery & what actual parsing, esp. since it does indeed save extra diagnostic info in the happy path (proactively).
See commit message for details.
<sub>(No LLM was or will be used by me during the entire creation process of this PR)</sub>
* `can_continue_expr_unambiguously`
* exhaustively match on `AssocOp`
* don't needlessly provide code snippets in comments for ops that are
unproblematic, that's not interesting
* instead, provide an example / explainer for each op that is ambiguous
which is far more relevant
* move it into `rustc_parse` since it's only used there & it's only
tailored towards parse error recovery
* `should_continue_as_assoc_expr`
* it used to match on a `(bool, _)` tuple & execute `AssocOp::from_token`
unconditionally even though all but one branch cared about the bool
being false
* instead, first check `expr_is_complete` and bail out early if it's
false; no need to execute `AssocOp::from_token` unnecessarily or
"bother" the rest of the code with the impossibility of
`expr_is_complete` not holding
* extract the recovery logic into a new
`recover_from_bin_op_after_complete_stmt_expr` that resides in
`expr/diagnostics.rs` to clearly separate what is recovery and what
is actual parsing
* in it check `can_continue_expr_unambiguously` *first* before
special-casing some ops (the `Mul | Sub | …` branch) because it's
more "important"; in any case it's a superset; renders the control
flow & the logic a lot more comprehensible
* rewrite the comments from scratch to make it clear that all of that
"bin op after complete stmt expr" is recovery code only! when I
first came across this function one year ago I was super confused &
briefly worried that typeck was potentially making belated parsing
decisions
* inline it because it's become a single expression
MIR move elimination [2/6]: TailCopyToMove
Depends on rust-lang/rust#163335
This PR implements a pre-pass for the `MoveElimination` pass from https://github.com/rust-lang/rfcs/pull/3943.
`TailCopyToMove` turns `Copy` into `Move` just before a `Return` terminator. This is valid even for places that have been borrowed because the `Return` will invalidate borrows anyway. This is necessary to allow the source of the copy to be unified with the return place.
r? tmiasko
do not complain about unstable target features on nightly
This was brought up in https://github.com/rust-lang/rust/issues/162235#issuecomment-5856608121: we currently print a warning on nightly saying that using an unstable target feature will become a hard error. That's a mistake.
We could either remove the part of the warning that talks about it becoming a hard error, or we could just hide the warning entirely for unstable features on nighty. I went for the latter -- nightly is meant for experimentation with those features so having un-silenceable warnings is not great.
Cc @workingjubilee
Add support for -Zsanitizer-cfi-minimal-runtime
For production use, we should only link in the ubsan_minimal runtime,
instead of the complete ubsan runtime. This adds support for both
cfi-recover and cfi-diag to use the minimal runtime when
`-Zsanitizer-cfi-minimal-runtime` is specified.
This also includes tests, to ensure the flag can only be used if either
cfi-recover or cfi-diag is enabled, it doesn't disrupt the original
behavior, and links in the correct runtime when specified.
cc @1c3t3a
?r rcvalle
Ensure llvm worker threads have sufficient stack space
The rustc driver sets the default stack size to 16MB, however worker threads spawned by the backend will use std's default stack size (usually 2MB).
Pass through the stack size chosen by the driver to the backend, and explicitly request the stack size.
This probably isn't the preferred way to do this, but maybe a starting point to educate me.
This doesn't catch every spawned thread (jobserver and ctrlc also create them through std). Are there other threads which might be sensitive to small stack sizes?
Fixesrust-lang/rust#163272
The rustc driver sets the default stack size to 16MB, however
worker threads spawned by the backend will use std's default
stack size (usually 2MB).
Pass through the stack size chosen by the driver to the backend,
and explicitly request the stack size.
This is a part of the stack-protector work.
Adding support for merge-functions is not strictly needed, but it does
prevent breakage if the GCC function merging logic changes.
Not adding tests for -Z merge-functions since I don't see any such tests for the
LLVM side of it.
Build a new incr comp session dir from scratch every time
Rather than copying the old incr comp dir and then modifying it. This saves a copy/hardlink for files that are modified. And it removes the need for accurate work product tracking to avoid accumulating cruft, which is non-trivial. We don't accurately track the pre-LTO bitcode files for ThinLTO for example.
Part of https://github.com/rust-lang/compiler-team/issues/908
Rather than copying the old incr comp dir and then modifying it. This
saves a copy/hardlink for files that are modified. And it removes the
need for accurate work product tracking to avoid accumulating cruft,
which is non-trivial. We don't accurately track the pre-LTO bitcode
files for ThinLTO for example.
Only check `SanitizerCfiMinimalRuntimeRequiresCfiRecoverOrDiag` if CFI
is actually enabled.
Use `is_sanitizer_cfi_minimal_runtime_enabled()` helper within
`link.rs`.
`core::num::f16b` Rust's 16bit Brain Float
Implements the [RFC: f16b type](https://github.com/rust-lang/rfcs/pull/3983). Best reviewed commit by commit, happy to split into separate PRs if that is deemed easier to review. However the line count and surface area is, in my opinion, reasonably small.
Adds;
- ABI plumbing for the `f16b` along with `bfloat` lang item to work with LLVM, GCC is explicitly `unimplemented!(...)`
- `f16b` feature gate, page for `f16b` on libruscdoc and a `struct bf16` in `core::num`
- Tests
- Treat `f16b` as a scalar primitive for scalable vectors
Issues;
- [Tracking Issue](https://github.com/rust-lang/rust/issues/160630)
- [RFC](https://github.com/rust-lang/rfcs/pull/3983)
For production use, we should only link in the ubsan_minimal runtime,
instead of the complete ubsan runtime. This adds support for both
cfi-recover and cfi-diag to use the minimal runtime when
`-Zsanitizer-cfi-minimal-runtime` is specified.
This also includes tests, to ensure the flag can only be used if either
cfi-recover or cfi-diag is enabled, it doesn't disrupt the original
behavior, and links in the correct runtime when specified.
Co-Authored-By: Bastian Kersting <bkersting@google.com>
It encodes the `emit` behaviour, one of:
- `BugAbort`: Abort as a bug.
- `FatalAbort`: Abort as a fatal error.
- `ErrorGuaranteed`: Return an `ErrorGuaranteed` (the default).
- `()`: return `()`.
This is useful but `Diag` is very widely used and it's arguably not
useful enough to be worth the generic parameter. This commit removes it.
Benefits:
- Many fewer generic parameters, including many functions where the
genericity is entirely uninteresting. This includes every
`Diagnostic::into_diag` and `Subdiagnostic::add_to_diag`. Removes the
need for the comment about `<G>` on `trait Diagnostic`.
- No `PhantomData` in `Diag`.
- Makes bootstrapping a bit faster.
The downside is there is no longer a single `emit` method.
- Getting a `!` return type requires calling `emit_bug` or `emit_fatal`.
This only affects a few call sites.
- Getting an `ErrorGuaranteed` return type requires calling `emit_err`.
This affects a lot of call sites. (It arguably makes call sites more
self-documenting.)
- Using the wrong `emit_*` on a diagnostic causes a runtime abort, which
previously couldn't occur. Seems unlikely, but it's not impossible.
- Note: If no particular return type is needed, `emit` can be used for
any diagnostic. (Bug/fatal diagnostics will still trigger abort.)
rustc: Tweak the effect of `--jobs` on frontend parallelism
We need to be able to express both of these things for the frontend parallelism:
- "Use exactly `N` threads", `--jobs-frontend=N` does this.
- "Use some default number of threads, but not larger than `N`", `--jobs=N` will do it after this PR.
The "default number" may be 1 now, or 2 during the upcoming nightly testing, or 4 or 8 after the stabilization if we decide to cap the parallelism.
Part of https://github.com/rust-lang/compiler-team/issues/1005.
r? @bjorn3
It's now possible to get the backend features (a `Vec<String>`) when the
codegen backend is started, pass it back through `CodegenBackendInit`,
and just store it in the `Session`. This removes the need for the query.
Also:
- `WriteBackendMethods::target_machine_factory` no longer needs the
`target_features` parameter, because it's now available through the
`sess` parameter.
- `CodegenContext` no longer needs the `backend_features` field because
we can use `sess.global_backend_features` instead.
- `CodegenBackend::provide` is now a no-op for all the in-tree backends.
I haven't removed it because out-of-tree backends still rely on it.
Session creation is currently awkward: we build a mostly-initialized
session, then use it to initialize a codegen backend, and then use the
codegen backend to finish initializing the session.
And it's not just awkward: within the Cranelift backend's `init` method
`sess.lto()` is called, which consults `sess.thin_lto_supported`,
*before* that field has been properly set! In practice it had no effect
but it's worth fixing.
This commit cleans up this mess. It introduces `EarlySession`, which
contains just four `Session` fields, the ones that are needed for
codegen backend initialization. It is now a field within `Session`, and
`Session` derefs to `EarlySession` to avoid changing a zillion
`sess.target`/`sess.opts`/etc. occurrences. `EarlySession` is passed to
`init`, which returns a `CodegenBackendInit` that contains the
backend-specific information needed to build a `Session`. (It replaces
the `replaced_intrinsics`, `fallback_intrinsics`, and
`thin_lto_supported` methods.) The `Session` can then be built in a
single step. No more `Session`/`CodegenBackend` initialization
intermingling.
A few functions that previously took a `Session` now take something
else, e.g. a `Target`. Some `Session` methods are now `EarlySession`
methods. And a new `early_lto` method is used for Cranelift's LTO check.
Simplify the `G` in `Diag<'a, G>`
The `EmissionGuarantee` trait isn't necessary, and just complicates things. Details in individual commits.
r? @oli-obk
Remove `EarlyDiagCtxt::early_{note,help}`
We have some top-level `help` and `note` diagnostics emittable at startup. These are weird. This PR eliminates them. Details in individual commits.
r? @estebank
It's a trait that is used to give `Diag::emit` different return types
depending on the `G` in `Diag<'_, G>`. It works, but it's overkill.
There are only four different `G` types in practice (`BugAbort`,
`FatalAbort`, `ErrorGuaranteed`, and `()`) and having inherent `emit`
methods for each of those four concrete `Diag<'_, G>` types is good
enough.
This commit makes that change. This removes `EmissionGuarantee`
trait bounds from many places, which is nice.
Currently the command `rustc -Zunstable-options -j3 --jobs-backend=1
a.rs`, when run on stable rustc, gives this:
```
error: the option `Z` is only accepted on the nightly compiler
error: the option `jobs` is only accepted on the nightly compiler
error: the option `jobs-backend` is only accepted on the nightly compiler
help: consider switching to a nightly toolchain: `rustup default nightly`
note: selecting a toolchain with `+toolchain` arguments require a rustup proxy; see <https://rust-lang.github.io/rustup/concepts/index.html>
note: for more information about Rust's stability policy, see <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html#unstable-features>
error: 3 nightly options were parsed
```
The errors are fine, but top-level help and notes are weird.
This commit changes it to this, which is more normal:
```
error: the option `Z` is only accepted on the nightly compiler
error: the option `jobs` is only accepted on the nightly compiler
error: the option `jobs-backend` is only accepted on the nightly compiler
error: 3 nightly options were parsed
|
= help: consider switching to a nightly toolchain: `rustup default nightly`
= note: selecting a toolchain with `+toolchain` arguments require a rustup proxy; see <https://rust-lang.github.io/rustup/concepts/index.html>
= note: for more information about Rust's stability policy, see <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html#unstable-features>
```
It's a slightly different order but that seems fine.
The commit also makes two other small improvements:
- An unnecessary `match opt.stability` is removed. (There's another
`opt.stability` check a few lines above.)
- It fixes "1 nightly option were parsed" to "1 nightly option was
parsed".
Add missing option to `-Zself-profile-event` help message as well as information about what the default options are
The `query-cache-hit-count` option was forgotten to be added to the help text when it was added.
The new help text also documents which of the options are part of `default`, and now conveys the information that `args` is also a compound/group option and what it contains.
sanitizers: Implicitly disable mutually exclusive sanitizers
This attempts to match clang's behavior of implicitly disabling sanitizers that are incompatible. Specifically, if a set of default sanitizers would be incompatible with ones provided by -Zsanitize=..., then clang (and now rust) will opt for keeping the ones specified via flags over the ones used as platform defaults. This helps maintain build consistency where we can just enable sanitizers via flags for both rust and c++ code without needing to manually disable others.
The driving reason for this is asan and safestack where we'd like to enable safestack by default for x86_64 fuchsia but disable it if -Zsanitize=address is passed (matching clang's behavior).
This commit also refactors all uses of `self.opts.unstable_opts.sanitizer` to go through the updated `sanitizer()` method.
AI: Gemini was used to help review the code and write some tests, but it did not generate the whole patch. I edited and reviewed this PR to the best of my ability before pushing for review.
sanitizers: Implement support for the sanitize ignorelist
The sanitize ignorelist gives central controls over which functions, files, etc. should be ignored and not sanitized. It is a common file format for clang and explained here:
https://clang.llvm.org/docs/SanitizerSpecialCaseList.html. This change adds support for this list in Rust as well.
r? @rcvalle
The sanitize ignorelist gives central controls over which functions,
files, etc. should be ignored and not sanitized. It is a common file
format for clang and explained here:
https://clang.llvm.org/docs/SanitizerSpecialCaseList.html. This change
adds support for this list in Rust as well.