Make tcx.mk_const more permissive wrt kind argument

- Accept `impl Into`
- Implement `From<>` for `ConstKind`

Note: this adds a dependency on `derive_more` (MIT license). It allows
to derive a lot of traits (like `From` here) that would be otherwise
tedious to implement.
This commit is contained in:
Maybe Waffle 2022-11-28 11:11:45 +00:00
parent 8a09420ac4
commit e20e506f7d
4 changed files with 31 additions and 2 deletions

View File

@ -870,6 +870,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "convert_case"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
[[package]]
name = "core"
version = "0.0.0"
@ -1060,6 +1066,19 @@ dependencies = [
"syn",
]
[[package]]
name = "derive_more"
version = "0.99.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
dependencies = [
"convert_case",
"proc-macro2",
"quote",
"rustc_version",
"syn",
]
[[package]]
name = "diff"
version = "0.1.13"
@ -3979,6 +3998,7 @@ version = "0.0.0"
dependencies = [
"bitflags",
"chalk-ir",
"derive_more",
"either",
"gsgdt",
"polonius-engine",

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
bitflags = "1.2.1"
chalk-ir = "0.87.0"
derive_more = "0.99.17"
either = "1.5.0"
gsgdt = "0.1.2"
polonius-engine = "0.13.0"

View File

@ -49,6 +49,7 @@ impl<'tcx> UnevaluatedConst<'tcx> {
/// Represents a constant in Rust.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
#[derive(derive_more::From)]
pub enum ConstKind<'tcx> {
/// A const generic parameter.
Param(ty::ParamConst),
@ -71,12 +72,19 @@ pub enum ConstKind<'tcx> {
/// A placeholder for a const which could not be computed; this is
/// propagated to avoid useless error messages.
#[from(ignore)]
Error(ErrorGuaranteed),
/// Expr which contains an expression which has partially evaluated items.
Expr(Expr<'tcx>),
}
impl<'tcx> From<ty::ConstVid<'tcx>> for ConstKind<'tcx> {
fn from(const_vid: ty::ConstVid<'tcx>) -> Self {
InferConst::Var(const_vid).into()
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
pub enum Expr<'tcx> {

View File

@ -2598,8 +2598,8 @@ impl<'tcx> TyCtxt<'tcx> {
}
#[inline]
pub fn mk_const(self, kind: ty::ConstKind<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
self.mk_const_internal(ty::ConstS { kind, ty })
pub fn mk_const(self, kind: impl Into<ty::ConstKind<'tcx>>, ty: Ty<'tcx>) -> Const<'tcx> {
self.mk_const_internal(ty::ConstS { kind: kind.into(), ty })
}
#[inline]