about summary refs log tree commit diff
path: root/tests/ui/pattern
AgeCommit message (Collapse)AuthorLines
2025-01-30Rollup merge of #135434 - dianne:match-2024-for-edition-2024, r=NadrierilMatthias Krüger-303/+1832
Match Ergonomics 2024: update edition 2024 behavior of feature gates This updates the edition 2024 behavior of the feature gates `ref_pat_eat_one_layer_2024_structural` and `ref_pat_eat_one_layer_2024` to correspond to the left and right typing rules compared [here](https://nadrieril.github.io/typing-rust-patterns/?compare=true&opts2=AQEBAAABAQABAgIAAQEBAAEBAAABAAA%3D&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=rules&do_cmp=true&ty_d=3&style=SequentBindingMode), respectively. I'll implement the proposed new behavior for editions ≤ 2021 in another PR. The tests are split up a bit awkwardly for practical reasons, but I've added new tests from 3 places: - I got tests for where the typing rules differ from the "Compare" tab of the page linked above. These had to be split up based on where the errors are emitted and how rustfixable they are, so they've ended up in different files to keep tidy. Within each file, though, the order of the tests matches the order the typing differences appear in that comparison (as of when this was written). - I used [this other comparison](https://nadrieril.github.io/typing-rust-patterns/?q=%5B%26mut+%26%28mut+x%29%5D%3A+%26mut+%5B%26CT%5D&compare=true&opts2=AQEBAgABAQEBAgIAAQEBAAEBAAABAAA%3D&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=compare&do_cmp=true&ty_d=3&style=SequentBindingMode) to test the `Deref(EatInner, FallbackToOuter)` rule of the left/"structural"/eat-inner ruleset. These are all in `well-typed-edition-2024.rs`. - I added some select tests for cases where the new typing rules differ from current stable Rust. I had to be pickier about what I included here, but I tried to make sure each typing rule got some coverage. That said, my approach for these tests was a bit ad-hoc, so I may have missed something. Relevant tracking issue: #123076 r? ````@ghost````
2025-01-29Handle all `PatExpr`s in dead code analysisOli Scherer-18/+3
2025-01-29Add regression test showing we don't realize some consts are usedOli Scherer-1/+24
2025-01-27Remove all dead files inside tests/ui/León Orell Valerian Liehr-778/+0
2025-01-21add tests differing between stable and new rules (with errors on new rules)dianne-18/+155
Since there are so many ways to write these, I've opted to only include two sorts of test: simple tests that directly target the rules differing between rulesets and nuanced tests that produce different errors under different rulesets. I've also tried not to add any duplicate tests. `well-typed-edition-2024.rs` already has tests disagreeing with stable, so I've opted not to include any in this commit that are well-typed under the new rules.
2025-01-21organize old well-typed-edition-2024 testsdianne-85/+100
This doesn't (or at least shouldn't!) add, remove, or change any test cases. I've grouped them by which rule variants they test.
2025-01-21add a stable edition 2021 revision to pattern typing testsdianne-198/+1103
This serves two purposes. First, they're additional tests that stable Rust behavior hasn't been messed with. There's plenty of other pattern tests, so this is less important, but these at least are targeted at what's being changed. Second, this helps document exactly where the new rulesets agree and disagree with stable pattern typing. This will be especially important after the new rules for old editions are updated, since they need to be strictly more permissive; any patterns well-typed on stable should also be well-typed with the same resultant bindings on the (upcoming) new new old-edition rules. The unusual test ordering on `borrowck-errors.rs` and `ref-binding-on-inh-ref-errors.rs` are to hopefully reduce how much adding new tests will mess with line numbers in their stderr.
2025-01-21add more information to old testsdianne-28/+36
2025-01-21minor test cleanupdianne-59/+26
- Removes some excess parens - Removes 3 duplicated tests
2025-01-20rename tests' revisions to allow testing multiple editionsdianne-93/+93
2025-01-20"structural" ruleset: match against the inherited ref when a reference ↵dianne-62/+61
pattern doesn't match the mutability of an inner reference This is the `Deref(EatInner, FallbackToOuter)` rule in Typing Rust Patterns.
2025-01-20"structural" ruleset: use the "classic" ruleset's diagnostic and fallback ↵dianne-355/+120
for inherited ref mutability mismatches I think the diagnostic could use some work, but it's more helpful than the alternative. The previous error was misleading, since it ignored the inherited reference altogether.
2025-01-20"structural" ruleset: account for dbm mutability cap in Deref(EatInner) rulesdianne-12/+106
2025-01-20add more tests where the rulesets disagreedianne-6/+659
These come directly from the "Compare" tab of Typing Rust Patterns, though they had to be split across multiple files. They're not comprehensive, but they do provide some previously-missing coverage and are easier to check against the spec. Possibly they should be split up some more, since `pattern-errors.rs` is getting a bit unwieldy, but I'm not sure how best to go about that.
2025-01-20remove Rule 3 from `ref_pat_eat_one_layer_2024`dianne-31/+17
The debug assertion ensuring that the pattern mutability cap holds assumes the presence of Rule 3, so it now checks for that. I considered going back to only tracking the mutability cap when Rule 3 is present, but since the mutability cap is used in Rule 5's implementation too, the debug assertion would still need to check which typing rules are present. This also required some changes to tests: - `ref_pat_eat_one_layer_2021.rs` had a test for Rule 3; I'll be handling tests for earlier editions in a later commit, so as a stopgap I've #[cfg]ed it out. - One test case had to be moved from `well-typed-edition-2024.rs` to `borrowck-errors.rs` in order to get borrowck to run on it and emit an error.
2025-01-20Auto merge of #135754 - jieyouxu:rollup-j4q1hpr, r=jieyouxubors-1/+1
Rollup of 7 pull requests Successful merges: - #135542 (Add the concrete syntax for precise capturing to 1.82 release notes.) - #135700 (Emit single privacy error for struct literal with multiple private fields and add test for `default_field_values` privacy) - #135722 (make it possible to use ci-rustc on tarball sources) - #135729 (Add debug assertions to compiler profile) - #135736 (rustdoc: Fix flaky doctest test) - #135738 (Replace usages of `map_or(bool, ...)` with `is_{some_and|none_or|ok_and}`) - #135747 (Rename FileName::QuoteExpansion to CfgSpec) r? `@ghost` `@rustbot` modify labels: rollup
2025-01-20Rollup merge of #135237 - dianne:match-2024-cleanup, r=Nadrieril许杰友 Jieyou Xu (Joe)-9/+1150
Match Ergonomics 2024: document and reorganize the currently-implemented feature gates The hope here is to make it easier to adjust, understand, and test the experimental pattern typing rules implemented in the compiler. This PR doesn't (or at isn't intended to) change any behavior or add any new tests; I'll be handling that later. I've also included some reasoning/commentary on the more involved changes in the commit messages. Relevant tracking issue: #123076 r? `@Nadrieril`
2025-01-18Emit a single privacy error for multiple fields on the same struct expressionEsteban Küber-1/+1
Collect all unreachable fields in a single struct literal struct and emit a single error, instead of one error per private field. ``` error[E0451]: fields `beta` and `gamma` of struct `Alpha` are private --> $DIR/visibility.rs:18:13 | LL | let _x = Alpha { | ----- in this type LL | beta: 0, | ^^^^^^^ private field LL | .. | ^^ field `gamma` is private ```
2025-01-12remove `tests/ui/pattern/no_ref_mut_behind_and.rs`dianne-9/+0
This was a test for `ref_pat_eat_one_layer_2024` downgrading a `mut ref` default binding mode to `ref` within a shared reference pattern (i.e. Rule 3) on edition 2021 specifically. Since it's near-identical to another existing test (currently in `ref_pat_eat_one_layer_2021.rs` in the "experimental" rfc 3627 subdirectory) and that particular feature gate's typing rulesets are planned to no longer have Rule 3, I'm opting to remove it rather than continue maintaining it separately.
2025-01-12rename and comment the test for "Rule 5"-related mutability errorsdianne-7/+91
This also makes it test the "structural" ruleset, in preparation for additional tests where the rulesets disagree.
2025-01-08Rollup merge of #135222 - estebank:issue-135209, r=compiler-errorsMatthias Krüger-3/+64
Ensure that we don't try to access fields on a non-struct pattern type Fix #135209.
2025-01-08reorganize and comment some of the experimental pattern typing testsdianne-497/+529
This only includes previously existing tests (with a couple duplicates removed). I plan on adding more comprarisons where the rules differ once I've updated the pattern typing rules. I also haven't touched the tests for new rules in old editions; I'll see how best to handle that once those rules are updated as well.
2025-01-08move the experimental match ergonomics tests to be with the other rfc 3627 testsdianne-0/+1034
2025-01-08Rename PatKind::Lit to ExprOli Scherer-1/+1
2025-01-08modify test to side-step platform-dependent stderr outputEsteban Küber-18/+27
2025-01-08Add check for missing fields in enum variant patternEsteban Küber-3/+20
2025-01-07Ensure that we don't try to access fields on a non-struct pattern type in ↵Esteban Küber-0/+35
diagnostic Fix #135209.
2024-12-31Rollup merge of #133486 - dianne:fix-move-error-suggestion, r=estebankTrevor Gross-0/+10
borrowck diagnostics: make `add_move_error_suggestions` use the HIR rather than `SourceMap` This PR aims to fix #132806 by rewriting `add_move_error_suggestions`[^1]. Previously, it manually scanned the source text to find a leading `&`, which isn't always going to produce a correct result (see: that issue). Admittedly, the HIR visitor in this PR introduces a lot of boilerplate, but hopefully the logic at its core isn't too complicated (I go over it in the comments). I also tried a simpler version that didn't use a HIR visitor and suggested adding `ref` always, but the `&ref x` suggestions really didn't look good. As a bonus for the added complexity though, it's now able to produce nice `&`-removing suggestions in more cases. I tried to do this such that it avoids edition-dependent checks and its suggestions can be applied together with those from the match ergonomics 2024 migration lint. I haven't added tests for that since the details of match ergonomics 2024 are still being sorted out, but I can try if desired once that's finalized. [^1]: In brief, it fires on patterns where users try to bind by-value in such a way that moves out of a reference to a non-Copy type (including slice references with non-copy elements). The suggestions are to change the binding's mode to be by-reference, either by removing[^2] an enclosing `&`/`&mut` or adding `ref` to the binding. [^2]: Incidentally, I find the terminology of "consider removing the borrow" a bit confusing for a suggestion to remove a `&` pattern in order to make bindings borrow rather than move. I'm not sure what a good, concise way to explain that would be though, and that should go in a separate PR anyway.
2024-12-19Rollup merge of #134474 - oli-obk:push-yomnkntvzlxw, r=compiler-errors许杰友 Jieyou Xu (Joe)-11/+15
Forbid overwriting types in typeck While trying to figure out some type setting logic in https://github.com/rust-lang/rust/pull/134248 I realized that we sometimes set a type twice. While hopefully that would have been the same type, we didn't ensure that at all and just silently accepted it. So now we reject setting it twice, unless errors are happening, then we don't care. Best reviewed commit by commit. No behaviour change is intended.
2024-12-18Forbid overwriting types in typeckOli Scherer-11/+15
2024-12-17Add the edition guide link from the match 2024 migration lint to the error ↵dianne-0/+8
as well
2024-12-17Improve the pattern migration 2024 migration lint's messagedianne-62/+62
2024-12-17Clarify the match ergonomics 2024 migration lint's outputdianne-150/+203
2024-12-17Rollup merge of #134368 - ehuss:edition-links, r=jieyouxuJacob Pratt-15/+15
Use links to edition guide for edition migrations This switches the migration lints for the 2024 edition to point to the edition guide documentation instead of the tracking issues. I expect the documentation should be easier to understand for a user, compared to most of the issues which don't have any direct information, and can be a bit confusing to navigate, or have outdated information.
2024-12-15Use links to edition guide for edition migrationsEric Huss-15/+15
2024-12-15Use `span_label` as it looks better when we show pattern missing binding in ↵Esteban Küber-17/+5
order
2024-12-13Keep track of patterns that could have introduced a binding, but didn'tEsteban Küber-0/+52
When we recover from a pattern parse error, or a pattern uses `..`, we keep track of that and affect resolution error for missing bindings that could have been provided by that pattern. We differentiate between `..` and parse recovery. We silence resolution errors likely caused by the pattern parse error. ``` error[E0425]: cannot find value `title` in this scope --> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:19:30 | LL | println!("[{}]({})", title, url); | ^^^^^ not found in this scope | note: `Website` has a field `title` which could have been included in this pattern, but it wasn't --> $DIR/struct-pattern-with-missing-fields-resolve-error.rs:17:12 | LL | / struct Website { LL | | url: String, LL | | title: Option<String> , | | ----- defined here LL | | } | |_- ... LL | if let Website { url, .. } = website { | ^^^^^^^^^^^^^^^^^^^ this pattern doesn't include `title`, which is available in `Website` ``` Fix #74863.
2024-12-08Rollup merge of #133424 - Nadrieril:guard-patterns-parsing, r=fee1-deadMatthias Krüger-2/+22
Parse guard patterns This implements the parsing of [RFC3637 Guard Patterns](https://rust-lang.github.io/rfcs/3637-guard-patterns.html) (see also [tracking issue](https://github.com/rust-lang/rust/issues/129967)). This PR is extracted from https://github.com/rust-lang/rust/pull/129996 with minor modifications. cc `@max-niederman`
2024-12-07Address review commentsNadrieril-0/+20
2024-12-04Add additional context for non-sructural type constant used in patternEsteban Küber-9/+17
- Point at type that should derive `PartialEq` to be structural. - Point at manual `impl PartialEq`, explaining that it is not sufficient to be structural. ``` error: constant of non-structural type `MyType` in a pattern --> $DIR/const-partial_eq-fallback-ice.rs:14:12 | LL | struct MyType; | ------------- `MyType` must be annotated with `#[derive(PartialEq)]` to be usable in patterns ... LL | const CONSTANT: &&MyType = &&MyType; | ------------------------ constant defined here ... LL | if let CONSTANT = &&MyType { | ^^^^^^^^ constant of non-structural type | note: the `PartialEq` trait must be derived, manual `impl`s are not sufficient; see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details --> $DIR/const-partial_eq-fallback-ice.rs:5:1 | LL | impl PartialEq<usize> for MyType { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ```
2024-12-04Specify type kind of constant that can't be used in patternsEsteban Küber-6/+6
``` error: trait object `dyn Send` cannot be used in patterns --> $DIR/issue-70972-dyn-trait.rs:6:9 | LL | const F: &'static dyn Send = &7u32; | -------------------------- constant defined here ... LL | F => panic!(), | ^ trait object can't be used in patterns ```
2024-12-04Tweak output of some const pattern errorsEsteban Küber-4/+10
- Add primary span labels. - Point at const generic parameter used as pattern. - Point at statics used as pattern. - Point at let bindings used in const pattern.
2024-12-04Tweak ptr in pattern errorEsteban Küber-16/+32
Conform to error style guide.
2024-12-04On `const` pattern errors, point at the `const` item definitionEsteban Küber-0/+33
Centralize emitting an error in `const_to_pat` so that all errors from that evaluating a `const` in a pattern can add addditional information. With this, now point at the `const` item's definition: ``` error[E0158]: constant pattern depends on a generic parameter --> $DIR/associated-const-type-parameter-pattern.rs:20:9 | LL | pub trait Foo { | ------------- LL | const X: EFoo; | ------------- constant defined here ... LL | A::X => println!("A::X"), | ^^^^ ```
2024-11-25`add_move_error_suggestions`: use a HIR visitor rather than `SourceMap`dianne-0/+10
2024-11-26tests: remove `//@ pretty-expanded` usages许杰友 Jieyou Xu (Joe)-2/+0
Done with ```bash sd '//@ pretty-expanded.*\n' '' tests/ui/**/*.rs ``` and ``` sd '//@pretty-expanded.*\n' '' tests/ui/**/*.rs ```
2024-11-24parse guard patternsNadrieril-2/+2
Co-authored-by: Max Niederman <max@maxniederman.com>
2024-11-23Auto merge of #131859 - chriskrycho:update-trpl, r=onur-ozkanbors-25/+25
Update TRPL to add new Chapter 17: Async and Await - Add support to `rustbook` to pass through the `-L`/`--library-path` flag to `mdbook` so that references to the `trpl` crate - Build the `trpl` crate as part of the book tests. Make it straightforward to add other such book dependencies in the future if needed by implementing that in a fairly general way. - Update the submodule for the book to pull in the new chapter on async and await, as well as a number of other fixes. This will happen organically/automatically in a week, too, but this lets me group this change with the next one: - Update the compiler messages which reference the existing chapters 17–20, which are now chapters 18-21. There are only two, both previously referencing chapter 18. - Update the UI tests which reference the compiler message outputs.
2024-11-23Update tests for new TRPL chapter orderChris Krycho-25/+25
2024-11-23Rollup merge of #133286 - jieyouxu:bug-ourselves, r=compiler-errors许杰友 Jieyou Xu (Joe)-0/+36
Re-delay a resolve `bug` related to `Self`-ctor in patterns For the code pattern reported in <https://github.com/rust-lang/rust/issues/133272>, ```rs impl Foo { fn fun() { let S { ref Self } = todo!(); } } ``` <https://github.com/rust-lang/rust/pull/121208> converted this to a `span_bug` from a `span_delayed_bug` because this specific self-ctor code pattern lacked test coverage. It turns out this can be hit but we just lacked test coverage, so change it back to a `span_delayed_bug` and add a targeted test case. Follow-up to #121208, cc ``@nnethercote`` (very good exercise to expose our test coverage gaps). Fixes #133272.