Skip to content

Commit 0c4cb48

Browse files
authored
Rollup merge of rust-lang#58670 - saleemjaffer:refactor_typecast_check_kinds, r=oli-obk
fixes rust-lang#52482
2 parents 5e52010 + 9902f8c commit 0c4cb48

File tree

5 files changed

+32
-47
lines changed

5 files changed

+32
-47
lines changed

src/librustc/ty/context.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::ty::steal::Steal;
4141
use crate::ty::subst::{UserSubsts, UnpackedKind};
4242
use crate::ty::{BoundVar, BindingMode};
4343
use crate::ty::CanonicalPolyFnSig;
44-
use crate::util::nodemap::{DefIdMap, DefIdSet, ItemLocalMap};
44+
use crate::util::nodemap::{DefIdMap, DefIdSet, ItemLocalMap, ItemLocalSet};
4545
use crate::util::nodemap::{FxHashMap, FxHashSet};
4646
use errors::DiagnosticBuilder;
4747
use rustc_data_structures::interner::HashInterner;
@@ -409,9 +409,9 @@ pub struct TypeckTables<'tcx> {
409409
/// MIR construction and hence is not serialized to metadata.
410410
fru_field_types: ItemLocalMap<Vec<Ty<'tcx>>>,
411411

412-
/// Maps a cast expression to its kind. This is keyed on the
413-
/// *from* expression of the cast, not the cast itself.
414-
cast_kinds: ItemLocalMap<ty::cast::CastKind>,
412+
/// For every coercion cast we add the HIR node ID of the cast
413+
/// expression to this set.
414+
coercion_casts: ItemLocalSet,
415415

416416
/// Set of trait imports actually used in the method resolution.
417417
/// This is used for warning unused imports. During type
@@ -456,7 +456,7 @@ impl<'tcx> TypeckTables<'tcx> {
456456
closure_kind_origins: Default::default(),
457457
liberated_fn_sigs: Default::default(),
458458
fru_field_types: Default::default(),
459-
cast_kinds: Default::default(),
459+
coercion_casts: Default::default(),
460460
used_trait_imports: Lrc::new(Default::default()),
461461
tainted_by_errors: false,
462462
free_region_map: Default::default(),
@@ -718,19 +718,19 @@ impl<'tcx> TypeckTables<'tcx> {
718718
}
719719
}
720720

721-
pub fn cast_kinds(&self) -> LocalTableInContext<'_, ty::cast::CastKind> {
722-
LocalTableInContext {
723-
local_id_root: self.local_id_root,
724-
data: &self.cast_kinds
725-
}
721+
pub fn is_coercion_cast(&self, hir_id: hir::HirId) -> bool {
722+
validate_hir_id_for_typeck_tables(self.local_id_root, hir_id, true);
723+
self.coercion_casts.contains(&hir_id.local_id)
726724
}
727725

728-
pub fn cast_kinds_mut(&mut self) -> LocalTableInContextMut<'_, ty::cast::CastKind> {
729-
LocalTableInContextMut {
730-
local_id_root: self.local_id_root,
731-
data: &mut self.cast_kinds
732-
}
726+
pub fn set_coercion_cast(&mut self, id: ItemLocalId) {
727+
self.coercion_casts.insert(id);
728+
}
729+
730+
pub fn coercion_casts(&self) -> &ItemLocalSet {
731+
&self.coercion_casts
733732
}
733+
734734
}
735735

736736
impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
@@ -753,7 +753,7 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
753753
ref liberated_fn_sigs,
754754
ref fru_field_types,
755755

756-
ref cast_kinds,
756+
ref coercion_casts,
757757

758758
ref used_trait_imports,
759759
tainted_by_errors,
@@ -798,7 +798,7 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
798798
closure_kind_origins.hash_stable(hcx, hasher);
799799
liberated_fn_sigs.hash_stable(hcx, hasher);
800800
fru_field_types.hash_stable(hcx, hasher);
801-
cast_kinds.hash_stable(hcx, hasher);
801+
coercion_casts.hash_stable(hcx, hasher);
802802
used_trait_imports.hash_stable(hcx, hasher);
803803
tainted_by_errors.hash_stable(hcx, hasher);
804804
free_region_map.hash_stable(hcx, hasher);

src/librustc_mir/hair/cx/expr.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc::hir::def::{Def, CtorKind};
88
use rustc::mir::interpret::{GlobalId, ErrorHandled, ConstValue};
99
use rustc::ty::{self, AdtKind, Ty};
1010
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability};
11-
use rustc::ty::cast::CastKind as TyCastKind;
1211
use rustc::ty::subst::{InternalSubsts, SubstsRef};
1312
use rustc::hir;
1413
use rustc::hir::def_id::LocalDefId;
@@ -655,11 +654,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
655654

656655
// Check to see if this cast is a "coercion cast", where the cast is actually done
657656
// using a coercion (or is a no-op).
658-
let cast = if let Some(&TyCastKind::CoercionCast) =
659-
cx.tables()
660-
.cast_kinds()
661-
.get(source.hir_id)
662-
{
657+
let cast = if cx.tables().is_coercion_cast(source.hir_id) {
663658
// Convert the lexpr to a vexpr.
664659
ExprKind::Use { source: source.to_ref() }
665660
} else {

src/librustc_passes/rvalue_promotion.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// - It's not possible to take the address of a static item with unsafe interior. This is enforced
1515
// by borrowck::gather_loans
1616

17-
use rustc::ty::cast::CastKind;
17+
use rustc::ty::cast::CastTy;
1818
use rustc::hir::def::{Def, CtorKind};
1919
use rustc::hir::def_id::DefId;
2020
use rustc::middle::expr_use_visitor as euv;
@@ -318,15 +318,12 @@ fn check_expr_kind<'a, 'tcx>(
318318
hir::ExprKind::Cast(ref from, _) => {
319319
let expr_promotability = v.check_expr(from);
320320
debug!("Checking const cast(id={})", from.hir_id);
321-
match v.tables.cast_kinds().get(from.hir_id) {
322-
None => {
323-
v.tcx.sess.delay_span_bug(e.span, "no kind for cast");
324-
NotPromotable
325-
},
326-
Some(&CastKind::PtrAddrCast) | Some(&CastKind::FnPtrAddrCast) => {
327-
NotPromotable
328-
}
329-
_ => expr_promotability
321+
let cast_in = CastTy::from_ty(v.tables.expr_ty(from));
322+
let cast_out = CastTy::from_ty(v.tables.expr_ty(e));
323+
match (cast_in, cast_out) {
324+
(Some(CastTy::FnPtr), Some(CastTy::Int(_))) |
325+
(Some(CastTy::Ptr(_)), Some(CastTy::Int(_))) => NotPromotable,
326+
(_, _) => expr_promotability
330327
}
331328
}
332329
hir::ExprKind::Path(ref qpath) => {

src/librustc_typeck/check/cast.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,12 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
428428
} else if self.try_coercion_cast(fcx) {
429429
self.trivial_cast_lint(fcx);
430430
debug!(" -> CoercionCast");
431-
fcx.tables.borrow_mut().cast_kinds_mut().insert(self.expr.hir_id,
432-
CastKind::CoercionCast);
431+
fcx.tables.borrow_mut().set_coercion_cast(self.expr.hir_id.local_id);
432+
433433
} else {
434434
match self.do_check(fcx) {
435435
Ok(k) => {
436436
debug!(" -> {:?}", k);
437-
fcx.tables.borrow_mut().cast_kinds_mut().insert(self.expr.hir_id, k);
438437
}
439438
Err(e) => self.report_cast_error(fcx, e),
440439
};

src/librustc_typeck/check/writeback.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5050
wbcx.visit_liberated_fn_sigs();
5151
wbcx.visit_fru_field_types();
5252
wbcx.visit_opaque_types(body.value.span);
53-
wbcx.visit_cast_types();
53+
wbcx.visit_coercion_casts();
5454
wbcx.visit_free_region_map();
5555
wbcx.visit_user_provided_tys();
5656
wbcx.visit_user_provided_sigs();
@@ -355,19 +355,13 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
355355
}
356356
}
357357

358-
fn visit_cast_types(&mut self) {
358+
fn visit_coercion_casts(&mut self) {
359359
let fcx_tables = self.fcx.tables.borrow();
360-
let fcx_cast_kinds = fcx_tables.cast_kinds();
360+
let fcx_coercion_casts = fcx_tables.coercion_casts();
361361
debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
362-
let mut self_cast_kinds = self.tables.cast_kinds_mut();
363-
let common_local_id_root = fcx_tables.local_id_root.unwrap();
364362

365-
for (&local_id, &cast_kind) in fcx_cast_kinds.iter() {
366-
let hir_id = hir::HirId {
367-
owner: common_local_id_root.index,
368-
local_id,
369-
};
370-
self_cast_kinds.insert(hir_id, cast_kind);
363+
for local_id in fcx_coercion_casts {
364+
self.tables.set_coercion_cast(*local_id);
371365
}
372366
}
373367

0 commit comments

Comments
 (0)