about summary refs log tree commit diff
path: root/compiler/rustc_mir_build/src/builder
AgeCommit message (Collapse)AuthorLines
2025-07-26Rollup merge of #143585 - folkertdev:loop-match-suggest-const-block, r=oli-obkTrevor Gross-3/+26
`loop_match`: suggest extracting to a `const` item tracking issue: https://github.com/rust-lang/rust/issues/132306 fixes https://github.com/rust-lang/rust/issues/143310 fixes https://github.com/rust-lang/rust/issues/143936
2025-07-26`loop_match`: suggest extracting to a `const` itemFolkert de Vries-3/+26
if the expression cannot be evaluated in a straightforward way
2025-07-24Auto merge of #144389 - scottmcm:no-more-mir-cast-assume, r=davidtwcobors-78/+2
MIR-build: No longer emit assumes in enum-as casting This just uses the `valid_range` from the backend, so it's duplicating the range metadata that now we include on parameters and loads, and thus no longer seems to be useful -- notably there's no codegen test failures from removing it. (Because it's using data from the same source as the backend annotations, it doesn't do anything to mitigate things like rust-lang/rust#144388 where the range in the layout is more permissive than the actual possible discriminants. A variant of this that actually checked the discriminants more specifically might be useful, so could potentially be added in future, but I don't think the *current* checks are actually providing value.) r? mir Randomly turns out that this Fixes https://github.com/rust-lang/rust/issues/121097
2025-07-24MIR-build: No longer emit assumes in enum-as castingScott McMurray-78/+2
This just uses the `valid_range` from the backend, so it's duplicating the range metadata that now we include on parameters and loads.
2025-07-23Remove useless lifetime parameter.Camille GILLOT-22/+4
2025-07-23Give an AllocId to ConstValue::Slice.Camille GILLOT-13/+28
2025-07-09Auto merge of #143472 - dianne:deref-pat-column-check, r=Nadrierilbors-0/+1
`rustc_pattern_analysis`: always check that deref patterns don't match on the same place as normal constructors In rust-lang/rust#140106, deref pattern validation was tied to the `deref_patterns` feature to temporarily avoid affecting perf. However: - As of rust-lang/rust#143414, box patterns are represented as deref patterns in `rustc_pattern_analysis`. Since they can be used by enabling `box_patterns` instead of `deref_patterns`, it was possible for them to skip validation, resulting in an ICE. This fixes that and adds a regression test. - External tooling (e.g. rust-analyzer) will also need to validate matches containing deref patterns, which was not possible. This fixes that by making `compute_match_usefulness` validate deref patterns by default. In order to avoid doing an extra pass for anything with patterns, the second commit makes `RustcPatCtxt` keep track of whether it encounters a deref pattern, so that it only does the check if so. This is purely for performance. If the perf impact of the first commit is negligible and the complexity cost introduced by the second commit is significant, it may be worth dropping the latter. r? `@Nadrieril`
2025-07-07`loop_match`: fix 'no terminator on block'Folkert de Vries-1/+3
2025-07-05Remove yields_in_scope from the scope tree.Camille GILLOT-3/+0
2025-07-04only check for mixed deref/normal constructors when neededdianne-0/+1
2025-07-01loop match: handle opaque patternsFolkert de Vries-2/+4
fixes issue 143203
2025-07-01loop match: run exhaustiveness checkFolkert de Vries-4/+8
2025-07-01Auto merge of #141875 - nnethercote:ByteSymbol, r=petrochenkovbors-11/+12
Introduce `ByteSymbol` 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.
2025-06-30Introduce `ByteSymbol`.Nicholas Nethercote-11/+12
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-06-29mir: Add a `new` method to `statement`dianqk-55/+50
Avoid introducing a large number of changes when adding optional initialization fields.
2025-06-23Add `#[loop_match]` for improved DFA codegenbjorn3-14/+525
Co-authored-by: Folkert de Vries <folkert@folkertdev.nl>
2025-06-13Unimplement unsized_localsmejrs-3/+3
2025-06-05Replace some `Option<Span>` with `Span` and use DUMMY_SP instead of NoneOli Scherer-4/+4
2025-05-31Rollup merge of #141740 - nnethercote:hir-ItemKind-field-order, r=fee1-deadMatthias Krüger-1/+1
Hir item kind field order A follow-up to rust-lang/rust#141675. r? `@fee1-dead`
2025-05-30Reorder fields in `hir::ItemKind` variants.Nicholas Nethercote-1/+1
Specifically `TyAlias`, `Enum`, `Struct`, `Union`. So the fields match the textual order in the source code. The interesting part of the change is in `compiler/rustc_hir/src/hir.rs`. The rest is extremely mechanical refactoring.
2025-05-29Structurally normalize types as needed in projection_ty_coreMichael Goulet-2/+2
2025-05-29Rollup merge of #141683 - compiler-errors:unsafe-binder-capture, r=oli-obkJacob Pratt-2/+2
Handle ed2021 precise capturing of unsafe binder Missing pieces from last pr. r? oli-obk Fixes https://github.com/rust-lang/rust/issues/141655
2025-05-28Rollup merge of #141627 - nnethercote:drop-cleanups, r=matthewjasperTrevor Gross-53/+59
Drop-build cleanups Some cleanups I made while trying to speed up the program in rust-lang/rust#134404. r? ```@matthewjasper```
2025-05-28Handle e2021 precise capturing of unsafe binderMichael Goulet-2/+2
2025-05-27Avoid `fold`/`flat_map`.Nicholas Nethercote-5/+6
This pattern of iterating over scopes and drops occurs multiple times in this file, with slight variations. All of them use `for` loops except this one. This commits changes it for consistency.
2025-05-27Factor out some repeated code in `build_exit_tree`.Nicholas Nethercote-12/+6
2025-05-27Rename `DropTree::drops` as `DropTree::drop_nodes`.Nicholas Nethercote-34/+46
Because `Scope` also has a field named `drops`, and I found having two fields with the same name made this code harder to read.
2025-05-27Remove `DropNodeKey::kind`.Nicholas Nethercote-2/+1
It's not needed, because `next` and `local` fields uniquely identify the drop. This is a ~2% speed win on the very large program in #134404, and it's also a tiny bit simpler.
2025-05-27Rollup merge of #141513 - nia-e:allocbytes-extend, r=RalfJung许杰友 Jieyou Xu (Joe)-3/+3
interpret: add allocation parameters to `AllocBytes` Necessary for a better implementation of [rust-lang/miri#4343](https://github.com/rust-lang/miri/pull/4343). Also included here is the code from that PR, adapted to this new interface for the sake of example and so that CI can run on them; the Miri changes can be reverted and merged separately, though. r? `@RalfJung`
2025-05-26extend allocbytes with associated typeNia Espera-3/+3
2025-05-23Properly analyze captures from unsafe bindersMichael Goulet-0/+3
2025-05-21Rollup merge of #141357 - dianne:unhardcode-unpretty-thir-tree-body-expr, ↵Matthias Krüger-4/+0
r=compiler-errors `unpretty=thir-tree`: don't require the final expr to be the body's value Two motivations for this: - I couldn't find a comment motivating this hard-coding. I can imagine it might be easier to read `unpretty=thir-flat` output if the final expression in the THIR is always the body's value, but if that's the reason, that should be the justification in the source. I can also imagine it's meant to check that all expressions will be visited by the pretty-printer, but the existing check doesn't quite do that either. - Guard patterns (#129967) contain expressions, so lowering params containing guard patterns may add more expressions to the THIR. Currently a body's params are lowered after its expression, so guard expressions in params would end up last, breaking this. As an alternative, the params could be lowered first (#141356).
2025-05-21`unpretty=thir-tree`: don't require the final expr to be the entrypointdianne-4/+0
2025-05-20typeck: catch `continue`s pointing to blocksdianne-2/+1
This taints the typeck results with errors if a `continue` is found not pointing to a loop, which fixes an ICE. A few things were going wrong here. First, since this wasn't caught in typeck, we'd end up building the THIR and then running liveness lints on ill-formed HIR. Since liveness assumes all `continue`s point to loops, it wasn't setting a live node for the `continue`'s destination. However, the fallback for this was faulty; it would create a new live node to represent the erroneous state after the analysis's RWU table had already been built. This would ICE if the new live node was used in operations, such as merging results from the arms of a match. I've removed this error-recovery since it was buggy, and we should really catch bad labels before liveness. I've also replaced an outdated comment about when liveness lints are run. At this point, I think the call to `check_liveness` could be moved elsewhere, but if it can be run when the typeck results are tainted by errors, it'll need some slight refactoring so it can bail out in that case. In lieu of that, I've added an assertion.
2025-05-12Fix typosomahs-1/+1
2025-05-04Initial support for dynamically linked cratesBryanskiy-1/+3
2025-04-28Rollup merge of #140022 - dianne:box-deref-pats, r=NadrierilChris Denton-2/+9
allow deref patterns to move out of boxes This adds a case to lower deref patterns on boxes using a built-in deref instead of a `Deref::deref` or `DerefMut::deref_mut` call: if `deref!(inner): Box<T>` is matching on place `place`, the inner pattern `inner` now matches on `*place` rather than a temporary. No longer needing to call a method also means it won't borrow the scrutinee in match arms. This allows for bindings in `inner` to move out of `*place`. For comparison with box patterns, this uses the same MIR lowering but different THIR. Consequently, deref patterns on boxes are treated the same as any other deref patterns in match exhaustiveness analysis. Box patterns can't quite be implemented in terms of deref patterns until exhaustiveness checking for deref patterns is implemented (I'll open a PR for exhaustiveness soon!). Tracking issue: #87121 r? ``@Nadrieril``
2025-04-28AsyncDrop implementation using shim codegen of ↵Andrew Zhogin-20/+143
async_drop_in_place::{closure}, scoped async drop added.
2025-04-24lower deref patterns on boxes using built-in derefsdianne-2/+9
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-24Rollup merge of #140028 - dianne:lit-deref-pats-p1, r=oli-obkMatthias Krüger-0/+23
`deref_patterns`: support string and byte string literals in explicit `deref!("...")` patterns When `deref_patterns` is enabled, this allows string literal patterns to be used where `str` is expected and byte string literal patterns to be used where `[u8]` or `[u8; N]` is expected. This lets them be used in explicit `deref!("...")` patterns to match on `String`, `Box<str>`, `Vec<u8>`, `Box<[u8;N]>`, etc. (as well as to match on slices and arrays obtained through other means). Implementation-wise, this follows up on #138992: similar to how byte string literals matching on `&[u8]` is implemented, this changes the type of the patterns as determined by HIR typeck, which informs const-to-pat on how to translate them to THIR (though strings needed a bit of extra work since we need references to call `<str as PartialEq>::eq` in the MIR lowering for string equality tests). This PR does not add support for implicit deref pattern syntax (e.g. `"..."` matching on `String`, as `string_deref_patterns` allows). I have that implemented locally, but I'm saving it for a follow-up PR[^1]. This also does not add support for using named or associated constants of type `&str` where `str` is expected (nor likewise with named byte string constants). It'd be possible to add that if there's an appetite for it, but I figure it's simplest to start with literals. This is gated by the `deref_patterns` feature since it's motivated by deref patterns. That said, its impact reaches outside of deref patterns; it may warrant a separate experiment and feature gate, particularly factoring in the follow-up[^1]. Even without deref patterns, I think there's probably motivation for these changes. The update to the unstable book added by this will conflict with #140022, so they shouldn't be merged at the same time. Tracking issue for deref patterns: #87121 r? ``@oli-obk`` cc ``@Nadrieril`` [^1]: The piece missing from this PR to support implicit deref pattern syntax is to allow string literal patterns to implicitly dereference their scrutinees before matching (see #44849). As a consequence, it also makes examples like the one in that issue work (though it's still gated by `deref_patterns`). I can provide more information on how I've implemented it or open a draft if it'd help in reviewing this PR.
2025-04-22make `str` literal patterns usable in deref patternsdianne-0/+23
Specifically, this allows string literal patterns to be used where a `str` is expected when `deref_patterns` is enabled.
2025-04-20remove a couple clonesMatthias Krüger-1/+1
2025-04-18Rollup merge of #139615 - nnethercote:rm-name_or_empty, r=jdonszelmannMatthias Krüger-2/+3
Remove `name_or_empty` Another step towards #137978. r? ``@jdonszelmann``
2025-04-17do not emit `OpaqueCast` projections with `-Znext-solver`lcnr-11/+14
2025-04-17Replace infallible `name_or_empty` methods with fallible `name` methods.Nicholas Nethercote-2/+3
I'm removing empty identifiers everywhere, because in practice they always mean "no identifier" rather than "empty identifier". (An empty identifier is impossible.) It's better to use `Option` to mean "no identifier" because you then can't forget about the "no identifier" possibility. Some specifics: - When testing an attribute for a single name, the commit uses the `has_name` method. - When testing an attribute for multiple names, the commit uses the new `has_any_name` method. - When using `match` on an attribute, the match arms now have `Some` on them. In the tests, we now avoid printing empty identifiers by not printing the identifier in the `error:` line at all, instead letting the carets point out the problem.
2025-04-15Rollup merge of #139669 - nnethercote:overhaul-AssocItem, r=oli-obkStuart Cook-1/+1
Overhaul `AssocItem` `AssocItem` has multiple fields that only make sense some of the time. E.g. the `name` can be empty if it's an RPITIT associated type. It's clearer and less error prone if these fields are moved to the relevant `kind` variants. r? ``@fee1-dead``
2025-04-15Rollup merge of #138393 - oli-obk:pattern-type-in-pattern, r=BoxyUwUStuart Cook-2/+27
Allow const patterns of matches to contain pattern types Trying to pattern match on a type containing a pattern type will currently fail with an ICE ```rust error: internal compiler error: compiler/rustc_mir_build/src/builder/matches/test.rs:459:18: invalid type for non-scalar compare: (u32) is 1.. --> src/main.rs:22:5 | 22 | TWO => {} | ^^^ ``` because the compiler tries to generate a MIR `BinOp(Eq)` operation on a pattern type, which is not supported. While we could support that, there are side effects of allowing this (none that would compile, but the compiler would simultaneously think it could `==` pattern types and that it could not because `PartialEq` is not implemented. So instead I change the logic for pattern matching to transmute pattern types to their base type before comparing. r? ```@BoxyUwU``` cc #123646 ```@scottmcm``` ```@joshtriplett```
2025-04-14Allow const patterns of matches to contain pattern typesOli Scherer-2/+27
2025-04-14Move `has_self` field to `hir::AssocKind::Fn`.Nicholas Nethercote-1/+1
`hir::AssocItem` currently has a boolean `fn_has_self_parameter` field, which is misplaced, because it's only relevant for associated fns, not for associated consts or types. This commit moves it (and renames it) to the `AssocKind::Fn` variant, where it belongs. This requires introducing a new C-style enum, `AssocTag`, which is like `AssocKind` but without the fields. This is because `AssocKind` values are passed to various functions like `find_by_ident_and_kind` to indicate what kind of associated item should be searched for, and having to specify `has_self` isn't relevant there. New methods: - Predicates `AssocItem::is_fn` and `AssocItem::is_method`. - `AssocItem::as_tag` which converts `AssocItem::kind` to `AssocTag`. Removed `find_by_name_and_kinds`, which is unused. `AssocItem::descr` can now distinguish between methods and associated functions, which slightly improves some error messages.
2025-04-07Make use generated TerminatorKind::Call have call_source UseSantiago Pastorino-1/+1