Better error message for unsized pointers
This commit is contained in:
parent
04c00585c3
commit
b2dee4226d
3 changed files with 55 additions and 1 deletions
|
@ -27,6 +27,7 @@ enum NonAsmTypeReason<'tcx> {
|
|||
UnevaluatedSIMDArrayLength(DefId, ty::Const<'tcx>),
|
||||
Invalid(Ty<'tcx>),
|
||||
InvalidElement(DefId, Ty<'tcx>),
|
||||
NotSizedPtr(Ty<'tcx>),
|
||||
}
|
||||
|
||||
impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
|
||||
|
@ -81,7 +82,13 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
|
|||
ty::Float(FloatTy::F64) => Ok(InlineAsmType::F64),
|
||||
ty::Float(FloatTy::F128) => Ok(InlineAsmType::F128),
|
||||
ty::FnPtr(..) => Ok(asm_ty_isize),
|
||||
ty::RawPtr(ty, _) if self.is_thin_ptr_ty(ty) => Ok(asm_ty_isize),
|
||||
ty::RawPtr(elem_ty, _) => {
|
||||
if self.is_thin_ptr_ty(elem_ty) {
|
||||
Ok(asm_ty_isize)
|
||||
} else {
|
||||
Err(NonAsmTypeReason::NotSizedPtr(ty))
|
||||
}
|
||||
}
|
||||
ty::Adt(adt, args) if adt.repr().simd() => {
|
||||
let fields = &adt.non_enum_variant().fields;
|
||||
let field = &fields[FieldIdx::ZERO];
|
||||
|
@ -187,6 +194,16 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
|
|||
can be used as arguments for inline assembly",
|
||||
).emit();
|
||||
}
|
||||
NonAsmTypeReason::NotSizedPtr(ty) => {
|
||||
let msg = format!(
|
||||
"cannot use value of unsized pointer type `{ty}` for inline assembly"
|
||||
);
|
||||
self.tcx
|
||||
.dcx()
|
||||
.struct_span_err(expr.span, msg)
|
||||
.with_note("only sized pointers can be used in inline assembly")
|
||||
.emit();
|
||||
}
|
||||
NonAsmTypeReason::InvalidElement(did, ty) => {
|
||||
let msg = format!(
|
||||
"cannot use SIMD vector with element type `{ty}` for inline assembly"
|
||||
|
|
19
tests/ui/asm/conditionally-sized-ptr-fail.rs
Normal file
19
tests/ui/asm/conditionally-sized-ptr-fail.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
//@ needs-asm-support
|
||||
|
||||
use std::arch::asm;
|
||||
|
||||
fn _f<T: ?Sized>(p: *mut T) {
|
||||
unsafe {
|
||||
asm!("/* {} */", in(reg) p);
|
||||
//~^ ERROR cannot use value of unsized pointer type `*mut T` for inline assembly
|
||||
}
|
||||
}
|
||||
|
||||
fn _g(p: *mut [u8]) {
|
||||
unsafe {
|
||||
asm!("/* {} */", in(reg) p);
|
||||
//~^ ERROR cannot use value of unsized pointer type `*mut [u8]` for inline assembly
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
18
tests/ui/asm/conditionally-sized-ptr-fail.stderr
Normal file
18
tests/ui/asm/conditionally-sized-ptr-fail.stderr
Normal file
|
@ -0,0 +1,18 @@
|
|||
error: cannot use value of unsized pointer type `*mut T` for inline assembly
|
||||
--> $DIR/conditionally-sized-ptr-fail.rs:7:34
|
||||
|
|
||||
LL | asm!("/* {} */", in(reg) p);
|
||||
| ^
|
||||
|
|
||||
= note: only sized pointers can be used in inline assembly
|
||||
|
||||
error: cannot use value of unsized pointer type `*mut [u8]` for inline assembly
|
||||
--> $DIR/conditionally-sized-ptr-fail.rs:14:34
|
||||
|
|
||||
LL | asm!("/* {} */", in(reg) p);
|
||||
| ^
|
||||
|
|
||||
= note: only sized pointers can be used in inline assembly
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue