Skip to content

Pass correct TypingEnv to InlineAsmCtxt #137548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions compiler/rustc_hir_analysis/src/check/intrinsicck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ enum NonAsmTypeReason<'tcx> {
UnevaluatedSIMDArrayLength(DefId, ty::Const<'tcx>),
Invalid(Ty<'tcx>),
InvalidElement(DefId, Ty<'tcx>),
NotSizedPtr(Ty<'tcx>),
}

impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
pub fn new(
tcx: TyCtxt<'tcx>,
def_id: LocalDefId,
typing_env: ty::TypingEnv<'tcx>,
get_operand_ty: impl Fn(&hir::Expr<'tcx>) -> Ty<'tcx> + 'a,
) -> Self {
InlineAsmCtxt {
tcx,
typing_env: ty::TypingEnv {
typing_mode: ty::TypingMode::non_body_analysis(),
param_env: ty::ParamEnv::empty(),
},
typing_env,
target_features: tcx.asm_target_features(def_id),
expr_ty: Box::new(get_operand_ty),
}
Expand Down Expand Up @@ -83,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];
Expand Down Expand Up @@ -189,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"
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.erase_regions(ty)
}
};
InlineAsmCtxt::new(self.tcx, enclosing_id, expr_ty).check_asm(asm);
InlineAsmCtxt::new(self.tcx, enclosing_id, self.typing_env(self.param_env), expr_ty)
.check_asm(asm);
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/ui/asm/conditionally-sized-ptr-fail.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions tests/ui/asm/conditionally-sized-ptr-fail.stderr
Original file line number Diff line number Diff line change
@@ -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

12 changes: 12 additions & 0 deletions tests/ui/asm/conditionally-sized-ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ check-pass
//@ needs-asm-support

use std::arch::asm;

fn _f<T>(p: *mut T) {
unsafe {
asm!("/* {} */", in(reg) p);
}
}

fn main() {}
Loading