2435 Commits

Author SHA1 Message Date
bors
2de2456fb7 Auto merge of #143376 - dianne:guard-scope, r=matthewjasper
add a scope for `if let` guard temporaries and bindings

This fixes my concern with `if let` guard drop order, namely that the guard's bindings and temporaries were being dropped after their arm's pattern's bindings, instead of before (https://github.com/rust-lang/rust/pull/141295#issuecomment-2968975596). The guard's bindings and temporaries now live in a new scope, which extends until (but not past) the end of the arm, guaranteeing they're dropped before the arm's pattern's bindings.

This only introduces a new scope for match arms with guards. Perf results (https://github.com/rust-lang/rust/pull/143376#issuecomment-3034922617) seemed to indicate there wasn't a significant hit to introduce a new scope on all match arms, but guard patterns (rust-lang/rust#129967) will likely benefit from only adding new scopes when necessary (with some patterns requiring multiple nested scopes).

Tracking issue for `if_let_guard`: rust-lang/rust#51114

Tests are adapted from examples by `@traviscross,` `@est31,` and myself on rust-lang/rust#141295.
2025-08-09 03:19:26 +00:00
Stuart Cook
562222b737
Rollup merge of #144999 - Zalathar:remove-mcdc, r=oli-obk
coverage: Remove all unstable support for MC/DC instrumentation

Preliminary support for a partial implementation of “Modified Condition/Decision Coverage” instrumentation was added behind the unstable flag `-Zcoverage-options=mcdc` in 2024. These are the most substantial PRs involved:

- rust-lang/rust#123409
- rust-lang/rust#126733

At the time, I accepted these PRs with relatively modest scrutiny, because I did not want to stand in the way of independent work on MC/DC instrumentation. My hope was that ongoing work by interested contributors would lead to the code becoming clearer and more maintainable over time.

---

However, that MC/DC code has proven itself to be a major burden on overall maintenance of coverage instrumentation, and a major obstacle to other planned improvements, such as internal changes needed for proper support of macro expansion regions.

I have also become reluctant to accept any further MC/DC-related changes that would increase this burden.

That tension has resulted in an unhappy impasse. On one hand, the present MC/DC implementation is not yet complete, and shows little sign of being complete at an acceptable level of code quality in the foreseeable future. On the other hand, the continued existence of this partial MC/DC implementation is imposing serious maintenance burdens on every other aspect of coverage instrumentation, and is preventing some of the very improvements that would make it easier to accept expanded MC/DC support in the future.

While I know this will be disappointing to some, I think the healthy way forward is accept that I made the wrong call in accepting the current implementation, and to remove it entirely from the compiler.
2025-08-08 12:52:54 +10:00
dianne
0bdaef5b63 only introduce a guard scope for arms with guards 2025-08-07 16:51:41 -07:00
dianne
b2241c78c8 add a scope for if let guard temporaries and bindings
This ensures `if let` guard temporaries and bindings are dropped before
the match arm's pattern's bindings.
2025-08-07 16:43:20 -07:00
Stuart Cook
5e781d05f6
Rollup merge of #143764 - dianne:primary-binding-drop-order, r=Nadrieril,traviscross
lower pattern bindings in the order they're written and base drop order on primary bindings' order

To fix rust-lang/rust#142163, this PR does two things:
- Makes match arms base their drop order on the first sub-branch instead of the last sub-branch. Together with the second change, this makes bindings' drop order correspond to the relative order of when each binding first appears (i.e. the order of the "primary" bindings).
- Lowers pattern bindings in the order they're written (still treating the right-hand side of a ``@`` as coming before the binding on the left). In each sub-branch of a match arm, this is the order that would be obtained if the or-alternatives chosen in that sub-branch were inlined into the arm's pattern. This both affects drop order (making bindings in or-patterns not be dropped first) and fixes the issue in [this test](2a023bf80a/tests/ui/pattern/bindings-after-at/bind-by-copy-or-pat.rs) from rust-lang/rust#121716.

My approach to the second point is admittedly a bit trickier than may be necessary. To avoid passing around a counter when building `FlatPat`s, I've instead added just enough information to recover the original structure of the pattern's bindings from a `MatchTreeSubBranch`'s path through the `Candidate` tree. Some alternatives:
- We could use a counter, then sort bindings by their ordinals when making `MatchTreeSubBranch`es.
- I'd like to experiment with always merging sub-candidates and removing `test_remaining_match_pairs_after_or`; that would require lowering bindings and guards in a different way. That makes it a bigger change too, though, so I figure it might be simplest to start here.
- For a very big change, we could track which or-alternatives succeed at runtime to base drop order on the binding order in the particular alternatives matched.

This is a breaking change. It will need a crater run, language team sign-off, and likely updates to the Reference.

This will conflict with rust-lang/rust#143376 and probably also rust-lang/rust#143028, so they shouldn't be merged at the same time.

r? `@matthewjasper` or `@Nadrieril`
2025-08-07 20:49:39 +10:00
Stuart Cook
4529dd9192
Rollup merge of #143028 - dianne:let-else-storage, r=oli-obk,traviscross
emit `StorageLive` and schedule `StorageDead` for `let`-`else`'s bindings after matching

This PR removes special handling of `let`-`else`, so that `StorageLive`s are emitted and `StorageDead`s are scheduled only after pattern-matching has succeeded. This means `StorageDead`s will no longer appear for all of its bindings on the `else` branch (because they're not live yet) and its drops&`StorageDead`s will happen together like they do elsewhere, rather than having all drops first, then all `StorageDead`s.

This fixes rust-lang/rust#142056, and is therefore a breaking change. I believe it'll need a crater run and a T-lang nomination/fcp thereafter. Specifically, this makes drop-checking slightly more restrictive for `let`-`else` to match the behavior of other variable binding forms. An alternative approach could be to change the relative order of drops and `StorageDead`s for other binding forms to make drop-checking more permissive, but making that consistent would be a significantly more involved change.

r? mir
cc `````@dingxiangfei2009`````

`````@rustbot````` label +T-lang +needs-crater
2025-08-07 20:49:38 +10:00
Guillaume Gomez
940a003985
Rollup merge of #144835 - compiler-errors:tail-call-sig-binder, r=WaffleLapkin
Anonymize binders in tail call sig

See the comment for explanation

Fixes rust-lang/rust#144826

r? WaffleLapkin
2025-08-06 21:29:27 +02:00
dianne
b7de539805 lower bindings in the order they're written 2025-08-06 12:13:40 -07:00
dianne
ea1eca5e3b base drop order on the first sub-branch 2025-08-06 12:13:12 -07:00
dianne
856e3816c3 don't schedule unnecessary drops when lowering or-patterns
This avoids scheduling drops and immediately unscheduling them. Drops
for guard bindings/temporaries are still scheduled and unscheduled as
before.
2025-08-06 11:42:15 -07:00
Zalathar
81ed042c8c coverage: Remove all unstable support for MC/DC instrumentation 2025-08-06 22:38:52 +10:00
Michael Goulet
f6ce4ac9d3 Anonymize binders in tail call sig 2025-08-04 15:31:50 +00:00
Waffle Lapkin
8b65f3d0e8
properly reject tail calls to &FnPtr or &FnDef 2025-08-04 09:09:49 +02:00
Samuel Tardieu
018c172b22
Rollup merge of #144851 - WaffleLapkin:instrinsic-deny, r=compiler-errors,scottmcm
Forbid tail calling intrinsics

There is only one intrinsic that can be called on stable, as far as I can find, (`transmute`). And in general tail calling intrinsics doesn't make much sense.

Alternative to rust-lang/rust#144815 (and thus closes rust-lang/rust#144815)
Fixes https://github.com/rust-lang/rust/issues/144806

r? ``@scottmcm``
2025-08-03 21:57:02 +02:00
Waffle Lapkin
c539890ae6
forbid tail calling intrinsics 2025-08-03 21:21:39 +02:00
Samuel Tardieu
d082ff4c04
Rollup merge of #144478 - joshtriplett:doc-code-formatting-prep, r=Amanieu
Improve formatting of doc code blocks

We don't currently apply automatic formatting to doc comment code blocks. As a
result, it has built up various idiosyncracies, which make such automatic
formatting difficult. Some of those idiosyncracies also make things harder for
human readers or other tools.

This PR makes a few improvements to doc code formatting, in the hopes of making
future automatic formatting easier, as well as in many cases providing net
readability improvements.

I would suggest reading each commit separately, as each commit contains one
class of changes.
2025-08-02 11:24:24 +02:00
Folkert de Vries
040f71e812
loop match: error on #[const_continue] outside #[loop_match] 2025-08-01 00:28:52 +02:00
Jana Dönszelmann
26c28ee2ef
Rollup merge of #144726 - jdonszelmann:move-attr-data-structures, r=lcnr
merge rustc_attr_data_structures into rustc_hir

this move was discussed on zulip: [#t-compiler > attribute parsing rework @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/attribute.20parsing.20rework/near/528530091)

Many PRs in the attribute rework depend on this move.
2025-07-31 17:19:40 +02:00
Jana Dönszelmann
e1d3ad89c7
remove rustc_attr_data_structures 2025-07-31 14:19:27 +02:00
Nicholas Nethercote
704f2ca172 Tidy up Cargo.toml files.
- Add some missing `tidy-alphabetical-*` markers.
- Remove some unnecessary blank lines.
2025-07-31 19:58:04 +10:00
Shoyu Vanilla
d87b4f2c77 fix: Reject upvar scrutinees for loop_match 2025-07-28 22:24:58 +09:00
bors
d242a8bd5a Auto merge of #144469 - Kivooeo:chains-cleanup, r=SparrowLii
Some `let chains` clean-up

Not sure if this kind of clean-up is welcoming because of size, but I decided to try out one

r? compiler
2025-07-28 05:25:23 +00:00
Kivooeo
bae38bad78 use let chains in hir, lint, mir 2025-07-28 06:10:14 +05:00
Trevor Gross
d8f4ceb8c0
Rollup merge of #143585 - folkertdev:loop-match-suggest-const-block, r=oli-obk
`loop_match`: suggest extracting to a `const` item

tracking issue: https://github.com/rust-lang/rust/issues/132306
fixes https://github.com/rust-lang/rust/issues/143310
fixes https://github.com/rust-lang/rust/issues/143936
2025-07-26 02:19:28 -05:00
Josh Triplett
715088094c Improve and regularize comment placement in doc code
Because doc code does not get automatically formatted, some doc code has
creative placements of comments that automatic formatting can't handle.
Reformat those comments to make the resulting code support standard Rust
formatting without breaking; this is generally an improvement to
readability as well.

Some comments are not indented to the prevailing indent, and are instead
aligned under some bit of code. Indent them to the prevailing indent,
and put spaces *inside* the comments to align them with code.

Some comments span several lines of code (which aren't the line the
comment is about) and expect alignment. Reformat them into one comment
not broken up by unrelated intervening code.

Some comments are placed on the same line as an opening brace, placing
them effectively inside the subsequent block, such that formatting would
typically format them like a line of that block. Move those comments to
attach them to what they apply to.

Some comments are placed on the same line as a one-line braced block,
effectively attaching them to the closing brace, even though they're
about the code inside the block. Reformat to make sure the comment will
stay on the same line as the code it's commenting.
2025-07-25 22:02:09 -07:00
Folkert de Vries
730d33dd64
loop_match: suggest extracting to a const item
if the expression cannot be evaluated in a straightforward way
2025-07-26 00:10:56 +02:00
bors
b56aaec52b Auto merge of #144389 - scottmcm:no-more-mir-cast-assume, r=davidtwco
MIR-build: No longer emit assumes in enum-as casting

This just uses the `valid_range` from the backend, so it's duplicating the range metadata that now we include on parameters and loads, and thus no longer seems to be useful -- notably there's no codegen test failures from removing it.

(Because it's using data from the same source as the backend annotations, it doesn't do anything to mitigate things like rust-lang/rust#144388 where the range in the layout is more permissive than the actual possible discriminants.  A variant of this that actually checked the discriminants more specifically might be useful, so could potentially be added in future, but I don't think the *current* checks are actually providing value.)

r? mir

Randomly turns out that this
Fixes https://github.com/rust-lang/rust/issues/121097
2025-07-24 20:38:53 +00:00
Scott McMurray
01524abb05 MIR-build: No longer emit assumes in enum-as casting
This just uses the `valid_range` from the backend, so it's duplicating the range metadata that now we include on parameters and loads.
2025-07-24 10:18:37 -07:00
Camille GILLOT
0460c92d52 Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
Camille GILLOT
9ff071219b Give an AllocId to ConstValue::Slice. 2025-07-23 23:54:37 +00:00
bors
ad635e5d06 Auto merge of #143779 - JonathanBrouwer:automatically_derived_parser, r=oli-obk
Port `#[automatically_derived]` to the new attribute parsing infrastructure

Ports `#[automatically_derived]` to the new attribute parsing infrastructure for https://github.com/rust-lang/rust/issues/131229#issuecomment-2971351163

r? `@oli-obk`
cc `@jdonszelmann`
2025-07-14 04:29:53 +00:00
Camille GILLOT
36bc0948e0 Generalize TyCtxt::item_name. 2025-07-13 13:50:00 +00:00
Jonathan Brouwer
ef82007ed7
Port #[automatically_derived] to the new attribute parsing infrastructure
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
2025-07-12 17:48:50 +02:00
bors
6b3ae3f6e4 Auto merge of #143472 - dianne:deref-pat-column-check, r=Nadrieril
`rustc_pattern_analysis`: always check that deref patterns don't match on the same place as normal constructors

In rust-lang/rust#140106, deref pattern validation was tied to the `deref_patterns` feature to temporarily avoid affecting perf. However:
- As of rust-lang/rust#143414, box patterns are represented as deref patterns in `rustc_pattern_analysis`. Since they can be used by enabling `box_patterns` instead of `deref_patterns`, it was possible for them to skip validation, resulting in an ICE. This fixes that and adds a regression test.
- External tooling (e.g. rust-analyzer) will also need to validate matches containing deref patterns, which was not possible. This fixes that by making `compute_match_usefulness` validate deref patterns by default.

In order to avoid doing an extra pass for anything with patterns, the second commit makes `RustcPatCtxt` keep track of whether it encounters a deref pattern, so that it only does the check if so. This is purely for performance. If the perf impact of the first commit is negligible and the complexity cost introduced by the second commit is significant, it may be worth dropping the latter.

r? `@Nadrieril`
2025-07-09 09:45:36 +00:00
bors
a2d45f73c7 Auto merge of #143601 - matthiaskrgr:rollup-9iw2sqk, r=matthiaskrgr
Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#132469 (Do not suggest borrow that is already there in fully-qualified call)
 - rust-lang/rust#143340 (awhile -> a while where appropriate)
 - rust-lang/rust#143438 (Fix the link in `rustdoc.md`)
 - rust-lang/rust#143539 (Regression tests for repr ICEs)
 - rust-lang/rust#143566 (Fix `x86_64-unknown-netbsd` platform support page)
 - rust-lang/rust#143572 (Remove unused allow attrs)
 - rust-lang/rust#143583 (`loop_match`: fix 'no terminator on block')
 - rust-lang/rust#143584 (make `Machine::load_mir` infallible)
 - rust-lang/rust#143591 (Fix missing words in future tracking issue)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-07-07 20:30:53 +00:00
bors
2f8eeb2bba Auto merge of #143182 - xdoardo:more-addrspace, r=workingjubilee
Allow custom default address spaces and parse `p-` specifications in the datalayout string

Some targets, such as CHERI, use as default an address space different from the "normal" default address space `0` (in the case of CHERI, [200 is used](https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-877.pdf)). Currently, `rustc` does not allow to specify custom address spaces and does not take into consideration [`p-` specifications in the datalayout string](https://llvm.org/docs/LangRef.html#langref-datalayout).

This patch tries to mitigate these problems by allowing targets to define a custom default address space (while keeping the default value to address space `0`) and adding the code to parse the `p-` specifications in `rustc_abi`. The main changes are that `TargetDataLayout` now uses functions to refer to pointer-related informations, instead of having specific fields for the size and alignment of pointers in the default address space; furthermore, the two `pointer_size` and `pointer_align` fields in `TargetDataLayout` are replaced with an `FxHashMap` that holds info for all the possible address spaces, as parsed by the `p-` specifications.

The potential performance drawbacks of not having ad-hoc fields for the default address space will be tested in this PR's CI run.

r? workingjubilee
2025-07-07 17:28:14 +00:00
Folkert de Vries
6d58a88c3c
loop_match: fix 'no terminator on block' 2025-07-07 13:55:40 +02:00
Edoardo Marangoni
93f1201c06
compiler: Parse p- specs in datalayout string, allow definition of custom default data address space 2025-07-07 09:04:53 +02:00
Camille GILLOT
39ee1b2d77 Remove yields_in_scope from the scope tree. 2025-07-05 15:24:15 +00:00
dianne
bb64315978 only check for mixed deref/normal constructors when needed 2025-07-04 23:47:31 -07:00
dianne
98659a339d treat box patterns as deref patterns in THIR and usefulness analysis
This removes special-casing of boxes from `rustc_pattern_analysis`, as a
first step in replacing `box_patterns` with `deref_patterns`.
Incidentally, it fixes a bug caused by box patterns being represented as
structs rather than pointers, where `exhaustive_patterns` could generate
spurious `unreachable_patterns` lints on arms required for
exhaustiveness; following the lint's advice would result in an error.
2025-07-04 01:28:35 -07:00
Jana Dönszelmann
f6d37a25a9
Rollup merge of #134006 - klensy:typos, r=nnethercote
setup typos check in CI

This allows to check typos in CI, currently for compiler only (to reduce commit size with fixes). With current setup, exclude list is quite short, so it worth trying?

Also includes commits with actual typo fixes.

MCP: https://github.com/rust-lang/compiler-team/issues/817

typos check currently turned for:
* ./compiler
* ./library
* ./src/bootstrap
* ./src/librustdoc

After merging, PRs which enables checks for other crates (tools) can be implemented too.

Found typos will **not break** other jobs immediately: (tests, building compiler for perf run). Job will be marked as red on completion in ~ 20 secs, so you will not forget to fix it whenever you want, before merging pr.

Check typos: `python x.py test tidy --extra-checks=spellcheck`
Apply typo fixes: `python x.py test tidy --extra-checks=spellcheck:fix` (in case if there only 1 suggestion of each typo)

Current fail in this pr is expected and shows how typo errors emitted. Commit with error will be removed after r+.
2025-07-03 13:29:35 +02:00
klensy
c76d032f01 setup CI and tidy to use typos for spellchecking and fix few typos 2025-07-03 10:51:06 +03:00
Folkert de Vries
8fdf0ef0ae
loop match: handle opaque patterns
fixes issue 143203
2025-07-01 15:53:52 +02:00
Folkert de Vries
aa7cc5d2f4
loop match: run exhaustiveness check 2025-07-01 15:53:50 +02:00
bors
6988a8fea7 Auto merge of #141875 - nnethercote:ByteSymbol, r=petrochenkov
Introduce `ByteSymbol`

It's like `Symbol` but for byte strings. The interner is now used for both `Symbol` and `ByteSymbol`. E.g. if you intern `"dog"` and `b"dog"` you'll get a `Symbol` and a `ByteSymbol` with the same index and the characters will only be stored once.

The motivation for this is to eliminate the `Arc`s in `ast::LitKind`, to make `ast::LitKind` impl `Copy`, and to avoid the need to arena-allocate `ast::LitKind` in HIR. The latter change reduces peak memory by a non-trivial amount on literal-heavy benchmarks such as `deep-vector` and `tuple-stress`.

`Encoder`, `Decoder`, `SpanEncoder`, and `SpanDecoder` all get some changes so that they can handle normal strings and byte strings.
2025-07-01 02:22:42 +00:00
Nicholas Nethercote
478f8287c0 Introduce ByteSymbol.
It's like `Symbol` but for byte strings. The interner is now used for
both `Symbol` and `ByteSymbol`. E.g. if you intern `"dog"` and `b"dog"`
you'll get a `Symbol` and a `ByteSymbol` with the same index and the
characters will only be stored once.

The motivation for this is to eliminate the `Arc`s in `ast::LitKind`, to
make `ast::LitKind` impl `Copy`, and to avoid the need to arena-allocate
`ast::LitKind` in HIR. The latter change reduces peak memory by a
non-trivial amount on literal-heavy benchmarks such as `deep-vector` and
`tuple-stress`.

`Encoder`, `Decoder`, `SpanEncoder`, and `SpanDecoder` all get some
changes so that they can handle normal strings and byte strings.

This change does slow down compilation of programs that use
`include_bytes!` on large files, because the contents of those files are
now interned (hashed). This makes `include_bytes!` more similar to
`include_str!`, though `include_bytes!` contents still aren't escaped,
and hashing is still much cheaper than escaping.
2025-06-30 20:42:27 +10: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
Jana Dönszelmann
63c5a84b74
Rollup merge of #142724 - xizheyin:avoid_overwrite_args, r=oli-obk
Add runtime check to avoid overwrite arg in `Diag`

## Origin PR description
At first, I set up a `debug_assert` check for the arg method to make sure that `args` in `Diag` aren't easily overwritten, and I added the `remove_arg()` method, so that if you do need to overwrite an arg, then you can explicitly call `remove_arg()` to remove it first, then call `arg()` to overwrite it.

For the code before the rust-lang/rust#142015 change, it won't compile because it will report an error
```
arg `instance`already exists.
```

This PR also modifies all diagnostics that fail the check to pass the check. There are two cases of check failure:

1. ~~Between *the parent diagnostic and the subdiagnostic*, or *between the subdiagnostics* have the same field between them. In this case, I renamed the conflicting fields.~~
2. ~~For subdiagnostics stored in `Vec`, the rendering may iteratively write the same arg over and over again. In this case, I changed the auto-generation with `derive(SubDiagnostic)` to manually implementing `SubDiagnostic` and manually rendered it with `eagerly_translate()`, similar to https://github.com/rust-lang/rust/issues/142031#issuecomment-2984812090, and after rendering it I manually deleted useless arg with the newly added `remove_arg` method.~~

## Final Decision

After trying and discussing, we made a final decision.

For `#[derive(Subdiagnostic)]`, This PR made two changes:

1. After the subdiagnostic is rendered, remove all args of this subdiagnostic, which allows for usage like `Vec<Subdiag>`.
2. Store `diag.args` before setting arguments, so that you can restore the contents of the main diagnostic after deleting the arguments after subdiagnostic is rendered, to avoid deleting the main diagnostic's arg when they have the same name args.
2025-06-25 22:14:55 +02:00
dianne
c6a97d3c59 emit StorageLive and schedule StorageDead for let-else after matching 2025-06-25 13:02:15 -07:00