Rust-GCC / Rust-GCC/gccrs

Rust makes all slice access an intrinsic

Open
#1,269 1 comment 0 reactions 1 assignee View on GitHub

@philberty is already working on this.

Since Dec 2, 2024.

bug
Dominant language
C++
Stars
2.9k
Forks
231
Avg merge
20h 2m
Merged PRs (30d)
66

Description

see: https://users.rust-lang.org/t/why-this-does-not-lead-to-recursion/50306/3

I tried this code:

unsafe impl<T> SliceIndex<[T]> for usize {
    type Output = T;

    fn get(self, slice: &[T]) -> Option<&T> {
        unsafe { Option::Some(&*self.get_unchecked(slice)) }
    }

    unsafe fn get_unchecked(self, slice: *const [T]) -> *const T {
        // SAFETY: the caller guarantees that `slice` is not dangling, so it
        // cannot be longer than `isize::MAX`. They also guarantee that
        // `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
        // so the call to `add` is safe.
        unsafe { slice.as_ptr().add(self) }
    }

    fn index(self, slice: &[T]) -> &T {    
        // It works if you change this to unsafe { &*self.get_unchecked(slice) }
        // N.B., use intrinsic indexing
        &(*slice)[self]        
    }
}

I expected to see this happen: explanation

Instead, this happened:

test.rs: In function ‘<T as test::Index>::index<i32, usize>’:
test.rs:140:9: internal compiler error: in expand_expr_real_1, at expr.cc:10583
  140 |         index.index(self)
      |         ^
0x1605b99 expand_expr_real_1(tree_node*, rtx_def*, machine_mode, expand_modifier, rtx_def**, bool)
        ../../gccrs/gcc/expr.cc:10583
0x15fd3e3 expand_expr_real(tree_node*, rtx_def*, machine_mode, expand_modifier, rtx_def**, bool)
        ../../gccrs/gcc/expr.cc:8736
0x140544f expand_normal
        ../../gccrs/gcc/expr.h:307
0x14079d0 precompute_register_parameters
        ../../gccrs/gcc/calls.cc:985
0x140f854 expand_call(tree_node*, rtx_def*, int)
        ../../gccrs/gcc/calls.cc:3375
0x160ab60 expand_expr_real_1(tree_node*, rtx_def*, machine_mode, expand_modifier, rtx_def**, bool)
        ../../gccrs/gcc/expr.cc:11537
0x15fd3e3 expand_expr_real(tree_node*, rtx_def*, machine_mode, expand_modifier, rtx_def**, bool)
        ../../gccrs/gcc/expr.cc:8736
0x15f2418 store_expr(tree_node*, rtx_def*, int, bool, bool)
        ../../gccrs/gcc/expr.cc:6087
0x15f0b6c expand_assignment(tree_node*, tree_node*, bool)
        ../../gccrs/gcc/expr.cc:5819
0x142e364 expand_call_stmt
        ../../gccrs/gcc/cfgexpand.cc:2829
0x1431fb8 expand_gimple_stmt_1
        ../../gccrs/gcc/cfgexpand.cc:3864
0x14325e6 expand_gimple_stmt
        ../../gccrs/gcc/cfgexpand.cc:4028
0x143ae02 expand_gimple_basic_block
        ../../gccrs/gcc/cfgexpand.cc:6069
0x143d424 execute
        ../../gccrs/gcc/cfgexpand.cc:6795
Meta
  • What version of Rust GCC were you using, git sha if possible. cd39861da5e1113207193bb8b3e6fb3dde92895f

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.