Skip to content

Commit b2dee42

Browse files
Better error message for unsized pointers
1 parent 04c0058 commit b2dee42

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

Diff for: compiler/rustc_hir_analysis/src/check/intrinsicck.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ enum NonAsmTypeReason<'tcx> {
2727
UnevaluatedSIMDArrayLength(DefId, ty::Const<'tcx>),
2828
Invalid(Ty<'tcx>),
2929
InvalidElement(DefId, Ty<'tcx>),
30+
NotSizedPtr(Ty<'tcx>),
3031
}
3132

3233
impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
@@ -81,7 +82,13 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
8182
ty::Float(FloatTy::F64) => Ok(InlineAsmType::F64),
8283
ty::Float(FloatTy::F128) => Ok(InlineAsmType::F128),
8384
ty::FnPtr(..) => Ok(asm_ty_isize),
84-
ty::RawPtr(ty, _) if self.is_thin_ptr_ty(ty) => Ok(asm_ty_isize),
85+
ty::RawPtr(elem_ty, _) => {
86+
if self.is_thin_ptr_ty(elem_ty) {
87+
Ok(asm_ty_isize)
88+
} else {
89+
Err(NonAsmTypeReason::NotSizedPtr(ty))
90+
}
91+
}
8592
ty::Adt(adt, args) if adt.repr().simd() => {
8693
let fields = &adt.non_enum_variant().fields;
8794
let field = &fields[FieldIdx::ZERO];
@@ -187,6 +194,16 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
187194
can be used as arguments for inline assembly",
188195
).emit();
189196
}
197+
NonAsmTypeReason::NotSizedPtr(ty) => {
198+
let msg = format!(
199+
"cannot use value of unsized pointer type `{ty}` for inline assembly"
200+
);
201+
self.tcx
202+
.dcx()
203+
.struct_span_err(expr.span, msg)
204+
.with_note("only sized pointers can be used in inline assembly")
205+
.emit();
206+
}
190207
NonAsmTypeReason::InvalidElement(did, ty) => {
191208
let msg = format!(
192209
"cannot use SIMD vector with element type `{ty}` for inline assembly"

Diff for: tests/ui/asm/conditionally-sized-ptr-fail.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ needs-asm-support
2+
3+
use std::arch::asm;
4+
5+
fn _f<T: ?Sized>(p: *mut T) {
6+
unsafe {
7+
asm!("/* {} */", in(reg) p);
8+
//~^ ERROR cannot use value of unsized pointer type `*mut T` for inline assembly
9+
}
10+
}
11+
12+
fn _g(p: *mut [u8]) {
13+
unsafe {
14+
asm!("/* {} */", in(reg) p);
15+
//~^ ERROR cannot use value of unsized pointer type `*mut [u8]` for inline assembly
16+
}
17+
}
18+
19+
fn main() {}

Diff for: tests/ui/asm/conditionally-sized-ptr-fail.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: cannot use value of unsized pointer type `*mut T` for inline assembly
2+
--> $DIR/conditionally-sized-ptr-fail.rs:7:34
3+
|
4+
LL | asm!("/* {} */", in(reg) p);
5+
| ^
6+
|
7+
= note: only sized pointers can be used in inline assembly
8+
9+
error: cannot use value of unsized pointer type `*mut [u8]` for inline assembly
10+
--> $DIR/conditionally-sized-ptr-fail.rs:14:34
11+
|
12+
LL | asm!("/* {} */", in(reg) p);
13+
| ^
14+
|
15+
= note: only sized pointers can be used in inline assembly
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)