Commit Graph

737 Commits

Author SHA1 Message Date
Dylan DPC 4183a99dd8 Rollup merge of #98705 - WaffleLapkin:closure_binder, r=cjgillot
Implement `for<>` lifetime binder for closures

This PR implements RFC 3216 ([TI](https://github.com/rust-lang/rust/issues/97362)) and allows code like the following:

```rust
let _f = for<'a, 'b> |a: &'a A, b: &'b B| -> &'b C { b.c(a) };
//       ^^^^^^^^^^^--- new!
```

cc ``@Aaron1011`` ``@cjgillot``
2022-07-14 14:14:21 +05:30
Dylan DPC 6894ea2488 Rollup merge of #99030 - rust-lang:notriddle/field-recovery, r=petrochenkov
diagnostics: error messages when struct literals fail to parse

If an expression is supplied where a field is expected, the parser can become convinced that it's a shorthand field syntax when it's not.

This PR addresses it by explicitly recording the permitted `:` token immediately after the identifier, and also adds a suggestion to insert the name of the field if it looks like a complex expression.

Fixes #98917
2022-07-13 19:32:35 +05:30
Michael Howell 2b0c367b21 Update compiler/rustc_parse/src/parser/expr.rs
Co-authored-by: Vadim Petrochenkov <vadim.petrochenkov@gmail.com>
2022-07-12 09:51:20 -07:00
Maybe Waffle cb145fb044 Parse closure binders
This is first step in implementing RFC 3216.
- Parse `for<'a>` before closures in ast
  - Error in lowering
- Add `closure_lifetime_binder` feature
2022-07-12 16:25:16 +04:00
Dylan DPC ade8c8c1d0 Rollup merge of #98633 - c410-f3r:yet-another-let-chain, r=estebank
Fix last `let_chains` blocker

In order to forbid things like `let x = (let y = 1);` or `if let a = 1 && { let x = let y = 1; } {}`, the parser **HAS** to know the context of `let`.

This context thing is not a surprise in the parser because you can see **a lot** of ad hoc fixes mixing parsing logic with validation logic creating code that looks more like spaghetti with tomato sauce.

To make things even greater, a new ad hoc fix was added to only allow `let`s in a valid `let_chains` context by checking the previously processed token. This was the only solution I could think of and believe me, I thought about it for a long time 👍

In the long term, it should be preferable to segregate different responsibilities or create a more robust and cleaner parser framework.

cc https://github.com/rust-lang/rust/pull/94927
cc https://github.com/rust-lang/rust/issues/53667
2022-07-12 17:06:33 +05:30
Caio 2cb68e749b Fix last let_chains blocker 2022-07-08 07:25:50 -03:00
bors 3187a86c22 Auto merge of #98638 - bjorn3:less_string_interning, r=tmiasko
Use less string interning

This removes string interning in a couple of places where doing so won't result in perf improvements. I also switched one place to use pre-interned symbols.
2022-07-08 10:03:27 +00:00
Michael Howell 4c37a2b6ce diagnostics: suggest naming a field after failing to parse 2022-07-07 15:20:08 -07:00
Michael Howell 2495451f8e diagnostics: mention the : token when struct fields fail to parse 2022-07-07 14:59:54 -07:00
bors 0991e2868e Auto merge of #98827 - aDotInTheVoid:suggest-extern-block, r=nagisa
Suggest using block for `extern "abi" fn` with no body

`@rustbot` modify labels: +A-diagnostics
2022-07-07 04:18:47 +00:00
bors 79268875b7 Auto merge of #98673 - pietroalbini:pa-bootstrap-update, r=Mark-Simulacrum
Bump bootstrap compiler

r? `@Mark-Simulacrum`
2022-07-03 06:55:50 +00:00
Nixon Enraght-Moony 48609c52c9 ast: Add span to Extern 2022-07-02 23:30:03 +01:00
Pietro Albini 6062805273 update cfg(bootstrap)s 2022-07-01 15:48:23 +02:00
Matthias Krüger c689b1018f Rollup merge of #98668 - TaKO8Ki:avoid-many-&str-to-string-conversions, r=Dylan-DPC
Avoid some `&str` to `String` conversions with `MultiSpan::push_span_label`

This patch removes some`&str` to `String` conversions with `MultiSpan::push_span_label`.
2022-06-29 20:35:07 +02:00
Takayuki Maeda 7c627cb06d avoid many &str to String conversions with MultiSpan::push_span_label 2022-06-29 21:16:43 +09:00
bjorn3 d1af1d92f6 Use pre-interned symbols in import recovery 2022-06-28 18:38:36 +00:00
David Wood 93ff0e52fb various: add rustc_lint_diagnostics to diag fns
The `rustc_lint_diagnostics` attribute is used by the diagnostic
translation/struct migration lints to identify calls where
non-translatable diagnostics or diagnostics outwith impls are being
created. Any function used in creating a diagnostic should be annotated
with this attribute so this commit adds the attribute to many more
functions.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-06-27 08:32:06 +01:00
Matthias Krüger 645d2648e1 Rollup merge of #98428 - davidtwco:translation-derive-typed-identifiers, r=oli-obk
macros: use typed identifiers in diag and subdiag derive

Using typed identifiers instead of strings with the Fluent identifiers in the diagnostic and subdiagnostic derives - this enables the diagnostic derive to benefit from the compile-time validation that comes with typed identifiers, namely that use of a non-existent Fluent identifier will not compile.

r? `````@oli-obk`````
2022-06-26 19:47:04 +02:00
Caio 7bc70c75ef [rustc_parse] Forbid lets in certain places 2022-06-25 08:08:38 -03:00
David Wood 9c0dae4836 macros: use typed identifiers in subdiag derive
As in the diagnostic derive, using typed identifiers in the
subdiagnostic derive improves the diagnostics of using the subdiagnostic
derive as Fluent messages will be confirmed to exist at compile-time.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-06-24 09:08:25 +01:00
David Wood f247a4eff1 macros: use typed identifiers in diag derive
Using typed identifiers instead of strings with the Fluent identifier
enables the diagnostic derive to benefit from the compile-time
validation that comes with typed identifiers - use of a non-existent
Fluent identifier will not compile.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-06-24 09:08:25 +01:00
Yuki Okushi c4e41197f1 Rollup merge of #98183 - dtolnay:emptybound, r=lcnr
Fix pretty printing of empty bound lists in where-clause

Repro:

```rust
macro_rules! assert_item_stringify {
    ($item:item $expected:literal) => {
        assert_eq!(stringify!($item), $expected);
    };
}

fn main() {
    assert_item_stringify! {
        fn f<'a, T>() where 'a:, T: {}
        "fn f<'a, T>() where 'a:, T: {}"
    }
}
```

Previously this assertion would fail because rustc renders the where-clause as `where 'a, T` which is invalid syntax.

This PR makes the above assertion pass.

This bug also affects `-Zunpretty=expanded`. The intention is for that to emit syntactically valid code, but the buggy output is not valid Rust syntax.

```console
$ rustc <(echo "fn f<'a, T>() where 'a:, T: {}") -Zunpretty=expanded
#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
fn f<'a, T>() where 'a, T {}
```

```console
$ rustc <(echo "fn f<'a, T>() where 'a:, T: {}") -Zunpretty=expanded | rustc -
error: expected `:`, found `,`
 --> <anon>:7:23
  |
7 | fn f<'a, T>() where 'a, T {}
  |                       ^ expected `:`
```
2022-06-20 16:41:46 +09:00
Takayuki Maeda f9236bbb22 remove the rest of unnecessary to_string 2022-06-17 18:48:09 +09:00
David Tolnay 14d48b4219 Fix pretty printing of empty type bound lists in where-clause 2022-06-16 17:24:50 -07:00
Chayim Refael Friedman 666c107112 Do not suggest adding semicolon/changing delimiters for macros in item position that originates in macros 2022-06-16 02:19:31 +00:00
Michael Goulet c2bbed33c0 Improve parsing errors and suggestions for bad if statements 2022-06-13 20:53:48 -07:00
Yuki Okushi 59f4be8bf6 Rollup merge of #95211 - terrarier2111:improve-parser, r=compiler-errors
Improve parser diagnostics

This pr fixes https://github.com/rust-lang/rust/issues/93867 and contains a couple of diagnostics related changes to the parser.
Here is a short list with some of the changes:
- don't suggest the same thing that is the current token
- suggest removing the current token if the following token is one of the suggestions (maybe incorrect)
- tell the user to put a type or lifetime after where if there is none (as a warning)
- reduce the amount of tokens suggested (via the new eat_noexpect and check_noexpect methods)

If any of these changes are undesirable, i can remove them, thanks!
2022-06-14 07:47:22 +09:00
Takayuki Maeda 3d154406f0 remove unnecessary to_string and String::new for tool_only_span_suggestion 2022-06-13 16:01:16 +09:00
Takayuki Maeda 13a63ffb67 remove unnecessary to_string and String::new 2022-06-13 15:48:40 +09:00
threadexception 7e4af5953b Improves parser diagnostics, fixes #93867 2022-06-12 17:48:52 +02:00
Takayuki Maeda bd58064310 use create_snapshot_for_diagnostic instead of clone 2022-06-12 17:27:36 +09:00
Michael Goulet 1b66eefb4b Rollup merge of #97857 - ChayimFriedman2:box-identifier-help, r=compiler-errors
Suggest escaping `box` as identifier

Fixes #97810.
2022-06-08 13:32:21 -07:00
Chayim Refael Friedman 3efac70fd2 Suggest escaping box as identifier 2022-06-08 06:17:25 +00:00
Dylan DPC 0f6b9c7f8f Rollup merge of #97823 - compiler-errors:missing-comma-match-arm, r=estebank
Recover missing comma after match arm

If we're missing a comma after a match arm expression, try parsing another pattern and a following `=>`. If we find both of those, then recover by suggesting to insert a `,`.

Fixes #80112
2022-06-08 07:37:32 +02:00
Michael Goulet 0ba2e044f7 recover import instead of use in item 2022-06-07 19:14:03 -07:00
Michael Goulet 2b11be9bca Recover missing comma after match arm 2022-06-07 02:19:51 -07:00
Waffle Maybe 3579c3653f Add spaces before and after expr in add {} suggestion
Co-authored-by: Michael Goulet <michael@errs.io>
2022-06-06 01:40:52 +04:00
Maybe Waffle b32f55338d Suggest removing label in 'label: non_block_expr 2022-06-05 23:34:11 +04:00
Maybe Waffle 902f8b4a8b Do not suggest adding labeled block if there are no labeled breaks 2022-06-05 23:12:51 +04:00
Maybe Waffle 464c435ee6 Suggest adding {} for 'label: non_block_expr 2022-06-05 16:45:29 +04:00
Dylan DPC 17a7064fc7 Rollup merge of #97587 - pvdrz:maybe-recover-from-bad-qpath-stage-2, r=davidtwco
Migrate more diagnostics to use the `#[derive(SessionDiagnostic)]`

r? ``@davidtwco``
2022-06-02 11:13:24 +02:00
Yuki Okushi 40428af6ac Rollup merge of #97166 - nnethercote:move-conditions-out, r=estebank
Move conditions out of recover/report functions.

`Parser` has six recover/report functions that are passed a boolean, and
nothing is done if the boolean has a particular value.

This PR moves the tests outside the functions. This has the following effects.
- The number of lines of code goes down.
- Some `use` items become shorter.
- Avoids the strangeness whereby 11 out of 12 calls to
  `maybe_recover_from_bad_qpath` pass `true` as the second argument.
- Makes it clear at the call site that only one of
  `maybe_recover_from_bad_type_plus` and `maybe_report_ambiguous_plus` will be
  run.

r? `@estebank`
2022-06-02 06:44:27 +09:00
Christian Poveda a1125d4bc4 rename sp to span 2022-06-01 07:14:33 -05:00
Christian Poveda 1dbd932fbb migrate check_for_for_in_in_typo diagnostic 2022-05-31 16:28:05 -05:00
Christian Poveda 1ab5105f36 merge diagnostics about incorrect uses of .await 2022-05-31 15:07:44 -05:00
Christian Poveda d9d4d1c6f5 migrate error_on_incorrect_await diagnostic 2022-05-31 14:32:07 -05:00
Christian Poveda 0fe68c7ddc use suggestion_short for incorrect semicolon diagnostic 2022-05-31 12:56:26 -05:00
Christian Poveda 0268d7bd28 migrate recover_from_await_method_call diagnostic 2022-05-31 12:33:35 -05:00
Christian Poveda 457c301c6d migrate maybe_consume_incorrect_semicolon diagnostic 2022-05-31 11:45:44 -05:00
Christian Poveda 80ceeb401e migrate maybe_recover_from_bad_qpath_stage_2 diagnostic 2022-05-31 11:22:26 -05:00