Commit graph

579 commits

Author SHA1 Message Date
Takayuki Maeda
47d91bc9e6 suggest removing a semicolon after derive attributes
use current token span
2022-03-06 01:28:35 +09:00
Esteban Kuber
004f2ed219 Do not recover from Ty? in macro parsing
Follow up to #92746. Address #94510.
2022-03-04 02:03:55 +00:00
Felix S. Klock II
d37da1e332 Adjusted diagnostic output so that if there is no use in a item sequence,
then we just suggest the first legal position where you could inject a use.

To do this, I added `inject_use_span` field to `ModSpans`, and populate it in
parser (it is the span of the first token found after inner attributes, if any).
Then I rewrote the use-suggestion code to utilize it, and threw out some stuff
that is now unnecessary with this in place. (I think the result is easier to
understand.)

Then I added a test of issue 87613.
2022-03-03 18:58:37 -05:00
Felix S. Klock II
b82795244e Associate multiple with a crate too. 2022-03-03 18:45:25 -05:00
Felix S. Klock II
e9035f7bef refactor: prepare to associate multiple spans with a module. 2022-03-03 14:38:50 -05:00
mark
e489a94dee rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
Matthias Krüger
5be38d2bb3
Rollup merge of #94445 - c410-f3r:more-let-chains, r=cjgillot
4 - Make more use of `let_chains`

Continuation of #94376.

cc #53667
2022-02-28 20:05:17 +01:00
Esteban Kuber
f42b4f595e Tweak diagnostics
* Recover from invalid `'label: ` before block.
* Make suggestion to enclose statements in a block multipart.
* Point at `match`, `while`, `loop` and `unsafe` keywords when failing
  to parse their expression.
* Do not suggest `{ ; }`.
* Do not suggest `|` when very unlikely to be what was wanted (in `let`
  statements).
2022-02-28 18:22:45 +00:00
Caio
e3e902bb06 4 - Make more use of let_chains
Continuation of #94376.

cc #53667
2022-02-28 07:49:56 -03:00
Michael Goulet
6b52ac240a Suggest {} around more bad const generic exprs 2022-02-25 17:16:58 -08:00
Matthias Krüger
731cd3fbeb
Rollup merge of #94344 - notriddle:notriddle/suggest-parens-more, r=oli-obk
diagnostic: suggest parens when users want logical ops, but get closures

Fixes #93536
2022-02-25 14:14:40 +01:00
Michael Howell
fd35770e8d diagnostic: suggest parens when users want logical ops, but get closures 2022-02-24 17:02:38 -07: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
8562d6b752 rustc_errors: remove struct_dummy. 2022-02-23 05:38:24 +00:00
Eduard-Mihai Burtescu
02ff9e0aef Replace &mut DiagnosticBuilder, in signatures, with &mut Diagnostic. 2022-02-23 05:38:19 +00:00
est31
76ea566677 Better error if the user tries to do assignment ... else 2022-02-21 08:59:39 +01:00
Matthias Krüger
f2d6770f77
Rollup merge of #94146 - est31:let_else, r=cjgillot
Adopt let else in more places

Continuation of #89933, #91018, #91481, #93046, #93590, #94011.

I have extended my clippy lint to also recognize tuple passing and match statements. The diff caused by fixing it is way above 1 thousand lines. Thus, I split it up into multiple pull requests to make reviewing easier. This is the biggest of these PRs and handles the changes outside of rustdoc, rustc_typeck, rustc_const_eval, rustc_trait_selection, which were handled in PRs #94139, #94142, #94143, #94144.
2022-02-20 00:37:34 +01:00
est31
2ef8af6619 Adopt let else in more places 2022-02-19 17:27:43 +01:00
Matthias Krüger
a144ea1c4b
Rollup merge of #93634 - matthiaskrgr:clippy_complexity_jan_2022, r=oli-obk
compiler: clippy::complexity fixes

useless_format
map_flatten
useless_conversion
needless_bool
filter_next
clone_on_copy
needless_option_as_deref
2022-02-18 16:23:33 +01:00
Matthias Krüger
637d8b89e8
Rollup merge of #94011 - est31:let_else, r=lcnr
Even more let_else adoptions

Continuation of #89933, #91018, #91481, #93046, #93590.
2022-02-17 23:00:59 +01:00
est31
60f969a4f2 Adopt let_else in even more places 2022-02-16 22:43:39 +01:00
Erin Petra Sofiya Moon
e59cda9ee1
suggest using raw string literals when invalid escapes appear
i'd guess about 70% of "bad escape" cases occur when someone meant to
use a raw string literal because they're passing it directly to
Regex::new(). this emits an advisory (Applicability::MaybeIncorrect)
help: suggestion to the user that they use an r"" string,
on top of the normal notes about looking at the
string literal documentation/spec.
2022-02-14 15:11:38 -05:00
Matthias Krüger
602898a305
Rollup merge of #93595 - compiler-errors:ice-on-lifetime-arg, r=jackh726
fix ICE when parsing lifetime as function argument

I don't really like this, but we basically need to emit an error instead of just delaying an bug, because there are too many places in the AST that aren't covered by my previous PRs...

cc: https://github.com/rust-lang/rust/issues/93282#issuecomment-1028052945
2022-02-12 09:26:21 +01:00
Matthias Krüger
b80057d08d compiler: clippy::complexity fixes
useless_format
map_flatten
useless_conversion
needless_bool
filter_next
clone_on_copy
needless_option_as_deref
2022-02-03 23:16:03 +01:00
Matthias Krüger
de2abc29e9 clippy::perf fixes
single_char_pattern and to_string_in_format_args
2022-02-03 21:45:51 +01:00
Michael Goulet
b79fc92db3 fix ICE when parsing lifetime as function argument 2022-02-02 10:33:13 -08:00
Michael Goulet
f35d43cdf0 better suggestion for duplicated where 2022-02-02 00:29:45 -08:00
Eric Huss
d7c0b4f706
Rollup merge of #93019 - 5225225:uppercase-suffix, r=wesleywiser
If an integer is entered with an upper-case base prefix (0Xbeef, 0O755, 0B1010), suggest to make it lowercase

The current error for this case isn't really great, it just complains about the whole thing past the `0` being an invalid suffix.
2022-01-31 20:12:55 -08:00
5225225
ec3b711a4b Write UI tests, tweak message 2022-01-31 17:34:10 +00:00
Matthias Krüger
2f4602a64c
Rollup merge of #93395 - camelid:reserved-sugg, r=davidtwco
Improve suggestion for escaping reserved keywords

r? `@davidtwco`
2022-01-31 07:00:40 +01:00
Noah Lev
c8198a608e Improve suggestion for escaping reserved keywords 2022-01-27 14:53:39 -08:00
5225225
7f24778102 Suggest making base prefix lowercase if parsing fails 2022-01-27 21:31:28 +00:00
Tomasz Miąsko
6236882127 Introduce a limit to Levenshtein distance computation
Incorporate distance limit from `find_best_match_for_name` directly into
Levenshtein distance computation.

Use the string size difference as a lower bound on the distance and exit
early when it exceeds the specified limit.

After finding a candidate within a limit, lower the limit further to
restrict the search space.
2022-01-26 12:38:22 +01:00
Michael Goulet
37bed05986 delay the bug once again, generalize turbofish suggestion 2022-01-25 11:11:13 -08:00
Michael Goulet
a090bb1dea Remove delayed bug when encountering label in bad turbofish 2022-01-25 10:47:10 -08:00
Caio
cbb0fffe59 Fix let_chains and if_let_guard feature flags 2022-01-22 17:45:45 -03:00
Matthias Krüger
f372476d2c
Rollup merge of #91150 - dtolnay:qpath, r=davidtwco
Let qpath contain NtTy: `<$:ty as $:ty>::…`

Example:

```rust
macro_rules! m {
    (<$type:ty as $trait:ty>::$name:ident) => {
        <$type as $trait>::$name
    };
}

fn main() {
    let _: m!(<str as ToOwned>::Owned);
}
```

Previous behavior:

```console
error: expected identifier, found `ToOwned`
 --> src/main.rs:3:19
  |
3 |         <$type as $trait>::$name
  |                   ^^^^^^ expected identifier
...
8 |     let _: m!(<str as ToOwned>::Owned);
  |            ---------------------------
  |            |
  |            this macro call doesn't expand to a type
  |            in this macro invocation
```

The <code>expected identifier, found \`ToOwned\`</code> error is particularly silly. I think it should be fine to accept this code as long as $trait is of the form `TyKind::Path(None, path)`; if it is any other kind of `NtTy`, we'll keep the same behavior as before.
2022-01-18 22:00:43 +01:00
kadmin
1c1ce2fbda Add term to ExistentialProjection
Also prevent ICE when adding a const in associated const equality.
2022-01-17 20:01:22 +00:00
kadmin
67f56671d0 Use Term in ProjectionPredicate
ProjectionPredicate should be able to handle both associated types and consts so this adds the
first step of that. It mainly just pipes types all the way down, not entirely sure how to handle
consts, but hopefully that'll come with time.
2022-01-17 17:44:56 +00:00
kadmin
fb57b7518d Add term
Instead of having a separate enum variant for types and consts have one but have either a const
or type.
2022-01-17 17:20:57 +00:00
kadmin
0765999622 add eq constraints on associated constants 2022-01-17 17:20:57 +00:00
Matthias Krüger
c6ff4be011
Rollup merge of #92876 - compiler-errors:fix-turbofish-lifetime-suggestion, r=nagisa
Fix suggesting turbofish with lifetime arguments

Now we suggest turbofish correctly given exprs like `foo<'_>`.

Also fix suggestion when we have `let x = foo<bar, baz>;` which was broken.
2022-01-17 06:08:15 +01:00
Matthias Krüger
9323a0d1be
Rollup merge of #92746 - estebank:question-mark-in-type, r=davidtwco
Parse `Ty?` as `Option<Ty>` and provide structured suggestion

Swift has specific syntax that desugars to `Option<T>` similar to our
`?` operator, which means that people might try to use it in Rust. Parse
it and gracefully recover.
2022-01-16 16:58:18 +01:00
Michael Goulet
867554ad7c Fix suggesting turbofish with lifetime arguments 2022-01-13 19:45:29 -08:00
Esteban Kuber
cfc0bd1258 Parse Ty? as Option<Ty> and provide structured suggestion
Swift has specific syntax that desugars to `Option<T>` similar to our
`?` operator, which means that people might try to use it in Rust. Parse
it and gracefully recover.
2022-01-14 00:07:23 +00:00
Lucas Kent
08829853d3 eplace usages of vec![].into_iter with [].into_iter 2022-01-09 14:09:25 +11:00
Vadim Petrochenkov
55595c5616 ast: Always keep a NodeId in ast::Crate
This makes it more uniform with other expanded nodes
2022-01-05 17:09:37 +08:00
Jack Huey
4391a11537 Parse and suggest moving where clauses after equals for type aliases 2021-12-28 14:20:15 -05:00
bors
a41a6925ba Auto merge of #91957 - nnethercote:rm-SymbolStr, r=oli-obk
Remove `SymbolStr`

This was originally proposed in https://github.com/rust-lang/rust/pull/74554#discussion_r466203544. As well as removing the icky `SymbolStr` type, it allows the removal of a lot of `&` and `*` occurrences.

Best reviewed one commit at a time.

r? `@oli-obk`
2021-12-19 09:31:37 +00:00
Matthias Krüger
54e7946d0f
Rollup merge of #87901 - poliorcetics:pub-pub-pub, r=jackh726
Fix suggestion of additional `pub` when using `pub pub fn ...`

Fix #87694.

Marked as draft to start with because I want to explore doing the same fix for `const const fn` and other repeated-but-valid keywords.

`@rustbot` label A-diagnostics D-invalid-suggestion T-compiler
2021-12-18 08:16:25 +01:00
bors
c5ecc15704 Auto merge of #91962 - matthiaskrgr:rollup-2g082jw, r=matthiaskrgr
Rollup of 7 pull requests

Successful merges:

 - #91880 (fix clippy::single_char_pattern perf findings)
 - #91885 (Remove `in_band_lifetimes` from `rustc_codegen_ssa`)
 - #91898 (Make `TyS::is_suggestable` check for non-suggestable types structually)
 - #91915 (Add another regression test for unnormalized fn args with Self)
 - #91916 (Fix a bunch of typos)
 - #91918 (Constify `bool::then{,_some}`)
 - #91920 (Use `tcx.def_path_hash` in `ExistentialPredicate.stable_cmp`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2021-12-15 12:41:42 +00:00
Matthias Krüger
9ca0bd518a
Rollup merge of #91880 - matthiaskrgr:clippy_perf_dec, r=jyn514
fix clippy::single_char_pattern perf findings
2021-12-15 10:56:58 +01:00
Nicholas Nethercote
056d48a2c9 Remove unnecessary sigils around Symbol::as_str() calls. 2021-12-15 17:32:14 +11:00
Jacob Pratt
d95f749f14
Stabilize destructuring_assignment 2021-12-14 22:38:51 -05:00
Nicholas Nethercote
8cddcd39ba Remove SymbolStr.
By changing `as_str()` to take `&self` instead of `self`, we can just
return `&str`. We're still lying about lifetimes, but it's a smaller lie
than before, where `SymbolStr` contained a (fake) `&'static str`!
2021-12-15 13:30:26 +11:00
Matthias Krüger
4b37cfc50c
Rollup merge of #91597 - r00ster91:lessthangreaterthan, r=oli-obk
Recover on invalid operators `<>` and `<=>`

Thanks to #89871 for showing me how to do this.
Next, I think it'd be nice to recover on `<=>` too, like #89871 intended, if this even works.
2021-12-14 20:47:27 +01:00
r00ster91
f194c9b26a Recover on invalid operators <> and <=> 2021-12-14 18:05:02 +01:00
Matthias Krüger
97e844a032 fix clippy::single_char_pattern perf findings 2021-12-14 12:40:28 +01:00
Alexis Bourget
be33ca7d5b Use Inherited Visibility instead of None when no vis is present 2021-12-13 22:41:42 +01:00
Alexis Bourget
b0806b2de9 Update test to new error message 2021-12-13 21:36:35 +01:00
Alexis Bourget
5c9950023b Suggest remove on const async const instead of const const async 2021-12-13 21:36:35 +01:00
Alexis Bourget
7bc8577d99 Fix broken span and related tests 2021-12-13 21:36:35 +01:00
Alexis Bourget
b88a612d9a Change error for pub in fn decl if already present 2021-12-13 21:36:32 +01:00
bors
753e569c9c Auto merge of #90207 - BoxyUwU:stabilise_cg_defaults, r=lcnr
Stabilise `feature(const_generics_defaults)`

`feature(const_generics_defaults)` is complete implementation wise and has a pretty extensive test suite so I think is ready for stabilisation.

needs stabilisation report and maybe an RFC 😅

r? `@lcnr`
cc `@rust-lang/project-const-generics`
2021-12-12 14:24:23 +00:00
Matthias Krüger
40988591ec
Rollup merge of #91625 - est31:remove_indexes, r=oli-obk
Remove redundant [..]s
2021-12-10 22:40:36 +01:00
Ellen
69d2d735bc remove feature gate and cleanup code 2021-12-10 19:20:31 +00:00
Matthias Krüger
8c94b2c375
Rollup merge of #91634 - terrarier2111:fix-recover-from-variant-ice, r=nagisa
Do not attempt to suggest help for overly malformed struct/function call

This fixes: https://github.com/rust-lang/rust/issues/91461
2021-12-09 05:08:33 +01:00
est31
15de4cbc4b Remove redundant [..]s 2021-12-09 00:01:29 +01:00
Matthias Krüger
d26fc45e5b
Rollup merge of #91337 - FabianWolff:issue-91227-misspelled-macro, r=nagisa
Add a suggestion if `macro_rules` is misspelled

Fixes #91227.
2021-12-08 23:18:04 +01:00
threadexception
b4c4bc09dd Do not attempt to suggest help for overly malformed struct/function call 2021-12-08 22:00:44 +01:00
Matthias Krüger
87f2c51dcd
Rollup merge of #91531 - notriddle:notriddle/issue-87647-expected-semicolon, r=estebank
Do not add `;` to expected tokens list when it's wrong

There's a few spots where semicolons are checked for to do error recovery, and should not be suggested (or checked for other stuff).

Fixes #87647
2021-12-08 11:08:59 +01:00
Michael Howell
6611567f9e Expect extern fn with no body when parsing
Also add a test case for inserting a semicolon on extern fns.

Without this fix, we got an error like this:

    error: expected one of `->`, `where`, or `{`, found `}`
     --> chk.rs:3:1
      |
    2 |   fn foo()
      |      ---  - expected one of `->`, `where`, or `{`
      |      |
      |      while parsing this `fn`
    3 | }
      | ^ unexpected token

Since this is inside an extern block, you're required to write function
prototypes with no body. This fixes a regression, and adds a test case
for it.
2021-12-06 11:16:46 -07:00
Michael Howell
61995926d2 Add better comments for FnParseMode 2021-12-06 11:16:02 -07:00
Matthias Krüger
29fe57def2
Rollup merge of #90022 - hkmatsumoto:self-upper-as-generic-parameter, r=jackh726
Explain why `Self` is invalid in generic parameters

Close #89985.

r? `@estebank`
2021-12-05 00:37:56 +01:00
Michael Howell
74437e477e Do not add ; to expected tokens list when it's wrong
There's a few spots where semicolons are checked for to do error recovery,
and should not be suggested (or checked for other stuff).

Fixes #87647
2021-12-04 11:05:30 -07:00
Matthias Krüger
dbb9e224af
Rollup merge of #91435 - FabianWolff:issue-91421-if-then, r=lcnr
Improve diagnostic for missing half of binary operator in `if` condition

Fixes #91421. I've also changed it so that it doesn't consume the `else` token in the error case, because it will try to consume it again afterwards, leading to this incorrect error message (where the `else` reported as missing is actually there):
```
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{`
 --> src/main.rs:4:12
  |
4 |     } else { 4 };
  |            ^ expected one of `.`, `;`, `?`, `else`, or an operator
```

r? `@lcnr`
2021-12-02 22:16:13 +01:00
Fabian Wolff
ba7374e517 Improve diagnostic for missing half of binary operator in if condition 2021-12-01 22:36:50 +01:00
Matthias Krüger
519a842c50
Rollup merge of #91313 - petrochenkov:cratexp, r=Aaron1011
expand: Turn `ast::Crate` into a first class expansion target

And stop creating a fake `mod` item for the crate root when expanding a crate, thus addressing FIXMEs left in https://github.com/rust-lang/rust/pull/82238, and making a step towards a proper support for crate-level macro attributes (cc #54726).

I haven't added token collection support for the whole crate in this PR, maybe later.
r? `@Aaron1011`
2021-12-01 20:57:43 +01:00
Esteban Kuber
c02710530c review comments: clean up 2021-11-29 18:39:08 +00:00
Fabian Wolff
b9b4f549a4 Add a suggestion if macro_rules is misspelled 2021-11-28 23:10:37 +01:00
Vadim Petrochenkov
141c6cc78e expand: Turn ast::Crate into a first class expansion target
And stop creating a fake `mod` item for the crate root when expanding a crate.
2021-11-28 15:48:55 +08:00
threadexception
3a13a72141 Improve unsafe diagnostic 2021-11-27 14:57:19 +01:00
David Tolnay
87a7defa8e
Reject generic arguments on mod style interpolated path 2021-11-25 15:05:07 -08:00
David Tolnay
0cbb00f898
Let qpath contain NtTy: <$:ty as $:ty>::rest 2021-11-25 15:03:59 -08:00
Esteban Küber
d6e34ad108 When recovering from a : in a pattern, use adequate AST pattern 2021-11-25 18:40:29 +00:00
bors
23a436606b Auto merge of #88781 - estebank:emoji-idents, r=oli-obk
Tokenize emoji as if they were valid identifiers

In the lexer, consider emojis to be valid identifiers and reject
them later to avoid knock down parse errors.

Partially address #86102.
2021-11-25 08:16:08 +00:00
bors
c6eda7d8a7 Auto merge of #85346 - estebank:issue-84946, r=nagisa,varkor
Account for incorrect `impl Foo<const N: ty> {}` syntax

Fix #84946
2021-11-25 05:09:51 +00:00
Esteban Kuber
505b09e326 Tweak span and add more tests 2021-11-24 22:04:52 +00:00
Esteban Küber
7190bc3097 Account for incorrect impl Foo<const N: ty> {} syntax
Fix #84946
2021-11-24 20:02:09 +00:00
bors
de4b242e1e Auto merge of #91149 - notriddle:notriddle/rustdoc-doctest-semicolon, r=jyn514
fix(doctest): detect extern crate items in statement doctests

This partially reverts #91026, because rustdoc needs to detect the extern statements, even when they appear inside implicit `main()`. It does not entirely revert it, so the old bug is still fixed, by duplicating some of the logic from `parse_mod` instead of trying to use it directly.

Fixes #91134
2021-11-23 23:48:55 +00:00
Esteban Kuber
d68add9ecc review comment: plural of emoji is emoji 2021-11-23 20:36:19 +00:00
Esteban Kuber
21224e6ee0 Account for confusable codepoints when recovering emoji identifiers 2021-11-23 20:36:19 +00:00
Esteban Kuber
5a68abb094 Tokenize emoji as if they were valid indentifiers
In the lexer, consider emojis to be valid identifiers and reject
them later to avoid knock down parse errors.
2021-11-23 20:35:07 +00:00
Michael Howell
bff1645bdb fix(doctest): detect extern crate items in statement doctests
This partially reverts #91026, because rustdoc needs to detect the extern statements,
even when they appear inside implicit `main()`. It does not entirely revert it,
so the old bug is still fixed, by duplicating some of the logic from `parse_mod`
instead of trying to use it directly.

Fixes #91134
2021-11-22 19:47:58 -07:00
Gary Guo
6d61d87b22 Split inline const to two feature gates 2021-11-22 22:17:03 +00:00
Matthias Krüger
3d4f3ee71d
Rollup merge of #90994 - Badel2:issue-90993, r=estebank
Fix ICE `#90993`: add missing call to cancel

Fix #90993
2021-11-20 22:33:49 +01:00
threadexception
5f6059d9a2 Fix float ICE
Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>
2021-11-20 16:41:36 +01:00
Badel2
77a105c91f Fix ICE #90993: add missing call to cancel 2021-11-20 14:32:25 +01:00
5225225
09e59c2875 Inline printable function 2021-11-16 08:06:31 +00:00