Commit graph

54 commits

Author SHA1 Message Date
Jacob Pratt 936d76009b
Rollup merge of #126127 - Alexendoo:other-trait-diag, r=pnkfelix
Spell out other trait diagnostic

I recently saw somebody confused about the diagnostic thinking it was suggesting to add an `as` cast. This change is longer but I think it's clearer
2024-06-16 03:41:57 -04:00
Esteban Küber 5de8e6edfc Tweak output of import suggestions
When both `std::` and `core::` items are available, only suggest the
`std::` ones. We ensure that in `no_std` crates we suggest `core::`
items.

Ensure that the list of items suggested to be imported are always in the
order of local crate items, `std`/`core` items and finally foreign crate
items.

Tweak wording of import suggestion: if there are multiple items but they
are all of the same kind, we use the kind name and not the generic "items".

Fix #83564.
2024-06-13 20:22:21 +00:00
Alex Macleod d0112c6849 Spell out other trait diagnostic 2024-06-12 12:34:47 +00:00
Gurinder Singh 6289ed8428 Remove note about iteration count in coerce
and replace it with a simple note suggesting
returning a value.

The type mismatch error was never due to
how many times the loop iterates. It is more
because of the peculiar structure of what the for
loop desugars to. So the note talking about
iteration count didn't make sense
2024-04-30 12:46:59 +05:30
Oli Scherer ae24fef028 Use TraitRef::to_string sorting in favor of TraitRef::ord, as the latter compares DefIds which we need to avoid 2024-03-27 14:02:15 +00:00
Oli Scherer ae50e36dfa Merge collect_mod_item_types query into check_well_formed 2024-03-07 14:26:31 +00:00
Nicholas Nethercote 951f2d9ae2 Use LitKind::Err for floats with empty exponents.
This prevents a follow-up type error in a test, which seems fine.
2024-02-28 20:59:27 +11:00
Markus Reiter f12d248a6a
Implement NonZero traits generically. 2024-02-17 21:58:56 +01:00
许杰友 Jieyou Xu (Joe) ec2cc761bc
[AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
Guillaume Gomez f81fd901db
Rollup merge of #119928 - d-sonuga:into-iter-sugg, r=compiler-errors
suggest `into_iter()` when `Iterator` method called on `impl IntoIterator`

Fix for issue #117711.
2024-02-16 17:08:11 +01:00
Oli Scherer c4371a79de
Rollup merge of #120530 - trevyn:issue-116434, r=compiler-errors
Be less confident when `dyn` suggestion is not checked for object safety

#120275 no longer checks bare traits for object safety when making a `dyn` suggestion on Rust < 2021. In this case, qualify the suggestion with a note that the trait must be object safe, to prevent user confusion as seen in #116434

r? ```@fmease```
2024-02-14 11:53:39 +01:00
demilade e99766d885 suggest into_iter() when Iterator method called on impl IntoIterator 2024-02-12 22:11:48 +01:00
trevyn 29fd82b24e Be less confident when dyn suggestion is not checked for object safety 2024-02-09 20:47:50 -08:00
Michael Goulet 08af64e96b Regular closures now built-in impls for AsyncFn* 2024-02-06 20:52:13 +00:00
Guillaume Gomez 0a4fd52c91
Rollup merge of #120293 - estebank:issue-102629, r=nnethercote
Deduplicate more sized errors on call exprs

Change the implicit `Sized` `Obligation` `Span` for call expressions to include the whole expression. This aids the existing deduplication machinery to reduce the number of errors caused by a single unsized expression.
2024-01-30 16:57:47 +01:00
Matthias Krüger 8c6cf3c934
Rollup merge of #119305 - compiler-errors:async-fn-traits, r=oli-obk
Add `AsyncFn` family of traits

I'm proposing to add a new family of `async`hronous `Fn`-like traits to the standard library for experimentation purposes.

## Why do we need new traits?

On the user side, it is useful to be able to express `AsyncFn` trait bounds natively via the parenthesized sugar syntax, i.e. `x: impl AsyncFn(&str) -> String` when experimenting with async-closure code.

This also does not preclude `AsyncFn` becoming something else like a trait alias if a more fundamental desugaring (which can take many[^1] different[^2] forms) comes around. I think we should be able to play around with `AsyncFn` well before that, though.

I'm also not proposing stabilization of these trait names any time soon (we may even want to instead express them via new syntax, like `async Fn() -> ..`), but I also don't think we need to introduce an obtuse bikeshedding name, since `AsyncFn` just makes sense.

## The lending problem: why not add a more fundamental primitive of `LendingFn`/`LendingFnMut`?

Firstly, for `async` closures to be as flexible as possible, they must be allowed to return futures which borrow from the async closure's captures. This can be done by introducing `LendingFn`/`LendingFnMut` traits, or (equivalently) by adding a new generic associated type to `FnMut` which allows the return type to capture lifetimes from the `&mut self` argument of the trait. This was proposed in one of [Niko's blog posts](https://smallcultfollowing.com/babysteps/blog/2023/05/09/giving-lending-and-async-closures/).

Upon further experimentation, for the purposes of closure type- and borrow-checking, I've come to the conclusion that it's significantly harder to teach the compiler how to handle *general* lending closures which may borrow from their captures. This is, because unlike `Fn`/`FnMut`, the `LendingFn`/`LendingFnMut` traits don't form a simple "inheritance" hierarchy whose top trait is `FnOnce`.

```mermaid
flowchart LR
    Fn
    FnMut
    FnOnce
    LendingFn
    LendingFnMut

    Fn -- isa --> FnMut
    FnMut -- isa --> FnOnce

    LendingFn -- isa --> LendingFnMut

    Fn -- isa --> LendingFn
    FnMut -- isa --> LendingFnMut
```

For example:

```
fn main() {
  let s = String::from("hello, world");
  let f = move || &s;
  let x = f(); // This borrows `f` for some lifetime `'1` and returns `&'1 String`.
```

That trait hierarchy means that in general for "lending" closures, like `f` above, there's not really a meaningful return type for `<typeof(f) as FnOnce>::Output` -- it can't return `&'static str`, for example.

### Special-casing this problem:

By splitting out these traits manually, and making sure that each trait has its own associated future type, we side-step the issue of having to answer the questions of a general `LendingFn`/`LendingFnMut` implementation, since the compiler knows how to generate built-in implementations for first-class constructs like async closures, including the required future types for the (by-move) `AsyncFnOnce` and (by-ref) `AsyncFnMut`/`AsyncFn` trait implementations.

[^1]: For example, with trait transformers, we may eventually be able to write: `trait AsyncFn = async Fn;`
[^2]: For example, via the introduction of a more fundamental "`LendingFn`" trait, plus a [special desugaring with augmented trait aliases](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Lending.20closures.20and.20Fn*.28.29.20-.3E.20impl.20Trait/near/408471480).
2024-01-25 08:39:41 +01:00
Esteban Küber a9841936fe Deduplicate more sized errors on call exprs
Change the implicit `Sized` `Obligation` `Span` for call expressions to
include the whole expression. This aids the existing deduplication
machinery to reduce the number of errors caused by a single unsized
expression.
2024-01-24 02:53:15 +00:00
Oli Scherer 0978f6e010 Avoid silencing relevant follow-up errors 2024-01-09 21:08:16 +00:00
Michael Goulet 0e6f7c6c7c Add AsyncFn family of traits 2023-12-25 20:31:28 +00:00
Nilstrieb 41e8d152dc Show number in error message even for one error
Co-authored-by: Adrian <adrian.iosdev@gmail.com>
2023-11-24 19:15:52 +01:00
Esteban Küber 289ce572b3 tweak logic of "unknown field" label 2023-11-18 00:40:11 +00:00
Esteban Küber 4f7dddd4a1 recover primary span label 2023-11-16 17:00:23 +00:00
Esteban Küber 8bd8f3b090 Suggest unwrap() on field not found for Result/Option
When encountering a `Result<T, _>` or `Option<T>` where `T` has a field
that's being accessed, suggest calling `.unwrap()` to get to the field.
2023-11-16 17:00:23 +00:00
Esteban Küber dfa75391f8 Suggest field typo through derefs
Take into account implicit dereferences when suggesting fields.

```
error[E0609]: no field `longname` on type `Arc<S>`
  --> $DIR/suggest-field-through-deref.rs:10:15
   |
LL |     let _ = x.longname;
   |               ^^^^^^^^ help: a field with a similar name exists: `long_name`
```

CC https://github.com/rust-lang/rust/issues/78374#issuecomment-719564114
2023-11-16 17:00:23 +00:00
bors 111adde7ed Auto merge of #115324 - francorbacho:master, r=davidtwco
Suggest removing redundant arguments in format!()

Closes #105225. This is also a follow-up to #105635, which seems to have become stale.

r? `@estebank`
2023-10-23 00:51:35 +00:00
Esteban Küber 890e92feed Unify suggestion wording 2023-10-17 17:33:55 +00:00
francorbacho c8ee7db6ea Only give autofix suggestion when no named args are present 2023-10-05 16:11:31 +02:00
francorbacho 38b0182832 Add suggestion test 2023-10-05 16:11:31 +02:00
francorbacho 905bace904 Highlight redundant arguments instead of the whole format string 2023-10-05 16:11:31 +02:00
francorbacho fcdd5c0b2d Plurals in format redundant arguments suggestion 2023-10-05 16:11:31 +02:00
francorbacho 04fc051a34 Use diagnostic impls and add suggestions in redundant format!() args 2023-10-05 16:09:57 +02:00
francorbacho 93df9e6d7d Suggest removing redundant arguments in format!() 2023-10-05 16:05:42 +02:00
León Orell Valerian Liehr 867cc41b5b
clean up struct field suggestions 2023-10-04 21:36:04 +02:00
Esteban Küber 3848ffcee7 Tweak wording of missing angle backets in qualified path 2023-09-28 00:37:20 +00:00
Esteban Küber 55f8c66a60 Point at return type when it influences non-first match arm
When encountering code like

```rust
fn foo() -> i32 {
    match 0 {
        1 => return 0,
        2 => "",
        _ => 1,
    }
}
```

Point at the return type and not at the prior arm, as that arm has type
`!` which isn't influencing the arm corresponding to arm `2`.

Fix #78124.
2023-08-14 21:43:56 +00:00
Mu001999 049c728c60 Suggests turbofish in patterns 2023-08-01 23:30:40 +08:00
Michael Goulet fe870424a7 Do not set up wrong span for adjustments 2023-07-10 20:09:26 +00:00
Michael Goulet 2c33dfea76 Don't sort strings right after we just sorted by types 2023-06-27 23:31:06 +00:00
Michael Goulet 140c011ca6 Don't mention already set fields 2023-06-05 21:00:08 +00:00
Ezra Shaw 57c6a3183c
tweak "make mut" spans (No. 3) 2023-05-05 22:40:05 +12:00
Ezra Shaw 9624d2b08e
tweak "make mut" spans (No. 2) 2023-05-05 22:40:05 +12:00
Ezra Shaw fd8aa5ec7d
tweak "make mut" spans when assigning to locals 2023-05-05 22:40:04 +12:00
Nilstrieb c63b6a437e Rip it out
My type ascription
Oh rip it out
Ah
If you think we live too much then
You can sacrifice diagnostics
Don't mix your garbage
Into my syntax
So many weird hacks keep diagnostics alive
Yet I don't even step outside
So many bad diagnostics keep tyasc alive
Yet tyasc doesn't even bother to survive!
2023-05-01 16:15:13 +08:00
Deadbeef 4c6ddc036b fix library and rustdoc tests 2023-04-16 11:38:52 +00:00
Esteban Küber 9fadcc143a Special-case item attributes in the suggestion output 2023-04-12 22:50:10 +00:00
Esteban Küber 5b40aa5eb4 Tweak output for 'add line' suggestion 2023-04-12 22:50:10 +00:00
bohan d8a4e7cf51 fix(middle): emit error rather than delay bug when reaching limit 2023-04-03 11:08:14 +08:00
lcnr c63861b9d5 evaluate: improve and fix recursion depth handling 2023-03-21 09:57:22 +01:00
Jacob Pratt c2f2a3cff2
Add test for println! typo 2023-02-19 03:46:03 +00:00
Michael Goulet 9dd5d3e8e4 Recover _ as .. in field pattern 2023-02-02 06:10:02 +00:00