Skip to content

Commit e0098a5

Browse files
committed
Auto merge of #105012 - WaffleLapkin:into, r=oli-obk
Make `tcx.mk_const` more permissive wrt `kind` argument (`impl Into`) r? `@oli-obk` you've asked for this >:)
2 parents c372b14 + a44eb3c commit e0098a5

File tree

20 files changed

+86
-98
lines changed

20 files changed

+86
-98
lines changed

Cargo.lock

+20
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,12 @@ dependencies = [
870870
"memchr",
871871
]
872872

873+
[[package]]
874+
name = "convert_case"
875+
version = "0.4.0"
876+
source = "registry+https://github.com/rust-lang/crates.io-index"
877+
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
878+
873879
[[package]]
874880
name = "core"
875881
version = "0.0.0"
@@ -1060,6 +1066,19 @@ dependencies = [
10601066
"syn",
10611067
]
10621068

1069+
[[package]]
1070+
name = "derive_more"
1071+
version = "0.99.17"
1072+
source = "registry+https://github.com/rust-lang/crates.io-index"
1073+
checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
1074+
dependencies = [
1075+
"convert_case",
1076+
"proc-macro2",
1077+
"quote",
1078+
"rustc_version",
1079+
"syn",
1080+
]
1081+
10631082
[[package]]
10641083
name = "diff"
10651084
version = "0.1.13"
@@ -3979,6 +3998,7 @@ version = "0.0.0"
39793998
dependencies = [
39803999
"bitflags",
39814000
"chalk-ir",
4001+
"derive_more",
39824002
"either",
39834003
"gsgdt",
39844004
"polonius-engine",

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'tcx> InferCtxt<'tcx> {
147147
CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { universe, name }, ty) => {
148148
let universe_mapped = universe_map(universe);
149149
let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, name };
150-
self.tcx.mk_const(ty::ConstKind::Placeholder(placeholder_mapped), ty).into()
150+
self.tcx.mk_const(placeholder_mapped, ty).into()
151151
}
152152
}
153153
}

compiler/rustc_infer/src/infer/combine.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
753753
origin: var_value.origin,
754754
val: ConstVariableValue::Unknown { universe: self.for_universe },
755755
});
756-
Ok(self.tcx().mk_const_var(new_var_id, c.ty()))
756+
Ok(self.tcx().mk_const(new_var_id, c.ty()))
757757
}
758758
}
759759
}
@@ -765,10 +765,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
765765
substs,
766766
substs,
767767
)?;
768-
Ok(self.tcx().mk_const(
769-
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }),
770-
c.ty(),
771-
))
768+
Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty()))
772769
}
773770
_ => relate::super_relate_consts(self, c, c),
774771
}
@@ -975,7 +972,7 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
975972
},
976973
},
977974
);
978-
Ok(self.tcx().mk_const_var(new_var_id, c.ty()))
975+
Ok(self.tcx().mk_const(new_var_id, c.ty()))
979976
}
980977
}
981978
}
@@ -988,10 +985,7 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
988985
substs,
989986
)?;
990987

991-
Ok(self.tcx().mk_const(
992-
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }),
993-
c.ty(),
994-
))
988+
Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty()))
995989
}
996990
_ => relate::super_relate_consts(self, c, c),
997991
}

compiler/rustc_infer/src/infer/freshen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
102102
Entry::Vacant(entry) => {
103103
let index = self.const_freshen_count;
104104
self.const_freshen_count += 1;
105-
let ct = self.infcx.tcx.mk_const_infer(freshener(index), ty);
105+
let ct = self.infcx.tcx.mk_const(freshener(index), ty);
106106
entry.insert(ct);
107107
ct
108108
}

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

+2-7
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,8 @@ impl<'tcx> InferCtxt<'tcx> {
9494
}))
9595
},
9696
consts: &mut |bound_var: ty::BoundVar, ty| {
97-
self.tcx.mk_const(
98-
ty::ConstKind::Placeholder(ty::PlaceholderConst {
99-
universe: next_universe,
100-
name: bound_var,
101-
}),
102-
ty,
103-
)
97+
self.tcx
98+
.mk_const(ty::PlaceholderConst { universe: next_universe, name: bound_var }, ty)
10499
},
105100
};
106101

compiler/rustc_infer/src/infer/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ impl<'tcx> InferCtxt<'tcx> {
10651065
}
10661066

10671067
pub fn next_const_var(&self, ty: Ty<'tcx>, origin: ConstVariableOrigin) -> ty::Const<'tcx> {
1068-
self.tcx.mk_const_var(self.next_const_var_id(origin), ty)
1068+
self.tcx.mk_const(self.next_const_var_id(origin), ty)
10691069
}
10701070

10711071
pub fn next_const_var_in_universe(
@@ -1079,7 +1079,7 @@ impl<'tcx> InferCtxt<'tcx> {
10791079
.borrow_mut()
10801080
.const_unification_table()
10811081
.new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } });
1082-
self.tcx.mk_const_var(vid, ty)
1082+
self.tcx.mk_const(vid, ty)
10831083
}
10841084

10851085
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx> {
@@ -1195,7 +1195,7 @@ impl<'tcx> InferCtxt<'tcx> {
11951195
origin,
11961196
val: ConstVariableValue::Unknown { universe: self.universe() },
11971197
});
1198-
self.tcx.mk_const_var(const_var_id, self.tcx.type_of(param.def_id)).into()
1198+
self.tcx.mk_const(const_var_id, self.tcx.type_of(param.def_id)).into()
11991199
}
12001200
}
12011201
}
@@ -1580,7 +1580,7 @@ impl<'tcx> InferCtxt<'tcx> {
15801580
span: Option<Span>,
15811581
) -> Result<ty::Const<'tcx>, ErrorHandled> {
15821582
match self.const_eval_resolve(param_env, unevaluated, span) {
1583-
Ok(Some(val)) => Ok(ty::Const::from_value(self.tcx, val, ty)),
1583+
Ok(Some(val)) => Ok(self.tcx.mk_const(val, ty)),
15841584
Ok(None) => {
15851585
let tcx = self.tcx;
15861586
let def_id = unevaluated.def.did;
@@ -2049,10 +2049,10 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
20492049
bug!("const `{ct}`'s type should not reference params or types");
20502050
}
20512051
tcx.mk_const(
2052-
ty::ConstKind::Placeholder(ty::PlaceholderConst {
2052+
ty::PlaceholderConst {
20532053
universe: ty::UniverseIndex::ROOT,
20542054
name: ty::BoundVar::from_usize(idx),
2055-
}),
2055+
},
20562056
ty,
20572057
)
20582058
.into()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ where
10871087
origin: var_value.origin,
10881088
val: ConstVariableValue::Unknown { universe: self.universe },
10891089
});
1090-
Ok(self.tcx().mk_const_var(new_var_id, a.ty()))
1090+
Ok(self.tcx().mk_const(new_var_id, a.ty()))
10911091
}
10921092
}
10931093
}

compiler/rustc_middle/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = "2021"
88
[dependencies]
99
bitflags = "1.2.1"
1010
chalk-ir = "0.87.0"
11+
derive_more = "0.99.17"
1112
either = "1.5.0"
1213
gsgdt = "0.1.2"
1314
polonius-engine = "0.13.0"

compiler/rustc_middle/src/mir/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2527,8 +2527,7 @@ impl<'tcx> ConstantKind<'tcx> {
25272527
let generics = tcx.generics_of(item_def_id);
25282528
let index = generics.param_def_id_to_index[&def_id];
25292529
let name = tcx.item_name(def_id);
2530-
let ty_const =
2531-
tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty);
2530+
let ty_const = tcx.mk_const(ty::ParamConst::new(index, name), ty);
25322531
debug!(?ty_const);
25332532

25342533
return Self::Ty(ty_const);

compiler/rustc_middle/src/ty/consts.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ impl<'tcx> Const<'tcx> {
7676
match Self::try_eval_lit_or_param(tcx, ty, expr) {
7777
Some(v) => v,
7878
None => tcx.mk_const(
79-
ty::ConstKind::Unevaluated(ty::UnevaluatedConst {
79+
ty::UnevaluatedConst {
8080
def: def.to_global(),
8181
substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
82-
}),
82+
},
8383
ty,
8484
),
8585
}
@@ -134,18 +134,12 @@ impl<'tcx> Const<'tcx> {
134134
let generics = tcx.generics_of(item_def_id);
135135
let index = generics.param_def_id_to_index[&def_id];
136136
let name = tcx.item_name(def_id);
137-
Some(tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty))
137+
Some(tcx.mk_const(ty::ParamConst::new(index, name), ty))
138138
}
139139
_ => None,
140140
}
141141
}
142142

143-
/// Interns the given value as a constant.
144-
#[inline]
145-
pub fn from_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Self {
146-
tcx.mk_const(ConstKind::Value(val), ty)
147-
}
148-
149143
/// Panics if self.kind != ty::ConstKind::Value
150144
pub fn to_valtree(self) -> ty::ValTree<'tcx> {
151145
match self.kind() {
@@ -154,26 +148,23 @@ impl<'tcx> Const<'tcx> {
154148
}
155149
}
156150

157-
pub fn from_scalar_int(tcx: TyCtxt<'tcx>, i: ScalarInt, ty: Ty<'tcx>) -> Self {
158-
let valtree = ty::ValTree::from_scalar_int(i);
159-
Self::from_value(tcx, valtree, ty)
160-
}
161-
162151
#[inline]
163152
/// Creates a constant with the given integer value and interns it.
164153
pub fn from_bits(tcx: TyCtxt<'tcx>, bits: u128, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Self {
165154
let size = tcx
166155
.layout_of(ty)
167156
.unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
168157
.size;
169-
Self::from_scalar_int(tcx, ScalarInt::try_from_uint(bits, size).unwrap(), ty.value)
158+
tcx.mk_const(
159+
ty::ValTree::from_scalar_int(ScalarInt::try_from_uint(bits, size).unwrap()),
160+
ty.value,
161+
)
170162
}
171163

172164
#[inline]
173165
/// Creates an interned zst constant.
174166
pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
175-
let valtree = ty::ValTree::zst();
176-
Self::from_value(tcx, valtree, ty)
167+
tcx.mk_const(ty::ValTree::zst(), ty)
177168
}
178169

179170
#[inline]
@@ -220,7 +211,7 @@ impl<'tcx> Const<'tcx> {
220211
pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Const<'tcx> {
221212
if let Some(val) = self.kind().try_eval_for_typeck(tcx, param_env) {
222213
match val {
223-
Ok(val) => Const::from_value(tcx, val, self.ty()),
214+
Ok(val) => tcx.mk_const(val, self.ty()),
224215
Err(guar) => tcx.const_error_with_guaranteed(self.ty(), guar),
225216
}
226217
} else {

compiler/rustc_middle/src/ty/consts/kind.rs

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl<'tcx> UnevaluatedConst<'tcx> {
4949
/// Represents a constant in Rust.
5050
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
5151
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
52+
#[derive(derive_more::From)]
5253
pub enum ConstKind<'tcx> {
5354
/// A const generic parameter.
5455
Param(ty::ParamConst),
@@ -71,12 +72,19 @@ pub enum ConstKind<'tcx> {
7172

7273
/// A placeholder for a const which could not be computed; this is
7374
/// propagated to avoid useless error messages.
75+
#[from(ignore)]
7476
Error(ErrorGuaranteed),
7577

7678
/// Expr which contains an expression which has partially evaluated items.
7779
Expr(Expr<'tcx>),
7880
}
7981

82+
impl<'tcx> From<ty::ConstVid<'tcx>> for ConstKind<'tcx> {
83+
fn from(const_vid: ty::ConstVid<'tcx>) -> Self {
84+
InferConst::Var(const_vid).into()
85+
}
86+
}
87+
8088
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
8189
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
8290
pub enum Expr<'tcx> {

compiler/rustc_middle/src/ty/context.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::traits;
1717
use crate::ty::query::{self, TyCtxtAt};
1818
use crate::ty::{
1919
self, AdtDef, AdtDefData, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig,
20-
ClosureSizeProfileData, Const, ConstS, ConstVid, DefIdTree, FloatTy, FloatVar, FloatVid,
21-
GenericParamDefKind, InferConst, InferTy, IntTy, IntVar, IntVid, List, ParamConst, ParamTy,
20+
ClosureSizeProfileData, Const, ConstS, DefIdTree, FloatTy, FloatVar, FloatVid,
21+
GenericParamDefKind, InferTy, IntTy, IntVar, IntVid, List, ParamConst, ParamTy,
2222
PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, PredicateS, ProjectionTy,
2323
Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut,
2424
UintTy, Visibility,
@@ -2604,13 +2604,8 @@ impl<'tcx> TyCtxt<'tcx> {
26042604
}
26052605

26062606
#[inline]
2607-
pub fn mk_const(self, kind: ty::ConstKind<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
2608-
self.mk_const_internal(ty::ConstS { kind, ty })
2609-
}
2610-
2611-
#[inline]
2612-
pub fn mk_const_var(self, v: ConstVid<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
2613-
self.mk_const(ty::ConstKind::Infer(InferConst::Var(v)), ty)
2607+
pub fn mk_const(self, kind: impl Into<ty::ConstKind<'tcx>>, ty: Ty<'tcx>) -> Const<'tcx> {
2608+
self.mk_const_internal(ty::ConstS { kind: kind.into(), ty })
26142609
}
26152610

26162611
#[inline]
@@ -2628,30 +2623,23 @@ impl<'tcx> TyCtxt<'tcx> {
26282623
self.mk_ty(Infer(it))
26292624
}
26302625

2631-
#[inline]
2632-
pub fn mk_const_infer(self, ic: InferConst<'tcx>, ty: Ty<'tcx>) -> ty::Const<'tcx> {
2633-
self.mk_const(ty::ConstKind::Infer(ic), ty)
2634-
}
2635-
26362626
#[inline]
26372627
pub fn mk_ty_param(self, index: u32, name: Symbol) -> Ty<'tcx> {
26382628
self.mk_ty(Param(ParamTy { index, name }))
26392629
}
26402630

2641-
#[inline]
2642-
pub fn mk_const_param(self, index: u32, name: Symbol, ty: Ty<'tcx>) -> Const<'tcx> {
2643-
self.mk_const(ty::ConstKind::Param(ParamConst { index, name }), ty)
2644-
}
2645-
26462631
pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
26472632
match param.kind {
26482633
GenericParamDefKind::Lifetime => {
26492634
self.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())).into()
26502635
}
26512636
GenericParamDefKind::Type { .. } => self.mk_ty_param(param.index, param.name).into(),
2652-
GenericParamDefKind::Const { .. } => {
2653-
self.mk_const_param(param.index, param.name, self.type_of(param.def_id)).into()
2654-
}
2637+
GenericParamDefKind::Const { .. } => self
2638+
.mk_const(
2639+
ParamConst { index: param.index, name: param.name },
2640+
self.type_of(param.def_id),
2641+
)
2642+
.into(),
26552643
}
26562644
}
26572645

compiler/rustc_middle/src/ty/print/pretty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1468,8 +1468,7 @@ pub trait PrettyPrinter<'tcx>:
14681468
}
14691469
// Aggregates, printed as array/tuple/struct/variant construction syntax.
14701470
(ty::ValTree::Branch(_), ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) => {
1471-
let contents =
1472-
self.tcx().destructure_const(ty::Const::from_value(self.tcx(), valtree, ty));
1471+
let contents = self.tcx().destructure_const(self.tcx().mk_const(valtree, ty));
14731472
let fields = contents.fields.iter().copied();
14741473
match *ty.kind() {
14751474
ty::Array(..) => {

compiler/rustc_middle/src/ty/relate.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -663,10 +663,7 @@ pub fn super_relate_consts<'tcx, R: TypeRelation<'tcx>>(
663663
au.substs,
664664
bu.substs,
665665
)?;
666-
return Ok(tcx.mk_const(
667-
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def: au.def, substs }),
668-
a.ty(),
669-
));
666+
return Ok(tcx.mk_const(ty::UnevaluatedConst { def: au.def, substs }, a.ty()));
670667
}
671668
// Before calling relate on exprs, it is necessary to ensure that the nested consts
672669
// have identical types.

compiler/rustc_mir_build/src/build/expr/as_constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7777
Constant { user_ty, span, literal }
7878
}
7979
ExprKind::ConstParam { param, def_id: _ } => {
80-
let const_param = tcx.mk_const(ty::ConstKind::Param(param), expr.ty);
80+
let const_param = tcx.mk_const(param, expr.ty);
8181
let literal = ConstantKind::Ty(const_param);
8282

8383
Constant { user_ty: None, span, literal }

compiler/rustc_mir_build/src/thir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,5 @@ pub(crate) fn lit_to_const<'tcx>(
6161
_ => return Err(LitToConstError::TypeError),
6262
};
6363

64-
Ok(ty::Const::from_value(tcx, valtree, ty))
64+
Ok(tcx.mk_const(valtree, ty))
6565
}

0 commit comments

Comments
 (0)