Commit Graph

40 Commits

Author SHA1 Message Date
Vadim Petrochenkov b25d3ba781 ast/hir: Rename field-related structures
StructField -> FieldDef ("field definition")
Field -> ExprField ("expression field", not "field expression")
FieldPat -> PatField ("pattern field", not "field pattern")

Also rename visiting and other methods working on them.
2021-03-16 11:41:24 +03:00
klensy c75c4a579b replaced some map_or with map_or_else 2021-02-24 02:43:35 +03:00
Dylan DPC 8e51bd4315
Rollup merge of #81235 - reese:rw-tuple-diagnostics, r=estebank
Improve suggestion for tuple struct pattern matching errors.

Closes #80174

This change allows numbers to be parsed as field names when pattern matching on structs, which allows us to provide better error messages when tuple structs are matched using a struct pattern.

r? ``@estebank``
2021-02-23 02:51:44 +01:00
Reese Williams d8540ae5a9 Fix suggestion span and move suggestions into new subwindow. 2021-02-20 15:33:08 -05:00
Reese Williams 7879099ad3 Clarify error message and remove pretty printing in help suggestions. 2021-02-14 13:15:37 -05:00
bors 26e5bcd220 Auto merge of #81350 - tmiasko:instrument-debug, r=lcnr
Reduce log level used by tracing instrumentation from info to debug

Restore log level to debug to avoid make info log level overly verbose (the uses of instrument attribute modified there, were for the most part a replacement for `debug!`;  one use was novel).
2021-02-11 13:44:00 +00:00
Jonas Schievink 84b6f46d6e
Rollup merge of #81422 - estebank:dotdot_sugg, r=davidtwco
Account for existing `_` field pattern when suggesting `..`

Follow up to #80017.
2021-01-31 01:47:33 +01:00
Esteban Küber a398994cb2 Account for existing `_` field pattern when suggesting `..`
Follow up to #80017.
2021-01-26 16:53:56 -08:00
Esteban Küber 0959f0f912 Tweak suggestion for missing field in patterns
Account for parser recovered struct and tuple patterns to avoid invalid
suggestion.

Follow up to #81103.
2021-01-26 11:27:53 -08:00
Tomasz Miąsko 59457ab86e Reduce log level used by tracing instrumentation from info to debug 2021-01-24 00:00:00 +00:00
Reese Williams 8a83c8f64f Improve suggestion for tuple struct pattern matching errors.
Currently, when a user uses a struct pattern to pattern match on
a tuple struct, the errors we emit generally suggest adding fields
using their field names, which are numbers. However, numbers are
not valid identifiers, so the suggestions, which use the shorthand
notation, are not valid syntax. This commit changes those errors
to suggest using the actual tuple struct pattern syntax instead,
which is a more actionable suggestion.
2021-01-20 23:06:19 -05:00
Zack M. Davis 14eb94fe7a don't suggest erroneous trailing comma after `..`
In #76612, suggestions were added for missing fields in
patterns. However, the suggestions are being inserted just at the end
of the last field in the pattern—before any trailing comma after the
last field. This resulted in the "if you don't care about missing
fields" suggestion to recommend code with a trailing comma after the
field ellipsis (`..,`), which is actually not legal ("`..` must be at
the end and cannot have a trailing comma")!

Incidentally, the doc-comment on `error_unmentioned_fields` was using
`you_cant_use_this_field` as an example field name (presumably
copy-paste inherited from the description of Issue #76077), but
the present author found this confusing, because unmentioned fields
aren't necessarily unusable.

The suggested code in the diff this commit introduces to
`destructuring-assignment/struct_destructure_fail.stderr` doesn't
work, but it didn't work beforehand, either (because of the "found
reserved identifier `_`" thing), so you can't really call it a
regression; it could be fixed in a separate PR.

Resolves #78511.
2021-01-16 16:01:36 -08:00
Camelid e8c87935e0 Include `..` suggestion if fields are all wildcards 2021-01-12 21:20:26 -08:00
Camelid d7307a71f5 Always show suggestions in their own subwindows 2021-01-12 19:25:51 -08:00
Camelid 9959d6deed Only suggest `..` if more than one field is missing 2021-01-12 19:25:51 -08:00
Camelid fe82cc38a0 Specialize `..` help message for all fields vs. the rest 2021-01-12 19:25:50 -08:00
Camelid a5e8e6ec2d Pluralize 'parenthesis' correctly
It's 'parentheses', not 'parenthesis', when you have more than one.
2021-01-12 19:25:50 -08:00
Camelid f3d9df54ee Suggest `Variant(..)` if all of the mentioned fields are `_` 2021-01-12 19:25:50 -08:00
Camelid 5fe61a79cc Simplify code
Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>
2021-01-12 19:25:50 -08:00
Camelid 16692ab66a Suggest `_` and `..` if a pattern has too few fields
For example, this code:

    struct S(i32, f32);

    let S(x) = S(0, 1.0);

will make the compiler suggest either:

    let S(x, _) = S(0, 1.0);

or:

    let S(x, ..) = S(0, 1.0);
2021-01-12 19:25:49 -08:00
LeSeulArtichaut 9cc563b70b Fixup: `filter().is_none()` -> `!any()` 2020-12-07 21:40:20 +01:00
Arlie Davis 5481c1bd6d Move lev_distance to rustc_ast, make non-generic
rustc_ast currently has a few dependencies on rustc_lexer. Ideally, an AST
would not have any dependency its lexer, for minimizing unnecessarily
design-time dependencies. Breaking this dependency would also have practical
benefits, since modifying rustc_lexer would not trigger a rebuild of rustc_ast.

This commit does not remove the rustc_ast --> rustc_lexer dependency,
but it does remove one of the sources of this dependency, which is the
code that handles fuzzy matching between symbol names for making suggestions
in diagnostics. Since that code depends only on Symbol, it is easy to move
it to rustc_span. It might even be best to move it to a separate crate,
since other tools such as Cargo use the same algorithm, and have simply
contain a duplicate of the code.

This changes the signature of find_best_match_for_name so that it is no
longer generic over its input. I checked the optimized binaries, and this
function was duplicated at nearly every call site, because most call sites
used short-lived iterator chains, generic over Map and such. But there's
no good reason for a function like this to be generic, since all it does
is immediately convert the generic input (the Iterator impl) to a concrete
Vec<Symbol>. This has all of the costs of generics (duplicated method bodies)
with no benefit.

Changing find_best_match_for_name to be non-generic removed about 10KB of
code from the optimized binary. I know it's a drop in the bucket, but we have
to start reducing binary size, and beginning to tame over-use of generics
is part of that.
2020-11-24 16:12:23 -08:00
Mara Bos b6f52410bb
Rollup merge of #79072 - oli-obk:byte_str_pat, r=estebank
Fix exhaustiveness in case a byte string literal is used at slice type

fixes #79048
2020-11-17 16:13:53 +01:00
oli a1cdf722f4 Fix exhaustiveness in case a byte string literal is used at slice type 2020-11-17 09:07:23 +00:00
Bastian Kauschke 2bf93bd852 compiler: fold by value 2020-11-16 22:34:57 +01:00
Dylan DPC abaa78baeb
Rollup merge of #78748 - fanzier:tuple-assignment, r=petrochenkov
Implement destructuring assignment for tuples

This is the first step towards implementing destructuring assignment (RFC: https://github.com/rust-lang/rfcs/pull/2909, tracking issue: #71126). This PR is the first part of #71156, which was split up to allow for easier review.

Quick summary: This change allows destructuring the LHS of an assignment if it's a (possibly nested) tuple.
It is implemented via a desugaring (AST -> HIR lowering) as follows:
```rust
(a,b) = (1,2)
```
... becomes ...
```rust
{
  let (lhs0,lhs1) = (1,2);
  a = lhs0;
  b = lhs1;
}
```

Thanks to `@varkor` who helped with the implementation, particularly around default binding modes.

r? `@petrochenkov`
2020-11-09 01:13:44 +01:00
Fabian Zaiser 3a7a997323 Implement destructuring assignment for tuples
Co-authored-by: varkor <github@varkor.com>
2020-11-07 13:17:19 +00:00
ankushduacodes 0af959d3a2 Fixing Spelling Typos 2020-11-06 09:25:58 +05:30
Camelid c877ff3664 Fix rustdoc warnings about invalid Rust syntax 2020-10-04 19:35:44 -07:00
varkor 94c789b275 `char` not char 2020-09-26 13:34:49 +01:00
Ralf Jung 5631b5d684
Rollup merge of #76749 - guswynn:hir_ranges, r=estebank
give *even better* suggestion when matching a const range

notice that the err already has "constant defined here"
so this is now *exceedingly clear*

extension to #76222

r? @estebank
2020-09-19 11:47:50 +02:00
Gus Wynn 230355f25f comments and factor to own method 2020-09-16 12:32:10 -07:00
Gus Wynn 1d93048324 give better suggestion when matching a const range 2020-09-16 10:13:11 -07:00
Matthias Krüger 73d4171ea6 fix a couple of stylistic clippy warnings
namely:

clippy::redundant_pattern_matching
clippy::redundant_pattern
clippy::search_is_some
clippy::filter_next
clippy::into_iter_on_ref
clippy::clone_on_copy
clippy::needless_return
2020-09-15 22:44:54 +02:00
bors 255ceeb5ff Auto merge of #76612 - estebank:pat-missing-fields-suggestion, r=davidtwco
Provide suggestion for missing fields in patterns
2020-09-15 00:17:13 +00:00
Gus Wynn 5e126c944b better diag when const ranges are used in patterns 2020-09-11 15:02:15 -07:00
Esteban Küber 21f8326cec Provide suggestion for missing fields in patterns 2020-09-11 13:47:33 -07:00
David Wood 409c141973
typeck/pat: inaccessible private fields
This commit adjusts the missing field diagnostic logic for struct
patterns in typeck to improve the diagnostic when the missing fields are
inaccessible.

Signed-off-by: David Wood <david@davidtw.co>
2020-09-10 18:52:00 +01:00
LeSeulArtichaut 3e14b684dd Change ty.kind to a method 2020-09-04 17:47:51 +02:00
mark 9e5f7d5631 mv compiler to compiler/ 2020-08-30 18:45:07 +03:00