Skip to content

Commit 01d48c6

Browse files
committed
Move LitToConstInput into ty::consts
Relocate LitToConstInput and const_lit_matches_ty from mir::interpret to ty::consts::lit
1 parent b4ee995 commit 01d48c6

12 files changed

Lines changed: 77 additions & 70 deletions

File tree

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
3535
use rustc_infer::traits::DynCompatibilityViolation;
3636
use rustc_macros::{TypeFoldable, TypeVisitable};
3737
use rustc_middle::middle::stability::AllowUnstable;
38-
use rustc_middle::mir::interpret::{LitToConstInput, const_lit_matches_ty};
3938
use rustc_middle::ty::print::PrintPolyTraitRefExt as _;
4039
use rustc_middle::ty::{
41-
self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, Ty, TyCtxt,
42-
TypeSuperFoldable, TypeVisitableExt, TypingMode, Upcast, fold_regions,
40+
self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, LitToConstInput, Ty, TyCtxt,
41+
TypeSuperFoldable, TypeVisitableExt, TypingMode, Upcast, const_lit_matches_ty, fold_regions,
4342
};
4443
use rustc_middle::{bug, span_bug};
4544
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;

compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::num::NonZero;
1313
use std::{fmt, io};
1414

1515
use rustc_abi::{AddressSpace, Align, Endian, HasDataLayout, Size};
16-
use rustc_ast::{LitKind, Mutability};
16+
use rustc_ast::Mutability;
1717
use rustc_data_structures::fx::FxHashMap;
1818
use rustc_data_structures::sharded::ShardedHashMap;
1919
use rustc_data_structures::sync::{AtomicU64, Lock};
@@ -73,55 +73,6 @@ impl<'tcx> GlobalId<'tcx> {
7373
}
7474
}
7575

76-
/// Input argument for `tcx.lit_to_const`.
77-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
78-
pub struct LitToConstInput<'tcx> {
79-
/// The absolute value of the resultant constant.
80-
pub lit: LitKind,
81-
/// The type of the constant.
82-
pub ty: Ty<'tcx>,
83-
/// If the constant is negative.
84-
pub neg: bool,
85-
}
86-
87-
pub fn const_lit_matches_ty<'tcx>(
88-
tcx: TyCtxt<'tcx>,
89-
kind: &LitKind,
90-
ty: Ty<'tcx>,
91-
neg: bool,
92-
) -> bool {
93-
match (*kind, ty.kind()) {
94-
(LitKind::Str(..), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => true,
95-
(LitKind::Str(..), ty::Str) if tcx.features().deref_patterns() => true,
96-
(LitKind::ByteStr(..), ty::Ref(_, inner_ty, _))
97-
if let ty::Slice(ty) | ty::Array(ty, _) = inner_ty.kind()
98-
&& matches!(ty.kind(), ty::Uint(ty::UintTy::U8)) =>
99-
{
100-
true
101-
}
102-
(LitKind::ByteStr(..), ty::Slice(inner_ty) | ty::Array(inner_ty, _))
103-
if tcx.features().deref_patterns()
104-
&& matches!(inner_ty.kind(), ty::Uint(ty::UintTy::U8)) =>
105-
{
106-
true
107-
}
108-
(LitKind::Byte(..), ty::Uint(ty::UintTy::U8)) => true,
109-
(LitKind::CStr(..), ty::Ref(_, inner_ty, _))
110-
if matches!(inner_ty.kind(), ty::Adt(def, _)
111-
if tcx.is_lang_item(def.did(), rustc_hir::LangItem::CStr)) =>
112-
{
113-
true
114-
}
115-
(LitKind::Int(..), ty::Uint(_)) if !neg => true,
116-
(LitKind::Int(..), ty::Int(_)) => true,
117-
(LitKind::Bool(..), ty::Bool) => true,
118-
(LitKind::Float(..), ty::Float(_)) => true,
119-
(LitKind::Char(..), ty::Char) => true,
120-
(LitKind::Err(..), _) => true,
121-
_ => false,
122-
}
123-
}
124-
12576
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
12677
pub struct AllocId(pub NonZero<u64>);
12778

compiler/rustc_middle/src/queries.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ use crate::middle::resolve_bound_vars::{ObjectLifetimeDefault, ResolveBoundVars,
112112
use crate::middle::stability::DeprecationEntry;
113113
use crate::mir::interpret::{
114114
EvalStaticInitializerRawResult, EvalToAllocationRawResult, EvalToConstValueResult,
115-
EvalToValTreeResult, GlobalId, LitToConstInput,
115+
EvalToValTreeResult, GlobalId,
116116
};
117117
use crate::mir::mono::{
118118
CodegenUnit, CollectionMode, MonoItem, MonoItemPartitions, NormalizationErrorInMono,
@@ -135,8 +135,8 @@ use crate::ty::layout::ValidityRequirement;
135135
use crate::ty::print::PrintTraitRefExt;
136136
use crate::ty::util::AlwaysRequiresDrop;
137137
use crate::ty::{
138-
self, CrateInherentImpls, GenericArg, GenericArgsRef, PseudoCanonicalInput, SizedTraitKind, Ty,
139-
TyCtxt, TyCtxtFeed,
138+
self, CrateInherentImpls, GenericArg, GenericArgsRef, LitToConstInput, PseudoCanonicalInput,
139+
SizedTraitKind, Ty, TyCtxt, TyCtxtFeed,
140140
};
141141
use crate::{dep_graph, mir, thir};
142142

compiler/rustc_middle/src/query/erase.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ impl_erasable_for_single_lifetime_types! {
445445
rustc_middle::mir::DestructuredConstant,
446446
rustc_middle::mir::ConstAlloc,
447447
rustc_middle::mir::interpret::GlobalId,
448-
rustc_middle::mir::interpret::LitToConstInput,
449448
rustc_middle::mir::interpret::EvalStaticInitializerRawResult,
450449
rustc_middle::mir::mono::MonoItemPartitions,
451450
rustc_middle::traits::query::MethodAutoderefStepsResult,
@@ -470,6 +469,7 @@ impl_erasable_for_single_lifetime_types! {
470469
rustc_middle::ty::InstanceKind,
471470
rustc_middle::ty::layout::FnAbiError,
472471
rustc_middle::ty::layout::LayoutError,
472+
rustc_middle::ty::LitToConstInput,
473473
rustc_middle::ty::ParamEnv,
474474
rustc_middle::ty::TypingEnv,
475475
rustc_middle::ty::Predicate,

compiler/rustc_middle/src/query/keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'tcx> Key for (Ty<'tcx>, Option<ty::ExistentialTraitRef<'tcx>>) {
8787
}
8888
}
8989

90-
impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
90+
impl<'tcx> Key for ty::LitToConstInput<'tcx> {
9191
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
9292
DUMMY_SP
9393
}

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ use crate::ty::{self, Ty, TyCtxt};
1111

1212
mod int;
1313
mod kind;
14+
mod lit;
1415
mod valtree;
1516

1617
pub use int::*;
1718
pub use kind::*;
19+
pub use lit::*;
1820
use rustc_span::{DUMMY_SP, ErrorGuaranteed};
1921
pub use valtree::*;
2022

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use rustc_ast::LitKind;
2+
use rustc_hir;
3+
use rustc_macros::HashStable;
4+
5+
use crate::ty::{self, Ty, TyCtxt};
6+
7+
/// Input argument for `tcx.lit_to_const`.
8+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
9+
pub struct LitToConstInput<'tcx> {
10+
/// The absolute value of the resultant constant.
11+
pub lit: LitKind,
12+
/// The type of the constant.
13+
pub ty: Ty<'tcx>,
14+
/// If the constant is negative.
15+
pub neg: bool,
16+
}
17+
18+
/// Checks whether a literal can be interpreted as a const of the given type.
19+
pub fn const_lit_matches_ty<'tcx>(
20+
tcx: TyCtxt<'tcx>,
21+
kind: &LitKind,
22+
ty: Ty<'tcx>,
23+
neg: bool,
24+
) -> bool {
25+
match (*kind, ty.kind()) {
26+
(LitKind::Str(..), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => true,
27+
(LitKind::Str(..), ty::Str) if tcx.features().deref_patterns() => true,
28+
(LitKind::ByteStr(..), ty::Ref(_, inner_ty, _))
29+
if let ty::Slice(ty) | ty::Array(ty, _) = inner_ty.kind()
30+
&& matches!(ty.kind(), ty::Uint(ty::UintTy::U8)) =>
31+
{
32+
true
33+
}
34+
(LitKind::ByteStr(..), ty::Slice(inner_ty) | ty::Array(inner_ty, _))
35+
if tcx.features().deref_patterns()
36+
&& matches!(inner_ty.kind(), ty::Uint(ty::UintTy::U8)) =>
37+
{
38+
true
39+
}
40+
(LitKind::Byte(..), ty::Uint(ty::UintTy::U8)) => true,
41+
(LitKind::CStr(..), ty::Ref(_, inner_ty, _))
42+
if matches!(inner_ty.kind(), ty::Adt(def, _)
43+
if tcx.is_lang_item(def.did(), rustc_hir::LangItem::CStr)) =>
44+
{
45+
true
46+
}
47+
(LitKind::Int(..), ty::Uint(_)) if !neg => true,
48+
(LitKind::Int(..), ty::Int(_)) => true,
49+
(LitKind::Bool(..), ty::Bool) => true,
50+
(LitKind::Float(..), ty::Float(_)) => true,
51+
(LitKind::Char(..), ty::Char) => true,
52+
(LitKind::Err(..), _) => true,
53+
_ => false,
54+
}
55+
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ pub use self::closure::{
7676
place_to_string_for_capture,
7777
};
7878
pub use self::consts::{
79-
AtomicOrdering, Const, ConstInt, ConstKind, ConstToValTreeResult, Expr, ExprKind, ScalarInt,
80-
SimdAlign, UnevaluatedConst, ValTree, ValTreeKindExt, Value,
79+
AtomicOrdering, Const, ConstInt, ConstKind, ConstToValTreeResult, Expr, ExprKind,
80+
LitToConstInput, ScalarInt, SimdAlign, UnevaluatedConst, ValTree, ValTreeKindExt, Value,
81+
const_lit_matches_ty,
8182
};
8283
pub use self::context::{
8384
CtxtInterners, CurrentGcx, Feed, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt, TyCtxtFeed, tls,

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
//! See docs in build/expr/mod.rs
1+
//! See docs in builder/expr/mod.rs
22
33
use rustc_abi::Size;
44
use rustc_ast::{self as ast};
55
use rustc_hir::LangItem;
6-
use rustc_middle::mir::interpret::{CTFE_ALLOC_SALT, LitToConstInput, Scalar};
6+
use rustc_middle::mir::interpret::{CTFE_ALLOC_SALT, Scalar};
77
use rustc_middle::mir::*;
88
use rustc_middle::thir::*;
99
use rustc_middle::ty::{
10-
self, CanonicalUserType, CanonicalUserTypeAnnotation, Ty, TyCtxt, TypeVisitableExt as _,
11-
UserTypeAnnotationIndex,
10+
self, CanonicalUserType, CanonicalUserTypeAnnotation, LitToConstInput, Ty, TyCtxt,
11+
TypeVisitableExt as _, UserTypeAnnotationIndex,
1212
};
1313
use rustc_middle::{bug, mir, span_bug};
1414
use tracing::{instrument, trace};

compiler/rustc_mir_build/src/thir/constant.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use rustc_abi::Size;
22
use rustc_ast::{self as ast, UintTy};
33
use rustc_hir::LangItem;
44
use rustc_middle::bug;
5-
use rustc_middle::mir::interpret::LitToConstInput;
6-
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt, TypeVisitableExt as _};
5+
use rustc_middle::ty::{self, LitToConstInput, ScalarInt, Ty, TyCtxt, TypeVisitableExt as _};
76
use tracing::trace;
87

98
use crate::builder::parse_float_into_scalar;

0 commit comments

Comments
 (0)