rust/compiler/rustc_hir_analysis/src
bors f0999ffdc4 Auto merge of #139118 - scottmcm:slice-get-unchecked-intrinsic, r=workingjubilee
`slice.get(i)` should use a slice projection in MIR, like `slice[i]` does

`slice[i]` is built-in magic, so ends up being quite different from `slice.get(i)` in MIR, even though they're both doing nearly identical operations -- checking the length of the slice then getting a ref/ptr to the element if it's in-bounds.

This PR adds a `slice_get_unchecked` intrinsic for `impl SliceIndex for usize` to use to fix that, so it no longer needs to do a bunch of lines of pointer math and instead just gets the obvious single statement.  (This is *not* used for the range versions, since `slice[i..]` and `slice[..k]` can't use the mir Slice projection as they're using fenceposts, not indices.)

I originally tried to do this with some kind of GVN pattern, but realized that I'm pretty sure it's not legal to optimize `BinOp::Offset` to `PlaceElem::Index` without an extremely complicated condition.  Basically, the problem is that the `Index` projection on a dereferenced slice pointer *cares about the metadata*, since it's UB to `PlaceElem::Index` outside the range described by the metadata.  But then you cast the fat pointer to a thin pointer then offset it, that *ignores* the slice length metadata, so it's possible to write things that are legal with `Offset` but would be UB if translated in the obvious way to `Index`.  Checking (or even determining) the necessary conditions for that would be complicated and error-prone, whereas this intrinsic-based approach is quite straight-forward.

Zero backend changes, because it just lowers to MIR, so it's already supported naturally by CTFE/Miri/cg_llvm/cg_clif.
2025-05-31 21:38:21 +00:00
..
check Auto merge of #139118 - scottmcm:slice-get-unchecked-intrinsic, r=workingjubilee 2025-05-31 21:38:21 +00:00
coherence Some require_lang_item -> is_lang_item replacements 2025-05-13 08:46:10 +00:00
collect Reorder fields in hir::ItemKind variants. 2025-05-30 02:23:20 +10:00
errors Don't compute name of associated item if it's an RPITIT 2025-04-20 16:08:39 +00:00
hir_ty_lowering Reorder fields in hir::ItemKind variants. 2025-05-30 02:23:20 +10:00
impl_wf_check convert some GenericArg to Term 2025-04-26 02:05:31 +00:00
outlives Fix some var names 2025-05-27 11:14:47 +00:00
variance Fix some var names 2025-05-27 11:14:47 +00:00
autoderef.rs respect the tcx's recursion limit when peeling 2025-04-16 15:42:12 -07:00
check_unused.rs Move methods from Map to TyCtxt, part 4. 2025-03-12 08:55:37 +11:00
collect.rs Reorder fields in hir::ItemKind variants. 2025-05-30 02:23:20 +10:00
constrained_generic_params.rs Remove weak alias terminology 2025-04-24 11:59:20 +01:00
delegation.rs Stop relying on rustc_type_ir in non-type-system crates 2025-03-15 06:42:48 +00:00
errors.rs Fix typos 2025-05-12 17:20:49 +00:00
hir_wf_check.rs Reorder fields in hir::ItemKind variants. 2025-05-30 02:23:20 +10:00
impl_wf_check.rs Move name field from AssocItem to AssocKind variants. 2025-04-15 08:07:15 +10:00
lib.rs Always evaluate free lifetime-generic constants 2025-05-27 18:58:51 +02:00