Skip to content

Commit 87f3908

Browse files
Rollup merge of #137548 - compiler-errors:asm-ty, r=oli-obk
Pass correct `TypingEnv` to `InlineAsmCtxt` Fixes #137512 r? oli-obk
2 parents 74e5366 + b2dee42 commit 87f3908

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,19 @@ 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> {
3334
pub fn new(
3435
tcx: TyCtxt<'tcx>,
3536
def_id: LocalDefId,
37+
typing_env: ty::TypingEnv<'tcx>,
3638
get_operand_ty: impl Fn(&hir::Expr<'tcx>) -> Ty<'tcx> + 'a,
3739
) -> Self {
3840
InlineAsmCtxt {
3941
tcx,
40-
typing_env: ty::TypingEnv {
41-
typing_mode: ty::TypingMode::non_body_analysis(),
42-
param_env: ty::ParamEnv::empty(),
43-
},
42+
typing_env,
4443
target_features: tcx.asm_target_features(def_id),
4544
expr_ty: Box::new(get_operand_ty),
4645
}
@@ -83,7 +82,13 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
8382
ty::Float(FloatTy::F64) => Ok(InlineAsmType::F64),
8483
ty::Float(FloatTy::F128) => Ok(InlineAsmType::F128),
8584
ty::FnPtr(..) => Ok(asm_ty_isize),
86-
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+
}
8792
ty::Adt(adt, args) if adt.repr().simd() => {
8893
let fields = &adt.non_enum_variant().fields;
8994
let field = &fields[FieldIdx::ZERO];
@@ -189,6 +194,16 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
189194
can be used as arguments for inline assembly",
190195
).emit();
191196
}
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+
}
192207
NonAsmTypeReason::InvalidElement(did, ty) => {
193208
let msg = format!(
194209
"cannot use SIMD vector with element type `{ty}` for inline assembly"

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
110110
self.tcx.erase_regions(ty)
111111
}
112112
};
113-
InlineAsmCtxt::new(self.tcx, enclosing_id, expr_ty).check_asm(asm);
113+
InlineAsmCtxt::new(self.tcx, enclosing_id, self.typing_env(self.param_env), expr_ty)
114+
.check_asm(asm);
114115
}
115116
}
116117

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() {}
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+
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ check-pass
2+
//@ needs-asm-support
3+
4+
use std::arch::asm;
5+
6+
fn _f<T>(p: *mut T) {
7+
unsafe {
8+
asm!("/* {} */", in(reg) p);
9+
}
10+
}
11+
12+
fn main() {}

0 commit comments

Comments
 (0)