Commit Graph

1295 Commits

Author SHA1 Message Date
surechen
40ae34194c remove redundant imports
detects redundant imports that can be eliminated.

for #117772 :

In order to facilitate review and modification, split the checking code and
removing redundant imports code into two PR.
2023-12-10 10:56:22 +08:00
Guillaume Gomez
546643c13c
Rollup merge of #118638 - nnethercote:rustc_mir_dataflow-more, r=cjgillot
More `rustc_mir_dataflow` cleanups

r? `@cjgillot`
2023-12-09 14:05:10 +01:00
Michael Goulet
96bb542a31 Implement async gen blocks 2023-12-08 17:23:25 +00:00
Nicholas Nethercote
4b364b6f0f Tweak GenKillAnalysis.
`GenKillAnalysis` has five methods that take a transfer function arg:
- `statement_effect`
- `before_statement_effect`
- `terminator_effect`
- `before_terminator_effect`
- `call_return_effect`

All the transfer function args have type `&mut impl GenKill<Self::Idx>`,
except for `terminator_effect`, which takes the simpler `Self::Domain`.

But only the first two need to be `impl GenKill`. The other
three can all be `Self::Domain`, just like `Analysis`. So this commit
changes the last two to take `Self::Domain`, making `GenKillAnalysis`
and `Analysis` more similar.

(Another idea would be to make all these methods `impl GenKill`. But
that doesn't work: `MaybeInitializedPlaces::terminator_effect` requires
the arg be `Self::Domain` so that `self_is_unwind_dead(place, state)`
can be called on it.)
2023-12-08 09:49:11 +11:00
Nicholas Nethercote
0158404e78 Remove BorrowckAnalyses.
This results in two non-generic types being used: `BorrowckResults` and
`BorrowckFlowState`. It's a net reduction in lines of code, and a little
easier to read.
2023-12-08 09:47:12 +11:00
Nicholas Nethercote
60e7c6898b Remove impl_visitable!.
It is used just once. With it removed, the relevant code is a little
boilerplate-y but much easier to read, and is the same length. Overall I
think it's an improvement.
2023-12-08 07:55:17 +11:00
Esteban Küber
cc80106cb5 Provide more suggestions for cloning immutable bindings
When encountering multiple mutable borrows, suggest cloning and adding
derive annotations as needed.

```
error[E0596]: cannot borrow `sm.x` as mutable, as it is behind a `&` reference
  --> $DIR/accidentally-cloning-ref-borrow-error.rs:32:9
   |
LL |     foo(&mut sm.x);
   |         ^^^^^^^^^ `sm` is a `&` reference, so the data it refers to cannot be borrowed as mutable
   |
help: `Str` doesn't implement `Clone`, so this call clones the reference `&Str`
  --> $DIR/accidentally-cloning-ref-borrow-error.rs:31:21
   |
LL |     let mut sm = sr.clone();
   |                     ^^^^^^^
help: consider annotating `Str` with `#[derive(Clone)]`
   |
LL + #[derive(Clone)]
LL | struct Str {
   |
help: consider specifying this binding's type
   |
LL |     let mut sm: &mut Str = sr.clone();
   |               ++++++++++
```

```
error[E0596]: cannot borrow `*inner` as mutable, as it is behind a `&` reference
  --> $DIR/issue-91206.rs:14:5
   |
LL |     inner.clear();
   |     ^^^^^ `inner` is a `&` reference, so the data it refers to cannot be borrowed as mutable
   |
help: you can `clone` the `Vec<usize>` value and consume it, but this might not be your desired behavior
  --> $DIR/issue-91206.rs:11:17
   |
LL |     let inner = client.get_inner_ref();
   |                 ^^^^^^^^^^^^^^^^^^^^^^
help: consider specifying this binding's type
   |
LL |     let inner: &mut Vec<usize> = client.get_inner_ref();
   |              +++++++++++++++++
```
2023-12-04 21:54:34 +00:00
Esteban Küber
210a672005 Deduplicate some logic 2023-12-04 21:54:33 +00:00
Esteban Küber
90db536741 Tweak output on specific case 2023-12-04 21:54:32 +00:00
Esteban Küber
98cfed7b97 Suggest cloning and point out obligation errors on move error
When encountering a move error, look for implementations of `Clone` for
the moved type. If there is one, check if all its obligations are met.
If they are, we suggest cloning without caveats. If they aren't, we
suggest cloning while mentioning the unmet obligations, potentially
suggesting `#[derive(Clone)]` when appropriate.

```
error[E0507]: cannot move out of a shared reference
  --> $DIR/suggest-clone-when-some-obligation-is-unmet.rs:20:28
   |
LL |     let mut copy: Vec<U> = map.clone().into_values().collect();
   |                            ^^^^^^^^^^^ ------------- value moved due to this method call
   |                            |
   |                            move occurs because value has type `HashMap<T, U, Hash128_1>`, which does not implement the `Copy` trait
   |
note: `HashMap::<K, V, S>::into_values` takes ownership of the receiver `self`, which moves value
  --> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
help: you could `clone` the value and consume it, if the `Hash128_1: Clone` trait bound could be satisfied
   |
LL |     let mut copy: Vec<U> = <HashMap<T, U, Hash128_1> as Clone>::clone(&map.clone()).into_values().collect();
   |                            ++++++++++++++++++++++++++++++++++++++++++++           +
help: consider annotating `Hash128_1` with `#[derive(Clone)]`
   |
LL + #[derive(Clone)]
LL | pub struct Hash128_1;
   |
```

Fix #109429.
2023-12-04 21:54:32 +00:00
Esteban Küber
03c88aaf21 Tweak .clone() suggestion to work in more cases
When going through auto-deref, the `<T as Clone>` impl sometimes needs
to be specified for rustc to actually clone the value and not the
reference.

```
error[E0507]: cannot move out of dereference of `S`
  --> $DIR/needs-clone-through-deref.rs:15:18
   |
LL |         for _ in self.clone().into_iter() {}
   |                  ^^^^^^^^^^^^ ----------- value moved due to this method call
   |                  |
   |                  move occurs because value has type `Vec<usize>`, which does not implement the `Copy` trait
   |
note: `into_iter` takes ownership of the receiver `self`, which moves value
  --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: you can `clone` the value and consume it, but this might not be your desired behavior
   |
LL |         for _ in <Vec<usize> as Clone>::clone(&self.clone()).into_iter() {}
   |                  ++++++++++++++++++++++++++++++            +
```

CC #109429.
2023-12-04 21:54:32 +00:00
bors
2da59b8676 Auto merge of #118470 - nnethercote:cleanup-error-handlers, r=compiler-errors
Cleanup error handlers

Mostly by making function naming more consistent. More to do after this, but this is enough for one PR.

r? compiler-errors
2023-12-02 02:48:34 +00:00
Nicholas Nethercote
a179a53565 Use Session::diagnostic in more places. 2023-12-02 09:01:35 +11:00
Nicholas Nethercote
5d1d384443 Rename HandlerInner::delay_span_bug as HandlerInner::span_delayed_bug.
Because the corresponding `Level` is `DelayedBug` and `span_delayed_bug`
follows the pattern used everywhere else: `span_err`, `span_warning`,
etc.
2023-12-02 09:01:19 +11:00
Nicholas Nethercote
7138845f61 Rename Handler::span_note_diag as struct_span_note.
Because `span_note_diag` doesn't follow the naming structure used for
the error reporting functions.
2023-12-02 08:58:25 +11:00
Rémy Rakic
de2b8b13d4 improve NLL/polonius scope equality assertion 2023-12-01 14:04:54 +00:00
Rémy Rakic
60d4eb2c1b move and maintain live loans in LivenessValues
Liveness data is pushed from multiple parts of NLL. Instead of changing
the call sites to maintain live loans, move the latter to `LivenessValues` where
this liveness data is pushed to, and maintain live loans there.

This fixes the differences in polonius scopes on some CFGs where a
variable was dead in tracing but as a MIR terminator its regions were marked
live from "constraint generation"
2023-12-01 14:04:54 +00:00
Rémy Rakic
231acddcc3 rename a couple of trivial variables
for consistency with how they're named everywhere else
2023-12-01 14:04:51 +00:00
Rémy Rakic
eddd3a7381 remove useless debug log 2023-12-01 14:02:34 +00:00
bors
caf7300432 Auto merge of #118216 - lqd:constraint-generation-non-non, r=matthewjasper
Refactor NLL constraint generation and most of polonius fact generation

As discussed in #118175, NLL "constraint generation" is only about liveness, but currently also contains legacy polonius fact generation. The latter is quite messy, and this PR cleans this up to prepare for its future removal:

- splits polonius fact generation out of NLL constraint generation
- merges NLL constraint generation to its more natural place, liveness
- extracts all of the polonius fact generation from NLLs apart from MIR typeck (as fact generation is somewhat in a single place there already, but should be cleaned up) into its own explicit module, with a single entry point instead of many.

There should be no behavior changes, and tests seem to behave the same as master: without polonius, with legacy polonius, with the in-tree polonius.

I've split everything into smaller logical commits for easier review, as it required quite a bit of code to be split and moved around, but it should all be trivial changes.

r? `@matthewjasper`
2023-12-01 11:33:43 +00:00
Josh Stone
b8cdd4338d Fix a typo in a format_args! note 2023-11-28 17:12:20 -08:00
Jake Goulding
87380cbc0c Address unused tuple struct fields in the compiler 2023-11-27 13:54:50 -05:00
bors
6cf088810f Auto merge of #118316 - Mark-Simulacrum:delete-copy-to-upvars, r=cjgillot
Remove borrowck Upvar duplication

This cuts out an extra allocation and copying over from the already cached closure capture information.
2023-11-26 21:47:19 +00:00
Mark Rousskov
d920dd8d38 Remove Upvar duplication
This cuts out an extra allocation and copying over from the already
cached closure capture information.
2023-11-26 13:19:10 -05:00
Guillaume Gomez
c6d20d70b4
Rollup merge of #118311 - bvanjoi:merge_coroutinue_into_closure, r=petrochenkov
merge `DefKind::Coroutine` into `Defkind::Closure`

Related to #118188

We no longer need to be concerned about the precise type whether it's `DefKind::Closure` or `DefKind::Coroutine`.

Furthermore, thanks for the great work done by `@petrochenkov` on investigating https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/Why.20does.20it.20hang.20when.20querying.20.EF.BB.BF.60opt_def_kind.60.3F

r? `@petrochenkov`
2023-11-26 15:44:54 +01:00
Guillaume Gomez
c67613bef9
Rollup merge of #118302 - mu001999:dead_code/clean, r=cjgillot
Clean dead codes

Clean dead codes detected by #118257
2023-11-26 15:44:54 +01:00
bohan
f23befe6c1 merge DefKind::Coroutine into DefKind::Closure 2023-11-26 21:05:08 +08:00
Rémy Rakic
f969af2195 move remaining legacy polonius fact generation out of NLL module 2023-11-26 13:03:32 +00:00
Rémy Rakic
96042bc247 merge NLL "constraint generation" into liveness 2023-11-26 13:03:32 +00:00
Rémy Rakic
626289a9e3 remove polonius fact generation from NLL constraint generation 2023-11-26 12:32:44 +00:00
Rémy Rakic
c976bc114a small polish of loan invalidations fact generation 2023-11-26 12:00:32 +00:00
Rémy Rakic
951901bedc rename polonius constraint generation to what it actually does: emit loan kills 2023-11-26 12:00:32 +00:00
Rémy Rakic
bfd88b0bf1 simplify polonius constraint generation 2023-11-26 12:00:32 +00:00
Rémy Rakic
f53412ab7c remove NLL liveness from polonius constraint generation 2023-11-26 12:00:32 +00:00
Rémy Rakic
9f14698680 extract polonius "constraint generation"
to help review, this duplicates the existing NLL + polonius constraint
generation component, before splitting them up to only do what they
individually need.
2023-11-26 12:00:32 +00:00
Rémy Rakic
49010833e9 another trivial cleanup
fix a comment and move a variable where it's used
2023-11-26 12:00:32 +00:00
Rémy Rakic
16a5da7be2 extract polonius loan invalidations fact generation
and move the polonius module to the borrowck root
2023-11-26 12:00:32 +00:00
Rémy Rakic
459a616c4f extract polonius universal regions fact generation 2023-11-26 12:00:32 +00:00
Rémy Rakic
3de68e074a extract polonius move fact generation 2023-11-26 12:00:32 +00:00
Rémy Rakic
eca2789f57 remove useless local variables 2023-11-26 12:00:32 +00:00
bors
3acb261e21 Auto merge of #118256 - petrochenkov:nohir, r=compiler-errors
rustc: `hir().local_def_id_to_hir_id()` -> `tcx.local_def_id_to_hir_id()` cleanup

Noticed this while working on https://github.com/rust-lang/rust/pull/118188.

The history here is that the method was moved from HIR map to tcx in https://github.com/rust-lang/rust/pull/93373 as a part of incremental compilation work, so it's unlikely to go back.
2023-11-26 10:43:41 +00:00
Vadim Petrochenkov
c697927f44 rustc: hir().local_def_id_to_hir_id() -> tcx.local_def_id_to_hir_id() cleanup 2023-11-26 12:41:21 +03:00
bors
274b5249eb Auto merge of #117880 - lqd:liveness-values, r=cjgillot
Refactor borrowck liveness values

This PR starts cleaning up `rustc_borrowck`, in particular around liveness values:
- refactors simple names that make no sense anymore: either referring to older structures using region elements, or to bitset containers and values.
- improves comments and fixes others
- removes unused return values and unneeded generic arguments

r? `@matthewjasper`
2023-11-26 08:44:28 +00:00
r0cky
91aee2de15 Clean dead codes 2023-11-26 09:25:07 +08:00
Michael Goulet
3b2f33ee28
Rollup merge of #118158 - nnethercote:reduce-fluent-boilerplate, r=compiler-errors
Reduce fluent boilerplate

Best reviewed one commit at a time.

r? `@davidtwco`
2023-11-25 17:23:33 -05:00
Nicholas Nethercote
57cd5e6551 Use rustc_fluent_macro::fluent_messages! directly.
Currently we always do this:
```
use rustc_fluent_macro::fluent_messages;
...
fluent_messages! { "./example.ftl" }
```
But there is no need, we can just do this everywhere:
```
rustc_fluent_macro::fluent_messages! { "./example.ftl" }
```
which is shorter.
2023-11-26 08:38:40 +11:00
Nicholas Nethercote
a733082be9 Avoid need for {D,Subd}iagnosticMessage imports.
The `fluent_messages!` macro produces uses of
`crate::{D,Subd}iagnosticMessage`, which means that every crate using
the macro must have this import:
```
use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage};
```

This commit changes the macro to instead use
`rustc_errors::{D,Subd}iagnosticMessage`, which avoids the need for the
imports.
2023-11-26 08:38:00 +11:00
Michael Goulet
fa7633dda1 Remove HirId from QPath::LangItem 2023-11-25 18:02:11 +00:00
bors
e2e978f713 Auto merge of #118203 - nnethercote:rustc_mir_dataflow, r=cjgillot
Minor `rustc_mir_dataflow` cleanups

r? `@cjgillot`
2023-11-25 07:10:46 +00:00
Nicholas Nethercote
118308ee03 Remove unused EverInitializedPlaces::tcx field. 2023-11-24 06:15:32 +11:00