From e20e506f7df271e933827a2290c0e0a704146443 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 28 Nov 2022 11:11:45 +0000 Subject: [PATCH] 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. --- Cargo.lock | 20 ++++++++++++++++++++ compiler/rustc_middle/Cargo.toml | 1 + compiler/rustc_middle/src/ty/consts/kind.rs | 8 ++++++++ compiler/rustc_middle/src/ty/context.rs | 4 ++-- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dbf1e06ee6e9..7d43dbc9e064 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/compiler/rustc_middle/Cargo.toml b/compiler/rustc_middle/Cargo.toml index fc1167c105ae..cf1ab47de861 100644 --- a/compiler/rustc_middle/Cargo.toml +++ b/compiler/rustc_middle/Cargo.toml @@ -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" diff --git a/compiler/rustc_middle/src/ty/consts/kind.rs b/compiler/rustc_middle/src/ty/consts/kind.rs index de63dae8a3df..becc2b805dd1 100644 --- a/compiler/rustc_middle/src/ty/consts/kind.rs +++ b/compiler/rustc_middle/src/ty/consts/kind.rs @@ -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> 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> { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index bf30a403d9b9..f422fdcc9aef 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -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: Ty<'tcx>) -> Const<'tcx> { + self.mk_const_internal(ty::ConstS { kind: kind.into(), ty }) } #[inline]