From f449f71c556968e992958bfde469974fba90ebc0 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Fri, 23 Aug 2024 16:38:20 +0200 Subject: [PATCH] fix ICE when `asm_const` and `const_refs_to_static` are combined --- compiler/rustc_borrowck/src/type_check/mod.rs | 9 +++++++- tests/ui/asm/const-refs-to-static.rs | 21 ++++++++++++++++++ tests/ui/asm/const-refs-to-static.stderr | 22 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/ui/asm/const-refs-to-static.rs create mode 100644 tests/ui/asm/const-refs-to-static.stderr diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 6983cda6ddf3b..7a1dc5ebe0f04 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -385,7 +385,14 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> { } else if let Some(static_def_id) = constant.check_static_ptr(tcx) { let unnormalized_ty = tcx.type_of(static_def_id).instantiate_identity(); let normalized_ty = self.cx.normalize(unnormalized_ty, locations); - let literal_ty = constant.const_.ty().builtin_deref(true).unwrap(); + let literal_ty = constant.const_.ty().builtin_deref(true).unwrap_or_else(|| { + span_mirbug_and_err!( + self, + constant, + "bad static type {:?}", + constant.const_.ty() + ) + }); if let Err(terr) = self.cx.eq_types( literal_ty, diff --git a/tests/ui/asm/const-refs-to-static.rs b/tests/ui/asm/const-refs-to-static.rs new file mode 100644 index 0000000000000..9fc010b576309 --- /dev/null +++ b/tests/ui/asm/const-refs-to-static.rs @@ -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() {} diff --git a/tests/ui/asm/const-refs-to-static.stderr b/tests/ui/asm/const-refs-to-static.stderr new file mode 100644 index 0000000000000..8fd69da0d1e92 --- /dev/null +++ b/tests/ui/asm/const-refs-to-static.stderr @@ -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 +