Commit Graph

240 Commits

Author SHA1 Message Date
Michael Goulet
a8877cf738 Handle reporting invariance of fn pointer 2022-04-04 19:37:14 -07:00
lcnr
89c66eb42d update comment 2022-03-31 15:41:52 +02:00
lcnr
d7cada1767 obligation cause: RepeatVec -> RepeatValueCopy 2022-03-31 12:51:46 +02:00
Dylan DPC
1b7d6dbd30
Rollup merge of #95497 - nyurik:compiler-spell-comments, r=compiler-errors
Spellchecking compiler comments

This PR cleans up the rest of the spelling mistakes in the compiler comments. This PR does not change any literal or code spelling issues.
2022-03-31 04:57:28 +02:00
bors
a40c595695 Auto merge of #95436 - cjgillot:static-mut, r=oli-obk
Remember mutability in `DefKind::Static`.

This allows to compute the `BodyOwnerKind` from `DefKind` only, and
removes a direct dependency of some MIR queries onto HIR.

As a side effect, it also simplifies metadata, since we don't need 4
flavours of `EntryKind::*Static` any more.
2022-03-30 22:09:56 +00:00
Yuri Astrakhan
5160f8f843 Spellchecking compiler comments
This PR cleans up the rest of the spelling mistakes in the compiler comments. This PR does not change any literal or code spelling issues.
2022-03-30 15:14:15 -04:00
bors
05142a7e44 Auto merge of #95466 - Dylan-DPC:rollup-g7ddr8y, r=Dylan-DPC
Rollup of 5 pull requests

Successful merges:

 - #95294 (Document Linux kernel handoff in std::io::copy and std::fs::copy)
 - #95443 (Clarify how `src/tools/x` searches for python)
 - #95452 (fix since field version for termination stabilization)
 - #95460 (Spellchecking compiler code)
 - #95461 (Spellchecking some comments)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2022-03-30 07:45:42 +00:00
Dylan DPC
03b3993ae8
Rollup merge of #95461 - nyurik:spelling, r=lcnr
Spellchecking some comments

This PR attempts to clean up some minor spelling mistakes in comments
2022-03-30 09:10:07 +02:00
Dylan DPC
a629b2ac68
Rollup merge of #95460 - nyurik:spelling-str, r=lcnr
Spellchecking compiler code

Address some spelling mistakes in strings, private function names, and function params.
2022-03-30 09:10:06 +02:00
Yuri Astrakhan
a9cc3f6564 Spellchecking compiler code
Address some spelling mistakes in strings, private function names, and function params.
2022-03-30 01:42:10 -04:00
Yuri Astrakhan
7e8201ae0a Spellchecking some comments
This PR attempts to clean up some minor spelling mistakes in comments
2022-03-30 01:39:38 -04:00
bors
f132bcf3bd Auto merge of #94081 - oli-obk:lazy_tait_take_two, r=nikomatsakis
Lazy type-alias-impl-trait take two

### user visible change 1: RPIT inference from recursive call sites

Lazy TAIT has an insta-stable change. The following snippet now compiles, because opaque types can now have their hidden type set from wherever the opaque type is mentioned.

```rust
fn bar(b: bool) -> impl std::fmt::Debug {
    if b {
        return 42
    }
    let x: u32 = bar(false); // this errors on stable
    99
}
```

The return type of `bar` stays opaque, you can't do `bar(false) + 42`, you need to actually mention the hidden type.

### user visible change 2: divergence between RPIT and TAIT in return statements

Note that `return` statements and the trailing return expression are special with RPIT (but not TAIT). So

```rust
#![feature(type_alias_impl_trait)]
type Foo = impl std::fmt::Debug;

fn foo(b: bool) -> Foo {
    if b {
        return vec![42];
    }
    std::iter::empty().collect() //~ ERROR `Foo` cannot be built from an iterator
}

fn bar(b: bool) -> impl std::fmt::Debug {
    if b {
        return vec![42]
    }
    std::iter::empty().collect() // Works, magic (accidentally stabilized, not intended)
}
```

But when we are working with the return value of a recursive call, the behavior of RPIT and TAIT is the same:

```rust
type Foo = impl std::fmt::Debug;

fn foo(b: bool) -> Foo {
    if b {
        return vec![];
    }
    let mut x = foo(false);
    x = std::iter::empty().collect(); //~ ERROR `Foo` cannot be built from an iterator
    vec![]
}

fn bar(b: bool) -> impl std::fmt::Debug {
    if b {
        return vec![];
    }
    let mut x = bar(false);
    x = std::iter::empty().collect(); //~ ERROR `impl Debug` cannot be built from an iterator
    vec![]
}
```

### user visible change 3: TAIT does not merge types across branches

In contrast to RPIT, TAIT does not merge types across branches, so the following does not compile.

```rust
type Foo = impl std::fmt::Debug;

fn foo(b: bool) -> Foo {
    if b {
        vec![42_i32]
    } else {
        std::iter::empty().collect()
        //~^ ERROR `Foo` cannot be built from an iterator over elements of type `_`
    }
}
```

It is easy to support, but we should make an explicit decision to include the additional complexity in the implementation (it's not much, see a721052457cf513487fb4266e3ade65c29b272d2 which needs to be reverted to enable this).

### PR formalities

previous attempt: #92007

This PR also includes #92306 and #93783, as they were reverted along with #92007 in #93893

fixes #93411
fixes #88236
fixes #89312
fixes #87340
fixes #86800
fixes #86719
fixes #84073
fixes #83919
fixes #82139
fixes #77987
fixes #74282
fixes #67830
fixes #62742
fixes #54895
2022-03-30 05:04:45 +00:00
Camille GILLOT
21a554caf6 Remember mutability in DefKind::Static.
This allows to compute the `BodyOwnerKind` from `DefKind` only, and
removes a direct dependency of some MIR queries onto HIR.

As a side effect, it also simplifies metadata, since we don't need 4
flavours of `EntryKind::*Static` any more.
2022-03-29 18:50:52 +02:00
Dylan DPC
eceb173de9
Rollup merge of #95415 - notriddle:notriddle/issue-82081, r=Dylan-DPC
diagnostics: regression test for HashMap iter_mut suggestion

Closes #82081
2022-03-29 17:11:52 +02:00
Michael Goulet
ac95e80186 Suggest function borrow ignoring needs_note
`needs_note` is false if we've already suggested why the type is Copy...
but that has nothing to do with the diagnostic.
2022-03-28 22:27:07 -07:00
Michael Goulet
a9b02e13a6 drive-by: move Copy bound suggestion to its own function 2022-03-28 22:27:07 -07:00
Michael Goulet
0f3c2933e0 Add suggestion to borrow opaque Fn and FnMut instead of move 2022-03-28 22:27:06 -07:00
Michael Howell
a063b3a4b6 diagnostics: do not suggest map.iter_mut()() 2022-03-28 11:48:14 -07:00
Oli Scherer
1163aa7e72 Remove opaque type obligation and just register opaque types as they are encountered.
This also registers obligations for the hidden type immediately.
2022-03-28 16:57:45 +00:00
Oli Scherer
d5b6510bfb Have the spans of TAIT type conflict errors point to the actual site instead of the owning function 2022-03-28 16:30:59 +00:00
Oli Scherer
264cd05b16 Revert "Auto merge of #93893 - oli-obk:sad_revert, r=oli-obk"
This reverts commit 6499c5e7fc, reversing
changes made to 78450d2d60.
2022-03-28 16:27:14 +00:00
bors
c74925438c Auto merge of #95149 - cjgillot:once-diag, r=estebank
Remove `Session::one_time_diagnostic`

This is untracked mutable state, which modified the behaviour of queries.
It was used for 2 things: some full-blown errors, but mostly for lint declaration notes ("the lint level is defined here" notes).

It is replaced by the diagnostic deduplication infra which already exists in the diagnostic emitter.
A new diagnostic level `OnceNote` is introduced specifically for lint notes, to deduplicate subdiagnostics.

As a drive-by, diagnostic emission takes a `&mut` to allow dropping the `SubDiagnostic`s.
2022-03-26 00:54:54 +00:00
Camille GILLOT
056951d628 Take &mut Diagnostic in emit_diagnostic.
Taking a Diagnostic by move would break the usual pattern
`diag.label(..).emit()`.
2022-03-20 20:36:08 +01:00
Michael Howell
306dcd6efa diagnostics: do not give Option::as_ref suggestion for complex match
Fixes #82528
2022-03-19 15:54:30 -07:00
Dylan DPC
a8956e6618
Rollup merge of #94698 - WaffleLapkin:simplify-copy-suggestions, r=estebank
Remove redundant code from copy-suggestions

Follow up to #94375, just remove some code that is not necessary anymore. This may make the perf of such suggestions a little bit worse, but I don't think this is significant.

r? `@estebank`
2022-03-17 22:55:03 +01:00
mark
bb8d4307eb rustc_error: make ErrorReported impossible to construct
There are a few places were we have to construct it, though, and a few
places that are more invasive to change. To do this, we create a
constructor with a long obvious name.
2022-03-16 10:35:24 -05:00
Nicholas Nethercote
ca5525d564 Improve AdtDef interning.
This commit makes `AdtDef` use `Interned`. Much the commit is tedious
changes to introduce getter functions. The interesting changes are in
`compiler/rustc_middle/src/ty/adt.rs`.
2022-03-11 13:31:24 +11:00
bors
85ce7fdfa2 Auto merge of #94737 - lcnr:pass-stuff-by-value, r=davidtwco
add `#[rustc_pass_by_value]` to more types

the only interesting changes here should be to `TransitiveRelation`, but I believe to be highly unlikely that we will ever use a non `Copy` type with this type.
2022-03-10 00:15:39 +00:00
bors
10dccdc7fc Auto merge of #94515 - estebank:tweak-move-error, r=davidtwco
Tweak move error

Point at method definition that causes type to be consumed.

Fix #94056.
2022-03-09 08:58:47 +00:00
lcnr
b8135fd5c8 add #[rustc_pass_by_value] to more types 2022-03-08 15:39:52 +01:00
Maybe Waffle
74d0866c62 Remove redundant code from copy-suggestions 2022-03-07 23:19:53 +04:00
Michael Goulet
5ddaa2d5e5 Erase regions when checking for missing Copy predicates 2022-03-06 17:21:39 -08:00
pierwill
f684acdd7e Update itertools
Update to 0.10.1
2022-03-04 11:54:28 -06:00
Esteban Kuber
98752776b8 Tweak move error
Point at method definition that causes type to be consumed.

Fix #94056.
2022-03-03 21:21:57 +00:00
Dylan DPC
7537b2036a
Rollup merge of #94375 - WaffleLapkin:copy-suggestion, r=estebank
Adt copy suggestions

Previously we've only suggested adding `Copy` bounds when the type being moved/copied is a type parameter (generic). With this PR we also suggest adding bounds when a type
- Can be copy
- All predicates that need to be satisfied for that are based on type params

i.e. we will suggest `T: Copy` for `Option<T>`, but won't suggest anything for `Option<String>`.

An example:
```rust
fn duplicate<T>(t: Option<T>) -> (Option<T>, Option<T>) {
    (t, t)
}
```
New error (current compiler doesn't provide `help`:):
```text
error[E0382]: use of moved value: `t`
 --> t.rs:2:9
  |
1 | fn duplicate<T>(t: Option<T>) -> (Option<T>, Option<T>) {
  |                 - move occurs because `t` has type `Option<T>`, which does not implement the `Copy` trait
2 |     (t, t)
  |      -  ^ value used here after move
  |      |
  |      value moved here
  |
help: consider restricting type parameter `T`
  |
1 | fn duplicate<T: Copy>(t: Option<T>) -> (Option<T>, Option<T>) {
  |               ++++++
```

Fixes #93623
r? ``````````@estebank``````````
``````````@rustbot`````````` label +A-diagnostics +A-suggestion-diagnostics +C-enhancement

----

I'm not at all sure if this is the right implementation for this kind of suggestion, but it seems to work :')
2022-03-03 01:09:11 +01:00
mark
e489a94dee rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
Maybe Waffle
400d343796 Suggest adding Copy bound when Adt is moved out
Previously we've only suggested adding `Copy` bounds when the type being
moved/copied is a type parameter (generic). With this commit we also
suggest adding bounds when a type
- Can be copy
- All predicates that need to be satisfied for that are based on type
  params

i.e. we will suggest `T: Copy` for `Option<T>`, but won't suggest
anything for `Option<String>`.

Future work: it would be nice to also suggest adding `.clone()` calls
2022-03-01 16:56:00 +03:00
cuishuang
eb2b9441e7 compiler: fix some typos 2022-03-01 20:02:47 +08:00
Caio
0eb8b32de3 3 - Make more use of let_chains
Continuation of #94376.

cc #53667
2022-02-27 11:10:20 -03:00
Matthias Krüger
3b276cbe04
Rollup merge of #93603 - connorff:compute-polonius-liveness-facts-flag, r=ecstatic-morse
Populate liveness facts when calling `get_body_with_borrowck_facts` without `-Z polonius`

For a new feature of [Flowistry](https://github.com/willcrichton/flowistry), a static-analysis tool, we need to obtain a `mir::Body`'s liveness facts using `get_body_with_borrowck_facts` (added in #86977). We'd like to do this without passing `-Z polonius` as a compiler arg to avoid borrow checking the entire crate.

Support for doing this was added in #88983, but the Polonius input facts used for liveness analysis are empty. This happens because the liveness input facts are populated in `liveness::generate` depending only on the value of `AllFacts::enabled` (which is toggled via compiler args).

This PR propagates the [`use_polonius`](8b09ba6a5d/compiler/rustc_borrowck/src/nll.rs (L168)) flag to `liveness::generate` to support populating liveness facts without requiring the `-Z polonius` flag.

This fix is somewhat patchy - if it'd be better to add more widely-accessible state (like `AllFacts::enabled`) I'd be open to ideas!
2022-02-26 00:49:20 +01:00
Mark Rousskov
22c3a71de1 Switch bootstrap cfgs 2022-02-25 08:00:52 -05:00
bors
d4de1f230c Auto merge of #93368 - eddyb:diagbld-guarantee, r=estebank
rustc_errors: let `DiagnosticBuilder::emit` return a "guarantee of emission".

That is, `DiagnosticBuilder` is now generic over the return type of `.emit()`, so we'll now have:
* `DiagnosticBuilder<ErrorReported>` for error (incl. fatal/bug) diagnostics
  * can only be created via a `const L: Level`-generic constructor, that limits allowed variants via a `where` clause, so not even `rustc_errors` can accidentally bypass this limitation
  * asserts `diagnostic.is_error()` on emission, just in case the construction restriction was bypassed (e.g. by replacing the whole `Diagnostic` inside `DiagnosticBuilder`)
  * `.emit()` returns `ErrorReported`, as a "proof" token that `.emit()` was called
    (though note that this isn't a real guarantee until after completing the work on
     #69426)
* `DiagnosticBuilder<()>` for everything else (warnings, notes, etc.)
  * can also be obtained from other `DiagnosticBuilder`s by calling `.forget_guarantee()`

This PR is a companion to other ongoing work, namely:
* #69426
  and it's ongoing implementation:
  #93222
  the API changes in this PR are needed to get statically-checked "only errors produce `ErrorReported` from `.emit()`", but doesn't itself provide any really strong guarantees without those other `ErrorReported` changes
* #93244
  would make the choices of API changes (esp. naming) in this PR fit better overall

In order to be able to let `.emit()` return anything trustable, several changes had to be made:
* `Diagnostic`'s `level` field is now private to `rustc_errors`, to disallow arbitrary "downgrade"s from "some kind of error" to "warning" (or anything else that doesn't cause compilation to fail)
  * it's still possible to replace the whole `Diagnostic` inside the `DiagnosticBuilder`, sadly, that's harder to fix, but it's unlikely enough that we can paper over it with asserts on `.emit()`
* `.cancel()` now consumes `DiagnosticBuilder`, preventing `.emit()` calls on a cancelled diagnostic
  * it's also now done internally, through `DiagnosticBuilder`-private state, instead of having a `Level::Cancelled` variant that can be read (or worse, written) by the user
  * this removes a hazard of calling `.cancel()` on an error then continuing to attach details to it, and even expect to be able to `.emit()` it
  * warnings were switched to *only* `can_emit_warnings` on emission (instead of pre-cancelling early)
  * `struct_dummy` was removed (as it relied on a pre-`Cancelled` `Diagnostic`)
* since `.emit()` doesn't consume the `DiagnosticBuilder` <sub>(I tried and gave up, it's much more work than this PR)</sub>,
  we have to make `.emit()` idempotent wrt the guarantees it returns
  * thankfully, `err.emit(); err.emit();` can return `ErrorReported` both times, as the second `.emit()` call has no side-effects *only* because the first one did do the appropriate emission
* `&mut Diagnostic` is now used in a lot of function signatures, which used to take `&mut DiagnosticBuilder` (in the interest of not having to make those functions generic)
  * the APIs were already mostly identical, allowing for low-effort porting to this new setup
  * only some of the suggestion methods needed some rework, to have the extra `DiagnosticBuilder` functionality on the `Diagnostic` methods themselves (that change is also present in #93259)
  * `.emit()`/`.cancel()` aren't available, but IMO calling them from an "error decorator/annotator" function isn't a good practice, and can lead to strange behavior (from the caller's perspective)
  * `.downgrade_to_delayed_bug()` was added, letting you convert any `.is_error()` diagnostic into a `delay_span_bug` one (which works because in both cases the guarantees available are the same)

This PR should ideally be reviewed commit-by-commit, since there is a lot of fallout in each.

r? `@estebank` cc `@Manishearth` `@nikomatsakis` `@mark-i-m`
2022-02-25 00:46:04 +00:00
bors
4b043faba3 Auto merge of #94131 - Mark-Simulacrum:fmt-string, r=oli-obk
Always format to internal String in FmtPrinter

This avoids monomorphizing for different parameters, decreasing generic code
instantiated downstream from rustc_middle -- locally seeing 7% unoptimized LLVM IR
line wins on rustc_borrowck, for example.

We likely can't/shouldn't get rid of the Result-ness on most functions, though some
further cleanup avoiding fmt::Error where we now know it won't occur may be possible,
though somewhat painful -- fmt::Write is a pretty annoying API to work with in practice
when you're trying to use it infallibly.
2022-02-24 17:18:07 +00:00
Eduard-Mihai Burtescu
b7e95dee65 rustc_errors: let DiagnosticBuilder::emit return a "guarantee of emission". 2022-02-23 06:38:52 +00:00
Eduard-Mihai Burtescu
0b9d70cf6d rustc_errors: take self by value in DiagnosticBuilder::cancel. 2022-02-23 06:08:06 +00:00
Eduard-Mihai Burtescu
02ff9e0aef Replace &mut DiagnosticBuilder, in signatures, with &mut Diagnostic. 2022-02-23 05:38:19 +00:00
bors
bafe8d06e0 Auto merge of #93984 - nnethercote:ChunkedBitSet, r=Mark-Simulacrum
Introduce `ChunkedBitSet` and use it for some dataflow analyses.

This reduces peak memory usage significantly for some programs with very
large functions.

r? `@ghost`
2022-02-23 01:26:07 +00:00
Nicholas Nethercote
36b495f3cf Introduce ChunkedBitSet and use it for some dataflow analyses.
This reduces peak memory usage significantly for some programs with very
large functions, such as:
- `keccak`, `unicode_normalization`, and `match-stress-enum`, from
  the `rustc-perf` benchmark suite;
- `http-0.2.6` from crates.io.

The new type is used in the analyses where the bitsets can get huge
(e.g. 10s of thousands of bits): `MaybeInitializedPlaces`,
`MaybeUninitializedPlaces`, and `EverInitializedPlaces`.

Some refactoring was required in `rustc_mir_dataflow`. All existing
analysis domains are either `BitSet` or a trivial wrapper around
`BitSet`, and access in a few places is done via `Borrow<BitSet>` or
`BorrowMut<BitSet>`. Now that some of these domains are `ClusterBitSet`,
that no longer works. So this commit replaces the `Borrow`/`BorrowMut`
usage with a new trait `BitSetExt` containing the needed bitset
operations. The impls just forward these to the underlying bitset type.
This required fiddling with trait bounds in a few places.

The commit also:
- Moves `static_assert_size` from `rustc_data_structures` to
  `rustc_index` so it can be used in the latter; the former now
  re-exports it so existing users are unaffected.
- Factors out some common "clear excess bits in the final word"
  functionality in `bit_set.rs`.
- Uses `fill` in a few places instead of loops.
2022-02-23 10:18:49 +11:00
lcnr
1245131a11 use List<Ty<'tcx>> for tuples 2022-02-21 07:09:11 +01:00
Mark Rousskov
efb99d780d Always format to internal String in FmtPrinter
This avoids monomorphizing for different parameters, decreasing generic code
instantiated downstream from rustc_middle.
2022-02-20 19:32:18 -05:00