Skip to content

Commit f50b72f

Browse files
authored
Rollup merge of #102617 - lcnr:deferred_transmute_checks, r=compiler-errors
`HirId` for `deferred_transmute_checks` directly interacting with spans is annoying ✨
2 parents e64177c + 550715d commit f50b72f

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

compiler/rustc_hir_analysis/src/check/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
542542
// been resolved or we errored. This is important as we can only check transmute
543543
// on concrete types, but the output type may not be known yet (it would only
544544
// be known if explicitly specified via turbofish).
545-
self.deferred_transmute_checks.borrow_mut().push((from, to, expr.span));
545+
self.deferred_transmute_checks.borrow_mut().push((from, to, expr.hir_id));
546546
}
547547
if !tcx.features().unsized_fn_params {
548548
// We want to remove some Sized bounds from std functions,

compiler/rustc_hir_analysis/src/check/fn_ctxt/checks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5050
pub(in super::super) fn check_transmutes(&self) {
5151
let mut deferred_transmute_checks = self.deferred_transmute_checks.borrow_mut();
5252
debug!("FnCtxt::check_transmutes: {} deferred checks", deferred_transmute_checks.len());
53-
for (from, to, span) in deferred_transmute_checks.drain(..) {
54-
self.check_transmute(span, from, to);
53+
for (from, to, hir_id) in deferred_transmute_checks.drain(..) {
54+
self.check_transmute(from, to, hir_id);
5555
}
5656
}
5757

compiler/rustc_hir_analysis/src/check/inherited.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub struct Inherited<'a, 'tcx> {
5555

5656
pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
5757

58-
pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, Span)>>,
58+
pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, hir::HirId)>>,
5959

6060
pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>,
6161

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use hir::HirId;
12
use rustc_ast::InlineAsmTemplatePiece;
23
use rustc_data_structures::fx::FxHashSet;
34
use rustc_errors::struct_span_err;
@@ -6,7 +7,7 @@ use rustc_index::vec::Idx;
67
use rustc_middle::ty::layout::{LayoutError, SizeSkeleton};
78
use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitable, UintTy};
89
use rustc_session::lint;
9-
use rustc_span::{Span, Symbol, DUMMY_SP};
10+
use rustc_span::{Symbol, DUMMY_SP};
1011
use rustc_target::abi::{Pointer, VariantIdx};
1112
use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
1213

@@ -40,11 +41,13 @@ fn unpack_option_like<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
4041
}
4142

4243
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
43-
pub fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) {
44+
pub fn check_transmute(&self, from: Ty<'tcx>, to: Ty<'tcx>, hir_id: HirId) {
45+
let tcx = self.tcx;
46+
let span = tcx.hir().span(hir_id);
4447
let convert = |ty: Ty<'tcx>| {
4548
let ty = self.resolve_vars_if_possible(ty);
46-
let ty = self.tcx.normalize_erasing_regions(self.param_env, ty);
47-
(SizeSkeleton::compute(ty, self.tcx, self.param_env), ty)
49+
let ty = tcx.normalize_erasing_regions(self.param_env, ty);
50+
(SizeSkeleton::compute(ty, tcx, self.param_env), ty)
4851
};
4952
let (sk_from, from) = convert(from);
5053
let (sk_to, to) = convert(to);
@@ -57,9 +60,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5760

5861
// Special-case transmuting from `typeof(function)` and
5962
// `Option<typeof(function)>` to present a clearer error.
60-
let from = unpack_option_like(self.tcx, from);
61-
if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (from.kind(), sk_to) && size_to == Pointer.size(&self.tcx) {
62-
struct_span_err!(self.tcx.sess, span, E0591, "can't transmute zero-sized type")
63+
let from = unpack_option_like(tcx, from);
64+
if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (from.kind(), sk_to) && size_to == Pointer.size(&tcx) {
65+
struct_span_err!(tcx.sess, span, E0591, "can't transmute zero-sized type")
6366
.note(&format!("source type: {from}"))
6467
.note(&format!("target type: {to}"))
6568
.help("cast with `as` to a pointer instead")
@@ -83,7 +86,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8386
};
8487

8588
let mut err = struct_span_err!(
86-
self.tcx.sess,
89+
tcx.sess,
8790
span,
8891
E0512,
8992
"cannot transmute between types of different sizes, \

0 commit comments

Comments
 (0)