Skip to content

MIR validation error at mir-opt-level>=3 #117355

Closed

Description

@cbeuw
#![feature(custom_mir, core_intrinsics)]
#![allow(unused_parens, unused_assignments, overflowing_literals)]
extern crate core;
use core::intrinsics::mir::*;

#[custom_mir(dialect = "runtime", phase = "initial")]
pub fn fn12(
    mut _6: *mut bool,
    mut _9: *mut bool,
) -> char {
    mir! {
    let _19: i8;
    {
    Goto(bb1)
    }
    bb1 = {
    Call((*_9) = fn13(_6), bb2)
    }
    bb2 = {
    _19 = 115_i8;
    match _19 {
    1 => bb3,
    _ => bb5
    }
    }
    bb3 = {
    Call((*_9) = fn13(_6), bb2)
    }
    bb5 = {
    Return()
    }

    }
}
#[custom_mir(dialect = "runtime", phase = "initial")]
fn fn13(mut _1: *mut bool) -> bool {
    mir! {
    {
    RET = true;
    Return()
    }
    }
}
pub fn main() {}

This compiles fine with rustc -Zmir-opt-level=2 -Copt-level=1 repro.rs, but

% rustc -Zmir-opt-level=3 -Copt-level=1 repro.rs
warning: the feature `custom_mir` is internal to the compiler or standard library
 --> repro.rs:1:12
  |
1 | #![feature(custom_mir, core_intrinsics)]
  |            ^^^^^^^^^^
  |
  = note: using it is strongly discouraged
  = note: `#[warn(internal_features)]` on by default

warning: 1 warning emitted

error: internal compiler error: no errors encountered even though `delay_span_bug` issued

error: internal compiler error: broken MIR in Item(DefId(0:5 ~ repro[f81f]::fn12)) (after phase change to runtime-optimized) at bb0[1]:
                                use of local _4, which has no storage here
  --> repro.rs:27:18
   |
27 |     Call((*_9) = fn13(_6), bb2)
   |                  ^^^^^^^^
   |
note: delayed at compiler/rustc_const_eval/src/transform/validate.rs:310:22
         0: std::backtrace::Backtrace::create
         1: <rustc_errors::HandlerInner>::emit_diagnostic
         2: <rustc_errors::Handler>::delay_span_bug::<rustc_span::span_encoding::Span, alloc::string::String>
         3: <rustc_const_eval::transform::validate::CfgChecker>::fail::<alloc::string::String>
         4: <rustc_const_eval::transform::validate::CfgChecker as rustc_middle::mir::visit::Visitor>::visit_local
         5: <rustc_const_eval::transform::validate::CfgChecker as rustc_middle::mir::visit::Visitor>::visit_statement
         6: <rustc_const_eval::transform::validate::Validator as rustc_middle::mir::MirPass>::run_pass
         7: rustc_mir_transform::pass_manager::run_passes_inner
         8: rustc_mir_transform::optimized_mir
         9: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
        10: <rustc_query_impl::query_impl::optimized_mir::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::DefId)>>::call_once
        11: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::DefId, rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        12: rustc_query_impl::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
        13: rustc_middle::query::plumbing::query_get_at::<rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::DefId, rustc_middle::query::erase::Erased<[u8; 8]>>>
        14: rustc_mir_transform::cross_crate_inline::cross_crate_inlinable
        15: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::cross_crate_inlinable::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 1]>>
        16: <rustc_query_impl::query_impl::cross_crate_inlinable::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::DefId)>>::call_once
        17: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::DefId, rustc_middle::query::erase::Erased<[u8; 1]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        18: rustc_query_impl::query_impl::cross_crate_inlinable::get_query_non_incr::__rust_end_short_backtrace
        19: rustc_passes::reachable::item_might_be_inlined
        20: rustc_passes::reachable::reachable_set
        21: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::reachable_set::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
        22: <rustc_query_impl::query_impl::reachable_set::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, ())>>::call_once
        23: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        24: rustc_query_impl::query_impl::reachable_set::get_query_non_incr::__rust_end_short_backtrace
        25: rustc_codegen_ssa::back::symbol_export::reachable_non_generics_provider
        26: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::reachable_non_generics::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
        27: <rustc_query_impl::query_impl::reachable_non_generics::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::CrateNum)>>::call_once
        28: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::VecCache<rustc_span::def_id::CrateNum, rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        29: rustc_query_impl::query_impl::reachable_non_generics::get_query_non_incr::__rust_end_short_backtrace
        30: rustc_codegen_ssa::back::symbol_export::is_reachable_non_generic_provider_local
        31: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::is_reachable_non_generic::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 1]>>
        32: <rustc_query_impl::query_impl::is_reachable_non_generic::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::DefId)>>::call_once
        33: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::DefId, rustc_middle::query::erase::Erased<[u8; 1]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        34: rustc_query_impl::query_impl::is_reachable_non_generic::get_query_non_incr::__rust_end_short_backtrace
        35: <rustc_monomorphize::collector::RootCollector>::push_if_root
        36: rustc_monomorphize::collector::collect_roots
        37: rustc_monomorphize::collector::collect_crate_mono_items
        38: rustc_monomorphize::partitioning::collect_and_partition_mono_items
        39: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 24]>>
        40: <rustc_query_impl::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, ())>>::call_once
        41: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 24]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        42: rustc_query_impl::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace
        43: rustc_codegen_ssa::base::codegen_crate::<rustc_codegen_llvm::LlvmCodegenBackend>
        44: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
        45: <rustc_session::session::Session>::time::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
        46: rustc_interface::passes::start_codegen
        47: <rustc_middle::ty::context::GlobalCtxt>::enter::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_span::ErrorGuaranteed>>
        48: <rustc_interface::queries::Queries>::ongoing_codegen
        49: rustc_span::set_source_map::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
        50: std::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface::util::run_in_thread_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>
        51: <<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
        52: std::sys::unix::thread::Thread::new::thread_start
        53: __pthread_joiner_wake
  --> repro.rs:27:18
   |
27 |     Call((*_9) = fn13(_6), bb2)
   |                  ^^^^^^^^

error: internal compiler error: broken MIR in Item(DefId(0:5 ~ repro[f81f]::fn12)) (after phase change to runtime-optimized) at bb0[1]:
                                use of local _5, which has no storage here
  --> repro.rs:27:18
   |
27 |     Call((*_9) = fn13(_6), bb2)
   |                  ^^^^^^^^
   |
note: delayed at compiler/rustc_const_eval/src/transform/validate.rs:310:22
         0: std::backtrace::Backtrace::create
         1: <rustc_errors::HandlerInner>::emit_diagnostic
         2: <rustc_errors::Handler>::delay_span_bug::<rustc_span::span_encoding::Span, alloc::string::String>
         3: <rustc_const_eval::transform::validate::CfgChecker>::fail::<alloc::string::String>
         4: <rustc_const_eval::transform::validate::CfgChecker as rustc_middle::mir::visit::Visitor>::visit_local
         5: <rustc_const_eval::transform::validate::CfgChecker as rustc_middle::mir::visit::Visitor>::visit_place
         6: <rustc_const_eval::transform::validate::CfgChecker as rustc_middle::mir::visit::Visitor>::visit_statement
         7: <rustc_const_eval::transform::validate::Validator as rustc_middle::mir::MirPass>::run_pass
         8: rustc_mir_transform::pass_manager::run_passes_inner
         9: rustc_mir_transform::optimized_mir
        10: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
        11: <rustc_query_impl::query_impl::optimized_mir::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::DefId)>>::call_once
        12: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::DefId, rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        13: rustc_query_impl::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
        14: rustc_middle::query::plumbing::query_get_at::<rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::DefId, rustc_middle::query::erase::Erased<[u8; 8]>>>
        15: rustc_mir_transform::cross_crate_inline::cross_crate_inlinable
        16: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::cross_crate_inlinable::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 1]>>
        17: <rustc_query_impl::query_impl::cross_crate_inlinable::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::DefId)>>::call_once
        18: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::DefId, rustc_middle::query::erase::Erased<[u8; 1]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        19: rustc_query_impl::query_impl::cross_crate_inlinable::get_query_non_incr::__rust_end_short_backtrace
        20: rustc_passes::reachable::item_might_be_inlined
        21: rustc_passes::reachable::reachable_set
        22: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::reachable_set::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
        23: <rustc_query_impl::query_impl::reachable_set::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, ())>>::call_once
        24: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        25: rustc_query_impl::query_impl::reachable_set::get_query_non_incr::__rust_end_short_backtrace
        26: rustc_codegen_ssa::back::symbol_export::reachable_non_generics_provider
        27: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::reachable_non_generics::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
        28: <rustc_query_impl::query_impl::reachable_non_generics::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::CrateNum)>>::call_once
        29: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::VecCache<rustc_span::def_id::CrateNum, rustc_middle::query::erase::Erased<[u8; 8]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        30: rustc_query_impl::query_impl::reachable_non_generics::get_query_non_incr::__rust_end_short_backtrace
        31: rustc_codegen_ssa::back::symbol_export::is_reachable_non_generic_provider_local
        32: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::is_reachable_non_generic::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 1]>>
        33: <rustc_query_impl::query_impl::is_reachable_non_generic::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::DefId)>>::call_once
        34: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::DefaultCache<rustc_span::def_id::DefId, rustc_middle::query::erase::Erased<[u8; 1]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        35: rustc_query_impl::query_impl::is_reachable_non_generic::get_query_non_incr::__rust_end_short_backtrace
        36: <rustc_monomorphize::collector::RootCollector>::push_if_root
        37: rustc_monomorphize::collector::collect_roots
        38: rustc_monomorphize::collector::collect_crate_mono_items
        39: rustc_monomorphize::partitioning::collect_and_partition_mono_items
        40: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 24]>>
        41: <rustc_query_impl::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, ())>>::call_once
        42: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 24]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, false>
        43: rustc_query_impl::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace
        44: rustc_codegen_ssa::base::codegen_crate::<rustc_codegen_llvm::LlvmCodegenBackend>
        45: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
        46: <rustc_session::session::Session>::time::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
        47: rustc_interface::passes::start_codegen
        48: <rustc_middle::ty::context::GlobalCtxt>::enter::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_span::ErrorGuaranteed>>
        49: <rustc_interface::queries::Queries>::ongoing_codegen
        50: rustc_span::set_source_map::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
        51: std::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface::util::run_in_thread_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>
        52: <<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_span::ErrorGuaranteed>>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
        53: std::sys::unix::thread::Thread::new::thread_start
        54: __pthread_joiner_wake
  --> repro.rs:27:18
   |
27 |     Call((*_9) = fn13(_6), bb2)
   |                  ^^^^^^^^

note: using internal features is not supported and expected to cause internal compiler errors when used incorrectly

note: please attach the file at `/Users/andy/Code/rustlantis/rustc-ice-2023-10-29T11_56_45-53492.txt` to your bug report

note: compiler flags: -Z mir-opt-level=3 -C opt-level=1

query stack during panic:
end of query stack
% rustc -v --version
rustc 1.75.0-nightly (e5cfc5547 2023-10-28)
binary: rustc
commit-hash: e5cfc55477eceed1317a02189fdf77a4a98f2124
commit-date: 2023-10-28
host: aarch64-apple-darwin
release: 1.75.0-nightly
LLVM version: 17.0.3

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 29, 2023
  2. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    C-bugCategory: This is a bug.
    A-mir-optArea: MIR optimizations
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 29, 2023
  3. saethlin commented on Oct 29, 2023

    @saethlin
    Member

    The minimal set of flags to trip this is:

    -Copt-level=1 -Zmir-enable-passes=+ConstProp -Zinline-mir
    

    I think you need -Copt-level=1 because if I add -Ztreat-err-as-bug we can see this is hit from cross_crate_inlinable:

    #0 [optimized_mir] optimizing MIR for `fn12`
    #1 [cross_crate_inlinable] whether the item should be made inlinable across crates
    #2 [reachable_set] reachability
    #3 [reachable_non_generics] looking up the exported symbols of a crate
    #4 [is_reachable_non_generic] checking whether `fn12` is an exported symbol
    #5 [collect_and_partition_mono_items] collect_and_partition_mono_items
    
  4. added 2 commits that reference this issue on Oct 29, 2023
  5. self-assigned this
    on Nov 9, 2023
  6. added a commit that references this issue on Nov 10, 2023
    d2ed699
  7. removed their assignment
    on Nov 10, 2023
  8. added 2 commits that reference this issue on Nov 14, 2023
    af26da3
    1aa6312
  9. added a commit that references this issue on Nov 19, 2023
    1acecda
  10. added a commit that references this issue on Nov 20, 2023
    db9e655
  11. added a commit that references this issue on Nov 20, 2023
    1be1c2e
  12. added a commit that references this issue on Nov 20, 2023
    79e961f
  13. added
    S-found-by-fuzzingStatus: This issue was found using fuzzing. It may not arise in practical cases.
    on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsA-mir-opt-inliningArea: MIR inliningA-rustlantisArea: A miscompilation found by RustlantisC-bugCategory: This is a bug.S-found-by-fuzzingStatus: This issue was found using fuzzing. It may not arise in practical cases.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions