Commit Graph

1689 Commits

Author SHA1 Message Date
Matthias Krüger
1f4aff7d2b
Rollup merge of #122487 - GuillaumeGomez:rename-stmtkind-local, r=oli-obk
Rename `StmtKind::Local` variant into `StmtKind::Let`

It comes from this [discussion](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Improve.20naming.20of.20.60ExprKind.3A.3ALet.60.3F).

Starting point was:

> I often end up looking at [ExprKind::Let](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/enum.ExprKind.html#variant.Let) instead of Local because of the name. I think renaming it (both the `ExprKind` variant and the Let struct) to `LetPattern` or LetPat could improve the situation as I'm not sure I'm not the only one encountering this issue.

And then it evolved into:

> It's already `Expr::Let` instead of `StmtKind::Local`. Counterproposal: rename `StmtKind::Local` to `StmtKind::Let`.

The goal here is to clear this confusion.

r? `@oli-obk`
2024-03-14 20:00:21 +01:00
Matthias Krüger
54a5a49af0
Rollup merge of #122322 - Zalathar:branch, r=oli-obk
coverage: Initial support for branch coverage instrumentation

(This is a review-ready version of the changes that were drafted in #118305.)

This PR adds support for branch coverage instrumentation, gated behind the unstable flag value `-Zcoverage-options=branch`. (Coverage instrumentation must also be enabled with `-Cinstrument-coverage`.)

During THIR-to-MIR lowering (MIR building), if branch coverage is enabled, we collect additional information about branch conditions and their corresponding then/else blocks. We inject special marker statements into those blocks, so that the `InstrumentCoverage` MIR pass can reliably identify them even after the initially-built MIR has been simplified and renumbered.

The rest of the changes are mostly just plumbing needed to gather up the information that was collected during MIR building, and include it in the coverage metadata that we embed in the final binary.

Note that `llvm-cov show` doesn't print branch coverage information in its source views by default; that needs to be explicitly enabled with `--show-branches=count` or similar.

---

The current implementation doesn't have any support for instrumenting `if let` or let-chains. I think it's still useful without that, and adding it would be non-trivial, so I'm happy to leave that for future work.
2024-03-14 20:00:19 +01:00
Guillaume Gomez
a4e0e50a3f Rename hir::StmtKind::Local into hir::StmtKind::Let 2024-03-14 12:42:04 +01:00
Zalathar
c1bec0ce6b coverage: Record branch information during MIR building 2024-03-14 16:31:44 +11:00
Zalathar
f9cdaeb6fd coverage: Data structures for recording branch info during MIR building 2024-03-14 16:27:42 +11:00
Nadrieril
f27540697e Rename RustcMatchCheckCtxt -> RustcPatCtxt 2024-03-13 14:07:44 +01:00
Matthias Krüger
e6ba504029
Rollup merge of #121908 - Nadrieril:dynamic-variant-collection, r=matthewjasper
match lowering: don't collect test alternatives ahead of time

I'm very happy with this one. Before this, when sorting candidates into the possible test branches, we manually computed `usize` indices to determine in which branch each candidate goes. To make this work we had a first pass that collected the possible alternatives we'd have to deal with, and a second pass that actually sorts the candidates.

In this PR, I replace `usize` indices with a dedicated enum. This makes `sort_candidates` easier to follow, and we don't need the first pass anymore.

r? ``@matthewjasper``
2024-03-13 06:41:21 +01:00
Matthias Krüger
1b198ba9fe
Rollup merge of #121820 - Nadrieril:idxpat2, r=compiler-errors
pattern analysis: Store field indices in `DeconstructedPat` to avoid virtual wildcards

For a pattern like `Struct { field3: true, .. }`, in pattern analysis we represent it as `Struct { field1: _, field2: _, field3: true, field4: _ }`. This PR makes it so we store `Struct { field3: true, .. }` instead. This means we never have to create fake `_` patterns during lowering.

r? ``@compiler-errors``
2024-03-13 06:41:20 +01:00
Oli Scherer
9816915954 Change DefKind::Static to a struct variant 2024-03-12 05:53:46 +00:00
Jubilee
e3f9b2f27c
Rollup merge of #122080 - Zalathar:drop-tree, r=oli-obk
Clarity improvements to `DropTree`

These changes are based on some points of confusion I had when initially trying to understand this code.

The only “functional” change is an additional assertion in `<ExitScopes as DropTreeBuilder>::link_entry_point`, checking that the dummy terminator is `TerminatorKind::UnwindResume` as expected.
2024-03-11 09:29:35 -07:00
Nadrieril
d339bdaa07 DeconstructedPat.data is always present now 2024-03-11 04:38:57 +01:00
Nadrieril
6ae9fa31f0 Store field indices in DeconstructedPat to avoid virtual wildcards 2024-03-11 04:37:21 +01:00
Nicholas Nethercote
541d7cc65c Rename AddToDiagnostic as Subdiagnostic.
To match `derive(Subdiagnostic)`.

Also rename `add_to_diagnostic{,_with}` as `add_to_diag{,_with}`.
2024-03-11 10:04:49 +11:00
Nicholas Nethercote
7a294e998b Rename IntoDiagnostic as Diagnostic.
To match `derive(Diagnostic)`.

Also rename `into_diagnostic` as `into_diag`.
2024-03-11 09:15:09 +11:00
Matthias Krüger
163573a368
Rollup merge of #122221 - Nadrieril:patextradata, r=oli-obk
match lowering: define a convenient struct

Small refactor PR: `bindings` and `ascriptions` always come together so I made a struct for them. I'll have one or two fields to add to it in a later PR as well.
2024-03-10 10:58:18 +01:00
Guillaume Boisseau
bc3bc2ba6b
Rollup merge of #121584 - klensy:itertools-up, r=Mark-Simulacrum
bump itertools to 0.12

still depend on 0.11 (temporary dupes version):
* <del>clippy</del>, https://github.com/rust-lang/rust-clippy/pull/12346
* rustfmt, sigh, https://github.com/rust-lang/rustfmt/pull/6093

https://github.com/rust-itertools/itertools/blob/v0.12.1/CHANGELOG.md

removed unused `derive_more` dep from `rustc_middle`
2024-03-09 21:40:08 +01:00
Nadrieril
594cf1de61 review 2024-03-09 15:28:26 +01:00
Nadrieril
7843e46f17 Factor out non-branch-related pattern data 2024-03-09 03:51:30 +01:00
klensy
52501c2a75 bump itertools to 0.12
still depend on 0.11:
* clippy
* rustfmt, sigh
2024-03-08 12:34:05 +03:00
Matthias Krüger
d774fbea7c
Rollup merge of #119365 - nbdd0121:asm-goto, r=Amanieu
Add asm goto support to `asm!`

Tracking issue: #119364

This PR implements asm-goto support, using the syntax described in "future possibilities" section of [RFC2873](https://rust-lang.github.io/rfcs/2873-inline-asm.html#asm-goto).

Currently I have only implemented the `label` part, not the `fallthrough` part (i.e. fallthrough is implicit). This doesn't reduce the expressive though, since you can use label-break to get arbitrary control flow or simply set a value and rely on jump threading optimisation to get the desired control flow. I can add that later if deemed necessary.

r? ``@Amanieu``
cc ``@ojeda``
2024-03-08 08:19:17 +01:00
Zalathar
570376c496 Don't pass a break scope to Builder::break_for_else
This method would previously take a target scope, and then verify that it
was equal to the scope on top of the if-then scope stack.

In practice, this means that callers have to go out of their way to pass around
redundant scope information that's already on the if-then stack.

So it's easier to just retrieve the correct scope directly from the if-then
stack, and simplify the other code that was passing it around.
2024-03-07 23:11:18 +11:00
Zalathar
5ba70bd3ec Replace tuples in DropTree with named structs
This allows us to use real field names instead of tuple element numbers.

Renaming `previous_drops` to `existing_drops_map` clarifies that "previous" was
unrelated to drop order.
2024-03-06 23:09:04 +11:00
Zalathar
fbdac30427 Rename DropTree::add_entry to add_entry_point
This clarifies that we're adding an "entry point", not just adding an "entry"
of some kind.
2024-03-06 23:02:38 +11:00
Zalathar
3bd8df96e1 Assert that link_entry_point sees the expected dummy terminator 2024-03-06 22:32:01 +11:00
Zalathar
30fa6a8b50 Rename DropTreeBuilder::add_entry to link_entry_point 2024-03-06 22:32:01 +11:00
Zalathar
250e697834 Additional comments for lowering if 2024-03-06 17:08:28 +11:00
Zalathar
3402f39bcb Clarify lowering the else arm into the else block 2024-03-06 17:08:28 +11:00
Zalathar
7396fd1fa0 Clarify how lowering if produces then/else blocks
This makes it easier to see that the call to `in_scope` returns both the then
block and the else block. The rather confusing `unpack!` step is confined to
its own separate line.

(This patch reindents several lines, so using "ignore whitespace" is
recommended in order to focus on the actual changes.)
2024-03-06 14:57:00 +11:00
Nicholas Nethercote
573267cf3c Rename SubdiagnosticMessageOp as SubdiagMessageOp. 2024-03-05 12:14:49 +11:00
Matthias Krüger
58a0f64c8b
Rollup merge of #121928 - Zalathar:then-else-args, r=Nadrieril
Extract an arguments struct for `Builder::then_else_break`

Most of this method's arguments are usually or always forwarded as-is to recursive invocations.

Wrapping them in a dedicated struct allows us to document each struct field, and lets us use struct-update syntax to indicate which arguments are being modified when making a recursive call.

---

While trying to understand the lowering of `if` expressions, I found it difficult to keep track of the half-dozen arguments passed through to every call to `then_else_break`. I tried switching over to an arguments struct, and I found that it really helps to make sense of what each argument does, and how each call is modifying the arguments.

I have some further ideas for how to streamline these recursive calls, but I've kept those out of this PR so that it's a pure refactoring with no behavioural changes.
2024-03-04 22:16:32 +01:00
Zalathar
4146136d6d Extract an arguments struct for Builder::then_else_break
Most of this method's arguments are usually or always forwarded as-is to
recursive invocations.

Wrapping them in a dedicated struct allows us to document each struct field,
and lets us use struct-update syntax to indicate which arguments are being
modified when making a recursive call.
2024-03-04 18:42:12 +11:00
Guillaume Gomez
7d8f74f8b2
Rollup merge of #121917 - GuillaumeGomez:pattern-complexity_limit.rs, r=Nadrieril
Add new `pattern_complexity` attribute to add possibility to limit and check recursion in pattern matching

Needed for https://github.com/rust-lang/rust-analyzer/issues/9528.

This PR adds a new attribute only available when running rust testsuite called `pattern_complexity` which allows to set the maximum recursion for the pattern matching. It is quite useful to ensure the complexity doesn't grow, like in `tests/ui/pattern/usefulness/issue-118437-exponential-time-on-diagonal-match.rs`.

r? `@Nadrieril`
2024-03-03 14:07:43 +01:00
Guillaume Gomez
be31b6b6cd Add new pattern_complexity attribute to add possibility to limit and check recursion in pattern matching 2024-03-03 13:10:15 +01:00
bors
0decdac390 Auto merge of #121914 - Nadrieril:rollup-ol98ncg, r=Nadrieril
Rollup of 5 pull requests

Successful merges:

 - #120761 (Add initial support for DataFlowSanitizer)
 - #121622 (Preserve same vtable pointer when cloning raw waker, to fix Waker::will_wake)
 - #121716 (match lowering: Lower bindings in a predictable order)
 - #121731 (Now that inlining, mir validation and const eval all use reveal-all, we won't be constraining hidden types here anymore)
 - #121841 (`f16` and `f128` step 2: intrinsics)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-03-02 22:59:19 +00:00
Guillaume Boisseau
30976fbe2b
Rollup merge of #121716 - Nadrieril:simple-binding-order, r=matthewjasper
match lowering: Lower bindings in a predictable order

After the recent refactorings, we can now lower bindings in a truly predictable order. The order in https://github.com/rust-lang/rust/pull/120214 was an improvement but not very clear. With this PR, we lower bindings from left to right, with the special case that `x @ pat` is traversed as `pat @ x` (i.e. `x` is lowered after any bindings in `pat`).

This description only applies in the absence of or-patterns. Or-patterns make everything complicated, because the binding place depends on the subpattern. Until I have a better idea I leave them to be handled in whatever weird order arises from today's code.

r? `@matthewjasper`
2024-03-02 20:13:23 +01:00
Nadrieril
d46ff6415c Fix a subtle regression
Before, the SwitchInt cases were computed in two passes: if the first
pass accepted e.g. 0..=5 and then 1, the second pass would not accept
0..=5 anymore because 1 would be listed in the SwitchInt options.

Now there's a single pass, so if we sort 0..=5 we must take care to not
sort a subsequent 1.
2024-03-02 18:38:37 +01:00
Nadrieril
edea739292 No need to collect test variants ahead of time 2024-03-02 18:35:53 +01:00
Nadrieril
8c3688cbb5 Allocate candidate vectors as we sort them 2024-03-02 18:33:19 +01:00
Nadrieril
3d3b321c60 Use an enum instead of manually tracking indices for target_blocks 2024-03-02 18:33:17 +01:00
Nadrieril
832b23ffcf Tiny missed simplification 2024-03-02 18:06:12 +01:00
Matthias Krüger
7bacfced95
Rollup merge of #121892 - Zalathar:expr-kind-let, r=Nadrieril
The ordinary lowering of `thir::ExprKind::Let` is unreachable

After desugaring, `let` expressions should only appear inside `if` expressions or `match` guards, possibly nested within a let-chain. In both cases they are specifically handled by the lowerings of those expressions, so this case is currently unreachable.

---

Context: https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Lowering.20of.20.60thir.3A.3AExprKind.3A.3ALet.60.20is.20unreachable

My conclusions are partly based on the observation that stubbing out this match arm doesn't cause any test failures. So either this really is unreachable, or it can be reached in some obscure circumstances that our test suite doesn't cover.

If we end up needing this code (or something like it) for an implementation of https://github.com/rust-lang/rfcs/pull/3573, it should be easy enough to pull it back out of version control history.

I looked into having the `if`/`match` lowerings call back into `expr_into_dest`, but from what I can tell that won't work well, because there are extra scoping considerations that require some awareness of the enclosing if/match.

r? ```@Nadrieril```
2024-03-02 16:53:16 +01:00
Zalathar
972d8daf47 The ordinary lowering of thir::ExprKind::Let is unreachable
After desugaring, `let` expressions should only appear inside `if` expressions
or `match` guards, possibly nested within a let-chain. In both cases they are
specifically handled by the lowerings of those expressions, so this case is
currently unreachable.
2024-03-02 20:49:29 +11:00
Matthias Krüger
b2c933a1b9
Rollup merge of #121715 - Nadrieril:testcase-or, r=matthewjasper
match lowering: pre-simplify or-patterns too

This is the final part of my work to simplify match pairs early: now we do it for or-patterns too. This makes it possible to collect fake borrows separately from the main match lowering algorithm. That'll enable more simplifications of or-pattern handling.

Note: I was tempted to have `Candidate` contain a `FlatPat`, but there are so many places that use `candidate.match_pairs` etc directly that I chose not to.

r? `@matthewjasper`
2024-03-02 10:09:35 +01:00
Matthias Krüger
0d2205f9a6
Rollup merge of #121750 - Nadrieril:switchkind-if, r=matthewjasper
match lowering: Separate the `bool` case from other integers in `TestKind`

`TestKind::SwitchInt` had a special case for `bool` essentially everywhere it's used, so I made `TestKind::If` to handle the bool case on its own.

r? `@matthewjasper`
2024-03-01 22:38:49 +01:00
Matthias Krüger
1fbc53af48
Rollup merge of #121706 - Nadrieril:simplify-sort-candidate, r=matthewjasper
match lowering: Remove hacky branch in sort_candidate

Reusing `self.test()` there wasn't actually pulling a lot of weight. In particular the `TestKind::Len` cases were all already correctly handled.

r? `@matthewjasper`
2024-03-01 22:38:48 +01:00
Matthias Krüger
1a4c93e3ed
Rollup merge of #121784 - Zalathar:if-or-converge, r=Nadrieril
Make the success arms of `if lhs || rhs` meet up in a separate block

Extracted from #118305, where this is necessary to avoid introducing a bug when injecting marker statements into the then/else arms.

---

In the previous code (#111752), the success block of `lhs` would jump directly to the success block of `rhs`. However, `rhs_success_block` could already contain statements that are specific to the RHS, and the direct goto causes them to be executed in the LHS success path as well.

This patch therefore creates a fresh block that the LHS and RHS success blocks can both jump to.

---

I think the reason we currently get away with this is that `rhs_success_block` usually doesn't contain anything other than StorageDead statements for locals used by the RHS, and those statements don't seem to cause problems in the LHS success path (which never makes those locals live).

But if we start adding meaningful statements for branch coverage (or MC/DC coverage), it's important to keep the LHS and RHS blocks separate.
2024-03-01 17:51:30 +01:00
bors
6cbf0926d5 Auto merge of #121728 - tgross35:f16-f128-step1-ty-updates, r=compiler-errors
Add stubs in IR and ABI for `f16` and `f128`

This is the very first step toward the changes in https://github.com/rust-lang/rust/pull/114607 and the [`f16` and `f128` RFC](https://rust-lang.github.io/rfcs/3453-f16-and-f128.html). It adds the types to `rustc_type_ir::FloatTy` and `rustc_abi::Primitive`, and just propagates those out as `unimplemented!` stubs where necessary.

These types do not parse yet so there is no feature gate, and it should be okay to use `unimplemented!`.

The next steps will probably be AST support with parsing and the feature gate.

r? `@compiler-errors`
cc `@Nilstrieb` suggested breaking the PR up in https://github.com/rust-lang/rust/pull/120645#issuecomment-1925900572
2024-03-01 03:36:11 +00:00
Guillaume Gomez
2e0a26a32a
Rollup merge of #121376 - Nadrieril:mir-half-ranges, r=pnkfelix
Skip unnecessary comparison with half-open range patterns

This is the last remaining detail in the implementation of half-open range patterns. Until now, a half-open range pattern like `10..` was converted to `10..T::MAX` before lowering to MIR, which generated an extra pointless comparison. With this PR we don't generate it.
2024-02-29 17:08:37 +01:00
Zalathar
a7832b14b1 Make the success arms of if lhs || rhs meet up in a separate block
In the previous code, the success block of `lhs` would jump directly to the
success block of `rhs`. However, `rhs_success_block` could already contain
statements that are specific to the RHS, and the direct goto causes them to be
executed in the LHS success path as well.

This patch therefore creates a fresh block that the LHS and RHS success blocks
can both jump to.
2024-02-29 20:24:43 +11:00
Trevor Gross
e3f63d9375 Add f16 and f128 to rustc_type_ir::FloatTy and rustc_abi::Primitive
Make changes necessary to support these types in the compiler.
2024-02-28 12:58:32 -05:00