about summary refs log tree commit diff
path: root/compiler/rustc_mir_build/src/thir/pattern/mod.rs
AgeCommit message (Collapse)AuthorLines
2025-08-14shrink TestBranch::Constant and PatRangeBoundary::FiniteRalf Jung-2/+3
2025-08-14use ty::Value instead of manual pairs of types and valtreesRalf Jung-3/+3
2025-08-14pattern testing: store constants as valtreesRalf Jung-10/+4
2025-08-14PatKind: store constants as valtreesRalf Jung-4/+9
2025-07-04treat box patterns as deref patterns in THIR and usefulness analysisdianne-3/+4
This removes special-casing of boxes from `rustc_pattern_analysis`, as a first step in replacing `box_patterns` with `deref_patterns`. Incidentally, it fixes a bug caused by box patterns being represented as structs rather than pointers, where `exhaustive_patterns` could generate spurious `unreachable_patterns` lints on arms required for exhaustiveness; following the lint's advice would result in an error.
2025-06-30Introduce `ByteSymbol`.Nicholas Nethercote-1/+1
It's like `Symbol` but for byte strings. The interner is now used for both `Symbol` and `ByteSymbol`. E.g. if you intern `"dog"` and `b"dog"` you'll get a `Symbol` and a `ByteSymbol` with the same index and the characters will only be stored once. The motivation for this is to eliminate the `Arc`s in `ast::LitKind`, to make `ast::LitKind` impl `Copy`, and to avoid the need to arena-allocate `ast::LitKind` in HIR. The latter change reduces peak memory by a non-trivial amount on literal-heavy benchmarks such as `deep-vector` and `tuple-stress`. `Encoder`, `Decoder`, `SpanEncoder`, and `SpanDecoder` all get some changes so that they can handle normal strings and byte strings. This change does slow down compilation of programs that use `include_bytes!` on large files, because the contents of those files are now interned (hashed). This makes `include_bytes!` more similar to `include_str!`, though `include_bytes!` contents still aren't escaped, and hashing is still much cheaper than escaping.
2025-05-04Auto merge of #140549 - BoxyUwU:proper_const_norm, r=lcnrbors-0/+2
Set groundwork for proper const normalization r? lcnr Updates a lot of our normalization/alias infrastructure to be setup to handle mgca aliases and normalization once const items are represented more like aliases than bodies. Inherent associated consts are still super busted, I didn't update the assertions that IACs the right arg setup because that winds up being somewhat involved to do *before* proper support for normalizing const aliases is implemented. I dont *intend* for this to have any effect on stable. We continue normalizing via ctfe on stable and the codepaths in `project` for consts should only be reachable with mgca or ace.
2025-05-01Set groundwork for proper const normalizationBoxy-0/+2
2025-05-01User type annotations for free consts in pattern positionBoxy-2/+1
2025-04-24lower deref patterns on boxes using built-in derefsdianne-7/+4
This allows deref patterns to move out of boxes. Implementation-wise, I've opted to put the information of whether a deref pattern uses a built-in deref or a method call in the THIR. It'd be a bit less code to check `.is_box()` everywhere, but I think this way feels more robust (and we don't have a `mutability` field in the THIR that we ignore when the smart pointer's a box). I'm not sure about the naming (or using `ByRef`), though.
2025-04-16lower implicit deref patterns to THIRdianne-9/+17
Since this uses `pat_adjustments`, I've also tweaked the documentation to mention implicit deref patterns and made sure the pattern migration diagnostic logic accounts for it. I'll adjust `ExprUseVisitor` in a later commit and add some tests there for closure capture inference.
2025-04-12store the kind of pattern adjustments in `pat_adjustments`dianne-4/+5
This allows us to better distinguish builtin and overloaded implicit dereferences.
2025-04-07Rollup merge of #139108 - Nadrieril:simplify-expandedconstant, r=oli-obkStuart Cook-72/+75
Simplify `thir::PatKind::ExpandedConstant` I made it a bit less ad-hoc. In particular, I removed `is_inline: bool` that was just caching the output of `tcx.def_kind(def_id)`. This makes inline consts a tiny bit less special in patterns. r? `@oli-obk` cc `@Zalathar`
2025-04-07Rollup merge of #139035 - nnethercote:PatKind-Missing, r=oli-obkStuart Cook-0/+2
Add new `PatKind::Missing` variants To avoid some ugly uses of `kw::Empty` when handling "missing" patterns, e.g. in bare fn tys. Helps with #137978. Details in the individual commits. r? ``@oli-obk``
2025-04-06Reuse `parent_args`Nadrieril-12/+3
2025-04-06Add the inline const type annotation in pattern loweringNadrieril-2/+34
2025-04-06Tweak `lower_pat_expr`Nadrieril-35/+33
2025-04-06Remove the `is_inline` field from `PatKind::ExpandedConstant`Nadrieril-18/+11
2025-04-06Let `const_to_pat` handle the `ExpandedConstant` wrappingNadrieril-14/+3
2025-03-28Add `{ast,hir,thir}::PatKind::Missing` variants.Nicholas Nethercote-0/+2
"Missing" patterns are possible in bare fn types (`fn f(u32)`) and similar places. Currently these are represented in the AST with `ast::PatKind::Ident` with no `by_ref`, no `mut`, an empty ident, and no sub-pattern. This flows through to `{hir,thir}::PatKind::Binding` for HIR and THIR. This is a bit nasty. It's very non-obvious, and easy to forget to check for the exceptional empty identifier case. This commit adds a new variant, `PatKind::Missing`, to do it properly. The process I followed: - Add a `Missing` variant to `{ast,hir,thir}::PatKind`. - Chang `parse_param_general` to produce `ast::PatKind::Missing` instead of `ast::PatKind::Missing`. - Look through `kw::Empty` occurrences to find functions where an existing empty ident check needs replacing with a `PatKind::Missing` check: `print_param`, `check_trait_item`, `is_named_param`. - Add a `PatKind::Missing => unreachable!(),` arm to every exhaustive match identified by the compiler. - Find which arms are actually reachable by running the test suite, changing them to something appropriate, usually by looking at what would happen to a `PatKind::Ident`/`PatKind::Binding` with no ref, no `mut`, an empty ident, and no subpattern. Quite a few of the `unreachable!()` arms were never reached. This makes sense because `PatKind::Missing` can't happen in every pattern, only in places like bare fn tys and trait fn decls. I also tried an alternative approach: modifying `ast::Param::pat` to hold an `Option<P<Pat>>` instead of a `P<Pat>`, but that quickly turned into a very large and painful change. Adding `PatKind::Missing` is much easier.
2025-03-26add a temporary workaround for `string_deref_patterns`dianne-2/+21
2025-03-26`lower_pat_expr`: use the pattern's type instead of the literal'sdianne-4/+13
This allows us to remove the field `treat_byte_string_as_slice` from `TypeckResults`, since the pattern's type contains everything necessary to get the correct lowering for byte string literal patterns. This leaves the implementation of `string_deref_patterns` broken, to be fixed in the next commit.
2025-03-09Explain weird quirk in user type annotation loweringMichael Goulet-1/+1
2025-02-17Rollup merge of #136817 - dianne:clean-and-comment-pat-migration, r=NadrierilMatthias Krüger-111/+23
Pattern Migration 2024: clean up and comment This follows up on #136577 by moving the pattern migration logic to its own module, removing a bit of unnecessary complexity, and adding comments. Since there's quite a bit of pattern migration logic now (and potentially more in #136496), I think it makes sense to keep it separate from THIR construction, at least as much as is convenient. r? ``@Nadrieril``
2025-02-14More comments for `lower_inline_const`Zalathar-1/+4
2025-02-14Clarify control-flow in `lower_path`Zalathar-30/+33
2025-02-14Rename `PatCtxt::lower_lit` to `lower_pat_expr`Zalathar-7/+7
This matches the HIR changes in #134228, which introduced `PatExpr` to hold the subset of "expressions" that can appear in a pattern.
2025-02-10move pattern migration internals to the `migration` moduledianne-57/+10
2025-02-10move pattern migration setup/emitting to a separate moduledianne-58/+17
2025-02-07Rollup merge of #136577 - dianne:simple-pat-migration-simplification, ↵Matthias Krüger-46/+100
r=Nadrieril Pattern Migration 2024: try to suggest eliding redundant binding modifiers This is based on #136475. Only the last commit is new. This is a simpler, more restrictive alternative to #136496, meant to partially address #136047. If a pattern can be migrated to Rust 2024 solely by removing redundant binding modifiers, this will make that suggestion; otherwise, it uses the old suggestion of making the pattern fully explicit. Relevant tracking issue: #131414 ``@rustbot`` label A-diagnostics A-patterns A-edition-2024 r? ``@Nadrieril``
2025-02-06Rollup merge of #136435 - Zalathar:thir-pat-stuff, r=NadrierilMatthias Krüger-5/+6
Simplify some code for lowering THIR patterns I've been playing around with some radically different ways of storing THIR patterns, and while those experiments haven't yet produced a clear win, I have noticed various smaller things in the existing code that can be made a bit nicer. Some of the more significant changes: - With a little bit of extra effort (and thoughtful use of Arc), we can completely remove an entire layer of `'pat` lifetimes from the intermediate data structures used for match lowering. - In several places, lists of THIR patterns were being double-boxed for no apparent reason.
2025-02-05try to suggest eliding redundant binding modifiersdianne-27/+31
2025-02-05peace of mind: remove a call to `Option::expect`dianne-2/+3
2025-02-05separate labels for default binding mode spans into their own notesdianne-10/+0
2025-02-04Rollup merge of #136526 - Zalathar:thir-cx, r=NadrierilMatthias Krüger-21/+13
mir_build: Rename `thir::cx::Cx` to `ThirBuildCx` and remove `UserAnnotatedTyHelpers` A combination of two loosely-related tweaks that would otherwise conflict with each other: - `Cx` is a pretty unhelpful type name, especially when jumping between THIR-building and MIR-building while trying to make changes to THIR data structures. - The `UserAnnotatedTyHelpers` trait doesn't appear to provide any benefit over a simple helper function, and its `tcx()` method is currently completely unnecessary. No functional change.
2025-02-04experimentally label the spans for default binding modesdianne-35/+88
2025-02-03highlight the whole problem subpattern when pointing out the default binding ↵dianne-2/+2
mode
2025-02-04Remove helper trait `UserAnnotatedTyHelpers`Zalathar-21/+13
This trait doesn't appear to provide any benefit over a simple helper function.
2025-02-03reword pattern migration diagnostic to make sense in all editionsdianne-8/+16
This aligns the main error message a bit more with the phrasing in the Edition Guide and provides a bit more information on the labels to (hopefully!) aid in understanding.
2025-02-03pattern migration: move labels out of the suggestion structdianne-13/+11
2025-02-03Avoid double-boxing lists of THIR subpatternsZalathar-4/+4
2025-02-03Remove `'pat` lifetime from some match-lowering data structuresZalathar-1/+2
By storing `PatRange` in an Arc, and copying a few fields out of `Pat`, we can greatly simplify the lifetimes involved in match lowering.
2025-02-03Simplify the pattern unpeeling in `lower_pattern_range_endpoint`Zalathar-26/+25
2025-02-03Return range endpoint ascriptions/consts via a `&mut Vec`Zalathar-12/+19
2025-02-03Flatten the option check in `lower_pattern_range_endpoint`Zalathar-31/+28
2025-01-29Eliminate PatKind::PathOli Scherer-4/+0
2025-01-09Remove the now-useless `Result` from `lit_to_const`Oli Scherer-5/+3
2025-01-09Use error constant instead of explicit error handlingOli Scherer-1/+0
2025-01-08Rename PatKind::Lit to ExprOli Scherer-1/+1
2025-01-08Exhaustively handle expressions in patternsOli Scherer-25/+12