rust-lang / rust

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

ICE: Broken MIR in NodeId when inferring SmallVec type #45011

Closed c0gent closed 1 year ago

c0gent commented 7 years ago

Apologies for the non-simplified example but basically asking the compiler to infer the type of the contents of the SmallVec (formats) causes an ICE.

    pub fn formats(&self, surface: &Surface) -> SmallVec<[::SurfaceFormatKhr; 64]> {
        let mut format_count = 0u32;
        let mut formats = SmallVec::new();
        unsafe {
            self.instance.proc_addr_loader().khr_surface.vkGetPhysicalDeviceSurfaceFormatsKHR(self.handle(),
                surface.handle(), &mut format_count, ptr::null_mut());
            assert!(format_count as usize <= formats.inline_size());
            formats.set_len(format_count as usize);
            if format_count != 0 {
                self.instance.proc_addr_loader().khr_surface.vkGetPhysicalDeviceSurfaceFormatsKHR(self.handle(),
                    surface.handle(), &mut format_count, formats.as_mut_ptr() as *mut vks::VkSurfaceFormatKHR);
            }
        }
        formats
    }

Annotating the type (e.g. let mut formats: SmallVec<[::SurfaceFormatKhr; 64]> = SmallVec::new();) avoids the ICE. Using a Vec likewise averts an ICE.

Full Error:

error: internal compiler error: broken MIR in NodeId(1813) (Terminator { source_info: SourceInfo { span: src\physical_device.rs:86:58: 86:65, scope: scope6 }, kind: _51 = const bitflags_::<unnamed>::ops::DerefMut::deref_mut(_52) -> [return: bb18, unwind: bb4] }): call dest mismatch (&mut [vks_::khr_surface::VkSurfaceFormatKHR] <- &mut [_]): Sorts(ExpectedFound { expected: vks_::khr_surface::VkSurfaceFormatKHR, found: structs::SurfaceFormatKhr })
  --> src\physical_device.rs:86:58
   |
86 |                     surface.handle(), &mut format_count, formats.as_mut_ptr() as *mut vks::VkSurfaceFormatKHR);
   |                                                          ^^^^^^^

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

note: rustc 1.22.0-nightly (185cc5f26 2017-10-02) running on x86_64-pc-windows-msvc

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'Box<Any>', src\librustc_errors\lib.rs:439:8
stack backtrace:
   0: _rdl_shrink_in_place
   1: std::panicking::Location::column
   2: std::panicking::Location::column
   3: std::panicking::rust_panic_with_hook
   4: <unknown>
   5: <unknown>
   6: <rustc_mir::transform::type_check::TypeVerifier<'a, 'b, 'gcx, 'tcx> as rustc::mir::visit::Visitor<'tcx>>::visit_mir
   7: <rustc_mir::transform::type_check::TypeckMir as rustc::mir::transform::MirPass>::run_pass
   8: <rustc_mir::transform::mir_keys::GatherCtors<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_variant_data
   9: <rustc_mir::transform::mir_keys::GatherCtors<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_variant_data
  10: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_const_qualif<'tcx>>::force
  11: rustc::dep_graph::graph::DepGraph::in_ignore
  12: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_const<'tcx>>::try_get
  13: rustc::ty::maps::TyCtxtAt::mir_const
  14: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::mir_const
  15: <rustc_mir::transform::check_unsafety::UnusedUnsafeVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_block
  16: rustc::dep_graph::graph::DepGraph::in_ignore
  17: rustc::ty::maps::<impl rustc::ty::maps::queries::unsafety_violations<'tcx>>::force
  18: <rustc_mir::transform::mir_keys::GatherCtors<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_variant_data
  19: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_const<'tcx>>::force
  20: rustc::dep_graph::graph::DepGraph::in_ignore
  21: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_validated<'tcx>>::try_get
  22: rustc::ty::maps::TyCtxtAt::mir_validated
  23: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::mir_validated
  24: rustc_borrowck::borrowck::provide
  25: rustc::ty::maps::<impl rustc::ty::maps::queries::coherent_trait<'tcx>>::force
  26: rustc::dep_graph::graph::DepGraph::in_ignore
  27: rustc::ty::maps::<impl rustc::ty::maps::queries::borrowck<'tcx>>::try_get
  28: rustc::ty::maps::TyCtxtAt::borrowck
  29: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::borrowck
  30: rustc_borrowck::borrowck::check_crate
  31: <rustc_driver::derive_registrar::Finder as rustc::hir::itemlikevisit::ItemLikeVisitor<'v>>::visit_trait_item
  32: rustc_driver::driver::compile_input
  33: rustc_driver::run_compiler
  34: <unknown>
  35: _rust_maybe_catch_panic
  36: <rustc_driver::derive_registrar::Finder as rustc::hir::itemlikevisit::ItemLikeVisitor<'v>>::visit_trait_item
  37: std::sys::imp::thread::Thread::new
  38: BaseThreadInitThunk

error: Could not compile `voodoo`.

rustc version

rustc 1.22.0-nightly (185cc5f26 2017-10-02)
binary: rustc
commit-hash: 185cc5f26d2c8a794189b028b43f6a3b8fc586db
commit-date: 2017-10-02
host: x86_64-pc-windows-msvc
release: 1.22.0-nightly
LLVM version: 4.0
estebank commented 7 years ago

@c0gent could you provide a code snippet or project to reproduce this? Could you check wether this ICE is present in stable or beta?

c0gent commented 7 years ago

Interestingly the ICE is present in stable but not beta. Below are the details of error from stable.

I'm having trouble reproducing using simplified snippets. There may be an important key buried within the function I am calling. It and functions like it (which also produce an ICE when they are used in the same way) are designed in a complicated way using macros (vkGetPhysicalDeviceSurfaceFormatsKHR). That may or may not be an important detail. I'll see if I can spend some time getting to the bottom of it tomorrow.

EDIT: The full project source can be found here on the smallvec_ice branch: https://github.com/cogciprocate/voodoo/tree/smallvec_ice

Stable

rustc

rustc 1.20.0 (f3d6973f4 2017-08-27)
binary: rustc
commit-hash: f3d6973f41a7d1fb83029c9c0ceaf0f5d4fd7208
commit-date: 2017-08-27
host: x86_64-pc-windows-msvc
release: 1.20.0
LLVM version: 4.0

output

   Compiling voodoo v0.0.1 (file:///C:/src/voodoo)
error: internal compiler error: broken MIR (Terminator { source_info: SourceInfo { span: src\physical_device.rs:86:58: 86:65, scope: scope3 }, kind: _51 = const std::ops::DerefMut::deref_mut(_52) -> [return: bb29, unwind: bb30] }): call dest mismatch (&mut [vks_::khr_surface::VkSurfaceFormatKHR] <- &mut [_]): Sorts(ExpectedFound { expected: vks_::khr_surface::VkSurfaceFormatKHR, found: structs::SurfaceFormatKhr })
  --> src\physical_device.rs:86:58
   |
86 |                     surface.handle(), &mut format_count, formats.as_mut_ptr() as *mut vks::VkSurfaceFormatKHR);
   |                                                          ^^^^^^^

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

note: rustc 1.20.0 (f3d6973f4 2017-08-27) running on x86_64-pc-windows-msvc

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'Box<Any>', src\librustc_errors\lib.rs:437:8
stack backtrace:
   0: _rdl_grow_in_place
   1: std::panicking::Location::column
   2: std::panicking::Location::column
   3: std::panicking::rust_panic_with_hook
   4: <unknown>
   5: <unknown>
   6: <rustc_mir::transform::type_check::TypeVerifier<'a, 'b, 'gcx, 'tcx> as rustc::mir::visit::Visitor<'tcx>>::visit_mir
   7: <rustc_mir::transform::type_check::TypeckMir as rustc::mir::transform::MirPass>::run_pass
   8: <rustc_mir::transform::mir_keys::GatherCtors<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_variant_data
   9: <rustc_mir::transform::mir_keys::GatherCtors<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_variant_data
  10: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_const_qualif<'tcx>>::force
  11: rustc::dep_graph::graph::DepGraph::in_task
  12: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_const<'tcx>>::try_get
  13: rustc::ty::maps::TyCtxtAt::mir_const
  14: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::mir_const
  15: <rustc_mir::transform::mir_keys::GatherCtors<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_variant_data
  16: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_const<'tcx>>::force
  17: rustc::dep_graph::graph::DepGraph::in_task
  18: rustc::ty::maps::<impl rustc::ty::maps::queries::mir_validated<'tcx>>::try_get
  19: rustc::ty::maps::TyCtxtAt::mir_validated
  20: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::mir_validated
  21: rustc_borrowck::borrowck::provide
  22: rustc::dep_graph::graph::DepGraph::in_task
  23: rustc::ty::maps::<impl rustc::ty::maps::queries::borrowck<'tcx>>::try_get
  24: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::borrowck
  25: rustc_borrowck::borrowck::check_crate
  26: rustc_driver::driver::count_nodes
  27: rustc_driver::driver::count_nodes
  28: rustc_driver::driver::compile_input
  29: rustc_driver::run_compiler
  30: <unknown>
  31: _rust_maybe_catch_panic
  32: <rustc_driver::derive_registrar::Finder as rustc::hir::itemlikevisit::ItemLikeVisitor<'v>>::visit_trait_item
  33: std::sys::imp::thread::Thread::new
  34: BaseThreadInitThunk

error: Could not compile `voodoo`.

Beta

Beta (1.21.0-beta.3 (ea1fd7d42 2017-09-15)) compiles and runs fine.

CraftSpider commented 1 year ago

I attempted to reproduce/minimize this issue - I couldn't get the linked branch working with Rust 1.20 to confirm the issue reproduces locally, but I could get it building with modern stable/nightly, and they do not appear to reproduce the issue. I believe this was likely fixed incidentally by some change or another.