rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.33k stars 12.58k forks source link

index out of range unexpected panic #27631

Closed tyoc213 closed 8 years ago

tyoc213 commented 9 years ago

I tried this code (my little follow up learning rust and ogl)

#[macro_use]
extern crate glium;
extern crate num;
extern crate image;
extern crate nalgebra;

use std::io::Cursor;

fn main() {
    use glium::{DisplayBuild, Surface};
    use nalgebra::*;
    use num::traits::Float;
    let display = glium::glutin::WindowBuilder::new().build_glium().unwrap();

    let image = image::load(Cursor::new(&include_bytes!("/Users/tyoc213/Desktop/lala.png")[..]),
                            image::PNG).unwrap();
    let texture = glium::texture::Texture2d::new(&display, image).unwrap();
    let image2 = image::load(Cursor::new(&include_bytes!("/Users/tyoc213/Desktop/lala2.png")[..]),
                            image::PNG).unwrap();
    let texture2 = glium::texture::Texture2d::new(&display, image2).unwrap();

    #[derive(Copy, Clone)]
    struct Vertex {
        position: [f32; 2],
        tex_coords: [f32; 2],
    }

    let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);

    let vertex_shader_src = r#"
        #version 140

        in vec2 position;
        in vec2 tex_coords;
        out vec2 v_tex_coords;
        out float some;

        uniform mat4 matrix;
        uniform mat4 trans;
        uniform mat4 vera;

        void main() {
            v_tex_coords = tex_coords;
            some = matrix[0][0];
            // gl_Position = matrix * vec4(position, 0.0, 1.0); // the other matrix
            gl_Position = vera * trans * vec4(position, 0.0, 1.0);
        }
    "#;

    let fragment_shader_src = r#"
        #version 140

        in float some;
        in vec2 v_tex_coords;
        out vec4 color;

        uniform sampler2D tex;
        uniform sampler2D tex2;

        void main() {
            if(v_tex_coords.y < 0.25){
                vec4 color1 = texture(tex, v_tex_coords) * vec4(1.0, 0, 0, 1.0);;
                vec4 color2 = texture(tex2, v_tex_coords); // * vec4(1.0, 0, 0, 1.0);;
                color = mix(color1, color2, 0.8+some);
            }else if(v_tex_coords.y < 0.5){
                vec4 color1 = texture(tex, v_tex_coords) * vec4(1.0, 0, 0, 1.0);;
                color = color1;
            }else if(v_tex_coords.y < 0.75){
                vec4 color2 = texture(tex2, v_tex_coords); // * vec4(1.0, 0, 0, 1.0);;
                color = color2;
            } else {
                color = texture(tex2, vec2(v_tex_coords.x + sin(v_tex_coords.y * 60.0 + some * 2.0) / 30.0, 1.0 - v_tex_coords.y)) * vec4(0.7, 0.7, 1.0, 1.0);
            }

        }
    "#;

    implement_vertex!(Vertex, position, tex_coords);

    let vertex1 = Vertex { position: [-0.5, -0.5], tex_coords: [0.0, 0.0] };
    let vertex2 = Vertex { position: [ 0.0,  0.5], tex_coords: [0.0, 1.0] };
    let vertex3 = Vertex { position: [ 0.5, -0.25], tex_coords: [1.0, 0.0] };
    let shape = vec![vertex1, vertex2, vertex3];

    let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();

    let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap();

    let mut t = -0.5;

    loop {
        // we update `t`
        t += 0.0002;
        if t > 0.5 {
            t = -0.5;
        }

        let mut target = display.draw();
        target.clear_color(0.0, 0.0, 1.0, 1.0);

        let mut thing: Mat4<f32> = nalgebra::one();
        thing.m11 = t.cos();
        thing.m12 = t.sin();
        thing.m21 = -t.sin();
        thing.m22 = t.cos();
        let mut trot = Rot3::new(Vec3::new(0.0f64, 0.0,  <f64 as BaseFloat>::pi()));
        println!("trot 1 {:?}", trot);
        trot.look_at(&Vec::new(1.2, 1.2, 1.2), &Vec3::new(0.0,0.0,1.0));
        println!("trot 2 {:?}", trot);
        let uniforms = uniform! {
            matrix: [
                [t.cos(), t.sin(), 0.0, 0.0], // [1.0, 0.0, 0.0, 0.0],
                [-t.sin(), t.cos(), 0.0, 0.0], // [0.0, 1.0, 0.0, 0.0],
                [0.0, 0.0, 1.0, 0.0],
                [ t , 0.0, 0.0, 1.0],
            ],
            tex: &texture,
            tex2: &texture2,
            trans: thing,
            view: trot,
        };

        target.draw(&vertex_buffer, &indices, &program, &uniforms,
                    &Default::default()).unwrap();
        target.finish().unwrap();

        for ev in display.poll_events() {
            match ev {
                glium::glutin::Event::Closed => return,
                _ => ()
            }
        }
    }
}
$ rustc --version --verbose
rustc 1.2.0 (082e47636 2015-08-03)
binary: rustc
commit-hash: 082e4763615bdbe7b4dd3dfd6fc2210b7773edf5
commit-date: 2015-08-03
host: x86_64-apple-darwin
release: 1.2.0

It breaks the compiler. I get this

rc/main.rs:110:23: 110:46 error: this function takes 0 parameters but 3 parameters were supplied [E0061]
src/main.rs:110         trot.look_at(&Vec::new(1.2, 1.2, 1.2), &Vec3::new(0.0,0.0,1.0));
                                      ^~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:110:23: 110:46 help: run `rustc --explain E0061` to see a detailed explanation
src/main.rs:110:22: 110:46 error: mismatched types:
 expected `&nalgebra::structs::vec::Vec3<f64>`,
    found `&collections::vec::Vec<_>`
(expected struct `nalgebra::structs::vec::Vec3`,
    found struct `collections::vec::Vec`) [E0308]
src/main.rs:110         trot.look_at(&Vec::new(1.2, 1.2, 1.2), &Vec3::new(0.0,0.0,1.0));
                                     ^~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:110:22: 110:46 help: run `rustc --explain E0308` to see a detailed explanation
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
thread 'rustc' panicked at 'index out of bounds: the len is 146 but the index is 147', ../src/libcollections/vec.rs:1362

stack backtrace:
   1:        0x10cc4c7b5 - sys::backtrace::write::hf5ea20500b66cd24uns
   2:        0x10cc55013 - panicking::on_panic::hbe02cb0d925cad49iGw
   3:        0x10cc10dd2 - rt::unwind::begin_unwind_inner::h12ba0ba9dffdecc2uow
   4:        0x10cc11b29 - rt::unwind::begin_unwind_fmt::hadf0dbf11d345ebfAnw
   5:        0x10cc54b9c - rust_begin_unwind
   6:        0x10cca8d95 - panicking::panic_fmt::h987a4890059dc6e0H8B
   7:        0x10cca3515 - panicking::panic_bounds_check::hf900a758401f2ca8N7B
   8:        0x10a0741ea - middle::infer::freshen::TypeFreshener<'a, 'tcx>.TypeFolder<'tcx>::fold_ty::hbd8e45b2c49f8e6fb5z
   9:        0x10a075759 - middle::ty_fold::TypeFolder::fold_substs::h10425087199912035611
  10:        0x109fdf669 - middle::traits::select::SelectionContext<'cx, 'tcx>::select::ha4cbca36867c39c7hkT
  11:        0x10a0e7a08 - middle::traits::fulfill::FulfillmentContext<'tcx>::select::haa02db2d5e8207e379Q
  12:        0x10a0e6cc1 - middle::traits::fulfill::FulfillmentContext<'tcx>::select_where_possible::h408c4cc97d8070edM8Q
  13:        0x109c4c021 - check::FnCtxt<'a, 'tcx>::select_obligations_where_possible::h26aca0e1e77478dd7Ep
  14:        0x109bdc4e1 - check::FnCtxt<'a, 'tcx>::resolve_type_vars_if_possible::h0780ac8e80e69b5a6Uo
  15:        0x109ba227e - check::structurally_resolved_type::h0dce595f8068585dSxt
  16:        0x109c27457 - check::callee::check_call::h256e0321ee60df0eL1l
  17:        0x109c7eb20 - check::check_expr_with_unifier::h1015587448309277605
  18:        0x109c568b6 - check::check_expr_with_unifier::h5311411268102988624
  19:        0x109c2c576 - check::check_argument_types::he95ba5663be186cb89p
  20:        0x109c2db94 - check::check_method_argument_types::h27c51671b6bc1cc6x7p
  21:        0x109c59d0d - check::check_expr_with_unifier::check_method_call::h99e4ecef0bfb9a66MJq
  22:        0x109c84163 - check::check_expr_with_unifier::h17134099806292258680
  23:        0x109c94d58 - check::check_stmt::h3b6a8003dad3fd00bhs
  24:        0x109c44a18 - check::check_block_with_expected::h6e216c80ce807e01mls
  25:        0x109c592a4 - check::check_block_no_value::ha5efaef0c62f142enks
  26:        0x109c6f006 - check::check_expr_with_unifier::h13136853694612127998
  27:        0x109c44f22 - check::check_block_with_expected::h6e216c80ce807e01mls
  28:        0x109c26c81 - check::check_fn::h8b46dfec97d603addXn
  29:        0x109c3efe0 - check::check_bare_fn::h88c035244660e365WMn
  30:        0x109c3cdc7 - check::check_item_body::h9873e3da412bca20ydo
  31:        0x109c3ebe2 - check::check_item_types::h63240bfbe991be87tKn
  32:        0x109cfccd9 - check_crate::h117ec0c1269afe619fD
  33:        0x109538d16 - driver::phase_3_run_analysis_passes::closure.15766
  34:        0x109537204 - middle::ty::with_ctxt::h14728011725879770170
  35:        0x10953200a - driver::phase_3_run_analysis_passes::h16713467199444562124
  36:        0x109515107 - driver::compile_input::hb6d2be5b0fa2247fTba
  37:        0x1095f113f - run_compiler::h21d74b88eec3fe3bx7b
  38:        0x1095ee9f3 - boxed::F.FnBox<A>::call_box::h1689969825914258414
  39:        0x1095ee1b7 - rt::unwind::try::try_fn::h11273853850686318048
  40:        0x10ccdfcc8 - rust_try_inner
  41:        0x10ccdfcb5 - rust_try
  42:        0x10cc3ec95 - rt::unwind::try::inner_try::h480e3107f6a4b5b9nkw
  43:        0x1095ee3e8 - boxed::F.FnBox<A>::call_box::h888215220722514405
  44:        0x10cc53a9d - sys::thread::Thread::new::thread_start::hdb3d925f69c5da4aHIv
  45:     0x7fff8fc74267 - _pthread_body
  46:     0x7fff8fc741e4 - _pthread_start

Could not compile `ex1`.
therustmonk commented 9 years ago

Look at this:

expected `&nalgebra::structs::vec::Vec3<f64>`,
    found `&collections::vec::Vec<_>`

You have a mistake this string:

trot.look_at(&Vec::new(1.2, 1.2, 1.2), &Vec3::new(0.0,0.0,1.0));

Use Vec3::new instead of Vec::new.

tyoc213 commented 9 years ago

That was the code like I have it in that moment but thanks (will love more examples of nalgebra thought), anyway I reported it because if you see the log

error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
...
thread 'rustc' panicked at 'index out of bounds: the len is 146 but the index is 147', ../src/libcollections/vec.rs:1362

So I wrote this report as I did have the code in that moment and put all the "extra info".

steveklabnik commented 8 years ago

Without the versions of all these extra dependencies, it's not clear we can reproduce this :(

As such, I'm going to give it a close. @tyoc213 if you have a way, please let me know!