Skip to content

Commit a9d3cd1

Browse files
committed
Make Const::val() return a ref, to avoid copies.
The idea comes from the last commit of #90951.
1 parent 523a1b1 commit a9d3cd1

File tree

36 files changed

+64
-65
lines changed

36 files changed

+64
-65
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

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

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
4848
};
4949
match const_.val() {
5050
ConstKind::Value(_) => {}
51-
ConstKind::Unevaluated(unevaluated) => {
51+
&ConstKind::Unevaluated(unevaluated) => {
5252
if let Err(err) =
5353
fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None)
5454
{
@@ -128,7 +128,7 @@ pub(crate) fn codegen_constant<'tcx>(
128128
ConstantKind::Val(val, ty) => return codegen_const_value(fx, val, ty),
129129
};
130130
let const_val = match const_.val() {
131-
ConstKind::Value(const_val) => const_val,
131+
&ConstKind::Value(const_val) => const_val,
132132
ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
133133
if fx.tcx.is_static(def.did) =>
134134
{
@@ -137,7 +137,7 @@ pub(crate) fn codegen_constant<'tcx>(
137137

138138
return codegen_static_ref(fx, def.did, fx.layout_of(const_.ty())).to_cvalue(fx);
139139
}
140-
ConstKind::Unevaluated(unevaluated) => {
140+
&ConstKind::Unevaluated(unevaluated) => {
141141
match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) {
142142
Ok(const_val) => const_val,
143143
Err(_) => {

compiler/rustc_codegen_ssa/src/mir/constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3030
mir::ConstantKind::Val(val, _) => return Ok(val),
3131
};
3232
match ct.val() {
33-
ty::ConstKind::Unevaluated(ct) => self
33+
&ty::ConstKind::Unevaluated(ct) => self
3434
.cx
3535
.tcx()
3636
.const_eval_resolve(ty::ParamEnv::reveal_all(), ct, None)
3737
.map_err(|err| {
3838
self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered");
3939
err
4040
}),
41-
ty::ConstKind::Value(value) => Ok(value),
41+
&ty::ConstKind::Value(value) => Ok(value),
4242
err => span_bug!(
4343
constant.span,
4444
"encountered bad ConstKind after monomorphizing: {:?}",

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
477477

478478
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
479479
match ct.val() {
480-
ty::ConstKind::Infer(InferConst::Var(vid)) => {
480+
&ty::ConstKind::Infer(InferConst::Var(vid)) => {
481481
debug!("canonical: const var found with vid {:?}", vid);
482482
match self.infcx.probe_const_var(vid) {
483483
Ok(c) => {
@@ -502,14 +502,14 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
502502
ty::ConstKind::Infer(InferConst::Fresh(_)) => {
503503
bug!("encountered a fresh const during canonicalization")
504504
}
505-
ty::ConstKind::Bound(debruijn, _) => {
505+
&ty::ConstKind::Bound(debruijn, _) => {
506506
if debruijn >= self.binder_index {
507507
bug!("escaping bound type during canonicalization")
508508
} else {
509509
return ct;
510510
}
511511
}
512-
ty::ConstKind::Placeholder(placeholder) => {
512+
&ty::ConstKind::Placeholder(placeholder) => {
513513
return self.canonicalize_const_var(
514514
CanonicalVarInfo { kind: CanonicalVarKind::PlaceholderConst(placeholder) },
515515
ct,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
437437
}
438438
}
439439
GenericArgKind::Const(result_value) => {
440-
if let ty::ConstKind::Bound(debrujin, b) = result_value.val() {
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.

compiler/rustc_infer/src/infer/combine.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
141141

142142
match (a.val(), b.val()) {
143143
(
144-
ty::ConstKind::Infer(InferConst::Var(a_vid)),
145-
ty::ConstKind::Infer(InferConst::Var(b_vid)),
144+
&ty::ConstKind::Infer(InferConst::Var(a_vid)),
145+
&ty::ConstKind::Infer(InferConst::Var(b_vid)),
146146
) => {
147147
self.inner
148148
.borrow_mut()
@@ -158,11 +158,11 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
158158
bug!("tried to combine ConstKind::Infer/ConstKind::Infer(InferConst::Var)")
159159
}
160160

161-
(ty::ConstKind::Infer(InferConst::Var(vid)), _) => {
161+
(&ty::ConstKind::Infer(InferConst::Var(vid)), _) => {
162162
return self.unify_const_variable(relation.param_env(), vid, b, a_is_expected);
163163
}
164164

165-
(_, ty::ConstKind::Infer(InferConst::Var(vid))) => {
165+
(_, &ty::ConstKind::Infer(InferConst::Var(vid))) => {
166166
return self.unify_const_variable(relation.param_env(), vid, a, !a_is_expected);
167167
}
168168
(ty::ConstKind::Unevaluated(..), _) if self.tcx.lazy_normalization() => {
@@ -722,7 +722,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
722722
assert_eq!(c, c2); // we are abusing TypeRelation here; both LHS and RHS ought to be ==
723723

724724
match c.val() {
725-
ty::ConstKind::Infer(InferConst::Var(vid)) => {
725+
&ty::ConstKind::Infer(InferConst::Var(vid)) => {
726726
let mut inner = self.infcx.inner.borrow_mut();
727727
let variable_table = &mut inner.const_unification_table();
728728
let var_value = variable_table.probe_value(vid);
@@ -744,7 +744,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
744744
}
745745
}
746746
}
747-
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
747+
&ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
748748
if self.tcx().lazy_normalization() =>
749749
{
750750
assert_eq!(promoted, None);
@@ -952,7 +952,7 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
952952
debug!("ConstInferUnifier: c={:?}", c);
953953

954954
match c.val() {
955-
ty::ConstKind::Infer(InferConst::Var(vid)) => {
955+
&ty::ConstKind::Infer(InferConst::Var(vid)) => {
956956
// Check if the current unification would end up
957957
// unifying `target_vid` with a const which contains
958958
// an inference variable which is unioned with `target_vid`.
@@ -990,7 +990,7 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
990990
}
991991
}
992992
}
993-
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
993+
&ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
994994
if self.tcx().lazy_normalization() =>
995995
{
996996
assert_eq!(promoted, None);

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
410410
}
411411
GenericArgKind::Const(ct) => {
412412
match ct.val() {
413-
ty::ConstKind::Infer(InferConst::Var(vid)) => {
413+
&ty::ConstKind::Infer(InferConst::Var(vid)) => {
414414
let origin = self
415415
.inner
416416
.borrow_mut()

compiler/rustc_infer/src/infer/freshen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
223223

224224
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
225225
match ct.val() {
226-
ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
226+
&ty::ConstKind::Infer(ty::InferConst::Var(v)) => {
227227
let opt_ct = self
228228
.infcx
229229
.inner
@@ -239,7 +239,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
239239
ct.ty(),
240240
);
241241
}
242-
ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => {
242+
&ty::ConstKind::Infer(ty::InferConst::Fresh(i)) => {
243243
if i >= self.const_freshen_count {
244244
bug!(
245245
"Encountered a freshend const with id {} \

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ impl<'tcx> TyOrConstInferVar<'tcx> {
17611761
/// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
17621762
pub fn maybe_from_const(ct: ty::Const<'tcx>) -> Option<Self> {
17631763
match ct.val() {
1764-
ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
1764+
&ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
17651765
_ => None,
17661766
}
17671767
}
@@ -1781,7 +1781,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
17811781
}
17821782

17831783
fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1784-
if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val() {
1784+
if let &ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val() {
17851785
self.infcx
17861786
.inner
17871787
.borrow_mut()

0 commit comments

Comments
 (0)