about summary refs log tree commit diff
path: root/tests/ui/pattern
AgeCommit message (Collapse)AuthorLines
2024-03-19Fix incorrect mutable suggestion information for binding in ref pattern.surechen-1/+47
For ref pattern in func param, the mutability suggestion has to apply to the binding. For example: `fn foo(&x: &i32)` -> `fn foo(&(mut x): &i32)` fixes #122415
2024-03-18Rollup merge of #121823 - Nadrieril:never-witnesses, r=compiler-errorsMatthias Krüger-187/+880
never patterns: suggest `!` patterns on non-exhaustive matches When a match is non-exhaustive we now suggest never patterns whenever it makes sense. r? ``@compiler-errors``
2024-03-15Rollup merge of #122254 - estebank:issue-48677, r=oli-obkMatthias Krüger-33/+33
Detect calls to .clone() on T: !Clone types on borrowck errors When encountering a lifetime error on a type that *holds* a type that doesn't implement `Clone`, explore the item's body for potential calls to `.clone()` that are only cloning the reference `&T` instead of `T` because `T: !Clone`. If we find this, suggest `T: Clone`. ``` error[E0502]: cannot borrow `*list` as mutable because it is also borrowed as immutable --> $DIR/clone-on-ref.rs:7:5 | LL | for v in list.iter() { | ---- immutable borrow occurs here LL | cloned_items.push(v.clone()) | ------- this call doesn't do anything, the result is still `&T` because `T` doesn't implement `Clone` LL | } LL | list.push(T::default()); | ^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here LL | LL | drop(cloned_items); | ------------ immutable borrow later used here | help: consider further restricting this bound | LL | fn foo<T: Default + Clone>(list: &mut Vec<T>) { | +++++++ ``` ``` error[E0505]: cannot move out of `x` because it is borrowed --> $DIR/clone-on-ref.rs:23:10 | LL | fn qux(x: A) { | - binding `x` declared here LL | let a = &x; | -- borrow of `x` occurs here LL | let b = a.clone(); | ------- this call doesn't do anything, the result is still `&A` because `A` doesn't implement `Clone` LL | drop(x); | ^ move out of `x` occurs here LL | LL | println!("{b:?}"); | ----- borrow later used here | help: consider annotating `A` with `#[derive(Clone)]` | LL + #[derive(Clone)] LL | struct A; | ``` Fix #48677.
2024-03-13Tweak wordingEsteban Küber-33/+33
2024-03-13Remove `MaybeInfiniteInt::JustAfterMax`Nadrieril-4/+4
It was inherited from before half-open ranges, but it doesn't pull its weight anymore. We lose a tiny bit of diagnostic precision.
2024-03-13Rollup merge of #121908 - Nadrieril:dynamic-variant-collection, r=matthewjasperMatthias Krüger-0/+14
match lowering: don't collect test alternatives ahead of time I'm very happy with this one. Before this, when sorting candidates into the possible test branches, we manually computed `usize` indices to determine in which branch each candidate goes. To make this work we had a first pass that collected the possible alternatives we'd have to deal with, and a second pass that actually sorts the candidates. In this PR, I replace `usize` indices with a dedicated enum. This makes `sort_candidates` easier to follow, and we don't need the first pass anymore. r? ``@matthewjasper``
2024-03-12Don't suggest an arm when suggesting a never patternNadrieril-15/+15
2024-03-12Suggest never pattern instead of `_` for empty typesNadrieril-57/+57
2024-03-12Run the empty_types tests with never_patterns tooNadrieril-187/+880
2024-03-09Lint small gaps between rangesNadrieril-0/+310
2024-03-09Allow lint where we don't careNadrieril-38/+40
2024-03-03Rollup merge of #121917 - GuillaumeGomez:pattern-complexity_limit.rs, ↵Guillaume Gomez-0/+123
r=Nadrieril Add new `pattern_complexity` attribute to add possibility to limit and check recursion in pattern matching Needed for https://github.com/rust-lang/rust-analyzer/issues/9528. This PR adds a new attribute only available when running rust testsuite called `pattern_complexity` which allows to set the maximum recursion for the pattern matching. It is quite useful to ensure the complexity doesn't grow, like in `tests/ui/pattern/usefulness/issue-118437-exponential-time-on-diagonal-match.rs`. r? `@Nadrieril`
2024-03-03Add and update tests to use `pattern_complexity`Guillaume Gomez-0/+123
2024-03-02Rollup merge of #121716 - Nadrieril:simple-binding-order, r=matthewjasperGuillaume Boisseau-0/+48
match lowering: Lower bindings in a predictable order After the recent refactorings, we can now lower bindings in a truly predictable order. The order in https://github.com/rust-lang/rust/pull/120214 was an improvement but not very clear. With this PR, we lower bindings from left to right, with the special case that `x @ pat` is traversed as `pat @ x` (i.e. `x` is lowered after any bindings in `pat`). This description only applies in the absence of or-patterns. Or-patterns make everything complicated, because the binding place depends on the subpattern. Until I have a better idea I leave them to be handled in whatever weird order arises from today's code. r? `@matthewjasper`
2024-03-02Fix a subtle regressionNadrieril-0/+14
Before, the SwitchInt cases were computed in two passes: if the first pass accepted e.g. 0..=5 and then 1, the second pass would not accept 0..=5 anymore because 1 would be listed in the SwitchInt options. Now there's a single pass, so if we sort 0..=5 we must take care to not sort a subsequent 1.
2024-03-01Detect more cases of `=` to `:` typoEsteban Küber-2/+6
When a `Local` is fully parsed, but not followed by a `;`, keep the `:` span arround and mention it. If the type could continue being parsed as an expression, suggest replacing the `:` with a `=`. ``` error: expected one of `!`, `+`, `->`, `::`, `;`, or `=`, found `.` --> file.rs:2:32 | 2 | let _: std::env::temp_dir().join("foo"); | - ^ expected one of `!`, `+`, `->`, `::`, `;`, or `=` | | | while parsing the type for `_` | help: use `=` if you meant to assign ``` Fix #119665.
2024-02-28Add test for the known case that doesn't workNadrieril-0/+48
2024-02-23Rollup merge of #120742 - Nadrieril:use-min_exh_pats, r=compiler-errorsMatthias Krüger-220/+180
mark `min_exhaustive_patterns` as complete This is step 1 and 2 of my [proposal](https://github.com/rust-lang/rust/issues/119612#issuecomment-1918097361) to move `min_exhaustive_patterns` forward. The vast majority of in-tree use cases of `exhaustive_patterns` are covered by `min_exhaustive_patterns`. There are a few cases that still require `exhaustive_patterns` in tests and they're all behind references. r? ``@ghost``
2024-02-16[AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives许杰友 Jieyou Xu (Joe)-84/+84
2024-02-13Do not point at `#[allow(_)]` as the reason for compat lint triggeringEsteban Küber-6/+0
Fix #121009.
2024-02-13Prefer `min_exhaustive_patterns` in testsNadrieril-2/+2
2024-02-13Unmark the feature gate as incompleteNadrieril-218/+178
2024-02-09Move some testsCaio-0/+32
2024-02-08Rollup merge of #120775 - Nadrieril:more-min_exh_pats, r=compiler-errorsMatthias Krüger-16/+138
Make `min_exhaustive_patterns` match `exhaustive_patterns` better Split off from https://github.com/rust-lang/rust/pull/120742. There remained two edge cases where `min_exhaustive_patterns` wasn't behaving like `exhaustive_patterns`. This fixes them, and tests the feature in a bunch more cases. I essentially went through all uses of `exhaustive_patterns` to see which ones would be interesting to compare between the two features. r? `@compiler-errors`
2024-02-08Auto merge of #120767 - matthiaskrgr:rollup-0k8ib1c, r=matthiaskrgrbors-211/+410
Rollup of 9 pull requests Successful merges: - #119592 (resolve: Unload speculatively resolved crates before freezing cstore) - #120103 (Make it so that async-fn-in-trait is compatible with a concrete future in implementation) - #120206 (hir: Make sure all `HirId`s have corresponding HIR `Node`s) - #120214 (match lowering: consistently lower bindings deepest-first) - #120688 (GVN: also turn moves into copies with projections) - #120702 (docs: also check the inline stmt during redundant link check) - #120727 (exhaustiveness: Prefer "`0..MAX` not covered" to "`_` not covered") - #120734 (Add `SubdiagnosticMessageOp` as a trait alias.) - #120739 (improve pretty printing for associated items in trait objects) r? `@ghost` `@rustbot` modify labels: rollup
2024-02-08Test `min_exhaustive_patterns` in more casesNadrieril-16/+138
2024-02-08Continue to borrowck even if there were previous errorsOli Scherer-9/+76
2024-02-08Rollup merge of #120727 - Nadrieril:tweak-int-reporting, r=compiler-errorsMatthias Krüger-45/+189
exhaustiveness: Prefer "`0..MAX` not covered" to "`_` not covered" There was an exception when reporting integer ranges as missing, it's been there for as long as I can remember. This PR removes it. I think it's nicer to report "`0..MAX` not covered" than "`_` not covered". This also makes it consistent with enums, where we report individual enum variants in this case (as showcased in the rest of the `empty-match.rs` test). r? ``@estebank``
2024-02-08Rollup merge of #120214 - Nadrieril:fix-120210, r=pnkfelixMatthias Krüger-166/+221
match lowering: consistently lower bindings deepest-first Currently when lowering match expressions to MIR, we do a funny little dance with the order of bindings. I attempt to explain it in the third commit: we handle refutable (i.e. needing a test) patterns differently than irrefutable ones. This leads to inconsistencies, as reported in https://github.com/rust-lang/rust/issues/120210. The reason we need a dance at all is for situations like: ```rust fn foo1(x: NonCopyStruct) { let y @ NonCopyStruct { copy_field: z } = x; // the above should turn into let z = x.copy_field; let y = x; } ``` Here the `y ```````@```````` binding will move out of `x`, so we need to copy the field first. I believe that the inconsistency came about when we fixed https://github.com/rust-lang/rust/issues/69971, and didn't notice that the fix didn't extend to refutable patterns. My guess then is that ordering bindings by "deepest-first, otherwise source order" is a sound choice. This PR implements that (at least I hope, match lowering is hard to follow :smiling_face_with_tear:). Fixes https://github.com/rust-lang/rust/issues/120210 r? ```````@oli-obk``````` since you merged the original fix to https://github.com/rust-lang/rust/issues/69971 cc ```````@matthewjasper```````
2024-02-07Prefer "0..MAX not covered" to "_ not covered"Nadrieril-29/+29
2024-02-07Add testsNadrieril-32/+176
2024-02-07Update testsr0cky-1/+11
2024-02-06Rollup merge of #120423 - RalfJung:indirect-structural-match, r=petrochenkovMatthias Krüger-8/+114
update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing https://github.com/rust-lang/rfcs/pull/3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by https://github.com/rust-lang/rust/pull/116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes https://github.com/rust-lang/rust/issues/73448 by removing the affected analysis.
2024-02-06Rollup merge of #120597 - fmease:sugg-on-js-style-spread-op-in-pat, r=estebankMatthias Krüger-6/+71
Suggest `[tail @ ..]` on `[..tail]` and `[...tail]` where `tail` is unresolved Fixes #120591. ~~Will conflict with #120570~~ (rebased). r? estebank or compiler
2024-02-05update the tracking issue for structural match violationsRalf Jung-17/+17
and bless a test I missed
2024-02-05show indirect_structural_match and pointer_structural_match in future compat ↵Ralf Jung-0/+106
reports
2024-02-05Rollup merge of #116284 - RalfJung:no-nan-match, r=cjgillotMatthias Krüger-22/+19
make matching on NaN a hard error, and remove the rest of illegal_floating_point_literal_pattern These arms would never be hit anyway, so the pattern makes little sense. We have had a future-compat lint against float matches in general for a *long* time, so I hope we can get away with immediately making this a hard error. This is part of implementing https://github.com/rust-lang/rfcs/pull/3535. Closes https://github.com/rust-lang/rust/issues/41620 by removing the lint. https://github.com/rust-lang/reference/pull/1456 updates the reference to match.
2024-02-04Suggest `[tail @ ..]` on `[..tail]` and `[...tail]` where `tail` is unresolvedLeón Orell Valerian Liehr-6/+71
2024-02-01`#![feature(inline_const_pat)]` is no longer incompleteMatthew Jasper-5/+4
2024-01-26remove illegal_floating_point_literal_pattern lintRalf Jung-22/+19
2024-01-26Classify closure arguments in refutable pattern in argument errorDeadbeef-2/+2
2024-01-26Rollup merge of #118803 - Nadrieril:min-exhaustive-patterns, r=compiler-errorsMatthias Krüger-139/+773
Add the `min_exhaustive_patterns` feature gate ## Motivation Pattern-matching on empty types is tricky around unsafe code. For that reason, current stable rust conservatively requires arms for empty types in all but the simplest case. It has long been the intention to allow omitting empty arms when it's safe to do so. The [`exhaustive_patterns`](https://github.com/rust-lang/rust/issues/51085) feature allows the omission of all empty arms, but hasn't been stabilized because that was deemed dangerous around unsafe code. ## Proposal This feature aims to stabilize an uncontroversial subset of exhaustive_patterns. Namely: when `min_exhaustive_patterns` is enabled and the data we're matching on is guaranteed to be valid by rust's operational semantics, then we allow empty arms to be omitted. E.g.: ```rust let x: Result<T, !> = foo(); match x { // ok Ok(y) => ..., } let Ok(y) = x; // ok ``` If the place is not guaranteed to hold valid data (namely ptr dereferences, ref dereferences (conservatively) and union field accesses), then we keep stable behavior i.e. we (usually) require arms for the empty cases. ```rust unsafe { let ptr: *const Result<u32, !> = ...; match *ptr { Ok(x) => { ... } Err(_) => { ... } // still required } } let foo: Result<u32, &!> = ...; match foo { Ok(x) => { ... } Err(&_) => { ... } // still required because of the dereference } unsafe { let ptr: *const ! = ...; match *ptr {} // already allowed on stable } ``` Note that we conservatively consider that a valid reference can point to invalid data, hence we don't allow arms of type `&!` and similar cases to be omitted. This could eventually change depending on [opsem decisions](https://github.com/rust-lang/unsafe-code-guidelines/issues/413). Whenever opsem is undecided on a case, we conservatively keep today's stable behavior. I proposed this behavior in the [`never_patterns`](https://github.com/rust-lang/rust/issues/118155) feature gate but it makes sense on its own and could be stabilized more quickly. The two proposals nicely complement each other. ## Unresolved Questions Part of the question is whether this requires an RFC. I'd argue this doesn't need one since there is no design question beyond the intent to omit unreachable patterns, but I'm aware the problem can be framed in ways that require design (I'm thinking of the [original never patterns proposal](https://smallcultfollowing.com/babysteps/blog/2018/08/13/never-patterns-exhaustive-matching-and-uninhabited-types-oh-my/), which would frame this behavior as "auto-nevering" happening). EDIT: I initially proposed a future-compatibility lint as part of this feature, I don't anymore.
2024-01-25Put new bindings first in refutable cases tooNadrieril-163/+183
2024-01-25Add testsNadrieril-6/+41
2024-01-25Implement feature gate logicNadrieril-139/+773
2024-01-24remove StructuralEq traitRalf Jung-125/+105
2024-01-18Add testsNadrieril-90/+0
2024-01-15Auto merge of #119610 - Nadrieril:never_pattern_bindings, r=compiler-errorsbors-73/+13
never patterns: Check bindings wrt never patterns Never patterns: - Shouldn't contain bindings since they never match anything; - Don't count when checking that or-patterns have consistent bindings. r? `@compiler-errors`
2024-01-13Bless testsGeorge-lewis-0/+5
Update tests
2024-01-11Only lint ranges that really overlapNadrieril-25/+19