summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/interpret
AgeCommit message (Collapse)AuthorLines
2024-03-14downgrade mutable-ptr-in-final-value from hard-error to future-incompat lint ↵Felix S. Klock II-4/+8
to address issue 121610. (cherry picked from commit a8549b4152566d8fbd34c097236df1db3d9afd3c)
2024-01-26interpret/memory: fix safety comment for large array memset optimizationRalf Jung-5/+4
2024-01-26add test for GVN issue; cleanup in dataflow_const_propRalf Jung-1/+5
2024-01-26interpret: project_downcast: do not ICE for uninhabited variantsRalf Jung-19/+2
2024-01-25Auto merge of #119627 - oli-obk:const_prop_lint_n̵o̵n̵sense, r=cjgillotbors-49/+25
Remove all ConstPropNonsense We track all locals and projections on them ourselves within the const propagator and only use the InterpCx to actually do some low level operations or read from constants (via `OpTy` we get for said constants). This helps moving the const prop lint out from the normal pipeline and running it just based on borrowck information. This in turn allows us to make progress on https://github.com/rust-lang/rust/pull/108730#issuecomment-1875557745 there are various follow up cleanups that can be done after this PR (e.g. not matching on Rvalue twice and doing binop checks twice), but lets try landing this one first. r? `@RalfJung`
2024-01-24Auto merge of #118336 - saethlin:const-to-op-cache, r=RalfJungbors-7/+31
Return a finite number of AllocIds per ConstAllocation in Miri Before this, every evaluation of a const slice would produce a new AllocId. So in Miri, this program used to have unbounded memory use: ```rust fn main() { loop { helper(); } } fn helper() { "ouch"; } ``` Every trip around the loop creates a new AllocId which we need to keep track of a base address for. And the provenance GC can never clean up that AllocId -> u64 mapping, because the AllocId is for a const allocation which will never be deallocated. So this PR moves the logic of producing an AllocId for a ConstAllocation to the Machine trait, and the implementation that Miri provides will only produce 16 AllocIds for each allocation. The cache is also keyed on the Instance that the const is evaluated in, so that equal consts evaluated in two functions will have disjoint base addresses. r? RalfJung
2024-01-23Rollup merge of #120139 - compiler-errors:fnonce-shim, r=BoxyUwULeón Orell Valerian Liehr-2/+1
Do not normalize closure signature when building `FnOnce` shim It is not necessary to normalize the closure signature when building an `FnOnce` shim for an `Fn`/`FnMut` closure. That closure shim is just calling `FnMut::call_mut(&mut self)` anyways. It's also somewhat sketchy that we were ever doing this to begin with, since we're normalizing with a `ParamEnv::reveal_all()` param-env, which is definitely not right with possibly polymorphic substs. This cuts out a tiny bit of unnecessary work in `Instance::resolve` and simplifies the signature because now we can unconditionally return an `Instance`.
2024-01-23const prop nonsense eliminatedOli Scherer-49/+25
2024-01-23Add a doc comment for eval_mir_constantBen Kimock-0/+2
Co-authored-by: Ralf Jung <post@ralfj.de>
2024-01-22Do not normalize closure signature when building FnOnce shimMichael Goulet-2/+1
2024-01-22reword commentRalf Jung-7/+10
2024-01-22more clear codeRalf Jung-11/+6
Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de>
2024-01-22const-eval interner: from-scratch rewrite using mutability information from ↵Ralf Jung-439/+269
provenance rather than types
2024-01-17Optimize large array creation in const-evalMark Rousskov-12/+19
This changes repeated memcpy's to a memset for the case that we're propagating a single byte into a region of memory.
2024-01-15compiler: Lower fn call arg spans down to MIRMartin Nordholts-4/+4
To enable improved accuracy of diagnostics in upcoming commits.
2024-01-06Sometimes return the same AllocId for a ConstAllocationBen Kimock-7/+29
2023-12-28Remove movability from TyKind::CoroutineMichael Goulet-3/+3
2023-12-26Auto merge of #119146 - nnethercote:rm-DiagCtxt-api-duplication, ↵bors-7/+7
r=compiler-errors Remove `DiagCtxt` API duplication `DiagCtxt` defines the internal API for creating and emitting diagnostics: methods like `struct_err`, `struct_span_warn`, `note`, `create_fatal`, `emit_bug`. There are over 50 methods. Some of these methods are then duplicated across several other types: `Session`, `ParseSess`, `Parser`, `ExtCtxt`, and `MirBorrowckCtxt`. `Session` duplicates the most, though half the ones it does are unused. Each duplicated method just calls forward to the corresponding method in `DiagCtxt`. So this duplication exists to (in the best case) shorten chains like `ecx.tcx.sess.parse_sess.dcx.emit_err()` to `ecx.emit_err()`. This API duplication is ugly and has been bugging me for a while. And it's inconsistent: there's no real logic about which methods are duplicated, and the use of `#[rustc_lint_diagnostic]` and `#[track_caller]` attributes vary across the duplicates. This PR removes the duplicated API methods and makes all diagnostic creation and emission go through `DiagCtxt`. It also adds `dcx` getter methods to several types to shorten chains. This approach scales *much* better than API duplication; indeed, the PR adds `dcx()` to numerous types that didn't have API duplication: `TyCtxt`, `LoweringCtxt`, `ConstCx`, `FnCtxt`, `TypeErrCtxt`, `InferCtxt`, `CrateLoader`, `CheckAttrVisitor`, and `Resolver`. These result in a lot of changes from `foo.tcx.sess.emit_err()` to `foo.dcx().emit_err()`. (You could do this with more types, but it gets into diminishing returns territory for types that don't emit many diagnostics.) After all these changes, some call sites are more verbose, some are less verbose, and many are the same. The total number of lines is reduced, mostly because of the removed API duplication. And consistency is increased, because calls to `emit_err` and friends are always preceded with `.dcx()` or `.dcx`. r? `@compiler-errors`
2023-12-24Remove `Session` methods that duplicate `DiagCtxt` methods.Nicholas Nethercote-7/+7
Also add some `dcx` methods to types that wrap `TyCtxt`, for easier access.
2023-12-23interpret/memory: explain why we check is_thread_local_staticRalf Jung-0/+6
2023-12-18Rename many `DiagCtxt` and `EarlyDiagCtxt` locals.Nicholas Nethercote-3/+3
2023-12-18Rename `Session::span_diagnostic` as `Session::dcx`.Nicholas Nethercote-1/+1
2023-12-14Rollup merge of #118935 - RalfJung:interpret-downcast, r=saethlinMatthias Krüger-0/+18
interpret: extend comment on the inhabitedness check in downcast Cc https://github.com/rust-lang/rust/issues/115145 r? ``@saethlin``
2023-12-14interpret: extend comment on the inhabitedness check in downcastRalf Jung-0/+18
2023-12-13fix computing the dynamic alignment of packed structs with dyn trait tailsRalf Jung-25/+18
2023-12-13Auto merge of #118500 - ZetaNumbers:tcx_hir_refactor, r=petrochenkovbors-1/+1
Move some methods from `tcx.hir()` to `tcx` https://github.com/rust-lang/rust/pull/118256#issuecomment-1826442834 Renamed: - find -> opt_hir_node - get -> hir_node - find_by_def_id -> opt_hir_node_by_def_id - get_by_def_id -> hir_node_by_def_id
2023-12-13Auto merge of #118534 - RalfJung:extern-type-size-of-val, r=WaffleLapkinbors-4/+7
codegen: panic when trying to compute size/align of extern type The alignment is also computed when accessing a field of extern type at non-zero offset, so we also panic in that case. Previously `size_of_val` worked because the code path there assumed that "thin pointer" means "sized". But that's not true any more with extern types. The returned size and align are just blatantly wrong, so it seems better to panic than returning wrong results. We use a non-unwinding panic since code probably does not expect size_of_val to panic.
2023-12-12clippy::complexity fixesMatthias Krüger-1/+1
filter_map_identity needless_bool search_is_some unit_arg map_identity needless_question_mark derivable_impls
2023-12-12Move some methods from `tcx.hir()` to `tcx`zetanumbers-1/+1
Renamings: - find -> opt_hir_node - get -> hir_node - find_by_def_id -> opt_hir_node_by_def_id - get_by_def_id -> hir_node_by_def_id Fix rebase changes using removed methods Use `tcx.hir_node_by_def_id()` whenever possible in compiler Fix clippy errors Fix compiler Apply suggestions from code review Co-authored-by: Vadim Petrochenkov <vadim.petrochenkov@gmail.com> Add FIXME for `tcx.hir()` returned type about its removal Simplify with with `tcx.hir_node_by_def_id`
2023-12-12reject projecting to fields whose offset we cannot computeRalf Jung-4/+7
2023-12-11Auto merge of #118032 - RalfJung:char-u32, r=Mark-Simulacrumbors-0/+2
guarantee that char and u32 are ABI-compatible In https://github.com/rust-lang/rust/pull/116894 we added a guarantee that `char` has the same alignment as `u32`, but there is still one axis where these types could differ: function call ABI. So let's nail that down as well: in a function signature, `char` and `u32` are completely equivalent. This is a new stable guarantee, so it will need t-lang approval.
2023-12-07Auto merge of #118324 - RalfJung:ctfe-read-only-pointers, r=saethlinbors-73/+88
compile-time evaluation: detect writes through immutable pointers This has two motivations: - it unblocks https://github.com/rust-lang/rust/pull/116745 (and therefore takes a big step towards `const_mut_refs` stabilization), because we can now detect if the memory that we find in `const` can be interned as "immutable" - it would detect the UB that was uncovered in https://github.com/rust-lang/rust/pull/117905, which was caused by accidental stabilization of `copy` functions in `const` that can only be called with UB When UB is detected, we emit a future-compat warn-by-default lint. This is not a breaking change, so completely in line with [the const-UB RFC](https://rust-lang.github.io/rfcs/3016-const-ub.html), meaning we don't need t-lang FCP here. I made the lint immediately show up for dependencies since it is nearly impossible to even trigger this lint without `const_mut_refs` -- the accidentally stabilized `copy` functions are the only way this can happen, so the crates that popped up in #117905 are the only causes of such UB (in the code that crater covers), and the three cases of UB that we know about have all been fixed in their respective crates already. The way this is implemented is by making use of the fact that our interpreter is already generic over the notion of provenance. For CTFE we now use the new `CtfeProvenance` type which is conceptually an `AllocId` plus a boolean `immutable` flag (but packed for a more efficient representation). This means we can mark a pointer as immutable when it is created as a shared reference. The flag will be propagated to all pointers derived from this one. We can then check the immutable flag on each write to reject writes through immutable pointers. I just hope perf works out.
2023-12-07avoid marking as immutable what is already immutableRalf Jung-5/+12
this has been demonstrated to help performance
2023-12-07compile-time evaluation: emit a lint when a write through an immutable ↵Ralf Jung-14/+15
pointer occurs
2023-12-07ctfe interpreter: extend provenance so that it can track whether a pointer ↵Ralf Jung-56/+63
is immutable
2023-12-04Rollup merge of #118565 - RalfJung:numeric_intrinsic, r=davidtwcoGuillaume Gomez-46/+36
interpret: make numeric_intrinsic accessible from Miri This will let us share the code of the cttz and simd_cttz intrinsics (and same for ctlz).
2023-12-04Rollup merge of #118573 - petrochenkov:pathdatakind, r=TaKO8KiTakayuki Maeda-4/+2
rustc: Harmonize `DefKind` and `DefPathData` Follow up to https://github.com/rust-lang/rust/pull/118188. `DefPathData::(ClosureExpr,ImplTrait)` are renamed to match `DefKind::(Closure,OpaqueTy)`. `DefPathData::ImplTraitAssocTy` is replaced with `DefPathData::TypeNS(kw::Empty)` because both correspond to `DefKind::AssocTy`. It's possible that introducing `(DefKind,DefPathData)::AssocOpaqueTy` instead could be a better solution, but that would be a much more invasive change. Const generic parameters introduced for effects are moved from `DefPathData::TypeNS` to `DefPathData::ValueNS`, because constants are values. `DefPathData` is no longer passed to `create_def` functions to avoid redundancy.
2023-12-04Rollup merge of #118540 - RalfJung:unsized-packed-offset, r=TaKO8KiTakayuki Maeda-1/+11
codegen, miri: fix computing the offset of an unsized field in a packed struct `#[repr(packed)]` strikes again. Fixes https://github.com/rust-lang/rust/issues/118537 Fixes https://github.com/rust-lang/miri/issues/3200 `@bjorn3` I assume cranelift needs the same fix.
2023-12-03miri: support 'promising' alignment for symbolic alignment checkRalf Jung-16/+22
2023-12-03rustc: Harmonize `DefKind` and `DefPathData`Vadim Petrochenkov-4/+2
`DefPathData::(ClosureExpr,ImplTrait)` are renamed to match `DefKind::(Closure,OpaqueTy)`. `DefPathData::ImplTraitAssocTy` is replaced with `DefPathData::TypeNS(kw::Empty)` because both correspond to `DefKind::AssocTy`. It's possible that introducing `(DefKind,DefPathData)::AssocOpaqueTy` could be a better solution, but that would be a much more invasive change. Const generic parameters introduced for effects are moved from `DefPathData::TypeNS` to `DefPathData::ValueNS`, because constants are values. `DefPathData` is no longer passed to `create_def` functions to avoid redundancy.
2023-12-03interpret: make numeric_intrinsic accessible from MiriRalf Jung-46/+36
2023-12-03codegen, miri: fix computing the offset of an unsized field in a packed structRalf Jung-1/+11
2023-12-02Auto merge of #118470 - nnethercote:cleanup-error-handlers, r=compiler-errorsbors-10/+10
Cleanup error handlers Mostly by making function naming more consistent. More to do after this, but this is enough for one PR. r? compiler-errors
2023-12-02Use `Session::diagnostic` in more places.Nicholas Nethercote-1/+1
2023-12-02Rename `Handler::delay_good_path_bug` as `Handler::good_path_delayed_bug`.Nicholas Nethercote-5/+5
In line with the previous commits.
2023-12-02Rename `HandlerInner::delay_span_bug` as `HandlerInner::span_delayed_bug`.Nicholas Nethercote-4/+4
Because the corresponding `Level` is `DelayedBug` and `span_delayed_bug` follows the pattern used everywhere else: `span_err`, `span_warning`, etc.
2023-12-01Auto merge of #118482 - RalfJung:interpret-local-type, r=WaffleLapkinbors-0/+3
explain a good reason for why LocalValue does not store the type of the local As found out by `@lcnr` in https://github.com/rust-lang/rust/pull/112307, storing the type here can lead to subtle bugs when it gets out of sync with the MIR body. That's not the reason why the interpreter does it this way I think, but good thing we dodged that bullet. :)
2023-11-30explain a good reason for why LocalValue does not store the type of the localRalf Jung-0/+3
2023-11-28move write_aggregate into step file, and also extract write_repeat into ↵Ralf Jung-60/+72
separate function
2023-11-26rustc: `hir().local_def_id_to_hir_id()` -> `tcx.local_def_id_to_hir_id()` ↵Vadim Petrochenkov-1/+1
cleanup