Skip to content

Commit 9bb6fbe

Browse files
committed
Auto merge of rust-lang#113376 - Nilstrieb:pointer-coercions-are-not-casts-because-that-sounds-way-to-general-aaaa, r=oli-obk
Rename `adjustment::PointerCast` and variants using it to `PointerCoercion` It makes it sounds like the `ExprKind` and `Rvalue` are supposed to represent all pointer related casts, when in reality their just used to share a little enum variants. Make it clear there these are only coercions and that people who see this and think "why are so many pointer related casts not in these variants" aren't insane. This enum was added in rust-lang#59987. I'm not sure whether the variant sharing is actually worth it, but this at least makes it less confusing. r? oli-obk
2 parents ce519c5 + 2beabbb commit 9bb6fbe

File tree

85 files changed

+214
-185
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+214
-185
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::mir::{
99
Body, CallSource, CastKind, ConstraintCategory, FakeReadCause, Local, LocalInfo, Location,
1010
Operand, Place, Rvalue, Statement, StatementKind, TerminatorKind,
1111
};
12-
use rustc_middle::ty::adjustment::PointerCast;
12+
use rustc_middle::ty::adjustment::PointerCoercion;
1313
use rustc_middle::ty::{self, RegionVid, TyCtxt};
1414
use rustc_span::symbol::{kw, Symbol};
1515
use rustc_span::{sym, DesugaringKind, Span};
@@ -584,7 +584,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
584584
},
585585
// If we see an unsized cast, then if it is our data we should check
586586
// whether it is being cast to a trait object.
587-
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), operand, ty) => {
587+
Rvalue::Cast(
588+
CastKind::PointerCoercion(PointerCoercion::Unsize),
589+
operand,
590+
ty,
591+
) => {
588592
match operand {
589593
Operand::Copy(place) | Operand::Move(place) => {
590594
if let Some(from) = place.as_local() {

compiler/rustc_borrowck/src/type_check/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_middle::mir::AssertKind;
2828
use rustc_middle::mir::*;
2929
use rustc_middle::traits::query::NoSolution;
3030
use rustc_middle::traits::ObligationCause;
31-
use rustc_middle::ty::adjustment::PointerCast;
31+
use rustc_middle::ty::adjustment::PointerCoercion;
3232
use rustc_middle::ty::cast::CastTy;
3333
use rustc_middle::ty::subst::{SubstsRef, UserSubsts};
3434
use rustc_middle::ty::visit::TypeVisitableExt;
@@ -1908,7 +1908,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19081908
self.check_operand(op, location);
19091909

19101910
match cast_kind {
1911-
CastKind::Pointer(PointerCast::ReifyFnPointer) => {
1911+
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer) => {
19121912
let fn_sig = op.ty(body, tcx).fn_sig(tcx);
19131913

19141914
// The type that we see in the fcx is like
@@ -1937,7 +1937,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19371937
}
19381938
}
19391939

1940-
CastKind::Pointer(PointerCast::ClosureFnPointer(unsafety)) => {
1940+
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(unsafety)) => {
19411941
let sig = match op.ty(body, tcx).kind() {
19421942
ty::Closure(_, substs) => substs.as_closure().sig(),
19431943
_ => bug!(),
@@ -1962,7 +1962,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19621962
}
19631963
}
19641964

1965-
CastKind::Pointer(PointerCast::UnsafeFnPointer) => {
1965+
CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer) => {
19661966
let fn_sig = op.ty(body, tcx).fn_sig(tcx);
19671967

19681968
// The type that we see in the fcx is like
@@ -1991,7 +1991,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19911991
}
19921992
}
19931993

1994-
CastKind::Pointer(PointerCast::Unsize) => {
1994+
CastKind::PointerCoercion(PointerCoercion::Unsize) => {
19951995
let &ty = ty;
19961996
let trait_ref = ty::TraitRef::from_lang_item(
19971997
tcx,
@@ -2038,7 +2038,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20382038
);
20392039
}
20402040

2041-
CastKind::Pointer(PointerCast::MutToConstPointer) => {
2041+
CastKind::PointerCoercion(PointerCoercion::MutToConstPointer) => {
20422042
let ty::RawPtr(ty::TypeAndMut {
20432043
ty: ty_from,
20442044
mutbl: hir::Mutability::Mut,
@@ -2080,7 +2080,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20802080
}
20812081
}
20822082

2083-
CastKind::Pointer(PointerCast::ArrayToPointer) => {
2083+
CastKind::PointerCoercion(PointerCoercion::ArrayToPointer) => {
20842084
let ty_from = op.ty(body, tcx);
20852085

20862086
let opt_ty_elem_mut = match ty_from.kind() {

compiler/rustc_codegen_cranelift/src/base.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use rustc_ast::InlineAsmOptions;
44
use rustc_index::IndexVec;
5-
use rustc_middle::ty::adjustment::PointerCast;
5+
use rustc_middle::ty::adjustment::PointerCoercion;
66
use rustc_middle::ty::layout::FnAbiOf;
77
use rustc_middle::ty::print::with_no_trimmed_paths;
88

@@ -571,7 +571,7 @@ fn codegen_stmt<'tcx>(
571571
lval.write_cvalue(fx, res);
572572
}
573573
Rvalue::Cast(
574-
CastKind::Pointer(PointerCast::ReifyFnPointer),
574+
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer),
575575
ref operand,
576576
to_ty,
577577
) => {
@@ -596,17 +596,17 @@ fn codegen_stmt<'tcx>(
596596
}
597597
}
598598
Rvalue::Cast(
599-
CastKind::Pointer(PointerCast::UnsafeFnPointer),
599+
CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer),
600600
ref operand,
601601
to_ty,
602602
)
603603
| Rvalue::Cast(
604-
CastKind::Pointer(PointerCast::MutToConstPointer),
604+
CastKind::PointerCoercion(PointerCoercion::MutToConstPointer),
605605
ref operand,
606606
to_ty,
607607
)
608608
| Rvalue::Cast(
609-
CastKind::Pointer(PointerCast::ArrayToPointer),
609+
CastKind::PointerCoercion(PointerCoercion::ArrayToPointer),
610610
ref operand,
611611
to_ty,
612612
) => {
@@ -662,7 +662,7 @@ fn codegen_stmt<'tcx>(
662662
}
663663
}
664664
Rvalue::Cast(
665-
CastKind::Pointer(PointerCast::ClosureFnPointer(_)),
665+
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_)),
666666
ref operand,
667667
_to_ty,
668668
) => {
@@ -684,7 +684,11 @@ fn codegen_stmt<'tcx>(
684684
_ => bug!("{} cannot be cast to a fn ptr", operand.layout().ty),
685685
}
686686
}
687-
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), ref operand, _to_ty) => {
687+
Rvalue::Cast(
688+
CastKind::PointerCoercion(PointerCoercion::Unsize),
689+
ref operand,
690+
_to_ty,
691+
) => {
688692
let operand = codegen_operand(fx, operand);
689693
crate::unsize::coerce_unsized_into(fx, operand, lval);
690694
}

compiler/rustc_codegen_cranelift/src/unsize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! Codegen of the [`PointerCast::Unsize`] operation.
1+
//! Codegen of the [`PointerCoercion::Unsize`] operation.
22
//!
3-
//! [`PointerCast::Unsize`]: `rustc_middle::ty::adjustment::PointerCast::Unsize`
3+
//! [`PointerCoercion::Unsize`]: `rustc_middle::ty::adjustment::PointerCoercion::Unsize`
44
55
use crate::prelude::*;
66

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::mir;
1111
use rustc_middle::mir::Operand;
1212
use rustc_middle::ty::cast::{CastTy, IntTy};
1313
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, TyAndLayout};
14-
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, Ty, TyCtxt};
14+
use rustc_middle::ty::{self, adjustment::PointerCoercion, Instance, Ty, TyCtxt};
1515
use rustc_session::config::OptLevel;
1616
use rustc_span::source_map::{Span, DUMMY_SP};
1717
use rustc_target::abi::{self, FIRST_VARIANT};
@@ -32,7 +32,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3232
cg_operand.val.store(bx, dest);
3333
}
3434

35-
mir::Rvalue::Cast(mir::CastKind::Pointer(PointerCast::Unsize), ref source, _) => {
35+
mir::Rvalue::Cast(
36+
mir::CastKind::PointerCoercion(PointerCoercion::Unsize),
37+
ref source,
38+
_,
39+
) => {
3640
// The destination necessarily contains a fat pointer, so if
3741
// it's a scalar pair, it's a fat pointer or newtype thereof.
3842
if bx.cx().is_backend_scalar_pair(dest.layout) {
@@ -411,7 +415,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
411415
let lladdr = bx.ptrtoint(llptr, llcast_ty);
412416
OperandValue::Immediate(lladdr)
413417
}
414-
mir::CastKind::Pointer(PointerCast::ReifyFnPointer) => {
418+
mir::CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer) => {
415419
match *operand.layout.ty.kind() {
416420
ty::FnDef(def_id, substs) => {
417421
let instance = ty::Instance::resolve_for_fn_ptr(
@@ -427,7 +431,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
427431
_ => bug!("{} cannot be reified to a fn ptr", operand.layout.ty),
428432
}
429433
}
430-
mir::CastKind::Pointer(PointerCast::ClosureFnPointer(_)) => {
434+
mir::CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_)) => {
431435
match *operand.layout.ty.kind() {
432436
ty::Closure(def_id, substs) => {
433437
let instance = Instance::resolve_closure(
@@ -443,11 +447,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
443447
_ => bug!("{} cannot be cast to a fn ptr", operand.layout.ty),
444448
}
445449
}
446-
mir::CastKind::Pointer(PointerCast::UnsafeFnPointer) => {
450+
mir::CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer) => {
447451
// This is a no-op at the LLVM level.
448452
operand.val
449453
}
450-
mir::CastKind::Pointer(PointerCast::Unsize) => {
454+
mir::CastKind::PointerCoercion(PointerCoercion::Unsize) => {
451455
assert!(bx.cx().is_backend_scalar_pair(cast));
452456
let (lldata, llextra) = match operand.val {
453457
OperandValue::Pair(lldata, llextra) => {
@@ -470,7 +474,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
470474
base::unsize_ptr(bx, lldata, operand.layout.ty, cast.ty, llextra);
471475
OperandValue::Pair(lldata, llextra)
472476
}
473-
mir::CastKind::Pointer(PointerCast::MutToConstPointer)
477+
mir::CastKind::PointerCoercion(PointerCoercion::MutToConstPointer)
474478
| mir::CastKind::PtrToPtr
475479
if bx.cx().is_backend_scalar_pair(operand.layout) =>
476480
{
@@ -504,8 +508,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
504508
base::cast_to_dyn_star(bx, lldata, operand.layout, cast.ty, llextra);
505509
OperandValue::Pair(lldata, llextra)
506510
}
507-
mir::CastKind::Pointer(
508-
PointerCast::MutToConstPointer | PointerCast::ArrayToPointer,
511+
mir::CastKind::PointerCoercion(
512+
PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer,
509513
)
510514
| mir::CastKind::IntToInt
511515
| mir::CastKind::FloatToInt

compiler/rustc_const_eval/src/interpret/cast.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_apfloat::ieee::{Double, Single};
44
use rustc_apfloat::{Float, FloatConvert};
55
use rustc_middle::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
66
use rustc_middle::mir::CastKind;
7-
use rustc_middle::ty::adjustment::PointerCast;
7+
use rustc_middle::ty::adjustment::PointerCoercion;
88
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, TyAndLayout};
99
use rustc_middle::ty::{self, FloatTy, Ty, TypeAndMut};
1010
use rustc_target::abi::Integer;
@@ -24,51 +24,52 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2424
cast_ty: Ty<'tcx>,
2525
dest: &PlaceTy<'tcx, M::Provenance>,
2626
) -> InterpResult<'tcx> {
27-
use rustc_middle::mir::CastKind::*;
2827
// FIXME: In which cases should we trigger UB when the source is uninit?
2928
match cast_kind {
30-
Pointer(PointerCast::Unsize) => {
29+
CastKind::PointerCoercion(PointerCoercion::Unsize) => {
3130
let cast_ty = self.layout_of(cast_ty)?;
3231
self.unsize_into(src, cast_ty, dest)?;
3332
}
3433

35-
PointerExposeAddress => {
34+
CastKind::PointerExposeAddress => {
3635
let src = self.read_immediate(src)?;
3736
let res = self.pointer_expose_address_cast(&src, cast_ty)?;
3837
self.write_immediate(res, dest)?;
3938
}
4039

41-
PointerFromExposedAddress => {
40+
CastKind::PointerFromExposedAddress => {
4241
let src = self.read_immediate(src)?;
4342
let res = self.pointer_from_exposed_address_cast(&src, cast_ty)?;
4443
self.write_immediate(res, dest)?;
4544
}
4645

47-
IntToInt | IntToFloat => {
46+
CastKind::IntToInt | CastKind::IntToFloat => {
4847
let src = self.read_immediate(src)?;
4948
let res = self.int_to_int_or_float(&src, cast_ty)?;
5049
self.write_immediate(res, dest)?;
5150
}
5251

53-
FloatToFloat | FloatToInt => {
52+
CastKind::FloatToFloat | CastKind::FloatToInt => {
5453
let src = self.read_immediate(src)?;
5554
let res = self.float_to_float_or_int(&src, cast_ty)?;
5655
self.write_immediate(res, dest)?;
5756
}
5857

59-
FnPtrToPtr | PtrToPtr => {
58+
CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
6059
let src = self.read_immediate(&src)?;
6160
let res = self.ptr_to_ptr(&src, cast_ty)?;
6261
self.write_immediate(res, dest)?;
6362
}
6463

65-
Pointer(PointerCast::MutToConstPointer | PointerCast::ArrayToPointer) => {
64+
CastKind::PointerCoercion(
65+
PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer,
66+
) => {
6667
// These are NOPs, but can be wide pointers.
6768
let v = self.read_immediate(src)?;
6869
self.write_immediate(*v, dest)?;
6970
}
7071

71-
Pointer(PointerCast::ReifyFnPointer) => {
72+
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer) => {
7273
// All reifications must be monomorphic, bail out otherwise.
7374
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
7475

@@ -90,7 +91,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
9091
}
9192
}
9293

93-
Pointer(PointerCast::UnsafeFnPointer) => {
94+
CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer) => {
9495
let src = self.read_immediate(src)?;
9596
match cast_ty.kind() {
9697
ty::FnPtr(_) => {
@@ -101,7 +102,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
101102
}
102103
}
103104

104-
Pointer(PointerCast::ClosureFnPointer(_)) => {
105+
CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_)) => {
105106
// All reifications must be monomorphic, bail out otherwise.
106107
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
107108

@@ -122,7 +123,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
122123
}
123124
}
124125

125-
DynStar => {
126+
CastKind::DynStar => {
126127
if let ty::Dynamic(data, _, ty::DynStar) = cast_ty.kind() {
127128
// Initial cast from sized to dyn trait
128129
let vtable = self.get_vtable_ptr(src.layout.ty, data.principal())?;
@@ -136,7 +137,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
136137
}
137138
}
138139

139-
Transmute => {
140+
CastKind::Transmute => {
140141
assert!(src.layout.is_sized());
141142
assert!(dest.layout.is_sized());
142143
if src.layout.size != dest.layout.size {

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
99
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
1010
use rustc_middle::mir::*;
1111
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
12-
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt};
12+
use rustc_middle::ty::{self, adjustment::PointerCoercion, Instance, InstanceDef, Ty, TyCtxt};
1313
use rustc_middle::ty::{TraitRef, TypeVisitableExt};
1414
use rustc_mir_dataflow::{self, Analysis};
1515
use rustc_span::{sym, Span, Symbol};
@@ -521,20 +521,20 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
521521
}
522522

523523
Rvalue::Cast(
524-
CastKind::Pointer(
525-
PointerCast::MutToConstPointer
526-
| PointerCast::ArrayToPointer
527-
| PointerCast::UnsafeFnPointer
528-
| PointerCast::ClosureFnPointer(_)
529-
| PointerCast::ReifyFnPointer,
524+
CastKind::PointerCoercion(
525+
PointerCoercion::MutToConstPointer
526+
| PointerCoercion::ArrayToPointer
527+
| PointerCoercion::UnsafeFnPointer
528+
| PointerCoercion::ClosureFnPointer(_)
529+
| PointerCoercion::ReifyFnPointer,
530530
),
531531
_,
532532
_,
533533
) => {
534534
// These are all okay; they only change the type, not the data.
535535
}
536536

537-
Rvalue::Cast(CastKind::Pointer(PointerCast::Unsize), _, _) => {
537+
Rvalue::Cast(CastKind::PointerCoercion(PointerCoercion::Unsize), _, _) => {
538538
// Unsizing is implemented for CTFE.
539539
}
540540

compiler/rustc_const_eval/src/transform/validate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
650650
// FIXME: Add Checks for these
651651
CastKind::PointerFromExposedAddress
652652
| CastKind::PointerExposeAddress
653-
| CastKind::Pointer(_) => {}
653+
| CastKind::PointerCoercion(_) => {}
654654
CastKind::IntToInt | CastKind::IntToFloat => {
655655
let input_valid = op_ty.is_integral() || op_ty.is_char() || op_ty.is_bool();
656656
let target_valid = target_type.is_numeric() || target_type.is_char();

0 commit comments

Comments
 (0)