Commit Graph

221 Commits

Author SHA1 Message Date
Zalathar bc11e7e142 Rename match-candidate partitioning methods 2025-12-13 21:15:22 +11:00
Zalathar d602710e5a Move match-candidate partitioning code into its own module 2025-12-13 21:15:22 +11:00
Kivooeo 2a2da782d3 add check for uninhabited types along side never 2025-12-09 13:18:09 +00:00
bors af17d59d71 Auto merge of #149044 - clubby789:implicit-return-span, r=cjgillot
Reduce confusing `unreachable_code` lints

Closes rust-lang/rust#149042

Attempts to be smarter about identifying 'real'/user-written unreachable code to raise the lint
2025-11-22 06:28:24 +00:00
Jamie Hill-Daniel 98141e0beb Reduce confusing unreachable_code lints 2025-11-21 16:07:54 +00:00
Camille Gillot 72444372ae Replace OffsetOf by an actual sum. 2025-11-18 00:10:03 +00:00
Zalathar 34cccb2697 Document the let this = self; idiom used in MIR building 2025-11-17 17:48:24 +11:00
dianne cb427318cb compute temporary scopes when building MIR, not THIR 2025-11-13 00:07:36 -08:00
bors 25d319a0f6 Auto merge of #148658 - dianne:cleanup-rvalue-scopes, r=cjgillot
cleanup: merge `RvalueScopes` into `ScopeTree`

This gets rid of `RvalueCandidate`, inlines the definition of `RvalueScopes` into `ScopeTree`, and removes two `RvalueScopes`-specific modules, consolidating the scoping logic a bit. Removing the extra step of going from `RvalueCandidate`s to `RvalueScopes` and removing the duplication between them should also hopefully improve perf.

I've also taken the liberty of doing a bit of renaming and comment updates, changing some "rvalue scope"s to "extended temporary scope"s. This is a bit closer to the Reference's terminology and makes it clearer that it's specific to temporary lifetime extension. This isn't comprehensive. In particular, I've left `record_rvalue_scope_if_borrow_expr` untouched since rust-lang/rust#146098 gets rid of it.

Pulled out from rust-lang/rust#146098.

r? BoxyUwU as the reviewer of rust-lang/rust#146098 (though feel free to reassign/claim! this is just cleanup)

cc `@dingxiangfei2009`
2025-11-11 17:23:11 +00:00
Frank King 5ef48ed448 Implement &pin patterns and ref pin bindings 2025-11-10 09:57:08 +08:00
dianne 0e50d5a7ff merge RvalueScopes into ScopeTree
This removes some unneeded indirection and consolidates the logic for
scope resolution.
2025-11-07 18:44:01 -08:00
Frank King 26f35ae269 Implement pattern matching for &pin mut|const T 2025-10-30 07:56:16 +08:00
bors 907705abea Auto merge of #148208 - camsteffen:assign-desugar-span, r=wesleywiser
Remove unused `AssignDesugar` span
2025-10-29 01:59:52 +00:00
Cameron Steffen a45c6dd2c0 Remove AssignDesugar span 2025-10-28 11:18:58 -05:00
bors df984edf44 Auto merge of #147083 - dianne:non-extended-indices, r=matthewjasper
Do not lifetime-extend array/slice indices

When lowering non-overloaded indexing operations to MIR, this uses the temporary lifetime of the index expression for the index temporary, rather than applying the temporary lifetime of the indexing operation as a whole to the index.

For example, in
```rust
let x = &xs[i];
```
previously, the temporary containing the result of evaluating `i` would live until the end of the block due to the indexing operation being [lifetime-extended](https://doc.rust-lang.org/nightly/reference/destructors.html#temporary-lifetime-extension). Under this PR, the index temporary only lives to the end of the `let` statement because it uses the more precise temporary lifetime of the index expression.

I don't think this will affect semantics in an observable way, but the more precise `StorageDead` placement may slightly improve analysis/codegen performance.

r? mir
2025-10-28 03:02:00 +00:00
Camille Gillot 5dfbf67f94 Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
yukang 4a886add5c Fix ICE in pattern matching with generic const array length errors 2025-10-16 19:15:32 +08:00
bors ff6dc928c5 Auto merge of #142390 - cjgillot:mir-liveness, r=davidtwco
Perform unused assignment and unused variables lints on MIR.

Rebase of https://github.com/rust-lang/rust/pull/101500

Fixes https://github.com/rust-lang/rust/issues/51003.

The first commit moves detection of uninhabited types from the current liveness pass to MIR building.

In order to keep the same level of diagnostics, I had to instrument MIR a little more:
- keep for which original local a guard local is created;
- store in the `VarBindingForm` the list of introducer places and whether this was a shorthand pattern.

I am not very proud of the handling of self-assignments. The proposed scheme is in two parts: first detect probable self-assignments, by pattern matching on MIR, and second treat them specially during dataflow analysis. I welcome ideas.

Please review carefully the changes in tests. There are many small changes to behaviour, and I'm not sure all of them are desirable.
2025-10-12 13:00:04 +00:00
bors 3be68033b6 Auto merge of #145513 - beepster4096:erasedereftemps, r=saethlin,cjgillot
Validate CopyForDeref and DerefTemps better and remove them from runtime MIR

(split from my WIP rust-lang/rust#145344)

This PR:
- Removes `Rvalue::CopyForDeref` and `LocalInfo::DerefTemp` from runtime MIR
    - Using a new mir pass `EraseDerefTemps`
    - `CopyForDeref(x)` is turned into `Use(Copy(x))`
    - `DerefTemp` is turned into `Boring`
        - Not sure if this part is actually necessary, it made more sense in rust-lang/rust#145344 with `DerefTemp` storing actual data that I wanted to keep from having to be kept in sync with the rest of the body in runtime MIR
- Checks in validation that `CopyForDeref` and `DerefTemp` are only used together
- Removes special handling for `CopyForDeref` from many places
- Removes `CopyForDeref` from `custom_mir` reverting rust-lang/rust#111587
    - In runtime MIR simple copies can be used instead
    - In post cleanup analysis MIR it was already wrong to use due to the lack of support for creating `DerefTemp` locals
    - Possibly this should be its own PR?
 - Adds an argument to `deref_finder` to avoid creating new `DerefTemp`s and `CopyForDeref` in runtime MIR.
     - Ideally we would just avoid making intermediate derefs instead of fixing it at the end of a pass / during shim building
 - Removes some usages of `deref_finder` that I found out don't actually do anything

r? oli-obk
2025-10-12 02:34:20 +00:00
Camille GILLOT ca0379d6cd Diagnose liveness on MIR. 2025-10-11 20:50:21 +00:00
Camille GILLOT 89191084d5 Correct binding scope when building MIR. 2025-10-11 20:50:20 +00:00
Camille GILLOT f2535764d4 Introduce VarBindingIntroduction. 2025-10-11 20:50:20 +00:00
Camille GILLOT 9df2003860 Record for each MIR local where it has been introduced. 2025-10-11 20:50:20 +00:00
Camille GILLOT 5620c82e53 Report uninhabited call return types on MIR. 2025-10-11 20:50:20 +00:00
beepster4096 2da55cdb2c remove copyforderef from custom_mir
it did not create DerefTemp locals when used, so it was never actually correct.
2025-10-10 20:30:19 -07:00
Camille Gillot b7c2b3dc80 Remove StatementKind::Deinit. 2025-10-10 12:57:24 +00:00
bors 42b384ec0d Auto merge of #147055 - beepster4096:subtype_is_not_a_projection, r=lcnr
Turn ProjectionElem::Subtype into CastKind::Subtype

I noticed that drop elaboration can't, in general, handle `ProjectionElem::SubType`. It creates a disjoint move path that overlaps with other move paths. (`Subslice` does too, and I'm working on a different PR to make that special case less fragile.) If its skipped and treated as the same move path as its parent then `MovePath.place` has multiple possible projections. (It would probably make sense to remove all `Subtype` projections for the canonical place but it doesn't make sense to have this special case for a problem that doesn't actually occur in real MIR.)

The only reason this doesn't break is that `Subtype` is always the sole projection of the local its applied to. For the same reason, it works fine as a `CastKind` so I figured that makes more sense than documenting and validating this hidden invariant.

cc rust-lang/rust#112651, rust-lang/rust#133258

r? Icnr (bc you've been the main person dealing with `Subtype` it looks like)
2025-10-02 01:54:48 +00:00
dianne c0dc979549 do not lifetime-extend array/slice indices
When lowering non-overloaded indexing operations to MIR, this uses the
temporary lifetime of the index expression for the index, rather than
applying the temporary lifetime of the indexing operation as a whole to
the index.
2025-09-26 20:14:57 -07:00
Marijn Schouten fc703ec5c8 fix doc comments to be more standard 2025-09-26 09:25:56 +00:00
beepster4096 aa5a21450a ProjectionElem::Subtype -> CastKind::Subtype 2025-09-26 01:25:26 -07:00
Ding Xiang Fei b77de834c0 mark THIR use as candidate for constness check 2025-09-25 01:54:25 +08:00
Ding Xiang Fei a86f140727 do not materialise X in [X; 0] when X is unsizing a const 2025-09-25 01:54:23 +08:00
Camille Gillot 53b91ea87f Remove Rvalue::Len. 2025-09-16 22:23:19 +00:00
bors 154037ffb8 Auto merge of #144783 - folkertdev:loop-match-diverging-loop, r=SparrowLii
fix `#[loop_match]` on diverging loop

tracking issue: https://github.com/rust-lang/rust/issues/132306

fixes https://github.com/rust-lang/rust/issues/144492
fixes https://github.com/rust-lang/rust/issues/144493

fixes https://github.com/rust-lang/rust/issues/144781

this generated invalid MIR before. issue https://github.com/rust-lang/rust/issues/143806 still has an issue where we assign `state = state` which is invalid in MIR. Fixing that problem is tricky, so I'd like to do that separately.

r? `@bjorn3`
2025-09-01 14:42:07 +00:00
Nicholas Nethercote 5ce3797073 Introduce MirDumper and MirWriter.
MIR dumping is a mess. There are lots of functions and entry points,
e.g. `dump_mir`, `dump_mir_with_options`, `dump_polonius_mir`,
`dump_mir_to_writer`. Also, it's crucial that `create_dump_file` is
never called without `dump_enabled` first being checked, but there is no
mechanism for ensuring this and it's hard to tell if it is satisfied on
all paths. (`dump_enabled` is checked twice on some paths, however!)

This commit introduces `MirWriter`, which controls the MIR writing, and
encapsulates the `extra_data` closure and `options`. Two existing
functions are now methods of this type. It sets reasonable defaults,
allowing the removal of many `|_, _| Ok(())` closures.

The commit also introduces `MirDumper`, which is layered on top of
`MirWriter`, and which manages the creation of the dump files,
encapsulating pass names, disambiguators, etc. Four existing functions
are now methods of this type.
- `MirDumper::new` will only succeed if dumps are enabled, and will
  return `None` otherwise, which makes it impossible to dump when you
  shouldn't.
- It also sets reasonable defaults for various things like
  disambiguators, which means you no longer need to specify them in many
  cases. When they do need to be specified, it's now done via setter
  methods.
- It avoids some repetition. E.g. `dump_nll_mir` previously specifed the
  pass name `"nll"` four times and the disambiguator `&0` three times;
  now it specifies them just once, to put them in the `MirDumper`.
- For Polonius, the `extra_data` closure can now be specified earlier,
  which avoids having to pass some arguments through some functions.
2025-09-01 09:19:03 +10:00
Nicholas Nethercote d7faa5630d Avoid unnecessary mut-ness for various closures. 2025-09-01 08:52:34 +10:00
Jacob Pratt cd0972f6f5 Rollup merge of #144780 - bjorn3:mir_build_debug, r=matthewjasper
Add a method to dump MIR in the middle of MIR building

This makes it easier to debug issues with MIR building by inserting dump_for_debugging calls around the suspected code responsible for the bad MIR.
2025-08-21 17:57:50 -04:00
Stuart Cook d92e1fe8d7 Rollup merge of #145206 - scrabsha:push-uxovoqzrxnlx, r=jdonszelmann
Port `#[custom_mir(..)]` to the new attribute system

r? ``````````@jdonszelmann``````````
2025-08-18 15:31:10 +10:00
Sasha Pourcelot 51bccdd1ab Port #[custom_mir(..)] to the new attribute system 2025-08-15 11:19:29 +02:00
Ralf Jung 2330afab63 Apply suggestions from code review
Co-authored-by: Boxy <rust@boxyuwu.dev>
2025-08-14 09:44:22 +02:00
Ralf Jung e2cc7757e1 avoid unnecessary type sanity checks 2025-08-14 09:44:22 +02:00
Ralf Jung dbc030e034 shrink TestBranch::Constant and PatRangeBoundary::Finite 2025-08-14 09:44:22 +02:00
Ralf Jung a171eaab42 use ty::Value instead of manual pairs of types and valtrees 2025-08-14 09:44:22 +02:00
Ralf Jung d61fdbf266 pattern testing: store constants as valtrees 2025-08-14 09:44:19 +02:00
Ralf Jung 3f1e99dca4 PatKind: store constants as valtrees 2025-08-14 09:39:39 +02:00
bjorn3 695473ae67 Also dump coverage info 2025-08-13 14:37:50 +00:00
bjorn3 7b13a509cc Add a method to dump MIR in the middle of MIR building
This makes it easier to debug issues with MIR building by inserting
dump_for_debugging calls around the suspected code responsible for the
bad MIR.
2025-08-13 14:22:50 +00:00
Folkert de Vries 3e76b58453 add place mention for #[loop_match] scrutinee 2025-08-10 14:25:49 +02:00
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