Skip to content

Commit d00d42d

Browse files
committed
rustc_target: pass contexts by reference, not value.
1 parent ca4fa6f commit d00d42d

38 files changed

+175
-214
lines changed

Diff for: src/librustc/lint/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -783,11 +783,11 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
783783
}
784784
}
785785

786-
impl<'a, 'tcx> LayoutOf for &'a LateContext<'a, 'tcx> {
786+
impl<'a, 'tcx> LayoutOf for LateContext<'a, 'tcx> {
787787
type Ty = Ty<'tcx>;
788788
type TyLayout = Result<TyLayout<'tcx>, LayoutError<'tcx>>;
789789

790-
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
790+
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
791791
self.tcx.layout_of(self.param_env.and(ty))
792792
}
793793
}

Diff for: src/librustc/middle/intrinsicck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
8484
// `Option<typeof(function)>` to present a clearer error.
8585
let from = unpack_option_like(self.tcx.global_tcx(), from);
8686
if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (&from.sty, sk_to) {
87-
if size_to == Pointer.size(self.tcx) {
87+
if size_to == Pointer.size(&self.tcx) {
8888
struct_span_err!(self.tcx.sess, span, E0591,
8989
"can't transmute zero-sized type")
9090
.note(&format!("source type: {}", from))

Diff for: src/librustc/mir/interpret/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,18 @@ pub trait PointerArithmetic: layout::HasDataLayout {
8686
// These are not supposed to be overridden.
8787

8888
#[inline(always)]
89-
fn pointer_size(self) -> Size {
89+
fn pointer_size(&self) -> Size {
9090
self.data_layout().pointer_size
9191
}
9292

9393
//// Trunace the given value to the pointer size; also return whether there was an overflow
94-
fn truncate_to_ptr(self, val: u128) -> (u64, bool) {
94+
fn truncate_to_ptr(&self, val: u128) -> (u64, bool) {
9595
let max_ptr_plus_1 = 1u128 << self.pointer_size().bits();
9696
((val % max_ptr_plus_1) as u64, val >= max_ptr_plus_1)
9797
}
9898

9999
// Overflow checking only works properly on the range from -u64 to +u64.
100-
fn overflowing_signed_offset(self, val: u64, i: i128) -> (u64, bool) {
100+
fn overflowing_signed_offset(&self, val: u64, i: i128) -> (u64, bool) {
101101
// FIXME: is it possible to over/underflow here?
102102
if i < 0 {
103103
// trickery to ensure that i64::min_value() works fine
@@ -109,23 +109,23 @@ pub trait PointerArithmetic: layout::HasDataLayout {
109109
}
110110
}
111111

112-
fn overflowing_offset(self, val: u64, i: u64) -> (u64, bool) {
112+
fn overflowing_offset(&self, val: u64, i: u64) -> (u64, bool) {
113113
let (res, over1) = val.overflowing_add(i);
114114
let (res, over2) = self.truncate_to_ptr(res as u128);
115115
(res, over1 || over2)
116116
}
117117

118-
fn signed_offset<'tcx>(self, val: u64, i: i64) -> EvalResult<'tcx, u64> {
118+
fn signed_offset<'tcx>(&self, val: u64, i: i64) -> EvalResult<'tcx, u64> {
119119
let (res, over) = self.overflowing_signed_offset(val, i as i128);
120120
if over { err!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
121121
}
122122

123-
fn offset<'tcx>(self, val: u64, i: u64) -> EvalResult<'tcx, u64> {
123+
fn offset<'tcx>(&self, val: u64, i: u64) -> EvalResult<'tcx, u64> {
124124
let (res, over) = self.overflowing_offset(val, i);
125125
if over { err!(Overflow(mir::BinOp::Add)) } else { Ok(res) }
126126
}
127127

128-
fn wrapping_signed_offset(self, val: u64, i: i64) -> u64 {
128+
fn wrapping_signed_offset(&self, val: u64, i: i64) -> u64 {
129129
self.overflowing_signed_offset(val, i as i128).0
130130
}
131131
}
@@ -176,33 +176,33 @@ impl<'tcx, Tag> Pointer<Tag> {
176176
Pointer { alloc_id, offset, tag }
177177
}
178178

179-
pub fn wrapping_signed_offset(self, i: i64, cx: impl HasDataLayout) -> Self {
179+
pub fn wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
180180
Pointer::new_with_tag(
181181
self.alloc_id,
182182
Size::from_bytes(cx.data_layout().wrapping_signed_offset(self.offset.bytes(), i)),
183183
self.tag,
184184
)
185185
}
186186

187-
pub fn overflowing_signed_offset(self, i: i128, cx: impl HasDataLayout) -> (Self, bool) {
187+
pub fn overflowing_signed_offset(self, i: i128, cx: &impl HasDataLayout) -> (Self, bool) {
188188
let (res, over) = cx.data_layout().overflowing_signed_offset(self.offset.bytes(), i);
189189
(Pointer::new_with_tag(self.alloc_id, Size::from_bytes(res), self.tag), over)
190190
}
191191

192-
pub fn signed_offset(self, i: i64, cx: impl HasDataLayout) -> EvalResult<'tcx, Self> {
192+
pub fn signed_offset(self, i: i64, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
193193
Ok(Pointer::new_with_tag(
194194
self.alloc_id,
195195
Size::from_bytes(cx.data_layout().signed_offset(self.offset.bytes(), i)?),
196196
self.tag,
197197
))
198198
}
199199

200-
pub fn overflowing_offset(self, i: Size, cx: impl HasDataLayout) -> (Self, bool) {
200+
pub fn overflowing_offset(self, i: Size, cx: &impl HasDataLayout) -> (Self, bool) {
201201
let (res, over) = cx.data_layout().overflowing_offset(self.offset.bytes(), i.bytes());
202202
(Pointer::new_with_tag(self.alloc_id, Size::from_bytes(res), self.tag), over)
203203
}
204204

205-
pub fn offset(self, i: Size, cx: impl HasDataLayout) -> EvalResult<'tcx, Self> {
205+
pub fn offset(self, i: Size, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
206206
Ok(Pointer::new_with_tag(
207207
self.alloc_id,
208208
Size::from_bytes(cx.data_layout().offset(self.offset.bytes(), i.bytes())?),

Diff for: src/librustc/mir/interpret/value.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'tcx> ConstValue<'tcx> {
6565
pub fn new_slice(
6666
val: Scalar,
6767
len: u64,
68-
cx: impl HasDataLayout
68+
cx: &impl HasDataLayout
6969
) -> Self {
7070
ConstValue::ScalarPair(val, Scalar::Bits {
7171
bits: len as u128,
@@ -121,7 +121,7 @@ impl<'tcx, Tag> Scalar<Tag> {
121121
}
122122

123123
#[inline]
124-
pub fn ptr_null(cx: impl HasDataLayout) -> Self {
124+
pub fn ptr_null(cx: &impl HasDataLayout) -> Self {
125125
Scalar::Bits {
126126
bits: 0,
127127
size: cx.data_layout().pointer_size.bytes() as u8,
@@ -134,52 +134,52 @@ impl<'tcx, Tag> Scalar<Tag> {
134134
}
135135

136136
#[inline]
137-
pub fn ptr_signed_offset(self, i: i64, cx: impl HasDataLayout) -> EvalResult<'tcx, Self> {
138-
let layout = cx.data_layout();
137+
pub fn ptr_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
138+
let dl = cx.data_layout();
139139
match self {
140140
Scalar::Bits { bits, size } => {
141-
assert_eq!(size as u64, layout.pointer_size.bytes());
141+
assert_eq!(size as u64, dl.pointer_size.bytes());
142142
Ok(Scalar::Bits {
143-
bits: layout.signed_offset(bits as u64, i)? as u128,
143+
bits: dl.signed_offset(bits as u64, i)? as u128,
144144
size,
145145
})
146146
}
147-
Scalar::Ptr(ptr) => ptr.signed_offset(i, layout).map(Scalar::Ptr),
147+
Scalar::Ptr(ptr) => ptr.signed_offset(i, dl).map(Scalar::Ptr),
148148
}
149149
}
150150

151151
#[inline]
152-
pub fn ptr_offset(self, i: Size, cx: impl HasDataLayout) -> EvalResult<'tcx, Self> {
153-
let layout = cx.data_layout();
152+
pub fn ptr_offset(self, i: Size, cx: &impl HasDataLayout) -> EvalResult<'tcx, Self> {
153+
let dl = cx.data_layout();
154154
match self {
155155
Scalar::Bits { bits, size } => {
156-
assert_eq!(size as u64, layout.pointer_size.bytes());
156+
assert_eq!(size as u64, dl.pointer_size.bytes());
157157
Ok(Scalar::Bits {
158-
bits: layout.offset(bits as u64, i.bytes())? as u128,
158+
bits: dl.offset(bits as u64, i.bytes())? as u128,
159159
size,
160160
})
161161
}
162-
Scalar::Ptr(ptr) => ptr.offset(i, layout).map(Scalar::Ptr),
162+
Scalar::Ptr(ptr) => ptr.offset(i, dl).map(Scalar::Ptr),
163163
}
164164
}
165165

166166
#[inline]
167-
pub fn ptr_wrapping_signed_offset(self, i: i64, cx: impl HasDataLayout) -> Self {
168-
let layout = cx.data_layout();
167+
pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
168+
let dl = cx.data_layout();
169169
match self {
170170
Scalar::Bits { bits, size } => {
171-
assert_eq!(size as u64, layout.pointer_size.bytes());
171+
assert_eq!(size as u64, dl.pointer_size.bytes());
172172
Scalar::Bits {
173-
bits: layout.wrapping_signed_offset(bits as u64, i) as u128,
173+
bits: dl.wrapping_signed_offset(bits as u64, i) as u128,
174174
size,
175175
}
176176
}
177-
Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_signed_offset(i, layout)),
177+
Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_signed_offset(i, dl)),
178178
}
179179
}
180180

181181
#[inline]
182-
pub fn is_null_ptr(self, cx: impl HasDataLayout) -> bool {
182+
pub fn is_null_ptr(self, cx: &impl HasDataLayout) -> bool {
183183
match self {
184184
Scalar::Bits { bits, size } => {
185185
assert_eq!(size as u64, cx.data_layout().pointer_size.bytes());
@@ -301,7 +301,7 @@ impl<'tcx, Tag> Scalar<Tag> {
301301
Ok(b as u64)
302302
}
303303

304-
pub fn to_usize(self, cx: impl HasDataLayout) -> EvalResult<'static, u64> {
304+
pub fn to_usize(self, cx: &impl HasDataLayout) -> EvalResult<'static, u64> {
305305
let b = self.to_bits(cx.data_layout().pointer_size)?;
306306
assert_eq!(b as u64 as u128, b);
307307
Ok(b as u64)
@@ -331,7 +331,7 @@ impl<'tcx, Tag> Scalar<Tag> {
331331
Ok(b as i64)
332332
}
333333

334-
pub fn to_isize(self, cx: impl HasDataLayout) -> EvalResult<'static, i64> {
334+
pub fn to_isize(self, cx: &impl HasDataLayout) -> EvalResult<'static, i64> {
335335
let b = self.to_bits(cx.data_layout().pointer_size)?;
336336
let b = sign_extend(b, cx.data_layout().pointer_size) as i128;
337337
assert_eq!(b as i64 as i128, b);

Diff for: src/librustc/traits/error_reporting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
428428
));
429429
let tcx = self.tcx;
430430
if let Some(len) = len.val.try_to_scalar().and_then(|scalar| {
431-
scalar.to_usize(tcx).ok()
431+
scalar.to_usize(&tcx).ok()
432432
}) {
433433
flags.push((
434434
"_Self".to_owned(),

Diff for: src/librustc/ty/layout.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub use rustc_target::abi::*;
3030

3131
pub trait IntegerExt {
3232
fn to_ty<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, signed: bool) -> Ty<'tcx>;
33-
fn from_attr<C: HasDataLayout>(cx: C, ity: attr::IntType) -> Integer;
33+
fn from_attr<C: HasDataLayout>(cx: &C, ity: attr::IntType) -> Integer;
3434
fn repr_discr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
3535
ty: Ty<'tcx>,
3636
repr: &ReprOptions,
@@ -56,7 +56,7 @@ impl IntegerExt for Integer {
5656
}
5757

5858
/// Get the Integer type from an attr::IntType.
59-
fn from_attr<C: HasDataLayout>(cx: C, ity: attr::IntType) -> Integer {
59+
fn from_attr<C: HasDataLayout>(cx: &C, ity: attr::IntType) -> Integer {
6060
let dl = cx.data_layout();
6161

6262
match ity {
@@ -92,7 +92,7 @@ impl IntegerExt for Integer {
9292
let min_default = I8;
9393

9494
if let Some(ity) = repr.int {
95-
let discr = Integer::from_attr(tcx, ity);
95+
let discr = Integer::from_attr(&tcx, ity);
9696
let fit = if ity.is_signed() { signed_fit } else { unsigned_fit };
9797
if discr < fit {
9898
bug!("Integer::repr_discr: `#[repr]` hint too small for \
@@ -202,14 +202,13 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
202202
};
203203
}
204204

205-
#[derive(Copy, Clone)]
206205
pub struct LayoutCx<'tcx, C> {
207206
pub tcx: C,
208207
pub param_env: ty::ParamEnv<'tcx>
209208
}
210209

211210
impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
212-
fn layout_raw_uncached(self, ty: Ty<'tcx>)
211+
fn layout_raw_uncached(&self, ty: Ty<'tcx>)
213212
-> Result<&'tcx LayoutDetails, LayoutError<'tcx>> {
214213
let tcx = self.tcx;
215214
let param_env = self.param_env;
@@ -899,7 +898,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
899898

900899
let (mut min, mut max) = (i128::max_value(), i128::min_value());
901900
let discr_type = def.repr.discr_type();
902-
let bits = Integer::from_attr(tcx, discr_type).size().bits();
901+
let bits = Integer::from_attr(self, discr_type).size().bits();
903902
for (i, discr) in def.discriminants(tcx).enumerate() {
904903
if variants[i].iter().any(|f| f.abi.is_uninhabited()) {
905904
continue;
@@ -1141,7 +1140,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
11411140
/// This is invoked by the `layout_raw` query to record the final
11421141
/// layout of each type.
11431142
#[inline]
1144-
fn record_layout_for_printing(self, layout: TyLayout<'tcx>) {
1143+
fn record_layout_for_printing(&self, layout: TyLayout<'tcx>) {
11451144
// If we are running with `-Zprint-type-sizes`, record layouts for
11461145
// dumping later. Ignore layouts that are done with non-empty
11471146
// environments or non-monomorphic layouts, as the user only wants
@@ -1158,7 +1157,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
11581157
self.record_layout_for_printing_outlined(layout)
11591158
}
11601159

1161-
fn record_layout_for_printing_outlined(self, layout: TyLayout<'tcx>) {
1160+
fn record_layout_for_printing_outlined(&self, layout: TyLayout<'tcx>) {
11621161
// (delay format until we actually need it)
11631162
let record = |kind, packed, opt_discr_size, variants| {
11641163
let type_desc = format!("{:?}", layout.ty);
@@ -1478,7 +1477,7 @@ impl<'a, 'tcx> LayoutOf for LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
14781477

14791478
/// Computes the layout of a type. Note that this implicitly
14801479
/// executes in "reveal all" mode.
1481-
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
1480+
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
14821481
let param_env = self.param_env.with_reveal_all();
14831482
let ty = self.tcx.normalize_erasing_regions(param_env, ty);
14841483
let details = self.tcx.layout_raw(param_env.and(ty))?;
@@ -1505,7 +1504,7 @@ impl<'a, 'tcx> LayoutOf for LayoutCx<'tcx, ty::query::TyCtxtAt<'a, 'tcx, 'tcx>>
15051504

15061505
/// Computes the layout of a type. Note that this implicitly
15071506
/// executes in "reveal all" mode.
1508-
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
1507+
fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout {
15091508
let param_env = self.param_env.with_reveal_all();
15101509
let ty = self.tcx.normalize_erasing_regions(param_env, ty);
15111510
let details = self.tcx.layout_raw(param_env.and(ty))?;
@@ -1563,7 +1562,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
15631562
where C: LayoutOf<Ty = Ty<'tcx>> + HasTyCtxt<'tcx>,
15641563
C::TyLayout: MaybeResult<TyLayout<'tcx>>
15651564
{
1566-
fn for_variant(this: TyLayout<'tcx>, cx: C, variant_index: usize) -> TyLayout<'tcx> {
1565+
fn for_variant(this: TyLayout<'tcx>, cx: &C, variant_index: usize) -> TyLayout<'tcx> {
15671566
let details = match this.variants {
15681567
Variants::Single { index } if index == variant_index => this.details,
15691568

@@ -1602,7 +1601,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
16021601
}
16031602
}
16041603

1605-
fn field(this: TyLayout<'tcx>, cx: C, i: usize) -> C::TyLayout {
1604+
fn field(this: TyLayout<'tcx>, cx: &C, i: usize) -> C::TyLayout {
16061605
let tcx = cx.tcx();
16071606
cx.layout_of(match this.ty.sty {
16081607
ty::Bool |
@@ -1699,7 +1698,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx>
16991698
Variants::Tagged { tag: ref discr, .. } |
17001699
Variants::NicheFilling { niche: ref discr, .. } => {
17011700
assert_eq!(i, 0);
1702-
let layout = LayoutDetails::scalar(tcx, discr.clone());
1701+
let layout = LayoutDetails::scalar(cx, discr.clone());
17031702
return MaybeResult::from_ok(TyLayout {
17041703
details: tcx.intern_layout(layout),
17051704
ty: discr.value.to_ty(tcx)
@@ -1725,7 +1724,7 @@ struct Niche {
17251724
impl Niche {
17261725
fn reserve<'a, 'tcx>(
17271726
&self,
1728-
cx: LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>>,
1727+
cx: &LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>>,
17291728
count: u128,
17301729
) -> Option<(u128, Scalar)> {
17311730
if count > self.available {
@@ -1745,7 +1744,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
17451744
/// Find the offset of a niche leaf field, starting from
17461745
/// the given type and recursing through aggregates.
17471746
// FIXME(eddyb) traverse already optimized enums.
1748-
fn find_niche(self, layout: TyLayout<'tcx>) -> Result<Option<Niche>, LayoutError<'tcx>> {
1747+
fn find_niche(&self, layout: TyLayout<'tcx>) -> Result<Option<Niche>, LayoutError<'tcx>> {
17491748
let scalar_niche = |scalar: &Scalar, offset| {
17501749
let Scalar { value, valid_range: ref v } = *scalar;
17511750

Diff for: src/librustc/ty/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'tcx> fmt::Display for Discr<'tcx> {
4343
match self.ty.sty {
4444
ty::Int(ity) => {
4545
let bits = ty::tls::with(|tcx| {
46-
Integer::from_attr(tcx, SignedInt(ity)).size().bits()
46+
Integer::from_attr(&tcx, SignedInt(ity)).size().bits()
4747
});
4848
let x = self.val as i128;
4949
// sign extend the raw representation to be an i128
@@ -62,8 +62,8 @@ impl<'tcx> Discr<'tcx> {
6262
}
6363
pub fn checked_add<'a, 'gcx>(self, tcx: TyCtxt<'a, 'gcx, 'tcx>, n: u128) -> (Self, bool) {
6464
let (int, signed) = match self.ty.sty {
65-
Int(ity) => (Integer::from_attr(tcx, SignedInt(ity)), true),
66-
Uint(uty) => (Integer::from_attr(tcx, UnsignedInt(uty)), false),
65+
Int(ity) => (Integer::from_attr(&tcx, SignedInt(ity)), true),
66+
Uint(uty) => (Integer::from_attr(&tcx, UnsignedInt(uty)), false),
6767
_ => bug!("non integer discriminant"),
6868
};
6969

0 commit comments

Comments
 (0)