Skip to content

Commit 4f65da6

Browse files
committed
fix ICE when asm_const and const_refs_to_static are combined
1 parent b5723af commit 4f65da6

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

compiler/rustc_mir_build/src/thir/cx/expr.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -624,12 +624,15 @@ impl<'tcx> Cx<'tcx> {
624624
}
625625
}
626626
hir::InlineAsmOperand::Const { ref anon_const } => {
627-
let value = mir::Const::identity_unevaluated(
628-
tcx,
629-
anon_const.def_id.to_def_id(),
630-
)
631-
.instantiate_identity()
632-
.normalize(tcx, self.param_env);
627+
let def_id = anon_const.def_id.to_def_id();
628+
let const_type = tcx.type_of(def_id).skip_binder();
629+
let value = if let ty::Error(terr) = const_type.kind() {
630+
mir::Const::Ty(const_type, ty::Const::new_error(tcx, *terr))
631+
} else {
632+
mir::Const::identity_unevaluated(tcx, def_id)
633+
.instantiate_identity()
634+
.normalize(tcx, self.param_env)
635+
};
633636
let span = tcx.def_span(anon_const.def_id);
634637

635638
InlineAsmOperand::Const { value, span }

tests/ui/asm/const-refs-to-static.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ needs-asm-support
2+
//@ ignore-nvptx64
3+
//@ ignore-spirv
4+
5+
#![feature(const_refs_to_static)]
6+
7+
use std::arch::{asm, global_asm};
8+
use std::ptr::addr_of;
9+
10+
static FOO: u8 = 42;
11+
12+
global_asm!("{}", const addr_of!(FOO));
13+
//~^ ERROR invalid type for `const` operand
14+
15+
#[no_mangle]
16+
fn inline() {
17+
unsafe { asm!("{}", const addr_of!(FOO)) };
18+
//~^ ERROR invalid type for `const` operand
19+
}
20+
21+
fn main() {}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: invalid type for `const` operand
2+
--> $DIR/const-refs-to-static.rs:12:19
3+
|
4+
LL | global_asm!("{}", const addr_of!(FOO));
5+
| ^^^^^^-------------
6+
| |
7+
| is a `*const u8`
8+
|
9+
= help: `const` operands must be of an integer type
10+
11+
error: invalid type for `const` operand
12+
--> $DIR/const-refs-to-static.rs:17:25
13+
|
14+
LL | unsafe { asm!("{}", const addr_of!(FOO)) };
15+
| ^^^^^^-------------
16+
| |
17+
| is a `*const u8`
18+
|
19+
= help: `const` operands must be of an integer type
20+
21+
error: aborting due to 2 previous errors
22+

0 commit comments

Comments
 (0)