Skip to content

Rustc crashes rather than failing to compile when wrong sized type is used #39974

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
mcdenhoed opened this issue Feb 20, 2017 · 3 comments · Fixed by #42217
Closed

Rustc crashes rather than failing to compile when wrong sized type is used #39974

mcdenhoed opened this issue Feb 20, 2017 · 3 comments · Fixed by #42217
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@mcdenhoed
Copy link

I was playing around with some code and found a condition that should result in a compilation error but results in a crash instead.

This code compiles normally:

const LENGTH: usize = 4;

struct Thing {
    f: [[f64; 2]; LENGTH],
}

fn main() {
    let t = Thing { f: [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]] };
}

If you change const LENGTH: usize = 4; to const LENGTH: u32 = 4; you get a compilation error, as expected.

However, if you change it to const LENGTH: f64 = 4; you get a crash.

Meta

rustc 1.15.1 (021bd294c 2017-02-08)
binary: rustc
commit-hash: 021bd294c039bd54aa5c4aa85bcdffb0d24bc892
commit-date: 2017-02-08
host: x86_64-pc-windows-msvc
release: 1.15.1
LLVM version: 3.9

Backtrace:

thread 'rustc' panicked at 'Box<Any>', C:\bot\slave\stable-dist-rustc-win-msvc-64\build\src\librustc_errors\lib.rs:423
stack backtrace:
   0:     0x7ffa5edfedea - std::panicking::Location::line::h77e1a07e4d6d891a
   1:     0x7ffa5edfe274 - std::panicking::Location::line::h77e1a07e4d6d891a
   2:     0x7ffa5ee01ccd - std::panicking::rust_panic_with_hook::h537b769c8e0e1c2d
   3:     0x7ffa50452a1a - <unknown>
   4:     0x7ffa5046d59e - rustc_errors::Handler::bug::had89a2eafd9d44ba
   5:     0x7ffa27149fc5 - rustc::session::bug_fmt::hc10f935d6ba6cd93
   6:     0x7ffa27149df2 - rustc::session::bug_fmt::hc10f935d6ba6cd93
   7:     0x7ffa2714950c - rustc::session::bug_fmt::hc10f935d6ba6cd93
   8:     0x7ffa4f69453b - rustc_const_eval::eval::eval_const_expr_partial::h8506b167c7a43499
   9:     0x7ffa4f68b0be - rustc_const_eval::eval::eval_const_expr_partial::h8506b167c7a43499
  10:     0x7ffa4f68e053 - rustc_const_eval::eval::eval_const_expr_partial::h8506b167c7a43499
  11:     0x7ffa4f695153 - rustc_const_eval::eval::eval_length::h4e0736539c568f4d
  12:     0x7ffa43472646 - <rustc_typeck::astconv::AstConv<'gcx, 'tcx> + 'o>::ast_ty_to_ty::h39a0321307d078df
  13:     0x7ffa434814f8 - <rustc::hir::Generics as rustc_typeck::collect::GetTypeParameterBounds<'tcx>>::get_type_parameter_bounds::h82c8b3f9d32e8939
  14:     0x7ffa4347875f - <rustc_typeck::collect::CollectItemTypesVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_item::h07d02b3adc3c96ed
  15:     0x7ffa43477ea5 - rustc_typeck::collect::collect_item_types::h287fdae2e3e8d768
  16:     0x7ffa434aa4d4 - rustc_typeck::check_crate::h3d799501ecb83cfd
  17:     0x7ffa5f0bdb4e - rustc_driver::driver::count_nodes::hd8d06c8fe5cb012d
  18:     0x7ffa5f02502f - <unknown>
  19:     0x7ffa5f0a8b52 - rustc_driver::driver::compile_input::h4d6139b5511c2803
  20:     0x7ffa5f0f94e7 - rustc_driver::run_compiler::hc652dda834d2f45c
  21:     0x7ffa5efddd6e - <unknown>
  22:     0x7ffa5ee04681 - _rust_maybe_catch_panic
  23:     0x7ffa5f006904 - <unknown>
  24:     0x7ffa5edfc3ae - std::sys::imp::thread::Thread::new::hcb2a817bb8ef6963
  25:     0x7ffa67e38363 - BaseThreadInitThunk
@oli-obk
Copy link
Contributor

oli-obk commented Feb 20, 2017

The HIR const evaluator can't evaluate this before typeck. It should probably report a better error though. Easy to fix, but will get fixed along the way with const eval improvements anyway.

@johnthagen
Copy link
Contributor

FWIW, confirmed on macOS rustc as well.

default host: x86_64-apple-darwin

installed toolchains
--------------------

stable-x86_64-apple-darwin (default)

active toolchain
----------------

stable-x86_64-apple-darwin (default)
rustc 1.15.1 (021bd294c 2017-02-08)

Output:

error: internal compiler error: /Users/rustbuild/src/rust-buildbot/slave/stable-dist-rustc-mac/build/src/librustc_const_eval/eval.rs:1258: bad ty_hint: TyFloat(f64), Int(4, Unsuffixed)

note: the compiler unexpectedly panicked. this is a bug.
...
thread 'rustc' panicked at 'Box<Any>', /Users/rustbuild/src/rust-buildbot/slave/stable-dist-rustc-mac/build/src/librustc_errors/lib.rs:423

@sfackler sfackler added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Feb 20, 2017
@Mark-Simulacrum
Copy link
Member

E-needstest.

rustc 1.19.0-dev (5b13bff52 2017-05-23)
error[E0308]: mismatched types
 --> test.rs:8:24
  |
8 |     let t = Thing { f: [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]] };
  |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an array with a fixed size of 2 elements, found one with 4 elements
  |
  = note: expected type `[[f64; 2]; 2]`
             found type `[[f64; 2]; 4]`

error: aborting due to previous error

@Mark-Simulacrum Mark-Simulacrum added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label May 23, 2017
venkatagiri added a commit to venkatagiri/rust that referenced this issue May 25, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue May 26, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue May 27, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue May 27, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue May 28, 2017
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue May 28, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants