Skip to content

Commit a95fb8b

Browse files
committed
Overhaul Const.
Specifically, rename the `Const` struct as `ConstS` and re-introduce `Const` as this: ``` pub struct Const<'tcx>(&'tcx Interned<ConstS>); ``` This now matches `Ty` and `Predicate` more closely, including using pointer-based `eq` and `hash`. Notable changes: - `mk_const` now takes a `ConstS`. - `Const` was copy, despite being 48 bytes. Now `ConstS` is not, so need a we need separate arena for it, because we can't use the `Dropless` one any more. - Many `&'tcx Const<'tcx>`/`&Const<'tcx>` to `Const<'tcx>` changes - Many `ct.ty` to `ct.ty()` and `ct.val` to `ct.val()` changes. - Lots of tedious sigil fiddling.
1 parent 7eb1550 commit a95fb8b

File tree

116 files changed

+653
-618
lines changed

Some content is hidden

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

116 files changed

+653
-618
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ fn check_opaque_type_parameter_valid(
198198
GenericArgKind::Lifetime(lt) => {
199199
matches!(*lt, ty::ReEarlyBound(_) | ty::ReFree(_))
200200
}
201-
GenericArgKind::Const(ct) => matches!(ct.val, ty::ConstKind::Param(_)),
201+
GenericArgKind::Const(ct) => matches!(ct.val(), ty::ConstKind::Param(_)),
202202
};
203203

204204
if arg_is_param {

compiler/rustc_borrowck/src/renumber.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> {
7777
debug!(?region);
7878
}
7979

80-
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, _location: Location) {
81-
*constant = self.renumber_regions(&*constant);
80+
fn visit_const(&mut self, constant: &mut ty::Const<'tcx>, _location: Location) {
81+
*constant = self.renumber_regions(*constant);
8282
}
8383
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
383383
} else {
384384
let tcx = self.tcx();
385385
let maybe_uneval = match constant.literal {
386-
ConstantKind::Ty(ct) => match ct.val {
386+
ConstantKind::Ty(ct) => match ct.val() {
387387
ty::ConstKind::Unevaluated(uv) => Some(uv),
388388
_ => None,
389389
},
@@ -1956,7 +1956,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19561956
fn check_operand(&mut self, op: &Operand<'tcx>, location: Location) {
19571957
if let Operand::Constant(constant) = op {
19581958
let maybe_uneval = match constant.literal {
1959-
ConstantKind::Ty(ct) => match ct.val {
1959+
ConstantKind::Ty(ct) => match ct.val() {
19601960
ty::ConstKind::Unevaluated(uv) => Some(uv),
19611961
_ => None,
19621962
},

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
117117

118118
// We don't have to worry about the equality of consts during borrow checking
119119
// as consts always have a static lifetime.
120-
fn const_equate(&mut self, _a: &'tcx Const<'tcx>, _b: &'tcx Const<'tcx>) {}
120+
fn const_equate(&mut self, _a: Const<'tcx>, _b: Const<'tcx>) {}
121121

122122
fn normalization() -> NormalizationStrategy {
123123
NormalizationStrategy::Eager

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ fn codegen_stmt<'tcx>(
668668
let times = fx
669669
.monomorphize(times)
670670
.eval(fx.tcx, ParamEnv::reveal_all())
671-
.val
671+
.val()
672672
.try_to_bits(fx.tcx.data_layout.pointer_size)
673673
.unwrap();
674674
if operand.layout().size.bytes() == 0 {

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
4646
ConstantKind::Ty(ct) => ct,
4747
ConstantKind::Val(..) => continue,
4848
};
49-
match const_.val {
49+
match const_.val() {
5050
ConstKind::Value(_) => {}
5151
ConstKind::Unevaluated(unevaluated) => {
5252
if let Err(err) =
@@ -127,15 +127,15 @@ pub(crate) fn codegen_constant<'tcx>(
127127
ConstantKind::Ty(ct) => ct,
128128
ConstantKind::Val(val, ty) => return codegen_const_value(fx, val, ty),
129129
};
130-
let const_val = match const_.val {
130+
let const_val = match const_.val() {
131131
ConstKind::Value(const_val) => const_val,
132132
ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
133133
if fx.tcx.is_static(def.did) =>
134134
{
135135
assert!(substs.is_empty());
136136
assert!(promoted.is_none());
137137

138-
return codegen_static_ref(fx, def.did, fx.layout_of(const_.ty)).to_cvalue(fx);
138+
return codegen_static_ref(fx, def.did, fx.layout_of(const_.ty())).to_cvalue(fx);
139139
}
140140
ConstKind::Unevaluated(unevaluated) => {
141141
match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) {
@@ -152,7 +152,7 @@ pub(crate) fn codegen_constant<'tcx>(
152152
| ConstKind::Error(_) => unreachable!("{:?}", const_),
153153
};
154154

155-
codegen_const_value(fx, const_val, const_.ty)
155+
codegen_const_value(fx, const_val, const_.ty())
156156
}
157157

158158
pub(crate) fn codegen_const_value<'tcx>(
@@ -465,7 +465,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
465465
match operand {
466466
Operand::Constant(const_) => match const_.literal {
467467
ConstantKind::Ty(const_) => {
468-
fx.monomorphize(const_).eval(fx.tcx, ParamEnv::reveal_all()).val.try_to_value()
468+
fx.monomorphize(const_).eval(fx.tcx, ParamEnv::reveal_all()).val().try_to_value()
469469
}
470470
ConstantKind::Val(val, _) => Some(val),
471471
},

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ fn push_debuginfo_type_name<'tcx>(
146146
if cpp_like_debuginfo {
147147
output.push_str("array$<");
148148
push_debuginfo_type_name(tcx, inner_type, true, output, visited);
149-
match len.val {
149+
match len.val() {
150150
ty::ConstKind::Param(param) => write!(output, ",{}>", param.name).unwrap(),
151151
_ => write!(output, ",{}>", len.eval_usize(tcx, ty::ParamEnv::reveal_all()))
152152
.unwrap(),
153153
}
154154
} else {
155155
output.push('[');
156156
push_debuginfo_type_name(tcx, inner_type, true, output, visited);
157-
match len.val {
157+
match len.val() {
158158
ty::ConstKind::Param(param) => write!(output, "; {}]", param.name).unwrap(),
159159
_ => write!(output, "; {}]", len.eval_usize(tcx, ty::ParamEnv::reveal_all()))
160160
.unwrap(),
@@ -645,19 +645,19 @@ fn push_generic_params_internal<'tcx>(
645645
true
646646
}
647647

648-
fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: &'tcx ty::Const<'tcx>, output: &mut String) {
649-
match ct.val {
648+
fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut String) {
649+
match ct.val() {
650650
ty::ConstKind::Param(param) => {
651651
write!(output, "{}", param.name)
652652
}
653-
_ => match ct.ty.kind() {
653+
_ => match ct.ty().kind() {
654654
ty::Int(ity) => {
655-
let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty);
655+
let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty());
656656
let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
657657
write!(output, "{}", val)
658658
}
659659
ty::Uint(_) => {
660-
let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty);
660+
let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty());
661661
write!(output, "{}", val)
662662
}
663663
ty::Bool => {
@@ -672,7 +672,7 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: &'tcx ty::Const<'tcx>, output:
672672
let mut hasher = StableHasher::new();
673673
hcx.while_hashing_spans(false, |hcx| {
674674
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
675-
ct.val.hash_stable(hcx, &mut hasher);
675+
ct.val().hash_stable(hcx, &mut hasher);
676676
});
677677
});
678678
// Let's only emit 64 bits of the hash value. That should be plenty for

compiler/rustc_codegen_ssa/src/mir/constant.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2929
mir::ConstantKind::Ty(ct) => ct,
3030
mir::ConstantKind::Val(val, _) => return Ok(val),
3131
};
32-
match ct.val {
32+
match ct.val() {
3333
ty::ConstKind::Unevaluated(ct) => self
3434
.cx
3535
.tcx()
@@ -61,11 +61,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6161
let c = ty::Const::from_value(bx.tcx(), val, ty);
6262
let values: Vec<_> = bx
6363
.tcx()
64-
.destructure_const(ty::ParamEnv::reveal_all().and(&c))
64+
.destructure_const(ty::ParamEnv::reveal_all().and(c))
6565
.fields
6666
.iter()
6767
.map(|field| {
68-
if let Some(prim) = field.val.try_to_scalar() {
68+
if let Some(prim) = field.val().try_to_scalar() {
6969
let layout = bx.layout_of(field_ty);
7070
let scalar = match layout.abi {
7171
Abi::Scalar(x) => x,
@@ -78,7 +78,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
7878
})
7979
.collect();
8080
let llval = bx.const_struct(&values, false);
81-
(llval, c.ty)
81+
(llval, c.ty())
8282
})
8383
.unwrap_or_else(|_| {
8484
bx.tcx().sess.span_err(span, "could not evaluate shuffle_indices at compile time");

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ fn const_to_valtree_inner<'tcx>(
139139
pub(crate) fn destructure_const<'tcx>(
140140
tcx: TyCtxt<'tcx>,
141141
param_env: ty::ParamEnv<'tcx>,
142-
val: &'tcx ty::Const<'tcx>,
142+
val: ty::Const<'tcx>,
143143
) -> mir::DestructuredConst<'tcx> {
144144
trace!("destructure_const: {:?}", val);
145145
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
146146
let op = ecx.const_to_op(val, None).unwrap();
147147

148148
// We go to `usize` as we cannot allocate anything bigger anyway.
149-
let (field_count, variant, down) = match val.ty.kind() {
149+
let (field_count, variant, down) = match val.ty().kind() {
150150
ty::Array(_, len) => (usize::try_from(len.eval_usize(tcx, param_env)).unwrap(), None, op),
151151
ty::Adt(def, _) if def.variants.is_empty() => {
152152
return mir::DestructuredConst { variant: None, fields: &[] };
@@ -173,8 +173,8 @@ pub(crate) fn destructure_const<'tcx>(
173173
pub(crate) fn deref_const<'tcx>(
174174
tcx: TyCtxt<'tcx>,
175175
param_env: ty::ParamEnv<'tcx>,
176-
val: &'tcx ty::Const<'tcx>,
177-
) -> &'tcx ty::Const<'tcx> {
176+
val: ty::Const<'tcx>,
177+
) -> ty::Const<'tcx> {
178178
trace!("deref_const: {:?}", val);
179179
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
180180
let op = ecx.const_to_op(val, None).unwrap();
@@ -203,5 +203,5 @@ pub(crate) fn deref_const<'tcx>(
203203
},
204204
};
205205

206-
tcx.mk_const(ty::Const { val: ty::ConstKind::Value(op_to_const(&ecx, &mplace.into())), ty })
206+
tcx.mk_const(ty::ConstS { val: ty::ConstKind::Value(op_to_const(&ecx, &mplace.into())), ty })
207207
}

compiler/rustc_const_eval/src/interpret/intrinsics/type_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
6868
}
6969
}
7070

71-
fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
71+
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
7272
self.pretty_print_const(ct, false)
7373
}
7474

0 commit comments

Comments
 (0)