Skip to content

Commit

Permalink
fix ICE when asm_const and const_refs_to_static are combined
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Sep 2, 2024
1 parent b5723af commit 4f65da6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
15 changes: 9 additions & 6 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,12 +624,15 @@ impl<'tcx> Cx<'tcx> {
}
}
hir::InlineAsmOperand::Const { ref anon_const } => {
let value = mir::Const::identity_unevaluated(
tcx,
anon_const.def_id.to_def_id(),
)
.instantiate_identity()
.normalize(tcx, self.param_env);
let def_id = anon_const.def_id.to_def_id();
let const_type = tcx.type_of(def_id).skip_binder();
let value = if let ty::Error(terr) = const_type.kind() {
mir::Const::Ty(const_type, ty::Const::new_error(tcx, *terr))
} else {
mir::Const::identity_unevaluated(tcx, def_id)
.instantiate_identity()
.normalize(tcx, self.param_env)
};
let span = tcx.def_span(anon_const.def_id);

InlineAsmOperand::Const { value, span }
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/asm/const-refs-to-static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//@ needs-asm-support
//@ ignore-nvptx64
//@ ignore-spirv

#![feature(const_refs_to_static)]

use std::arch::{asm, global_asm};
use std::ptr::addr_of;

static FOO: u8 = 42;

global_asm!("{}", const addr_of!(FOO));
//~^ ERROR invalid type for `const` operand

#[no_mangle]
fn inline() {
unsafe { asm!("{}", const addr_of!(FOO)) };
//~^ ERROR invalid type for `const` operand
}

fn main() {}
22 changes: 22 additions & 0 deletions tests/ui/asm/const-refs-to-static.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: invalid type for `const` operand
--> $DIR/const-refs-to-static.rs:12:19
|
LL | global_asm!("{}", const addr_of!(FOO));
| ^^^^^^-------------
| |
| is a `*const u8`
|
= help: `const` operands must be of an integer type

error: invalid type for `const` operand
--> $DIR/const-refs-to-static.rs:17:25
|
LL | unsafe { asm!("{}", const addr_of!(FOO)) };
| ^^^^^^-------------
| |
| is a `*const u8`
|
= help: `const` operands must be of an integer type

error: aborting due to 2 previous errors

0 comments on commit 4f65da6

Please sign in to comment.