about summary refs log tree commit diff
path: root/compiler/rustc_parse/src/parser/pat.rs
AgeCommit message (Collapse)AuthorLines
2023-12-23Give `DiagnosticBuilder` a default type.Nicholas Nethercote-4/+4
`IntoDiagnostic` defaults to `ErrorGuaranteed`, because errors are the most common diagnostic level. It makes sense to do likewise for the closely-related (and much more widely used) `DiagnosticBuilder` type, letting us write `DiagnosticBuilder<'a, ErrorGuaranteed>` as just `DiagnosticBuilder<'a>`. This cuts over 200 lines of code due to many multi-line things becoming single line things.
2023-12-18Use `.into_diagnostic()` less.Nicholas Nethercote-5/+6
This commit replaces this pattern: ``` err.into_diagnostic(dcx) ``` with this pattern: ``` dcx.create_err(err) ``` in a lot of places. It's a little shorter, makes the error level explicit, avoids some `IntoDiagnostic` imports, and is a necessary prerequisite for the next commit which will add a `level` arg to `into_diagnostic`. This requires adding `track_caller` on `create_err` to avoid mucking up the output of `tests/ui/track-diagnostics/track4.rs`. It probably should have been there already.
2023-12-18Rename `Parser::span_diagnostic` as `Parser::dcx`.Nicholas Nethercote-2/+2
2023-12-08Auto merge of #118527 - Nadrieril:never_patterns_parse, r=compiler-errorsbors-8/+4
never_patterns: Parse match arms with no body Never patterns are meant to signal unreachable cases, and thus don't take bodies: ```rust let ptr: *const Option<!> = ...; match *ptr { None => { foo(); } Some(!), } ``` This PR makes rustc accept the above, and enforces that an arm has a body xor is a never pattern. This affects parsing of match arms even with the feature off, so this is delicate. (Plus this is my first non-trivial change to the parser). ~~The last commit is optional; it introduces a bit of churn to allow the new suggestions to be machine-applicable. There may be a better solution? I'm not sure.~~ EDIT: I removed that commit r? `@compiler-errors`
2023-12-03Detect attempts to expand a macro to a match arm againNadrieril-6/+2
Because a macro invocation can expand to a never pattern, we can't rule out a `arm!(),` arm at parse time. Instead we detect that case at expansion time, if the macro tries to output a pattern followed by `=>`.
2023-12-03Parse a pattern with no armNadrieril-2/+2
2023-12-02Use `Session::diagnostic` in more places.Nicholas Nethercote-2/+2
2023-11-29Avoid unnecessary pattern parse errors on `ref box`Esteban Küber-3/+3
2023-11-29When parsing patterns, bubble all errors except reserved idents that aren't ↵Esteban Küber-1/+13
likely to appear in for head or match arm
2023-11-29Make `parse_pat_ident` not recover bad nameEsteban Küber-1/+1
2023-11-29Rollup merge of #118157 - Nadrieril:never_pat-feature-gate, r=compiler-errorsMatthias Krüger-1/+5
Add `never_patterns` feature gate This PR adds the feature gate and most basic parsing for the experimental `never_patterns` feature. See the tracking issue (https://github.com/rust-lang/rust/issues/118155) for details on the experiment. `@scottmcm` has agreed to be my lang-team liaison for this experiment.
2023-11-29Add `never_patterns` feature gateNadrieril-1/+5
2023-11-27Suggest swapping the order of `ref` and `box`Hirochika Matsumoto-2/+8
2023-11-21Fix `clippy::needless_borrow` in the compilerNilstrieb-2/+2
`x clippy compiler -Aclippy::all -Wclippy::needless_borrow --fix`. Then I had to remove a few unnecessary parens and muts that were exposed now.
2023-11-17Auto merge of #114292 - estebank:issue-71039, r=b-naberbors-1/+1
More detail when expecting expression but encountering bad macro argument On nested macro invocations where the same macro fragment changes fragment type from one to the next, point at the chain of invocations and at the macro fragment definition place, explaining that the change has occurred. Fix #71039. ``` error: expected expression, found pattern `1 + 1` --> $DIR/trace_faulty_macros.rs:49:37 | LL | (let $p:pat = $e:expr) => {test!(($p,$e))}; | ------- -- this is interpreted as expression, but it is expected to be pattern | | | this macro fragment matcher is expression ... LL | (($p:pat, $e:pat)) => {let $p = $e;}; | ------ ^^ expected expression | | | this macro fragment matcher is pattern ... LL | test!(let x = 1+1); | ------------------ | | | | | this is expected to be expression | in this macro invocation | = note: when forwarding a matched fragment to another macro-by-example, matchers in the second macro will see an opaque AST of the fragment type, not the underlying tokens = note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info) ```
2023-11-16Smaller span for unnessary `mut` suggestionEsteban Küber-5/+5
2023-11-16More detail when expecting expression but encountering bad macro argumentEsteban Küber-1/+1
Partially address #71039.
2023-11-02Minimize `pub` usage in `source_map.rs`.Nicholas Nethercote-1/+2
Most notably, this commit changes the `pub use crate::*;` in that file to `use crate::*;`. This requires a lot of `use` items in other crates to be adjusted, because everything defined within `rustc_span::*` was also available via `rustc_span::source_map::*`, which is bizarre. The commit also removes `SourceMap::span_to_relative_line_string`, which is unused.
2023-10-30Account for `ref` and `mut` in the wrong place for pattern ident renamingEsteban Küber-1/+33
If the user writes `S { ref field: name }` instead of `S { field: ref name }`, we suggest the correct code. Fix #72298.
2023-09-12Only suggest turbofish in patterns if we may recoverLeón Orell Valerian Liehr-1/+2
2023-08-04Rollup merge of #113999 - Centri3:macro-arm-expand, r=wesleywiserMatthias Krüger-2/+6
Specify macro is invalid in certain contexts Adds a note when a macro is used where it really shouldn't be. Closes #113766
2023-08-03Rollup merge of #114300 - MU001999:fix/turbofish-pat, r=estebankMatthias Krüger-22/+46
Suggests turbofish in patterns Fixes #114112 r? ```@estebank```
2023-08-03Avoid too many expected symbols and reduce `None`sr0cky-15/+28
2023-08-03Apply suggestionsr0cky-2/+4
2023-08-03Keep the suggestion for wrong arbitrary self typesMu001999-14/+22
2023-08-01Suggests turbofish in patternsMu001999-0/+1
2023-07-30inline format!() args up to and including rustc_middleMatthias Krüger-3/+3
2023-07-24Recover from some macrosCatherine Flores-5/+1
2023-07-24Specify macro is invalid in certain contextsCatherine-2/+10
2023-07-20Don't translate compiler-internal bug messagesOli Scherer-33/+16
2023-06-03Fix suggestion for matching struct with `..` on both ends许杰友 Jieyou Xu (Joe)-12/+45
2023-05-03Restrict `From<S>` for `{D,Subd}iagnosticMessage`.Nicholas Nethercote-3/+3
Currently a `{D,Subd}iagnosticMessage` can be created from any type that impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static, str>`, which are reasonable. It also includes `&String`, which is pretty weird, and results in many places making unnecessary allocations for patterns like this: ``` self.fatal(&format!(...)) ``` This creates a string with `format!`, takes a reference, passes the reference to `fatal`, which does an `into()`, which clones the reference, doing a second allocation. Two allocations for a single string, bleh. This commit changes the `From` impls so that you can only create a `{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static, str>`. This requires changing all the places that currently create one from a `&String`. Most of these are of the `&format!(...)` form described above; each one removes an unnecessary static `&`, plus an allocation when executed. There are also a few places where the existing use of `&String` was more reasonable; these now just use `clone()` at the call site. As well as making the code nicer and more efficient, this is a step towards possibly using `Cow<'static, str>` in `{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing the `From<&'a str>` impls to `From<&'static str>`, which is doable, but I'm not yet sure if it's worthwhile.
2023-05-01Rip it outNilstrieb-5/+5
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-04-27Migrate trivially translatable `rustc_parse` diagnosticsclubby789-13/+8
2023-03-20feat: implement error recovery in `expected_ident_found`Ezra Shaw-2/+8
2023-03-19refactor: improve "ident starts with number" errorEzra Shaw-5/+1
2023-03-09feat/refactor: improve errors in case of ident with number at startEzra Shaw-0/+4
2023-02-22errors: generate typed identifiers in each crateDavid Wood-3/+2
Instead of loading the Fluent resources for every crate in `rustc_error_messages`, each crate generates typed identifiers for its own diagnostics and creates a static which are pulled together in the `rustc_driver` crate and provided to the diagnostic emitter. Signed-off-by: David Wood <david.wood@huawei.com>
2023-02-21Use `ThinVec` in `ast::PatKind::Struct`.Nicholas Nethercote-4/+4
2023-02-21Use `ThinVec` in various AST types.Nicholas Nethercote-2/+3
This commit changes the sequence parsers to produce `ThinVec`, which triggers numerous conversions.
2023-02-02Rollup merge of #107493 - clubby789:range-fat-arrow-followup, r=estebankMatthias Krüger-1/+1
Improve diagnostic for missing space in range pattern Improves the diagnostic in #107425 by turning it into a note explaining the parsing issue. r? `@compiler-errors`
2023-02-02Rollup merge of #106919 - compiler-errors:underscore-typo-in-field-pat, ↵Matthias Krüger-7/+11
r=jackh726 Recover `_` as `..` in field pattern
2023-02-02Improve diagnostic for missing space in range patternclubby789-1/+1
2023-02-02Recover _ as .. in field patternMichael Goulet-7/+11
2023-02-02Revert "Teach parser to understand fake anonymous enum syntax" and related ↵Michael Goulet-2/+1
commits Revert "review comment: Remove AST AnonTy" This reverts commit 020cca8d36cb678e3ddc2ead41364be314d19e93. Revert "Ensure macros are not affected" This reverts commit 12d18e403139eeeeb339e8611b2bed4910864edb. Revert "Emit fewer errors on patterns with possible type ascription" This reverts commit c847a01a3b1f620c4fdb98c75805033e768975d1. Revert "Teach parser to understand fake anonymous enum syntax" This reverts commit 2d824206655bfb26cb5eed43490ee396542b153e.
2023-02-01Convert rustc_parse::parser::pat::Expected to enumXiretza-16/+32
This is required in order to support translatable diagnostics.
2023-02-01rustc_parse: migrate more to diagnostic structsXiretza-177/+104
2023-01-28Migrate some range parsing diagnosticsclubby789-19/+8
2023-01-28Check for missing space between fat arrow and range patternclubby789-13/+31
2023-01-17Teach parser to understand fake anonymous enum syntaxEsteban Küber-1/+2
Parse `-> Ty | OtherTy`. Parse type ascription in top level patterns.