Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

BorrwError when calling MirConst.eval_target_usize() #96

Open
zhuyutian57 opened this issue Mar 16, 2025 · 0 comments
Open

BorrwError when calling MirConst.eval_target_usize() #96

zhuyutian57 opened this issue Mar 16, 2025 · 0 comments

Comments

@zhuyutian57
Copy link

fn main() -> ExitCode {
  let cli = Cli::parse();
  match run!(cli.rustc_args(), || demo(cli)) {
    Ok(_) | Err(CompilerError::Skipped | CompilerError::Interrupted(_))
      => ExitCode::SUCCESS,
    _ => ExitCode::FAILURE,
  }
}

struct MyT;

impl MirVisitor for MyT {
  fn visit_mir_const(&mut self, constant: &ty::MirConst, location: mir::visit::Location) {
    println!("{:?}", constant.eval_target_usize());
  }
}

fn demo(cli: Cli) -> ControlFlow<()> {
  let mut v = MyT;
  
  v.visit_body(&stable_mir::entry_fn().unwrap().body());

  ControlFlow::Break(())
}

I try to evaluate a const value via the above api. But it fails to borrow the Context.

thread 'rustc' panicked at compiler/rustc_smir/src/rustc_smir/context.rs:238:29:
already mutably borrowed: BorrowError
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::cell::panic_already_mutably_borrowed
   3: <rustc_smir::rustc_smir::context::TablesWrapper as stable_mir::compiler_interface::Context>::def_name
   4: <stable_mir::crate_def::DefId as core::fmt::Debug>::fmt
   5: core::fmt::Formatter::debug_tuple_field1_finish
   6: <stable_mir::ty::ConstDef as core::fmt::Debug>::fmt
   7: core::fmt::builders::DebugStruct::field
   8: core::fmt::Formatter::debug_struct_field3_finish
   9: <&stable_mir::ty::UnevaluatedConst as core::fmt::Debug>::fmt
  10: core::fmt::Formatter::debug_tuple_field1_finish
  11: <stable_mir::ty::ConstantKind as core::fmt::Debug>::fmt
  12: core::fmt::builders::DebugStruct::field
  13: core::fmt::Formatter::debug_struct_field3_finish
  14: <&stable_mir::ty::MirConst as core::fmt::Debug>::fmt
  15: core::fmt::write
  16: alloc::fmt::format::format_inner
  17: <rustc_smir::rustc_smir::context::TablesWrapper as stable_mir::compiler_interface::Context>::eval_target_usize
  18: <stable_mir::ty::MirConst>::eval_target_usize
  19: <stable_mir_analysis::MyT as stable_mir::mir::visit::MirVisitor>::visit_mir_const
             at ./src/main.rs:47:22
  20: stable_mir::mir::visit::MirVisitor::super_const_operand
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:376:9
  21: stable_mir::mir::visit::MirVisitor::visit_const_operand
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:112:9
  22: stable_mir::mir::visit::MirVisitor::super_operand
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:359:17
  23: stable_mir::mir::visit::MirVisitor::visit_operand
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:99:9
  24: stable_mir::mir::visit::MirVisitor::super_rvalue
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:348:17
  25: stable_mir::mir::visit::MirVisitor::visit_rvalue
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:95:9
  26: stable_mir::mir::visit::MirVisitor::super_statement
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:195:17
  27: stable_mir::mir::visit::MirVisitor::visit_statement
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:64:9
  28: stable_mir::mir::visit::MirVisitor::super_basic_block
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:170:13
  29: stable_mir::mir::visit::MirVisitor::visit_basic_block
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:48:9
  30: stable_mir::mir::visit::MirVisitor::super_body
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:146:13
  31: stable_mir::mir::visit::MirVisitor::visit_body
             at /home/zhuyt/.rustup/toolchains/nightly-2025-03-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/compiler/stable_mir/src/mir/visit.rs:44:9
  32: stable_mir_analysis::demo
             at ./src/main.rs:54:3

Btw, is there any way to read the initial value of a global const variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant