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

+1-1
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

+2-2
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

+2-2
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

+1-1
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

+1-1
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

+5-5
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

+8-8
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

+4-4
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

+5-5
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

+1-1
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

compiler/rustc_const_eval/src/interpret/operand.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
561561
/// "universe" (param_env).
562562
pub fn const_to_op(
563563
&self,
564-
val: &ty::Const<'tcx>,
564+
val: ty::Const<'tcx>,
565565
layout: Option<TyAndLayout<'tcx>>,
566566
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
567-
match val.val {
567+
match val.val() {
568568
ty::ConstKind::Param(_) | ty::ConstKind::Bound(..) => throw_inval!(TooGeneric),
569569
ty::ConstKind::Error(_) => throw_inval!(AlreadyReported(ErrorReported)),
570570
ty::ConstKind::Unevaluated(uv) => {
@@ -574,7 +574,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
574574
ty::ConstKind::Infer(..) | ty::ConstKind::Placeholder(..) => {
575575
span_bug!(self.cur_span(), "const_to_op: Unexpected ConstKind {:?}", val)
576576
}
577-
ty::ConstKind::Value(val_val) => self.const_val_to_op(val_val, val.ty, layout),
577+
ty::ConstKind::Value(val_val) => self.const_val_to_op(val_val, val.ty(), layout),
578578
}
579579
}
580580

@@ -584,7 +584,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
584584
layout: Option<TyAndLayout<'tcx>>,
585585
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
586586
match val {
587-
mir::ConstantKind::Ty(ct) => self.const_to_op(ct, layout),
587+
mir::ConstantKind::Ty(ct) => self.const_to_op(*ct, layout),
588588
mir::ConstantKind::Val(val, ty) => self.const_val_to_op(*val, *ty, layout),
589589
}
590590
}

compiler/rustc_const_eval/src/interpret/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ where
5555
assert!(matches!(ty.kind(), ty::Param(_)))
5656
}
5757
ty::subst::GenericArgKind::Const(ct) => {
58-
assert!(matches!(ct.val, ty::ConstKind::Param(_)))
58+
assert!(matches!(ct.val(), ty::ConstKind::Param(_)))
5959
}
6060
ty::subst::GenericArgKind::Lifetime(..) => (),
6161
},
@@ -68,8 +68,8 @@ where
6868
}
6969
}
7070

71-
fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
72-
match c.val {
71+
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
72+
match c.val() {
7373
ty::ConstKind::Param(..) => ControlFlow::Break(FoundParam),
7474
_ => c.super_visit_with(self),
7575
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ where
355355

356356
// Check the qualifs of the value of `const` items.
357357
if let Some(ct) = constant.literal.const_for_ty() {
358-
if let ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs: _, promoted }) = ct.val {
358+
if let ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs: _, promoted }) = ct.val() {
359359
// Use qualifs of the type for the promoted. Promoteds in MIR body should be possible
360360
// only for `NeedsNonConstDrop` with precise drop checking. This is the only const
361361
// check performed after the promotion. Verify that with an assertion.

compiler/rustc_const_eval/src/transform/promote_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
839839
span,
840840
user_ty: None,
841841
literal: tcx
842-
.mk_const(ty::Const {
842+
.mk_const(ty::ConstS {
843843
ty,
844844
val: ty::ConstKind::Unevaluated(ty::Unevaluated {
845845
def,

compiler/rustc_infer/src/infer/at.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
299299
}
300300
}
301301

302-
impl<'tcx> ToTrace<'tcx> for &'tcx Const<'tcx> {
302+
impl<'tcx> ToTrace<'tcx> for Const<'tcx> {
303303
fn to_trace(
304304
_: TyCtxt<'tcx>,
305305
cause: &ObligationCause<'tcx>,

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
475475
}
476476
}
477477

478-
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
479-
match ct.val {
478+
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
479+
match ct.val() {
480480
ty::ConstKind::Infer(InferConst::Var(vid)) => {
481481
debug!("canonical: const var found with vid {:?}", vid);
482482
match self.infcx.probe_const_var(vid) {
@@ -493,7 +493,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
493493
ui = ty::UniverseIndex::ROOT;
494494
}
495495
return self.canonicalize_const_var(
496-
CanonicalVarInfo { kind: CanonicalVarKind::Const(ui, ct.ty) },
496+
CanonicalVarInfo { kind: CanonicalVarKind::Const(ui, ct.ty()) },
497497
ct,
498498
);
499499
}
@@ -769,17 +769,17 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
769769
fn canonicalize_const_var(
770770
&mut self,
771771
info: CanonicalVarInfo<'tcx>,
772-
const_var: &'tcx ty::Const<'tcx>,
773-
) -> &'tcx ty::Const<'tcx> {
772+
const_var: ty::Const<'tcx>,
773+
) -> ty::Const<'tcx> {
774774
let infcx = self.infcx;
775775
let bound_to = infcx.shallow_resolve(const_var);
776776
if bound_to != const_var {
777777
self.fold_const(bound_to)
778778
} else {
779779
let var = self.canonical_var(info, const_var.into());
780-
self.tcx().mk_const(ty::Const {
780+
self.tcx().mk_const(ty::ConstS {
781781
val: ty::ConstKind::Bound(self.binder_index, var),
782-
ty: self.fold_ty(const_var.ty),
782+
ty: self.fold_ty(const_var.ty()),
783783
})
784784
}
785785
}

compiler/rustc_infer/src/infer/canonical/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
149149
let universe_mapped = universe_map(universe);
150150
let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, name };
151151
self.tcx
152-
.mk_const(ty::Const {
152+
.mk_const(ty::ConstS {
153153
val: ty::ConstKind::Placeholder(placeholder_mapped),
154154
ty: name.ty,
155155
})

compiler/rustc_infer/src/infer/canonical/query_response.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,12 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
437437
}
438438
}
439439
GenericArgKind::Const(result_value) => {
440-
if let ty::Const { val: ty::ConstKind::Bound(debrujin, b), .. } = result_value {
440+
if let ty::ConstKind::Bound(debrujin, b) = result_value.val() {
441441
// ...in which case we would set `canonical_vars[0]` to `Some(const X)`.
442442

443443
// We only allow a `ty::INNERMOST` index in substitutions.
444-
assert_eq!(*debrujin, ty::INNERMOST);
445-
opt_values[*b] = Some(*original_value);
444+
assert_eq!(debrujin, ty::INNERMOST);
445+
opt_values[b] = Some(*original_value);
446446
}
447447
}
448448
}
@@ -670,7 +670,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> {
670670
});
671671
}
672672

673-
fn const_equate(&mut self, _a: &'tcx Const<'tcx>, _b: &'tcx Const<'tcx>) {
673+
fn const_equate(&mut self, _a: Const<'tcx>, _b: Const<'tcx>) {
674674
span_bug!(
675675
self.cause.span(self.infcx.tcx),
676676
"generic_const_exprs: unreachable `const_equate`"

0 commit comments

Comments
 (0)