Skip to content
This repository was archived by the owner on Oct 31, 2025. It is now read-only.

Commit d148daf

Browse files
authored
Improve error message for pointer casts (#153)
1 parent 27df71f commit d148daf

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
318318
}
319319
}
320320

321-
fn zombie_bitcast_ptr(&self, def: Word) {
321+
fn zombie_bitcast_ptr(&self, def: Word, from_ty: Word, to_ty: Word) {
322322
let is_logical = self
323323
.emit()
324324
.module_ref()
@@ -328,7 +328,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
328328
inst.operands[0].unwrap_addressing_model() == AddressingModel::Logical
329329
});
330330
if is_logical {
331-
self.zombie(def, "OpBitcast on ptr without AddressingModel != Logical")
331+
if self.is_system_crate() {
332+
self.zombie(def, "OpBitcast on ptr without AddressingModel != Logical")
333+
} else {
334+
self.struct_err("Cannot cast between pointer types")
335+
.note(&format!("from: {}", self.debug_type(from_ty)))
336+
.note(&format!("to: {}", self.debug_type(to_ty)))
337+
.emit()
338+
}
332339
}
333340
}
334341

@@ -1111,7 +1118,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
11111118
let val_is_ptr = matches!(self.lookup_type(val.ty), SpirvType::Pointer{..});
11121119
let dest_is_ptr = matches!(self.lookup_type(dest_ty), SpirvType::Pointer{..});
11131120
if val_is_ptr || dest_is_ptr {
1114-
self.zombie_bitcast_ptr(result.def);
1121+
self.zombie_bitcast_ptr(result.def, val.ty, dest_ty);
11151122
}
11161123
result
11171124
}
@@ -1207,7 +1214,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
12071214
.bitcast(dest_ty, None, val.def)
12081215
.unwrap()
12091216
.with_type(dest_ty);
1210-
self.zombie_bitcast_ptr(result.def);
1217+
self.zombie_bitcast_ptr(result.def, val.ty, dest_ty);
12111218
result
12121219
}
12131220
}

rustc_codegen_spirv/src/builder/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_codegen_ssa::traits::{
1717
CoverageInfoBuilderMethods, DebugInfoBuilderMethods, HasCodegen, InlineAsmOperandRef,
1818
StaticBuilderMethods,
1919
};
20+
use rustc_errors::DiagnosticBuilder;
2021
use rustc_hir::LlvmInlineAsmInner;
2122
use rustc_middle::mir::coverage::{
2223
CodeRegion, CounterValueReference, ExpressionOperandId, InjectedExpressionIndex, Op,
@@ -63,15 +64,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6364
}
6465
}
6566

66-
/*
6767
pub fn struct_err(&self, msg: &str) -> DiagnosticBuilder<'_> {
6868
if let Some(current_span) = self.current_span {
6969
self.tcx.sess.struct_span_err(current_span, msg)
7070
} else {
7171
self.tcx.sess.struct_err(msg)
7272
}
7373
}
74-
*/
7574

7675
pub fn err(&self, msg: &str) {
7776
if let Some(current_span) = self.current_span {

rustc_codegen_spirv/src/codegen_cx/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'tcx> CodegenCx<'tcx> {
136136
self.zombie_values.borrow_mut().insert(word, reason);
137137
}
138138

139-
fn is_system_crate(&self) -> bool {
139+
pub fn is_system_crate(&self) -> bool {
140140
self.tcx
141141
.sess
142142
.contains_name(self.tcx.hir().krate_attrs(), sym::compiler_builtins)

0 commit comments

Comments
 (0)