Skip to content

Commit e7a736a

Browse files
committed
try adding dbg! calls into rustc
1 parent 52c15ef commit e7a736a

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

src/librustc_typeck/check/cast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
597597
use rustc_middle::ty::cast::CastTy::*;
598598
use rustc_middle::ty::cast::IntTy::*;
599599

600+
//dbg!("calling do_check", &self.expr_ty, self.cast_ty, (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty)));
601+
600602
let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
601603
{
602604
(Some(t_from), Some(t_cast)) => (t_from, t_cast),
@@ -640,6 +642,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
640642
},
641643
// array-ptr-cast
642644
Ptr(mt) => {
645+
dbg!("calling check_ref_cast");
643646
self.check_ref_cast(fcx, TypeAndMut { mutbl, ty: inner_ty }, mt)
644647
}
645648
_ => Err(CastError::NonScalar),
@@ -787,6 +790,9 @@ impl<'a, 'tcx> CastCheck<'tcx> {
787790
)
788791
});
789792

793+
// TODO: Remove this!
794+
dbg!("enter demand_eqtype", &self.span, &ety, &m_cast.ty);
795+
790796
// this will report a type mismatch if needed
791797
fcx.demand_eqtype(self.span, ety, m_cast.ty);
792798
return Ok(CastKind::ArrayPtrCast);

src/librustc_typeck/check/demand.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7171

7272
pub fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
7373
if let Some(mut err) = self.demand_eqtype_diag(sp, expected, actual) {
74+
// TODO: Remove this!
75+
dbg!((&sp, &expected, &actual));
76+
7477
err.emit();
7578
}
7679
}

src/tools/clippy/clippy_lints/src/transmute.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,23 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
333333
let to_ty = cx.typeck_results().expr_ty(e);
334334

335335
match (&from_ty.kind, &to_ty.kind) {
336+
// put this back at the bottom later
337+
(_, _) if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) => span_lint_and_then(
338+
cx,
339+
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
340+
e.span,
341+
&format!(
342+
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
343+
from_ty,
344+
to_ty
345+
),
346+
|diag| {
347+
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
348+
let sugg = arg.as_ty(&to_ty.to_string()).to_string();
349+
diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
350+
}
351+
}
352+
),
336353
_ if from_ty == to_ty => span_lint(
337354
cx,
338355
USELESS_TRANSMUTE,
@@ -628,22 +645,6 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
628645
);
629646
}
630647
},
631-
(_, _) if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) => span_lint_and_then(
632-
cx,
633-
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
634-
e.span,
635-
&format!(
636-
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
637-
from_ty,
638-
to_ty
639-
),
640-
|diag| {
641-
if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
642-
let sugg = arg.as_ty(&to_ty.to_string()).to_string();
643-
diag.span_suggestion(e.span, "try", sugg, Applicability::Unspecified);
644-
}
645-
}
646-
),
647648
_ => {
648649
return;
649650
},

src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use std::mem::transmute;
88

99
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
1010
// valid, which we quote from below.
11-
12-
fn main() {
11+
fn valid_cast_cases() {
1312
// We should see an error message for each transmute, and no error messages for
1413
// the casts, since the casts are the recommended fixes.
1514

15+
1616
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
1717
let ptr_i32_transmute = unsafe {
1818
transmute::<isize, *const i32>(-1)
@@ -63,3 +63,17 @@ fn main() {
6363
};
6464
let usize_from_fn_ptr = foo as *const usize;
6565
}
66+
67+
type DCErrIn<'a> = &'a [i32; 1];
68+
type DCErrOut = *const u8;
69+
fn trigger_do_check_to_emit_error(in_param: DCErrIn<'_>) -> DCErrOut {
70+
unsafe { transmute::<DCErrIn, DCErrOut>(in_param) }
71+
}
72+
73+
type OtherErrIn<'a> = &'a i32;
74+
type OtherErrOut = *const u8;
75+
fn other(in_param: OtherErrIn<'_>) -> OtherErrOut {
76+
unsafe { transmute::<OtherErrIn, OtherErrOut>(in_param) }
77+
}
78+
79+
fn main() {}

0 commit comments

Comments
 (0)