Commit Graph

2227 Commits

Author SHA1 Message Date
Matthias Krüger
dbdfa7914c
Rollup merge of #134980 - lqd:polonius-next-episode-7, r=jackh726
Location-sensitive polonius prototype: endgame

This PR sets up the naive location-sensitive analysis end-to-end, and replaces the location-insensitive analysis. It's roughly all the in-progress work I wanted to land for the prototype, modulo cleanups I still want to do after the holidays, or the polonius debugger, and so on.

Here, we traverse the localized constraint graph, have to deal with kills and time-traveling (👌), and record that as loan liveness for the existing scope and active loans computations.

Then the near future looks like this, especially if the 2025h1 project goal is accepted:
- gradually bringing it up to completion
- analyzing and fixing the few remaining test failures
- going over the *numerous* fixmes in this prototype (one of which is similar to a hang on one test's millions and millions of constraints)
- trying to see how to lower the impact of the lack of NLL liveness optimization on diagnostics, and their categorization of local variables and temporaries (the vast majority of blessed expectations differences), as well as the couple ICEs trying to find an NLL constraint to blame for errors.
- dealing with the theoretical weakness around kills, conflating reachability for the two TCS, etc that is described ad nauseam in the code.
- switching the compare mode to the in-tree implementation, and blessing the diagnostics
- apart from the hang, it's not catastrophically slower on our test suite, so then we can try to enable it on CI
- checking crater, maybe trying to make it faster :3, etc.

I've tried to gradually introduce this PR's work over 4 commits, because it's kind of subtle/annoying, and Niko/I are not completely convinced yet. That one comment explaining the situation is maybe 30% of the PR 😓. Who knew that spacetime reachability and time-traveling could be mind bending.

I kinda found this late and the impact on this part of the computation was a bit unexpected to us. A bit more care/thought will be needed here. I've described my plan in the comments though. In any case, I believe we have the current implementation is a conservative approximation that shouldn't result in unsoundness but false positives at worst. So it feels fine for now.

r? ``@jackh726``

---

Fixes #127628 -- which was a assertion triggered for a difference in loan computation between NLLs and the location-insensitive analysis. That doesn't exist anymore so I've removed this crash test.
2025-01-17 09:11:17 +01:00
Luca Versari
8fee6a7739 Coerce safe-to-call target_feature functions to fn pointers. 2025-01-16 10:54:33 +01:00
Jubilee
aa8bc2563e
Rollup merge of #134940 - compiler-errors:scrape, r=lcnr
Make sure to scrape region constraints from deeply normalizing type outlives assumptions in borrowck

Otherwise we're just randomly registering these region relations into the infcx which isn't good

r? lcnr
2025-01-14 19:56:30 -08:00
Michael Goulet
5775190dba Make sure to scrape region constraints from deeply normalizing type outlives assumptions in borrowck 2025-01-14 19:13:18 +00:00
lcnr
99657aa338 mir borrowck: cleanup late-bound region handling 2025-01-14 14:16:12 +01:00
bors
35c2908177 Auto merge of #135465 - jhpratt:rollup-7p93bct, r=jhpratt
Rollup of 10 pull requests

Successful merges:

 - #134498 (Fix cycle error only occurring with -Zdump-mir)
 - #134977 (Detect `mut arg: &Ty` meant to be `arg: &mut Ty` and provide structured suggestion)
 - #135390 (Re-added regression test for #122638)
 - #135393 (uefi: helpers: Introduce OwnedDevicePath)
 - #135440 (rm unnecessary `OpaqueTypeDecl` wrapper)
 - #135441 (Make sure to mark `IMPL_TRAIT_REDUNDANT_CAPTURES` as `Allow` in edition 2024)
 - #135444 (Update books)
 - #135450 (Fix emscripten-wasm-eh with unwind=abort)
 - #135452 (bootstrap: fix outdated feature name in comment)
 - #135454 (llvm: Allow sized-word rather than ymmword in tests)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-01-14 03:08:59 +00:00
lcnr
87f03a4238 rm unnecessary OpaqueTypeDecl wrapper 2025-01-13 14:33:18 +01:00
Michael Goulet
9bf9f5db9b Assert that Instance::try_resolve is only used on body-like things 2025-01-13 02:20:08 +00:00
bors
7bb9888953 Auto merge of #135402 - matthiaskrgr:rollup-cz7hs13, r=matthiaskrgr
Rollup of 6 pull requests

Successful merges:

 - #129259 (Add inherent versions of MaybeUninit methods for slices)
 - #135374 (Suggest typo fix when trait path expression is typo'ed)
 - #135377 (Make MIR cleanup for functions with impossible predicates into a real MIR pass)
 - #135378 (Remove a bunch of diagnostic stashing that doesn't do anything)
 - #135397 (compiletest: add erroneous variant to `string_enum`s conversions error)
 - #135398 (add more crash tests)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-01-12 14:43:10 +00:00
Matthias Krüger
b53239668a
Rollup merge of #135378 - compiler-errors:unnecessary-stashing, r=chenyukang
Remove a bunch of diagnostic stashing that doesn't do anything

#121669 removed a bunch of conditional diagnostic stashing/canceling, but left around the `steal` calls which just emitted the error eagerly instead of canceling the diagnostic. I think that these no-op `steal` calls don't do much and are confusing to encounter, so let's remove them.

The net effect is:
1. We emit more duplicated errors, since stashing has the side effect of duplicating diagnostics. This is not a big deal, since outside of `-Zdeduplicate-diagnostics=no`, the errors are already being deduplicated by the compiler.
2. It changes the order of diagnostics, since we're no longer stashing and then later stealing the errors. I don't think this matters much for the changes that the UI test suite manifests, and it makes these errors less order dependent.
2025-01-12 12:07:58 +01:00
Matthias Krüger
fcf81b8cc3
Rollup merge of #135364 - yotamofek:borrowck-diag-fix, r=compiler-errors
Cleanup `suggest_binding_for_closure_capture_self` diag in borrowck

Mostly grammar fix/improvement, but also a small cleanup to use iterators instead of for loops for collecting into a vector.
2025-01-12 09:14:12 +01:00
Rémy Rakic
8ac045dd4c move out of scope precomputer code
this addresses review comments while:
- keeping the symmetry between the NLL and Polonius out of scope
  precomputers
- keeping the unstable `calculate_borrows_out_of_scope_at_location`
  function to avoid churn for consumers
2025-01-12 07:39:20 +00:00
Rémy Rakic
0f3dd33e1d deal with naive reachability weakness
it's a bit mind-bending
2025-01-12 07:29:03 +00:00
Rémy Rakic
550cf1f4a4 handle kills in reachability 2025-01-12 07:29:03 +00:00
Rémy Rakic
67a1bb1554 replace location-insensitive analysis with location-sensitive analysis
we're in in the endgame now

set up the location-sensitive analysis end to end:
- stop recording inflowing loans and loan liveness in liveness
- replace location-insensitive liveness data with live loans computed by
  reachability
- remove equivalence between polonius scopes and NLL scopes, and only
  run one scope computation
2025-01-12 07:29:03 +00:00
Rémy Rakic
0c978bc4e6 introduce reachability to the constraint graph 2025-01-12 07:29:03 +00:00
Rémy Rakic
5055864071 disable NLL liveness optimization when using polonius
in NLLs some locals are marked live at all points if one of their
regions escapes the function but that doesn't work in a flow-sensitive
setting like polonius
2025-01-12 07:29:03 +00:00
Michael Goulet
85c9ce6d79 Remove a bunch of diagnostic stashing that doesn't do anything 2025-01-11 19:22:06 +00:00
Yotam Ofek
27b0693464 collect diag suggestions instead of pushing into vector repeatedly 2025-01-11 13:21:15 +00:00
Yotam Ofek
6680bc5554 improve clunky grammar in borrowck diagnostic 2025-01-11 13:20:17 +00:00
Rémy Rakic
a13354bea0 rename BitSet to DenseBitSet
This should make it clearer that this bitset is dense, with the
advantages and disadvantages that it entails.
2025-01-11 11:34:01 +00:00
dianne
72945beedd Remove special-casing for argument patterns in MIR typeck 2025-01-08 17:59:27 -08:00
bors
e26ff2f908 Auto merge of #135260 - matthiaskrgr:rollup-8irqs72, r=matthiaskrgr
Rollup of 6 pull requests

Successful merges:

 - #134228 (Exhaustively handle expressions in patterns)
 - #135194 (triagebot: mark tidy changes with a more specific `A-tidy` label)
 - #135222 (Ensure that we don't try to access fields on a non-struct pattern type)
 - #135250 (A couple simple borrowck cleanups)
 - #135252 (Fix release notes link)
 - #135253 (Revert #131365)

Failed merges:

 - #135195 (Make `lit_to_mir_constant` and `lit_to_const` infallible)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-01-08 21:31:51 +00:00
Matthias Krüger
9ea76e44e5
Rollup merge of #135250 - lqd:simple-cleanups, r=matthewjasper
A couple simple borrowck cleanups

This PR has a couple simple renamings:
- it's been a long time since the mapping from `Location`s to `PointIndex`es was extracted from `RegionElements` into the `DenseLocationMap`, but only the types were renamed at the time. borrowck still refers to this map as `elements`. That's confusing, especially since sometimes we also use the mapping via `LivenessValues`, and makes more sense as `location_map` instead.
- to clarify `LocationTable` is not as general as it sounds, and is only for datalog polonius. In this branch I didn't rename the handful of `location_table` fields and params to `polonius_table`, but can do that to differentiate it even more from `location_map`. I did try it locally and it looks worthwhile, so if you'd prefer I can also push it here. (Or we could even switch these datalog types and fields to even more explicit names)
- to clarify the incomprehensible `AllFacts`, it is renamed to `PoloniusFacts`. These can be referred to as `facts` within the legacy polonius module, but as `polonius_facts` outside of it to make it clear that they're not about NLLs (nor are they about in-tree polonius but that'll be magically fixed when they're removed in the future)

r? `@matthewjasper`
2025-01-08 18:21:02 +01:00
Michael Goulet
c55eefe8bc Try to explain borrow for tail expr temporary drop order change in 2024 2025-01-08 16:02:44 +00:00
Michael Goulet
4a099b29cd Don't do AccessDepth::Drop for types with no drop impl 2025-01-08 15:58:10 +00:00
wieDasDing
13c7122df8 remove an extraneous comment
Co-authored-by: Rémy Rakic <remy.rakic+github@gmail.com>
2025-01-08 15:58:09 +00:00
Ding Xiang Fei
34edb21f0b apply suggestions on fn name 2025-01-08 15:58:09 +00:00
Ding Xiang Fei
045271cccc run borrowck tests on BIDs and emit tail-expr-drop-order lints for
potential violations
2025-01-08 15:58:09 +00:00
Rémy Rakic
36ea00c20d rename AllFacts to PoloniusFacts
This is another strangely named struct (and associated fields) that is
hard to see was related to datalog polonius.
2025-01-08 13:23:54 +00:00
Rémy Rakic
3a1a621115 rename LocationTable to PoloniusLocationTable
Its original naming hides the fact that it's related to datalog
polonius, and bound to be deleted in the near future.

It also conflicts with the expected name for the actual NLL location
map, and prefixing it with its use will make the differentiation
possible.
2025-01-08 13:08:25 +00:00
bors
6afee111c2 Auto merge of #133858 - dianne:better-blame-constraints-for-static, r=lcnr
`best_blame_constraint`: Blame better constraints when the region graph has cycles from invariance or `'static`

This fixes #132749 by changing which constraint is blamed for region errors in several cases. `best_blame_constraint` had a heuristic that tried to pinpoint the constraint causing an error by filtering out any constraints where the outliving region is unified with the ultimate target region being outlived. However, it used the SCCs of the region graph to do this, which is unreliable; in particular, if the target region is `'static`, or if there are cycles from the presence of invariant types, it was skipping over the constraints it should be blaming. As is the case in that issue, this could lead to confusing diagnostics. The simplest fix seems to work decently, judging by test stderr: this makes `best_blame_constraint` no longer filter constraints by their outliving region's SCC.

There are admittedly some quirks in the test output. In many cases, subdiagnostics that depend on the particular constraint being blamed have either started or stopped being emitted. After starting at this for quite a while, I think anything too fickle about whether it outputs based on the particular constraint being blamed should instead be looking at the constraint path as a whole, similar to what's done for [the placeholder-from-predicate note](https://github.com/rust-lang/rust/compare/master...dianne:rust:better-blame-constraints-for-static#diff-3c0de6462469af483c9ecdf2c4b00cb26192218ef2d5c62a0fde75107a74caaeR506).

Very many tests involving invariant types gained a note pointing out the types' invariance, but in a few cases it was lost. A particularly illustrative example is [tests/ui/lifetimes/copy_modulo_regions.stderr](https://github.com/rust-lang/rust/compare/master...dianne:rust:better-blame-constraints-for-static?expand=1#diff-96e1f8b29789b3c4ce2f77a5e0fba248829b97ef9d1ce39e7d2b4aa57b2cf4f0); I'd argue the new constraint is a better one to blame, but it lacks the variance diagnostic information that's elsewhere in the constraint path. If desired, I can try making that note check the whole path rather than just the blamed constraint.

The subdiagnostic [`BorrowExplanation::add_object_lifetime_default_note`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_borrowck/diagnostics/explain_borrow/enum.BorrowExplanation.html#method.add_object_lifetime_default_note) depends on a `Cast` being blamed, so [a special case](364ca7f99c) was necessary to keep it from disappearing from tests specifically testing for it. However, see the FIXME comment in that commit; I think the special case should be removed once that subdiagnostic works properly, but it's nontrivial enough to warrant a separate PR. Incidentally, this removes the note from a test where it was being added erroneously: in [tests/ui/borrowck/two-phase-surprise-no-conflict.stderr](https://github.com/rust-lang/rust/compare/master...dianne:rust:better-blame-constraints-for-static?expand=1#diff-8cf085af8203677de6575a45458c9e6b03412a927df879412adec7e4f7ff5e14), the object lifetime is explicitly provided and it's not `'static`.
2025-01-08 12:37:54 +00:00
Rémy Rakic
6f1c4177e7 stop calling DenseLocationMap "elements"
"Elements" are `RegionElement`s. The dense location mapping was removed
from the element containers a while ago but didn't rename its use-sites.
Most of the old naming only used the mapping, and are better named
`location_map`.
2025-01-08 11:46:18 +00:00
Jacob Pratt
808c8f84c3
Rollup merge of #134920 - lqd:polonius-next-episode-6, r=jackh726
Convert typeck constraints in location-sensitive polonius

In this PR, we do a big chunk of the work of localizing regular outlives constraints.

The slightly annoying thing is handling effectful statements: usually the subset graph propagates loans at a single point between regions, and liveness propagates loans between points within a single region, but some statements have effects applied on exit.

This was also a problem before, in datalog polonius terms and Niko's solution at the time, this is about: the mid-point. The idea was to duplicate all MIR locations into two physical points, and orchestrate the effects with that. Somewhat easier to do, but double the CFG.

We've always believed we didn't _need_ midpoints in principle, as we can represent changes on exit as on happening entry to the successor, but there's some difficulty in tracking the position information at sufficient granularity through outlives relation (especially since we also have bidirectional edges and time-traveling now).

Now, that is surely what we should be doing in the future. In the mean time, I infer this from the kind of statement/terminator where an outlives constraint arose. It's not particularly complicated but some explanation will help clarify the code.

Assignments (in their various forms) are the quintessential example of these crossover cases: loans that would flow into the LHS would not be visible on entry to the point but on exit -- so we'll localize these edges to the successor. Let's look at a real-world example, involving invariance for bidirectional edges:

```rust
let mut _1: HashMap<i32, &'7 i32>;
let mut _3: &'9 mut HashMap<i32, &'10 i32>;
...
/* at bb1[3]: */ _3 = &'3 mut _1;
```

Here, typeck expectedly produces 3 outlives constraints today:
1. `'3 -> '9`
2. `'7 -> '10`
3. `'10 -> '7`

And we localize them like so,

1. `'3 -> '9` flows into the LHS and becomes: `3_bb1_3 -> 9_bb1_4`
2. `'7 -> '10` flows into the LHS and becomes: `7_bb1_3 -> 10_bb1_4`
3. `'10 -> '7` flows from the LHS and becomes: `10_bb1_4 -> 7_bb1_3` (time traveling 👌)

---

r? ``@jackh726``

To keep you entertained during the holidays I also threw in a couple of small changes removing cruft in the borrow checker.

We're actually getting there. The next PR will be the last one needed to get end-to-end tests working.
2025-01-08 00:52:46 -05:00
Matthias Krüger
191fb23940
Rollup merge of #133810 - lcnr:remove-verify_bound, r=compiler-errors
remove unnecessary `eval_verify_bound`

This does not impact any tests. I feel like any cases where this could useful should instead be fixed by a general improvement to `eval_verify_bound` to avoid having to promote this `TypeTest` in the first place 🤔

r? types cc ``@nikomatsakis``
2025-01-07 21:39:38 +01:00
Josh Triplett
bb6bbfa13f Avoid naming variables str
This renames variables named `str` to other names, to make sure `str`
always refers to a type.

It's confusing to read code where `str` (or another standard type name)
is used as an identifier. It also produces misleading syntax
highlighting.
2025-01-07 14:30:02 +02:00
dianne
fe8b12f8cf only avoid blaming assignments from argument patterns 2025-01-06 16:12:11 -08:00
dianne
1b2281a493 point out unblamed constraints from Copy/Sized bounds in region errors 2025-01-06 16:12:11 -08:00
dianne
2c5815b285 make outlives constraints from pointer comparisons less boring 2025-01-06 16:12:11 -08:00
dianne
10061b3a4f make outlives constraints from generic arguments less boring 2025-01-06 16:12:11 -08:00
dianne
6421d4cf80 best_blame_constraint: prioritize blaming interesting-seeming constraints 2025-01-06 16:12:11 -08:00
dianne
45b2ae935d remove the unused ConstraintCategory::ClosureBounds 2025-01-06 16:12:11 -08:00
dianne
50222dba2e best_blame_constraint: avoid blaming assignments without user-provided types 2025-01-06 16:12:11 -08:00
dianne
31e4d8175a best_blame_constraint: avoid blaming constraints from MIR generated by desugaring 2025-01-06 16:12:11 -08:00
dianne
2864906fce best_blame_constraint: add a special case to recover object lifetime default notes 2025-01-06 16:12:04 -08:00
dianne
ac922245f0 best_blame_constraint: don't filter constraints by sup SCC
The SCCs of the region graph are not a reliable heuristic to use for blaming an interesting
constraint for diagnostics. For region errors, if the outlived region is `'static`, or the involved
types are invariant in their lifetiems, there will be cycles in the constraint graph containing both
the target region and the most interesting constraints to blame. To get better diagnostics in these
cases, this commit removes that heuristic.
2025-01-06 16:08:29 -08:00
dianne
2249232ad8 further clean up best_blame_constraint
This gets rid of `categorized_path`, as it was redundant given the `OutlivesConstraint`s in `path`
already have a category field.
2025-01-06 15:53:33 -08:00
dianne
aa8d5bff4c cleanup: remove ExtraConstraintInfo
`ExtraConstraintInfo` was used only for a single subdiagnostic, so this moves the logic for that
to its own function and eliminates the indirection. In order to do so cleanly, this also changes
the arguments to `BorrowExplanation::add_explanation_to_diagnostic`, which happens to simplify its
call sites.
2025-01-06 15:53:30 -08:00
Rémy Rakic
fc7ee238d1 address review comments
push constraint creation to where the statement/terminator info is gathered
2025-01-06 14:20:54 +00:00
Michael Goulet
339902908e Remove CallKind::Deref hack from UseSpans
It's not really necessary
2025-01-06 03:55:19 +00:00
Michael Goulet
3560a2b399 Improve span when temporary receiver is dropped in edition 2024 2025-01-06 03:14:04 +00:00
Ralf Jung
be65012aa3 turn hir::ItemKind::Fn into a named-field variant 2025-01-04 11:35:31 +01:00
Rémy Rakic
9d444c26c9 remove borrowck duplicate of std::ops::ControlFlow 2025-01-01 12:13:33 +00:00
Rémy Rakic
79d761d93c remove allow_two_phase_borrow
it's been simplified over the years, but now it's no longer useful.

- document its replacement in `BorrowKind`
- use that everywhere instead
2025-01-01 12:13:33 +00:00
Rémy Rakic
ff1aaa52ff remove empty util module 2025-01-01 12:13:33 +00:00
Rémy Rakic
56e7575ddd move find_assignments to its only use site
this is to remove the entire `util` module
2025-01-01 12:13:33 +00:00
Rémy Rakic
46154b2253 localize typeck constraints
it's still partially a skeleton, but works well enough for almost all tests to pass
2025-01-01 12:13:32 +00:00
Rémy Rakic
eb7da16408 move typeck constraints conversion to its own module 2025-01-01 12:13:32 +00:00
Stuart Cook
f91bfd97bf
Rollup merge of #134945 - compiler-errors:map-mutate-nits, r=estebank
Some small nits to the borrowck suggestions for mutating a map through index

1. Suggesting users to either use `.insert` or `.get_mut` (which do totally different things) can be a bit of a footgun, so let's make that a bit more nuanced.
2. I find the suggestion of `.get_mut(|val| { *val = whatever; })` to be a bit awkward. I changed this to be an if-let instead.
3. Fix a bug which was suppressing the structured suggestion for some mutations via the index operator on `HashMap`/`BTreeMap`.

r? estebank or reassign
2025-01-01 16:35:31 +11:00
Trevor Gross
3d3d898a2e
Rollup merge of #133486 - dianne:fix-move-error-suggestion, r=estebank
borrowck diagnostics: make `add_move_error_suggestions` use the HIR rather than `SourceMap`

This PR aims to fix #132806 by rewriting `add_move_error_suggestions`[^1]. Previously, it manually scanned the source text to find a leading `&`, which isn't always going to produce a correct result (see: that issue). Admittedly, the HIR visitor in this PR introduces a lot of boilerplate, but hopefully the logic at its core isn't too complicated (I go over it in the comments). I also tried a simpler version that didn't use a HIR visitor and suggested adding `ref` always, but the `&ref x` suggestions really didn't look good. As a bonus for the added complexity though, it's now able to produce nice `&`-removing suggestions in more cases.

I tried to do this such that it avoids edition-dependent checks and its suggestions can be applied together with those from the match ergonomics 2024 migration lint. I haven't added tests for that since the details of match ergonomics 2024 are still being sorted out, but I can try if desired once that's finalized.

[^1]: In brief, it fires on patterns where users try to bind by-value in such a way that moves out of a reference to a non-Copy type (including slice references with non-copy elements). The suggestions are to change the binding's mode to be by-reference, either by removing[^2] an enclosing `&`/`&mut` or adding `ref` to the binding.

[^2]: Incidentally, I find the terminology of "consider removing the borrow" a bit confusing for a suggestion to remove a `&` pattern in order to make bindings borrow rather than move. I'm not sure what a good, concise way to explain that would be though, and that should go in a separate PR anyway.
2024-12-31 18:42:23 -05:00
Michael Goulet
f28e13b055 Fix span for IndexMut method call on HashMap/BTreeMap 2024-12-31 02:21:17 +00:00
Michael Goulet
6a3474e653 Use if-let in structured suggestion instead of Option::map 2024-12-31 02:21:17 +00:00
Michael Goulet
b994124778 Explain how to mutate a HashMap/BTreeMap with more nuance 2024-12-31 01:20:53 +00:00
Rémy Rakic
099b80923b rename diags field 2024-12-30 06:51:16 +00:00
Rémy Rakic
8c86e52ed7 clean up BorrowckDiags
- rename it to what it does, buffer diagnostics
- remove single-use functions
- use derives
2024-12-30 06:51:16 +00:00
Rémy Rakic
fbefa2e267 merge diags module into diagnostics
it's a more natural place for diagnostics-related structures and
functions
2024-12-30 06:51:16 +00:00
Rémy Rakic
4107a3ceb4 move facts module to polonius legacy module
this is specific to the old datalog implementation and wasn't noticed in
the previous module move
2024-12-30 06:51:16 +00:00
Rémy Rakic
fbf48c177b simplify add_extra_drop_facts
this is mostly to remove imports of the polonius legacy module

also what is going on in this function, what the...
2024-12-30 06:51:16 +00:00
Rémy Rakic
50c152f347 move location module to polonius legacy module
this is specific to the old datalog implementation and wasn't noticed in
the previous module move
2024-12-30 06:51:16 +00:00
Rémy Rakic
b0fc1d47d5 fix a couple nits
- remove unneeded type ascription
- fix variable name
- fix typo in comment
- fix `var_origins` var and function name: these are `VarInfos`
2024-12-30 06:51:16 +00:00
Rémy Rakic
089c525df2 address review comments
- add a FIXME when looking for the region variance of unexpected regions
- drive-by: fix a doc comment link
- drive-by: simplify the variance match using exported variants instead
2024-12-29 17:58:36 +00:00
Rémy Rakic
93527d25dd liveness constraints: draw the rest of the owl 2024-12-29 17:47:30 +00:00
Rémy Rakic
6e88db90c2 finish filling polonius context
transpose liveness matrix and record live regions at the end of MIR typeck
2024-12-29 17:47:30 +00:00
Rémy Rakic
1bea7f9a44 record variance of regular live regions 2024-12-29 17:47:30 +00:00
Rémy Rakic
d1cadf6292 record variance of use/drop live regions
records the variance of:
- use live types
- drop live generic args
2024-12-29 17:47:30 +00:00
Rémy Rakic
de049e6622 add variance recording
Following the Generalizer's structure, relating e.g. a type with itself
will allow tracking the variance wrt the contained regions.
2024-12-29 17:47:30 +00:00
Rémy Rakic
42f28cbae6 introduce polonius context
This context struct will hold data to help creating localized
constraints:
- the live regions, with the shape matching a CFG walk, indexed per
  point
- the variance of these live regions, represented as the direction we'll
  add the appropriate

We also add this structure to the mir typeck to record liveness data,
and make it responsible for localized constraint creation.
2024-12-29 17:47:30 +00:00
bors
fd19773d2f Auto merge of #134627 - estebank:issue-133252, r=jackh726
Avoid ICE in borrowck

Provide a fallback in `best_blame_constraint` when `find_constraint_paths_between_regions` doesn't have a result. This code is due a rework to avoid the letf-over `unwrap()`, but avoids the ICE caused by the repro.

Fix #133252.
2024-12-29 07:23:59 +00:00
David Tolnay
2d96f2a48f
Rollup merge of #134827 - compiler-errors:borrowck-nits, r=lqd
Some random region tweaks

Remove a redundant function and add an assertion that I think is useful
2024-12-27 18:43:04 -08:00
Michael Goulet
85aad52ce8 Make sure there are no registered constraints from creating universal region vids 2024-12-27 17:58:16 +00:00
chloefeal
e1b65be417
Fix typos
Signed-off-by: chloefeal <188809157+chloefeal@users.noreply.github.com>
2024-12-27 21:35:57 +08:00
bors
f3343420c8 Auto merge of #134625 - compiler-errors:unsafe-binders-ty, r=oli-obk
Begin to implement type system layer of unsafe binders

Mostly TODOs, but there's a lot of match arms that are basically just noops so I wanted to split these out before I put up the MIR lowering/projection part of this logic.

r? oli-obk

Tracking:

- https://github.com/rust-lang/rust/issues/130516
2024-12-24 00:51:51 +00:00
bors
0eca4dd320 Auto merge of #134465 - lcnr:type-verifier, r=compiler-errors
cleanup `TypeVerifier`

We should merge it with the `TypeChecker` as we no longer bail in cases where it encounters an error since #111863.

It's quite inconsistent whether a check lives in the verifier or the `TypeChecker`, so this feels like a quite impactful cleanup. I expect that for this we may want to change the `TypeChecker` to also be a MIR visitor 🤔 this is non-trivial so I didn't fully do it in this PR.

Best reviewed commit by commit.

r? `@compiler-errors` feel free to reassign however
2024-12-23 04:15:41 +00:00
Michael Goulet
9a1c5eb5b3 Begin to implement type system layer of unsafe binders 2024-12-22 21:57:57 +00:00
Scott McMurray
5ba54c9e31 Delete Rvalue::Len
Everything's moved to `PtrMetadata` instead.
2024-12-22 06:12:39 -08:00
bors
426d173423 Auto merge of #134268 - lqd:polonius-next, r=jackh726
Foundations of location-sensitive polonius

I'd like to land the prototype I'm describing in the [polonius project goal](https://github.com/rust-lang/rust-project-goals/issues/118). It still is incomplete and naive and terrible but it's working "well enough" to consider landing.

I'd also like to make review easier by not opening a huge PR, but have a couple small-ish ones (the +/- line change summary of this PR looks big, but >80% is moving datalog to a single place).

This PR starts laying the foundation for that work:
- it refactors and collects 99% of the old datalog fact gen, which was spread around everywhere, into a single dedicated module. It's still present at 3 small places (one of which we should revert anyways) that are kinda deep within localized components and are not as easily extractable into the rest of fact gen, so it's fine for now.
- starts introducing the localized constraints, the building blocks of the naive way of implementing the location-sensitive analysis in-tree, which is roughly sketched out in https://smallcultfollowing.com/babysteps/blog/2023/09/22/polonius-part-1/ and https://smallcultfollowing.com/babysteps/blog/2023/09/29/polonius-part-2/ but with a different vibe than per-point environments described in these posts, just `r1@p: r2@q` constraints.
- sets up the skeleton of generating these localized constraints: converting NLL typeck constraints, and creating liveness constraints
- introduces the polonius dual to NLL MIR to help development and debugging. It doesn't do much currently but is a way to see these localized constraints: it's an NLL MIR dump + a dumb listing of the constraints, that can be dumped with `-Zdump-mir=polonius -Zpolonius=next`. Its current state is not intended to be a long-term thing, just for testing purposes -- I will replace its contents in the future with a different approach (an HTML+js file where we can more easily explore/filter/trace these constraints and loan reachability, have mermaid graphs of the usual graphviz dumps, etc).

I've started documenting the approach in this PR, I'll add more in the future. It's quite simple, and should be very clear when more constraints are introduced anyways.

r? `@matthewjasper`

Best reviewed per commit so that the datalog move is less bothersome to read, but if you'd prefer we separate that into a different PR, I can do that (and michael has offered to review these more mechanical changes if it'd help).
2024-12-21 21:15:31 +00:00
Esteban Küber
70fe5a150d Avoid ICE in borrowck
Provide a fallback in `best_blame_constraint` when `find_constraint_paths_between_regions` doesn't have a result. This code is due a rework to avoid the letf-over `unwrap()`, but avoids the ICE caused by the repro.

Fix #133252.
2024-12-21 19:08:30 +00:00
bors
9bd5f3387d Auto merge of #134501 - lcnr:member-constraints-yeet, r=oli-obk
handle member constraints directly in the mir type checker

cleaner, faster, easier to change going forward :> fixes #109654

r? `@oli-obk` `@compiler-errors`
2024-12-21 12:37:40 +00:00
Matthias Krüger
10a7405fde
Rollup merge of #134574 - lcnr:opaque-ty-hack-yeet, r=compiler-errors
next-solver: disable unnecessary hack

the new solver never constrains inference variables to normalizeable aliases, so this is no longer necessary.
2024-12-20 21:32:33 +01:00
lcnr
6bc1fe1c3a next-solver: rm opaque type hack 2024-12-20 18:36:39 +01:00
lcnr
adf549808e add comments 2024-12-20 13:09:20 +01:00
lcnr
9792cf0d6b remove non-borrowck member constraints 2024-12-20 10:04:01 +01:00
lcnr
674c6577a7 more directly handle member constraints 2024-12-20 10:04:01 +01:00
lcnr
fc9a14d31a cleanup promoteds move check 2024-12-20 09:59:48 +01:00
bors
3bf62ccc10 Auto merge of #134499 - jieyouxu:rollup-zmaveur, r=jieyouxu
Rollup of 7 pull requests

Successful merges:

 - #133702 (Variants::Single: do not use invalid VariantIdx for uninhabited enums)
 - #134427 (ci: remove duplicate task definition)
 - #134432 (Fix intra doc links not generated inside footnote definitions)
 - #134437 (reduce compiler `Assemble` complexity)
 - #134474 (Forbid overwriting types in typeck)
 - #134477 (move lint_unused_mut into sub-fn)
 - #134491 (Some destructor/drop related tweaks)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-12-19 11:25:43 +00:00
许杰友 Jieyou Xu (Joe)
7d962ecd17
Rollup merge of #134477 - lcnr:move-lint-into-subfn, r=lqd
move lint_unused_mut into sub-fn

also, stop `mem::take`-ing stuff we only use by reference 🤷
2024-12-19 16:48:11 +08:00
bors
a4079b29bb Auto merge of #133961 - lcnr:borrowck-cleanup, r=jackh726
cleanup region handling: add `LateParamRegionKind`

The second commit is to enable a split between `BoundRegionKind` and `LateParamRegionKind`, by avoiding `BoundRegionKind` where it isn't necessary.

The third comment then adds `LateParamRegionKind` to avoid having the same late-param region for separate bound regions. This fixes #124021.

r? `@compiler-errors`
2024-12-19 08:33:20 +00:00
bors
c434b4b4b6 Auto merge of #133328 - nnethercote:simplify-SwitchInt-handling, r=tmiasko
Simplify `SwitchInt` handling

Dataflow handling of `SwitchInt` is currently complicated. This PR simplifies it.

r? `@cjgillot`
2024-12-18 22:57:23 +00:00
lcnr
b0d923c33b move lint_unused_mut into subfn 2024-12-18 18:29:24 +01:00
lcnr
4d5aaa0f30 fix crashes 2024-12-18 16:05:44 +01:00
lcnr
085d931810 introduce LateParamRegionKind 2024-12-18 16:05:44 +01:00
lcnr
5bced1419f we aren't actually sanitizing anything anymore 2024-12-18 14:15:08 +01:00
lcnr
51cd03a127 merge PlaceTy field_ty computation 2024-12-18 14:15:08 +01:00
lcnr
809f55d66a TypeVerifier: stop computing types for later use 2024-12-18 14:13:17 +01:00
lcnr
c54e8157dd rm TypeChecker::sanitize_type 2024-12-18 13:07:47 +01:00
lcnr
5f90b15f09 get_ambient_variance to inherent method 2024-12-18 13:03:15 +01:00
Rémy Rakic
aeb3d103a6 address review comments
- move constraints to an Option
- check `-Zpolonius=next` only once
- rewrite fixme comments to make the actionable part clear
2024-12-18 07:33:26 +00:00
Rémy Rakic
c75c5176c5 introduce beginnings of polonius MIR dump
This is mostly for test purposes to show the localized constraints until
the MIR debugger is set up.
2024-12-18 07:33:26 +00:00
Rémy Rakic
ee93ce9c61 extract main NLL MIR dump function
this will allow calling from polonius MIR
2024-12-18 07:33:26 +00:00
Rémy Rakic
e7fb93bf9b set up skeleton for localized constraints conversion 2024-12-18 07:33:26 +00:00
Rémy Rakic
a5f05919e0 add general documentation on the polonius module
this describes the rough algorithm using the localized constraint graph
2024-12-18 07:33:26 +00:00
Rémy Rakic
b70a9159a6 introduce localized outlives constraints
these are the basic blocks of the naive polonius constraint graph
implementation.
2024-12-18 07:33:26 +00:00
bors
a89ca2c85e Auto merge of #134243 - nnethercote:re-export-more-rustc_span, r=jieyouxu
Re-export more `rustc_span::symbol` things from `rustc_span`.

`rustc_span::symbol` defines some things that are re-exported from `rustc_span`, such as `Symbol` and `sym`. But it doesn't re-export some closely related things such as `Ident` and `kw`. So you can do `use rustc_span::{Symbol, sym}` but you have to do `use rustc_span::symbol::{Ident, kw}`, which is inconsistent for no good reason.

This commit re-exports `Ident`, `kw`, and `MacroRulesNormalizedIdent`, and changes many `rustc_span::symbol::` qualifiers to `rustc_span::`. This is a 300+ net line of code reduction, mostly because many files with two `use rustc_span` items can be reduced to one.

r? `@jieyouxu`
2024-12-18 02:56:38 +00:00
Nicholas Nethercote
2620eb42d7 Re-export more rustc_span::symbol things from rustc_span.
`rustc_span::symbol` defines some things that are re-exported from
`rustc_span`, such as `Symbol` and `sym`. But it doesn't re-export some
closely related things such as `Ident` and `kw`. So you can do `use
rustc_span::{Symbol, sym}` but you have to do `use
rustc_span::symbol::{Ident, kw}`, which is inconsistent for no good
reason.

This commit re-exports `Ident`, `kw`, and `MacroRulesNormalizedIdent`,
and changes many `rustc_span::symbol::` qualifiers in `compiler/` to
`rustc_span::`. This is a 200+ net line of code reduction, mostly
because many files with two `use rustc_span` items can be reduced to
one.
2024-12-18 13:38:53 +11:00
Matthias Krüger
264566fc61
Rollup merge of #134378 - lqd:polonius-next-episode-2, r=jackh726
An octuple of polonius fact generation cleanups

This PR is extracted from https://github.com/rust-lang/rust/pull/134268 for easier review and contains its first 8 commits. They have already been reviewed by ````@jackh726```` over there.

r? ````@jackh726````
2024-12-17 22:34:43 +01:00
Jacob Pratt
cdd71c9f3d
Rollup merge of #134412 - lcnr:borrowck-cleanup-trivial, r=jackh726
small borrowck cleanup

the already approved parts of #133909 and #133961

r? `@jackh726`
2024-12-17 05:37:36 -05:00
Jacob Pratt
40ce4e0ccf
Rollup merge of #134397 - Enselic:raw-mut, r=compiler-errors
rustc_borrowck: Suggest changing `&raw const` to `&raw mut` if applicable

Closes #127562

For reference, here is the diff compared to the original error reported in that issue before #134244 stopped suggesting the invalid syntax:

```
diff --git a/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr b/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr
index 0da5d15cf7f..dbe834b6b78 100644
--- a/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr
+++ b/tests/ui/borrowck/no-invalid-mut-suggestion-for-raw-pointer-issue-127562.stderr
``@@`` -6,8 +6,8 ``@@`` LL |     unsafe { *ptr = 3; }
    |
 help: consider changing this to be a mutable pointer
    |
-LL |     let ptr = &mut raw const val;
-   |                +++
+LL |     let ptr = &raw mut val;
+   |                    ~~~

 error: aborting due to 1 previous error
```
2024-12-17 05:37:11 -05:00
lcnr
e6349b414e small refactor to region error handling 2024-12-17 09:00:29 +01:00
lcnr
a1b38aa437 move variable initialization 2024-12-17 09:00:28 +01:00
Martin Nordholts
70a0dc1f7e rustc_borrowck: Suggest changing &raw const to &raw mut if applicable 2024-12-16 20:44:23 +01:00
Martin Nordholts
47b559d00e rustc_borrowck: suggest_ampmut(): Inline unneeded local var
Since the previos commit renamed `assignment_rhs_span` to just
`rhs_span` there is no need for a variable just to shorten the
expression on the next line. Inline the variable.
2024-12-16 20:44:07 +01:00
Martin Nordholts
bfb027a50d rustc_borrowck: suggest_ampmut(): Just rename some variables
By making the variable names more descriptive it becomes easier to
understand the code. Especially with the more complicated code in the
next commit.
2024-12-16 20:29:33 +01:00
Oli Scherer
4032b9ddbd Avoid wrapping a trivially defaultable type in Option 2024-12-16 16:02:05 +00:00
Nicholas Nethercote
4d8316f4d4 Simplify dataflow SwitchInt handling.
Current `SwitchInt` handling has complicated control flow.

- The dataflow engine calls `Analysis::apply_switch_int_edge_effects`,
  passing in an "applier" that impls `SwitchIntEdgeEffects`.
- `apply_switch_int_edge_effects` possibly calls `apply` on the applier,
  passing it a closure.
- The `apply` method calls the closure on each `SwitchInt` edge.
- The closure operates on the edge.

I.e. control flow goes from the engine, to the analysis, to the applier
(which came from the engine), to the closure (which came from the
analysis). It took me a while to work this out.

This commit changes to a simpler structure that maintains the important
characteristics.

- The dataflow engine calls `Analysis::get_switch_int_data`.
- `get_switch_int_data` returns an `Option<Self::SwitchIntData>` value.
- If that returned value was `Some`, the dataflow engine calls
  `Analysis::apply_switch_int_edge_effect` on each edge, passing the
  `Self::SwitchIntData` value.
- `Analysis::apply_switch_int_edge_effect` operates on the edge.

I.e. control flow goes from the engine, to the analysis, to the
engine, to the analysis.

Added:
- The `Analysis::SwitchIntData` assoc type and the
  `Analysis::get_switch_int_data` method. Both only need to be
  defined by analyses that look at `SwitchInt` terminators.
- The `MaybePlacesSwitchIntData` struct, which has three fields.

Changes:
- `Analysis::apply_switch_int_edge_effects` becomes
  `Analysis::apply_switch_int_edge_effect`, which is a little simpler
  because it's dealing with a single edge instead of all edges.

Removed:
- The `SwitchIntEdgeEffects` trait, and its two impls:
  `BackwardSwitchIntEdgeEffectsApplier` (which has six fields) and
  `ForwardSwitchIntEdgeEffectsApplier` structs (which has four fields).
- The closure.

The new structure is more concise and simpler.
2024-12-16 09:36:07 +11:00
Rémy Rakic
8562497d0a improve consistency within fact gen
- fix names
- fix ordering of arguments
2024-12-15 14:44:07 +00:00
Rémy Rakic
7ad1f5bec5 refactor type_check module slightly
- use a consistent name for `TypeChecker`, which is usually referred to as `typeck`
- remove an incorrect doc comment
- remove a single-use local
2024-12-15 14:44:07 +00:00
Rémy Rakic
2024c5d869 simplify emit_outlives_facts
- integrate into `emit_facts` and remove from typeck
2024-12-15 14:44:07 +00:00
Rémy Rakic
1740a5f84a simplify emit_access_facts and fact generation
- integrate it within existing fact generation instead of being called
  in typeck
- simplify access fact extraction
- also remove single use fact emit functions in root fact generation
2024-12-15 14:44:07 +00:00
Rémy Rakic
5486857448 use let else more consistently in fact generation
also remove a useless trace
2024-12-15 14:44:07 +00:00
Rémy Rakic
afbe101f0e clean up translate_outlives_facts
- remove dependency on `TypeChecker`
- move to legacy fact generation module
- group facts emitted during typeck together
2024-12-15 14:44:07 +00:00
Rémy Rakic
9d8f58adb2 clean up emit_drop_facts
- remove dependency on `TypeChecker`
- move to legacy fact generation module
- remove polonius module from liveness
2024-12-15 14:44:07 +00:00
Rémy Rakic
2fd443888e clean up emit_access_facts
- remove dependency on `TypeChecker`
- move to legacy fact generation module
2024-12-15 14:44:07 +00:00
Stuart Cook
6c75fc848d
Rollup merge of #134315 - lqd:polonius-next-episode-1, r=jackh726
A couple of polonius fact generation cleanups

This PR is extracted from #134268 for easier review and contains its first two commits. They have already been reviewed by `@jackh726.`

r? `@jackh726`
2024-12-15 20:01:40 +11:00
Rémy Rakic
585c71fd3e refactor access fact generation
- use consistent names
- inline single use functions
- dedupe and simplify some paths
- fix fact generation timer activity: it was missing the walk and
  extraction process
2024-12-14 17:35:32 +00:00
Rémy Rakic
b0e04d5a0c move datalog fact generation into a legacy module 2024-12-14 17:35:32 +00:00
Matthias Krüger
fffdb1bc01
Rollup merge of #134191 - willcrichton:dev, r=RalfJung,lqd
Make some types and methods related to Polonius + Miri public

We have a tool, [Aquascope](https://github.com/cognitive-engineering-lab/aquascope/), which uses Polonius and Miri to visualize the compile-time and run-time semantics of a Rust program. Changes in the last few months to both APIs have hidden away details we depend upon. This PR re-exposes some of those details, specifically:

**Polonius:**
- `BorrowSet` and `BorrowData` are added to `rustc_borrowck::consumers`, and their fields are made `pub` instead of `pub(crate)`. We need this to interpret the `BorrowIndex`es generated by Polonius.
- `BorrowSet::build` is now `pub`. We need this because the borrowck API doesn't provide access to the `BorrowSet` constructed during checking.
- `PoloniusRegionVid` is added to `rustc_borrowck::consumers`. We need this because it's also contained in the Polonius facts.

**Miri:**
- `InterpCx::local_to_op` is now a special case of `local_at_frame_to_op`, which allows querying locals in any frame. We need this because we walk the whole stack at each step to collect the state of memory.
- `InterpCx::layout_of_local` is now `pub`. We need this because we need to know the layout of every local at each step.

If these changes go against some design goal for keeping certain types private, please let me know so we can hash out a better solution. Additionally, if there's a better way to document that it's important that certain types stay public, also let me know. For example, `BorrowSet` was previously public but was hidden in 6676cec, breaking our build.

cc ```@RalfJung``` ```@nnethercote``` ```@gavinleroy```
2024-12-14 14:07:57 +01:00
bors
f5079d00e6 Auto merge of #134185 - compiler-errors:impl-trait-in-bindings, r=oli-obk
(Re-)Implement `impl_trait_in_bindings`

This reimplements the `impl_trait_in_bindings` feature for local bindings.

"`impl Trait` in bindings" serve as a form of *trait* ascription, where the type basically functions as an infer var but additionally registering the `impl Trait`'s trait bounds for the infer type. These trait bounds can be used to enforce that predicates hold, and can guide inference (e.g. for closure signature inference):

```rust
let _: impl Fn(&u8) -> &u8 = |x| x;
```

They are implemented as an additional set of bounds that are registered when the type is lowered during typeck, and then these bounds are tied to a given `CanonicalUserTypeAscription` for borrowck. We enforce these `CanonicalUserTypeAscription` bounds during borrowck to make sure that the `impl Trait` types are sensitive to lifetimes:

```rust
trait Static: 'static {}
impl<T> Static for T where T: 'static {}

let local = 1;
let x: impl Static = &local;
//~^ ERROR `local` does not live long enough
```

r? oli-obk

cc #63065

---

Why can't we just use TAIT inference or something? Well, TAITs in bodies have the problem that they cannot reference lifetimes local to a body. For example:

```rust
type TAIT = impl Display;
let local = 0;
let x: TAIT = &local;
//~^ ERROR `local` does not live long enough
```

That's because TAITs requires us to do *opaque type inference* which is pretty strict, since we need to remap all of the lifetimes of the hidden type to universal regions. This is simply not possible here.

---

I consider this part of the "impl trait everywhere" experiment. I'm not certain if this needs yet another lang team experiment.
2024-12-14 10:22:43 +00:00
Michael Goulet
d714a22e7b (Re-)Implement impl_trait_in_bindings 2024-12-14 03:21:24 +00:00
Michael Goulet
1da411e750 Split UserTypeAnnotation to have a kind 2024-12-14 03:20:50 +00:00
Matthias Krüger
34e607594b
Rollup merge of #134244 - Enselic:no-mut-hint-for-raw-ref, r=jieyouxu
rustc_borrowck: Stop suggesting the invalid syntax `&mut raw const`

A legitimate suggestion would be to change from

    &raw const val

to

    &raw mut val

But until we have figured out how to make that happen we should at least
stop suggesting invalid syntax.

I recommend review commit-by-commit.

Part of #127562
2024-12-14 03:54:34 +01:00
Matthias Krüger
c6ebe6fe3b
Rollup merge of #133938 - nnethercote:rustc_mir_dataflow-renamings, r=oli-obk
`rustc_mir_dataflow` cleanups, including some renamings

Some opinionated commits in this collection, let's see how we go.

r? `@cjgillot`
2024-12-13 17:25:29 +01:00
Martin Nordholts
f6cb227f61 rustc_borrowck: Convert suggest_ampmut() 4-tuple to struct for readability 2024-12-13 16:35:43 +01:00
Martin Nordholts
2d2c6f2a80 rustc_borrowck: Stop suggesting the invalid syntax &mut raw const
A legitimate suggestion would be to change from

    &raw const val

to

    &raw mut val

But until we have figured out how to make that happen we should at least
stop suggesting invalid syntax.
2024-12-13 16:33:47 +01:00
Martin Nordholts
e17ca31b22 rustc_borrowck: Make suggest_ampmut() return type match its use
So that it becomes easy for a later commit to return `None`.
2024-12-13 16:31:24 +01:00
Will Crichton
4d5d4700f3 Make BorrowSet/BorrowData fields accessible via public getters 2024-12-12 12:34:43 -08:00
Will Crichton
eb10db0a76 Make some types and methods related to Polonius + Miri public. 2024-12-11 17:28:49 -08:00
Nicholas Nethercote
1d56943f34 Rename some Analysis and ResultsVisitor methods.
The words "before" and "after" have an obvious temporal meaning, e.g.
`seek_before_primary_effect`,
`visit_statement_{before,after}_primary_effect`. But "before" is also
used to name the effect that occurs before the primary effect of a
statement/terminator; this is `Effect::Before`. This leads to the
confusing possibility of talking about things happening "before/after
the before event".

This commit removes this awkward overloading of "before" by renaming
`Effect::Before` as `Effect::Early`. It also renames some of the
`Analysis` and `ResultsVisitor` methods to be more consistent.

Here are the before and after names:

- `Effect::{Before,Primary}`              -> `Effect::{Early,Primary}`
- `apply_before_statement_effect`         -> `apply_early_statement_effect`
- `apply_statement_effect`                -> `apply_primary_statement_effect`
- `visit_statement_before_primary_effect` -> `visit_after_early_statement_effect`
- `visit_statement_after_primary_effect`  -> `visit_after_primary_statement_effect`

(And s/statement/terminator/ for all the terminator events.)
2024-12-10 12:07:13 +11:00
Nicholas Nethercote
b059ea857c Rename EntrySets as EntryStates.
"Set" doesn't make much sense here, we refer to domain values as "state"
everywhere else. (This name confused me for a while.)
2024-12-10 12:04:45 +11:00
Nicholas Nethercote
2298104f3f Call all Domain values state.
Currently they are called (most common) `state`, or `trans`, or (rare)
`on_entry`. I think `trans` is short for "transfer function", which
perhaps made more sense when `GenKillAnalysis` existed. Using `state`
everywhere now is more consistent.
2024-12-10 12:03:56 +11:00
Nicholas Nethercote
d490ea1f39 Remove lifetimes from BorrowckDomain.
They are only present because it's currently defined in terms of the
domains of `Borrows` and `MaybeUninitializedPlaces` and
`EverInitializedPlaces` via associated types. This commit introduces
typedefs for those domains, avoiding the lifetimes.
2024-12-10 12:02:48 +11:00
Esteban Küber
9ac95c10c0 Introduce default_field_values feature
Initial implementation of `#[feature(default_field_values]`, proposed in https://github.com/rust-lang/rfcs/pull/3681.

Support default fields in enum struct variant

Allow default values in an enum struct variant definition:

```rust
pub enum Bar {
    Foo {
        bar: S = S,
        baz: i32 = 42 + 3,
    }
}
```

Allow using `..` without a base on an enum struct variant

```rust
Bar::Foo { .. }
```

`#[derive(Default)]` doesn't account for these as it is still gating `#[default]` only being allowed on unit variants.

Support `#[derive(Default)]` on enum struct variants with all defaulted fields

```rust
pub enum Bar {
    #[default]
    Foo {
        bar: S = S,
        baz: i32 = 42 + 3,
    }
}
```

Check for missing fields in typeck instead of mir_build.

Expand test with `const` param case (needs `generic_const_exprs` enabled).

Properly instantiate MIR const

The following works:

```rust
struct S<A> {
    a: Vec<A> = Vec::new(),
}
S::<i32> { .. }
```

Add lint for default fields that will always fail const-eval

We *allow* this to happen for API writers that might want to rely on users'
getting a compile error when using the default field, different to the error
that they would get when the field isn't default. We could change this to
*always* error instead of being a lint, if we wanted.

This will *not* catch errors for partially evaluated consts, like when the
expression relies on a const parameter.

Suggestions when encountering `Foo { .. }` without `#[feature(default_field_values)]`:

 - Suggest adding a base expression if there are missing fields.
 - Suggest enabling the feature if all the missing fields have optional values.
 - Suggest removing `..` if there are no missing fields.
2024-12-09 21:55:01 +00:00
bors
f6cb952dc1 Auto merge of #133891 - nnethercote:MixedBitSet, r=Mark-Simulacrum
Introduce `MixedBitSet`

`ChunkedBitSet` is good at avoiding excessive memory usage for programs with very large functgions where dataflow bitsets have very large domain sizes. But it's overly heavyweight for small bitsets, because any non-empty `ChunkedBitSet` takes up at least 256 bytes.

This PR introduces `MixedBitSet`, which is a simple bitset that uses `BitSet` for small/medium bitsets and `ChunkedBitSet` for large bitsets. It's a speed and memory usage win.

r? `@Mark-Simulacrum`
2024-12-09 07:13:11 +00:00
Esteban Küber
d860e5b088 Mention type parameter in more cases and don't suggest ~const bound already there 2024-12-07 21:37:13 +00:00
Nicholas Nethercote
a06547508a Change ChunkedBitSet<MovePathIndex>s to MixedBitSet.
It's a performance win because `MixedBitSet` is faster and uses less
memory than `ChunkedBitSet`.

Also reflow some overlong comment lines in
`lint_tail_expr_drop_order.rs`.
2024-12-05 20:07:26 +11:00
lcnr
1228b38cbd add assert 2024-12-04 17:18:11 +01:00
lcnr
baf4bb7100 remove unnecessary eval_verify_bound 2024-12-04 16:39:42 +01:00
Matthias Krüger
9fd0972677
Rollup merge of #133798 - lcnr:nested-bodies-opaques, r=compiler-errors
stop replacing bivariant args with `'static` when computing closure requirements

It is unnecessary, these get constrained when checking that the opaque type is well-formed.

It also results in the opaque type no longer being well formed. If you've got `fn foo<'a>() -> impl Sized + 'a` the opaque is `type Opaque<'a, 'aDummy> where 'a: 'aDummy, 'aDummy: 'a` where `'aDummy`  is bivariant. If we call `foo::<'b>()`  inside of a closure and its return type ends up in a type test, we start out with the WF `Opaque<'b, 'b>`, and then replace the bivariant `'b` with `'static`. `Opaque<'b, 'static>` is no longer well-formed. Given how these type tests are used, I don't think this caused any practical issues.

r? types
2024-12-04 05:42:08 +01:00
Matthias Krüger
453a1a8b7f
Rollup merge of #133545 - clubby789:symbol-intern-lit, r=jieyouxu
Lint against Symbol::intern on a string literal

Disabled in tests where this doesn't make much sense
2024-12-03 17:27:06 +01:00
lcnr
65d0b5dc2e small code cleanup 2024-12-03 14:07:04 +01:00
lcnr
8a47b442c4 closure requirements: don't replace bivariant opaque args
It is unnecessary, these get constrained when checking that the
opaque type is well-formed.

It also results in the opaque type no longer being well formed.
If you've got `fn foo<'a>() -> impl Sized + 'a` the opaque is
`type Opaque<'a, 'aDummy> where 'a: 'aDummy, 'aDummy: 'a` where
`'aDummy`  is bivariant. If we call `foo::<'b>()`  inside of a closure
and its return type ends up in a type test, we start out with the WF
`Opaque<'b, 'b>`, and then replace the bivariant `'b` with `'static`.
`Opaque<'b, 'static>`  is no longer well-formed. Given how these type
tests are used, I don't think this caused any practical issues.
2024-12-03 14:07:04 +01:00
lcnr
67defd72e6 update instrumentation 2024-12-03 13:42:43 +01:00
clubby789
71b698c0b8 Replace Symbol::intern calls with preinterned symbols 2024-11-28 15:45:27 +00:00
lcnr
9fe7750bcd uplift fold_regions to rustc_type_ir 2024-11-28 10:40:58 +01:00
Michael Goulet
26c77742c3 Structurally resolve before applying projection in borrowck 2024-11-27 20:39:49 +00:00
dianne
04d9bb7a9a add_move_error_suggestions: use a HIR visitor rather than SourceMap 2024-11-25 20:29:04 -08:00
Frank King
161221da9e Refactor where predicates, and reserve for attributes support 2024-11-25 16:38:35 +08:00
Ding Xiang Fei
297b618944
reduce false positives of tail-expr-drop-order from consumed values
take 2

open up coroutines

tweak the wordings

the lint works up until 2021

We were missing one case, for ADTs, which was
causing `Result` to yield incorrect results.

only include field spans with significant types

deduplicate and eliminate field spans

switch to emit spans to impl Drops

Co-authored-by: Niko Matsakis <nikomat@amazon.com>

collect drops instead of taking liveness diff

apply some suggestions and add explantory notes

small fix on the cache

let the query recurse through coroutine

new suggestion format with extracted variable name

fine-tune the drop span and messages

bugfix on runtime borrows

tweak message wording

filter out ecosystem types earlier

apply suggestions

clippy

check lint level at session level

further restrict applicability of the lint

translate bid into nop for stable mir

detect cycle in type structure
2024-11-20 20:53:11 +08:00
lcnr
948cec0fad move fn is_item_raw to TypingEnv 2024-11-19 18:06:20 +01:00
Nicholas Nethercote
75108b6067 Pass flow_inits by value.
It's simpler that way, and we don't need the explicit `drop`.
2024-11-19 11:46:33 +11:00
Nicholas Nethercote
af50fe407e Put param_env into infcx.
Because they get passed around together a lot.
2024-11-19 11:44:07 +11:00
Nicholas Nethercote
c9283f8fa9 Pass constraints to RegionInferenceContext::new.
Instead of destructuring it in advance and passing all the components
individually. It's less code that way.
2024-11-19 11:33:48 +11:00
Nicholas Nethercote
801b150737 Don't refcount PlaceholderIndices.
It's not necessary.
2024-11-19 11:33:48 +11:00
Nicholas Nethercote
fe3c49fe9a Inline and remove TypeVerifier::new.
It has a single call site.
2024-11-19 11:33:48 +11:00
Nicholas Nethercote
2709dc8a13 Compute upvars lazily.
It can be computed from `tcx` on demand, instead of computing it eagerly
and passing it around.
2024-11-19 11:33:48 +11:00
Nicholas Nethercote
952c6d5c96 Clean up UniversalRegions.
There is an `Rc<UniversalRegions>` within `UniversalRegionRelations`,
and yet the two types get passed around in tandem a lot.

This commit makes `UniversalRegionRelations` own `UniversalRegions`,
removing the `Rc` (which wasn't truly needed) and the tandem-passing.
This requires adding a `universal_relations` method to
`UniversalRegionRelations`, and renaming a couple of existing methods
producing iterators to avoid a name clash.
2024-11-19 11:33:48 +11:00
Nicholas Nethercote
ed11fbe5df Make TypeChecker::region_bound_pairs owned.
No reason not to be, and it's simpler that way.
2024-11-19 11:33:48 +11:00
Nicholas Nethercote
227ecc803f Make TypeChecker::known_type_outlives_obligations owned.
This avoids the need to arena allocate it. `ConstraintConversion` needs
some simple lifetime adjustments to allow this.
2024-11-19 11:33:48 +11:00
Nicholas Nethercote
4c8a23ab0d Don't pass universal_regions unnecessarily.
`TypeChecker` already has it in a field.
2024-11-19 11:33:48 +11:00
lcnr
2e087d2eaa review 2024-11-18 10:50:14 +01:00
lcnr
9cba14b95b use TypingEnv when no infcx is available
the behavior of the type system not only depends on the current
assumptions, but also the currentnphase of the compiler. This is
mostly necessary as we need to decide whether and how to reveal
opaque types. We track this via the `TypingMode`.
2024-11-18 10:38:56 +01:00
Jacob Pratt
fc4f71db68
Rollup merge of #133130 - dianne:fix-133118, r=compiler-errors
`suggest_borrow_generic_arg`: instantiate clauses properly

This simplifies and fixes the way `suggest_borrow_generic_arg` instantiates callees' predicates when testing them to see if a moved argument can instead be borrowed. Previously, it would ICE if the moved argument's type included a region variable, since it was getting passed to a call of `EarlyBinder::instantiate`. This makes the instantiation much more straightforward, which also fixes the ICE.

Fixes #133118

This also modifies `tests/ui/moves/moved-value-on-as-ref-arg.rs` to have more useful bounds on the tests for suggestions to borrow `Borrow` and `BorrowMut` arguments. With its old tautological `T: BorrowMut<T>` bound, this fix would make it suggest a shared borrow for that argument.
2024-11-17 22:30:49 -05:00
dianne
546ba3d310 suggest_borrow_generic_arg: instantiate clauses properly
Fixes issue 133118.
This also modifies `tests/ui/moves/moved-value-on-as-ref-arg.rs` to have more
useful bounds on the tests for suggestions to borrow `Borrow` and `BorrowMut`
arguments. With its old tautological `T: BorrowMut<T>` bound, this fix would
make it suggest a shared borrow for that argument.
2024-11-17 18:09:36 -08:00
Matthias Krüger
5c81dbf4fa
Rollup merge of #132134 - nnethercote:rm-ResultsVisitable, r=cjgillot
Remove `ResultsVisitable`

`ResultsVisitable` has annoyed me for a while. This PR removes it. Details in the individual commits.

r? `@cjgillot.`
2024-11-16 21:05:44 +01:00
dianne
2ab8480605 Suggest borrowing arguments in generic positions when trait bounds are satisfied
This subsumes the suggestions to borrow arguments with `AsRef`/`Borrow` bounds and those to borrow
arguments with `Fn` and `FnMut` bounds. It works for other traits implemented on references as well,
such as `std::io::Read`, `std::io::Write`, and `core::fmt::Write`.

Incidentally, by making the logic for suggesting borrowing closures general, this removes some
spurious suggestions to mutably borrow `FnMut` closures in assignments, as well as an unhelpful
suggestion to add a `Clone` constraint to an `impl Fn` argument.
2024-11-13 20:29:40 -08:00
dianne
ea37000b56 Use a common subdiagnostic format for generic borrows
This is setup for unifing the logic for suggestions to borrow arguments in generic positions.
As of this commit, it's still special cases for `AsRef`/`Borrow`-like traits and `Fn`-like traits.
2024-11-13 16:36:23 -08:00
dianne
2280d8ea00 Provide borrow-instead-of-move suggestions for calls of fn-like items from other crates
This also downgrades its applicability to MaybeIncorrect. Its suggestion can result in ill-typed
code when the type parameter it suggests providing a different generic argument for appears
elsewhere in the callee's signature or predicates.
2024-11-13 16:36:23 -08:00
Michael Goulet
a1f9d5bfba Dont suggest use<APIT> 2024-11-09 19:41:53 +00:00
Michael Goulet
e4c1a0016c Get rid of check_opaque_type_well_formed 2024-11-08 03:46:27 +00:00
bors
096277e989 Auto merge of #132580 - compiler-errors:globs, r=Noratrieb
Remove unnecessary pub enum glob-imports from `rustc_middle::ty`

We used to have an idiom in the compiler where we'd prefix or suffix all the variants of an enum, for example `BoundRegionKind`, with something like `Br`, and then *glob-import* that enum variant directly.

`@noratrieb` brought this up, and I think that it's easier to read when we just use the normal style `EnumName::Variant`.

This PR is a bit large, but it's just naming.

The only somewhat opinionated change that this PR does is rename `BorrowKind::Imm` to `BorrowKind::Immutable` and same for the other variants. I think these enums are used sparingly enough that the extra length is fine.

r? `@noratrieb` or reassign
2024-11-05 08:30:56 +00:00
Jubilee
972fef232e
Rollup merge of #131153 - VulnBandit:copy_impl_vuln, r=compiler-errors
Improve duplicate derive Copy/Clone diagnostics

Improve duplicate derive Copy/Clone diagnostics.
Closes #131083
2024-11-04 20:40:44 -08:00
Nicholas Nethercote
c904c6aaff Remove ResultsVisitable.
Now that `Results` is the only impl of `ResultsVisitable`, the trait can
be removed. This simplifies things by removining unnecessary layers of
indirection and abstraction.

- `ResultsVisitor` is simpler.
  - Its type parameter changes from `R` (an analysis result) to the
    simpler `A` (an analysis).
  - It no longer needs the `Domain` associated type, because it can use
    `A::Domain`.
  - Occurrences of `R` become `Results<'tcx, A>`, because there is now
    only one kind of analysis results.

- `save_as_intervals` also changes type parameter from `R` to `A`.

- The `results.reconstruct_*` method calls are replaced with
  `results.analysis.apply_*` method calls, which are equivalent.

- `Direction::visit_results_in_block` is simpler, with a single generic
  param (`A`) instead of two (`D` and `R`/`F`, with a bound connecting
  them). Likewise for `visit_results`.

- The `ResultsVisitor` impls for `MirBorrowCtxt` and
  `StorageConflictVisitor` are now specific about the type of the
  analysis results they work with. They both used to have a type param
  `R` but they weren't genuinely generic. In both cases there was only a
  single results type that made sense to instantiate them with.
2024-11-05 10:18:03 +11:00
Nicholas Nethercote
3350edf8fd Replace BorrowckResults with Borrowck.
The results of most analyses end up in a `Results<'tcx, A>`, where `A`
is the analysis. It's then possible to traverse the results via a
`ResultsVisitor`, which relies on the `ResultsVisitable` trait. (That
trait ends up using the same `apply_*` methods that were used when
computing the analysis, albeit indirectly.)

This pattern of "compute analysis results, then visit them" is common.
But there is one exception. For borrow checking we compute three
separate analyses (`Borrows`, `MaybeUninitializedPlaces`, and
`EverInitializedPlaces`), combine them into a single `BorrowckResults`,
and then do a single visit of that `BorrowckResults` with
`MirBorrowckResults`. `BorrowckResults` is just different enough from
`Results` that it requires the existence of `ResultsVisitable`, which
abstracts over the traversal differences between `Results` and
`BorrowckResults`.

This commit changes things by introducing `Borrowck` and bundling the
three borrowck analysis results into a standard `Results<Borrowck>`
instead of the exceptional `BorrowckResults`. Once that's done, the
results can be visited like any other analysis results.
`BorrowckResults` is removed, as is `impl ResultsVisitable for
BorrowckResults`. (It's instructive to see how similar the added `impl
Analysis for Borrowck` is to the removed `impl ResultsVisitable for
BorrowckResults`. They're both doing exactly the same things.)

Overall this increases the number of lines of code and might not seem
like a win. But it enables the removal of `ResultsVisitable` in the next
commit, which results in many simplifications.
2024-11-05 10:18:01 +11:00
Nicholas Nethercote
e0e7a432db BorrowckDiags tweaks.
- Store a mut ref to a `BorrowckDiags` in `MirBorrowckCtxt` instead of
  owning it, to save having to pass ownership in and out of
  `promoted_mbcx`.
- Use `buffer_error` in a couple of suitable places.
2024-11-04 17:36:25 +11:00
Nicholas Nethercote
ce2f0b4ce9 Simplify LocalUseMapBuild.
It has four different `insert` methods, with some duplication. This
commit finds the commonality and removes them all.
2024-11-04 17:36:25 +11:00
Nicholas Nethercote
6ecf80e1ad Merge BorrowCheckContext into TypeChecker.
Because there is no real reason for it to be a separate struct.
- It has no methods.
- It's easy to confuse with the nearby `BorrowckInferContext` (which
  does have methods).
- The `mut` ref to it in `TypeChecker` makes it seem like any of the
  fields within might be mutable, but only two (`all_facts` and
  `constraints`) actually are.
- Two of the fields are `pub(crate)` but can be private.

This change makes a lot of code more concise and readable.
2024-11-04 17:36:25 +11:00
Nicholas Nethercote
f86c76a782 Remove unnecessary qualifiers. 2024-11-04 17:36:25 +11:00
Nicholas Nethercote
a07011bde2 Remove unnecessary continue. 2024-11-04 17:36:25 +11:00
Nicholas Nethercote
e23bdd68f9 Remove ToUniverseInfo impl for CanonicalQueryInput<CustomTypeOp>.
It's unused.
2024-11-04 17:36:25 +11:00
Nicholas Nethercote
7901c03d2d Merge UniverseInfo and UniverseInfoInner.
It's strange to have a struct that contains a single anonymous field
that is an enum. This commit merges them. This does require increasing
the visibility of `TypeOfInfo` to `pub(crate)`, but that seems
worthwhile.
2024-11-04 17:36:25 +11:00
Nicholas Nethercote
bf1a5c2b7f Tidy up comments and some formatting.
Mostly by wrapping overly long comment lines, plus a few other things.
2024-11-04 17:36:25 +11:00