Move LitToConstInput into ty::consts
Relocate LitToConstInput and const_lit_matches_ty from mir::interpret to ty::consts::lit
This commit is contained in:
parent
b4ee9953d8
commit
01d48c62a5
12 changed files with 77 additions and 70 deletions
|
|
@ -13,7 +13,7 @@ use std::num::NonZero;
|
|||
use std::{fmt, io};
|
||||
|
||||
use rustc_abi::{AddressSpace, Align, Endian, HasDataLayout, Size};
|
||||
use rustc_ast::{LitKind, Mutability};
|
||||
use rustc_ast::Mutability;
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_data_structures::sharded::ShardedHashMap;
|
||||
use rustc_data_structures::sync::{AtomicU64, Lock};
|
||||
|
|
@ -73,55 +73,6 @@ impl<'tcx> GlobalId<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Input argument for `tcx.lit_to_const`.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
|
||||
pub struct LitToConstInput<'tcx> {
|
||||
/// The absolute value of the resultant constant.
|
||||
pub lit: LitKind,
|
||||
/// The type of the constant.
|
||||
pub ty: Ty<'tcx>,
|
||||
/// If the constant is negative.
|
||||
pub neg: bool,
|
||||
}
|
||||
|
||||
pub fn const_lit_matches_ty<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
kind: &LitKind,
|
||||
ty: Ty<'tcx>,
|
||||
neg: bool,
|
||||
) -> bool {
|
||||
match (*kind, ty.kind()) {
|
||||
(LitKind::Str(..), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => true,
|
||||
(LitKind::Str(..), ty::Str) if tcx.features().deref_patterns() => true,
|
||||
(LitKind::ByteStr(..), ty::Ref(_, inner_ty, _))
|
||||
if let ty::Slice(ty) | ty::Array(ty, _) = inner_ty.kind()
|
||||
&& matches!(ty.kind(), ty::Uint(ty::UintTy::U8)) =>
|
||||
{
|
||||
true
|
||||
}
|
||||
(LitKind::ByteStr(..), ty::Slice(inner_ty) | ty::Array(inner_ty, _))
|
||||
if tcx.features().deref_patterns()
|
||||
&& matches!(inner_ty.kind(), ty::Uint(ty::UintTy::U8)) =>
|
||||
{
|
||||
true
|
||||
}
|
||||
(LitKind::Byte(..), ty::Uint(ty::UintTy::U8)) => true,
|
||||
(LitKind::CStr(..), ty::Ref(_, inner_ty, _))
|
||||
if matches!(inner_ty.kind(), ty::Adt(def, _)
|
||||
if tcx.is_lang_item(def.did(), rustc_hir::LangItem::CStr)) =>
|
||||
{
|
||||
true
|
||||
}
|
||||
(LitKind::Int(..), ty::Uint(_)) if !neg => true,
|
||||
(LitKind::Int(..), ty::Int(_)) => true,
|
||||
(LitKind::Bool(..), ty::Bool) => true,
|
||||
(LitKind::Float(..), ty::Float(_)) => true,
|
||||
(LitKind::Char(..), ty::Char) => true,
|
||||
(LitKind::Err(..), _) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||
pub struct AllocId(pub NonZero<u64>);
|
||||
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ use crate::middle::resolve_bound_vars::{ObjectLifetimeDefault, ResolveBoundVars,
|
|||
use crate::middle::stability::DeprecationEntry;
|
||||
use crate::mir::interpret::{
|
||||
EvalStaticInitializerRawResult, EvalToAllocationRawResult, EvalToConstValueResult,
|
||||
EvalToValTreeResult, GlobalId, LitToConstInput,
|
||||
EvalToValTreeResult, GlobalId,
|
||||
};
|
||||
use crate::mir::mono::{
|
||||
CodegenUnit, CollectionMode, MonoItem, MonoItemPartitions, NormalizationErrorInMono,
|
||||
|
|
@ -135,8 +135,8 @@ use crate::ty::layout::ValidityRequirement;
|
|||
use crate::ty::print::PrintTraitRefExt;
|
||||
use crate::ty::util::AlwaysRequiresDrop;
|
||||
use crate::ty::{
|
||||
self, CrateInherentImpls, GenericArg, GenericArgsRef, PseudoCanonicalInput, SizedTraitKind, Ty,
|
||||
TyCtxt, TyCtxtFeed,
|
||||
self, CrateInherentImpls, GenericArg, GenericArgsRef, LitToConstInput, PseudoCanonicalInput,
|
||||
SizedTraitKind, Ty, TyCtxt, TyCtxtFeed,
|
||||
};
|
||||
use crate::{dep_graph, mir, thir};
|
||||
|
||||
|
|
|
|||
|
|
@ -445,7 +445,6 @@ impl_erasable_for_single_lifetime_types! {
|
|||
rustc_middle::mir::DestructuredConstant,
|
||||
rustc_middle::mir::ConstAlloc,
|
||||
rustc_middle::mir::interpret::GlobalId,
|
||||
rustc_middle::mir::interpret::LitToConstInput,
|
||||
rustc_middle::mir::interpret::EvalStaticInitializerRawResult,
|
||||
rustc_middle::mir::mono::MonoItemPartitions,
|
||||
rustc_middle::traits::query::MethodAutoderefStepsResult,
|
||||
|
|
@ -470,6 +469,7 @@ impl_erasable_for_single_lifetime_types! {
|
|||
rustc_middle::ty::InstanceKind,
|
||||
rustc_middle::ty::layout::FnAbiError,
|
||||
rustc_middle::ty::layout::LayoutError,
|
||||
rustc_middle::ty::LitToConstInput,
|
||||
rustc_middle::ty::ParamEnv,
|
||||
rustc_middle::ty::TypingEnv,
|
||||
rustc_middle::ty::Predicate,
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ impl<'tcx> Key for (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>) {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
|
||||
impl<'tcx> Key for ty::LitToConstInput<'tcx> {
|
||||
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
|
||||
DUMMY_SP
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,10 +11,12 @@ use crate::ty::{self, Ty, TyCtxt};
|
|||
|
||||
mod int;
|
||||
mod kind;
|
||||
mod lit;
|
||||
mod valtree;
|
||||
|
||||
pub use int::*;
|
||||
pub use kind::*;
|
||||
pub use lit::*;
|
||||
use rustc_span::{DUMMY_SP, ErrorGuaranteed};
|
||||
pub use valtree::*;
|
||||
|
||||
|
|
|
|||
55
compiler/rustc_middle/src/ty/consts/lit.rs
Normal file
55
compiler/rustc_middle/src/ty/consts/lit.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
use rustc_ast::LitKind;
|
||||
use rustc_hir;
|
||||
use rustc_macros::HashStable;
|
||||
|
||||
use crate::ty::{self, Ty, TyCtxt};
|
||||
|
||||
/// Input argument for `tcx.lit_to_const`.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
|
||||
pub struct LitToConstInput<'tcx> {
|
||||
/// The absolute value of the resultant constant.
|
||||
pub lit: LitKind,
|
||||
/// The type of the constant.
|
||||
pub ty: Ty<'tcx>,
|
||||
/// If the constant is negative.
|
||||
pub neg: bool,
|
||||
}
|
||||
|
||||
/// Checks whether a literal can be interpreted as a const of the given type.
|
||||
pub fn const_lit_matches_ty<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
kind: &LitKind,
|
||||
ty: Ty<'tcx>,
|
||||
neg: bool,
|
||||
) -> bool {
|
||||
match (*kind, ty.kind()) {
|
||||
(LitKind::Str(..), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => true,
|
||||
(LitKind::Str(..), ty::Str) if tcx.features().deref_patterns() => true,
|
||||
(LitKind::ByteStr(..), ty::Ref(_, inner_ty, _))
|
||||
if let ty::Slice(ty) | ty::Array(ty, _) = inner_ty.kind()
|
||||
&& matches!(ty.kind(), ty::Uint(ty::UintTy::U8)) =>
|
||||
{
|
||||
true
|
||||
}
|
||||
(LitKind::ByteStr(..), ty::Slice(inner_ty) | ty::Array(inner_ty, _))
|
||||
if tcx.features().deref_patterns()
|
||||
&& matches!(inner_ty.kind(), ty::Uint(ty::UintTy::U8)) =>
|
||||
{
|
||||
true
|
||||
}
|
||||
(LitKind::Byte(..), ty::Uint(ty::UintTy::U8)) => true,
|
||||
(LitKind::CStr(..), ty::Ref(_, inner_ty, _))
|
||||
if matches!(inner_ty.kind(), ty::Adt(def, _)
|
||||
if tcx.is_lang_item(def.did(), rustc_hir::LangItem::CStr)) =>
|
||||
{
|
||||
true
|
||||
}
|
||||
(LitKind::Int(..), ty::Uint(_)) if !neg => true,
|
||||
(LitKind::Int(..), ty::Int(_)) => true,
|
||||
(LitKind::Bool(..), ty::Bool) => true,
|
||||
(LitKind::Float(..), ty::Float(_)) => true,
|
||||
(LitKind::Char(..), ty::Char) => true,
|
||||
(LitKind::Err(..), _) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
@ -76,8 +76,9 @@ pub use self::closure::{
|
|||
place_to_string_for_capture,
|
||||
};
|
||||
pub use self::consts::{
|
||||
AtomicOrdering, Const, ConstInt, ConstKind, ConstToValTreeResult, Expr, ExprKind, ScalarInt,
|
||||
SimdAlign, UnevaluatedConst, ValTree, ValTreeKindExt, Value,
|
||||
AtomicOrdering, Const, ConstInt, ConstKind, ConstToValTreeResult, Expr, ExprKind,
|
||||
LitToConstInput, ScalarInt, SimdAlign, UnevaluatedConst, ValTree, ValTreeKindExt, Value,
|
||||
const_lit_matches_ty,
|
||||
};
|
||||
pub use self::context::{
|
||||
CtxtInterners, CurrentGcx, Feed, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt, TyCtxtFeed, tls,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue