Commit Graph

2035 Commits

Author SHA1 Message Date
Ralf Jung
be65012aa3 turn hir::ItemKind::Fn into a named-field variant 2025-01-04 11:35:31 +01: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
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