Rollup merge of #144712 - nnethercote:dedup-num-types, r=fmease

Deduplicate `IntTy`/`UintTy`/`FloatTy`.

There are identical definitions in `rustc_type_ir` and `rustc_ast`. This commit removes them and places a single definition in `rustc_ast_ir`. This requires adding `rust_span` as a dependency of `rustc_ast_ir`, but means a bunch of silly conversion functions can be removed.

r? `@fmease`
This commit is contained in:
Jana Dönszelmann 2025-07-31 17:19:39 +02:00 committed by GitHub
commit 5712d50648
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 312 additions and 403 deletions

View file

@ -3417,6 +3417,7 @@ dependencies = [
"rustc_data_structures",
"rustc_macros",
"rustc_serialize",
"rustc_span",
]
[[package]]

View file

@ -23,7 +23,7 @@ use std::{cmp, fmt};
pub use GenericArgs::*;
pub use UnsafeSource::*;
pub use rustc_ast_ir::{Movability, Mutability, Pinnedness};
pub use rustc_ast_ir::{FloatTy, IntTy, Movability, Mutability, Pinnedness, UintTy};
use rustc_data_structures::packed::Pu128;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::stack::ensure_sufficient_stack;
@ -2285,105 +2285,6 @@ pub struct FnSig {
pub span: Span,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum FloatTy {
F16,
F32,
F64,
F128,
}
impl FloatTy {
pub fn name_str(self) -> &'static str {
match self {
FloatTy::F16 => "f16",
FloatTy::F32 => "f32",
FloatTy::F64 => "f64",
FloatTy::F128 => "f128",
}
}
pub fn name(self) -> Symbol {
match self {
FloatTy::F16 => sym::f16,
FloatTy::F32 => sym::f32,
FloatTy::F64 => sym::f64,
FloatTy::F128 => sym::f128,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum IntTy {
Isize,
I8,
I16,
I32,
I64,
I128,
}
impl IntTy {
pub fn name_str(&self) -> &'static str {
match *self {
IntTy::Isize => "isize",
IntTy::I8 => "i8",
IntTy::I16 => "i16",
IntTy::I32 => "i32",
IntTy::I64 => "i64",
IntTy::I128 => "i128",
}
}
pub fn name(&self) -> Symbol {
match *self {
IntTy::Isize => sym::isize,
IntTy::I8 => sym::i8,
IntTy::I16 => sym::i16,
IntTy::I32 => sym::i32,
IntTy::I64 => sym::i64,
IntTy::I128 => sym::i128,
}
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum UintTy {
Usize,
U8,
U16,
U32,
U64,
U128,
}
impl UintTy {
pub fn name_str(&self) -> &'static str {
match *self {
UintTy::Usize => "usize",
UintTy::U8 => "u8",
UintTy::U16 => "u16",
UintTy::U32 => "u32",
UintTy::U64 => "u64",
UintTy::U128 => "u128",
}
}
pub fn name(&self) -> Symbol {
match *self {
UintTy::Usize => sym::usize,
UintTy::U8 => sym::u8,
UintTy::U16 => sym::u16,
UintTy::U32 => sym::u32,
UintTy::U64 => sym::u64,
UintTy::U128 => sym::u128,
}
}
}
/// A constraint on an associated item.
///
/// ### Examples

View file

@ -190,15 +190,15 @@ impl fmt::Display for LitKind {
LitKind::Int(n, ty) => {
write!(f, "{n}")?;
match ty {
ast::LitIntType::Unsigned(ty) => write!(f, "{}", ty.name())?,
ast::LitIntType::Signed(ty) => write!(f, "{}", ty.name())?,
ast::LitIntType::Unsigned(ty) => write!(f, "{}", ty.name_str())?,
ast::LitIntType::Signed(ty) => write!(f, "{}", ty.name_str())?,
ast::LitIntType::Unsuffixed => {}
}
}
LitKind::Float(symbol, ty) => {
write!(f, "{symbol}")?;
match ty {
ast::LitFloatType::Suffixed(ty) => write!(f, "{}", ty.name())?,
ast::LitFloatType::Suffixed(ty) => write!(f, "{}", ty.name_str())?,
ast::LitFloatType::Unsuffixed => {}
}
}

View file

@ -8,12 +8,16 @@ edition = "2024"
rustc_data_structures = { path = "../rustc_data_structures", optional = true }
rustc_macros = { path = "../rustc_macros", optional = true }
rustc_serialize = { path = "../rustc_serialize", optional = true }
rustc_span = { path = "../rustc_span", optional = true }
# tidy-alphabetical-end
[features]
# tidy-alphabetical-start
default = ["nightly"]
nightly = [
"dep:rustc_serialize",
"dep:rustc_data_structures",
"dep:rustc_macros",
"dep:rustc_serialize",
"dep:rustc_span",
]
# tidy-alphabetical-end

View file

@ -11,11 +11,221 @@
#![cfg_attr(feature = "nightly", feature(rustc_attrs))]
// tidy-alphabetical-end
use std::fmt;
#[cfg(feature = "nightly")]
use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
#[cfg(feature = "nightly")]
use rustc_span::{Symbol, sym};
pub mod visit;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
)]
pub enum IntTy {
Isize,
I8,
I16,
I32,
I64,
I128,
}
impl IntTy {
pub fn name_str(&self) -> &'static str {
match *self {
IntTy::Isize => "isize",
IntTy::I8 => "i8",
IntTy::I16 => "i16",
IntTy::I32 => "i32",
IntTy::I64 => "i64",
IntTy::I128 => "i128",
}
}
#[cfg(feature = "nightly")]
pub fn name(self) -> Symbol {
match self {
IntTy::Isize => sym::isize,
IntTy::I8 => sym::i8,
IntTy::I16 => sym::i16,
IntTy::I32 => sym::i32,
IntTy::I64 => sym::i64,
IntTy::I128 => sym::i128,
}
}
pub fn bit_width(&self) -> Option<u64> {
Some(match *self {
IntTy::Isize => return None,
IntTy::I8 => 8,
IntTy::I16 => 16,
IntTy::I32 => 32,
IntTy::I64 => 64,
IntTy::I128 => 128,
})
}
pub fn normalize(&self, target_width: u32) -> Self {
match self {
IntTy::Isize => match target_width {
16 => IntTy::I16,
32 => IntTy::I32,
64 => IntTy::I64,
_ => unreachable!(),
},
_ => *self,
}
}
pub fn to_unsigned(self) -> UintTy {
match self {
IntTy::Isize => UintTy::Usize,
IntTy::I8 => UintTy::U8,
IntTy::I16 => UintTy::U16,
IntTy::I32 => UintTy::U32,
IntTy::I64 => UintTy::U64,
IntTy::I128 => UintTy::U128,
}
}
}
impl fmt::Debug for IntTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name_str())
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
)]
pub enum UintTy {
Usize,
U8,
U16,
U32,
U64,
U128,
}
impl UintTy {
pub fn name_str(&self) -> &'static str {
match *self {
UintTy::Usize => "usize",
UintTy::U8 => "u8",
UintTy::U16 => "u16",
UintTy::U32 => "u32",
UintTy::U64 => "u64",
UintTy::U128 => "u128",
}
}
#[cfg(feature = "nightly")]
pub fn name(self) -> Symbol {
match self {
UintTy::Usize => sym::usize,
UintTy::U8 => sym::u8,
UintTy::U16 => sym::u16,
UintTy::U32 => sym::u32,
UintTy::U64 => sym::u64,
UintTy::U128 => sym::u128,
}
}
pub fn bit_width(&self) -> Option<u64> {
Some(match *self {
UintTy::Usize => return None,
UintTy::U8 => 8,
UintTy::U16 => 16,
UintTy::U32 => 32,
UintTy::U64 => 64,
UintTy::U128 => 128,
})
}
pub fn normalize(&self, target_width: u32) -> Self {
match self {
UintTy::Usize => match target_width {
16 => UintTy::U16,
32 => UintTy::U32,
64 => UintTy::U64,
_ => unreachable!(),
},
_ => *self,
}
}
pub fn to_signed(self) -> IntTy {
match self {
UintTy::Usize => IntTy::Isize,
UintTy::U8 => IntTy::I8,
UintTy::U16 => IntTy::I16,
UintTy::U32 => IntTy::I32,
UintTy::U64 => IntTy::I64,
UintTy::U128 => IntTy::I128,
}
}
}
impl fmt::Debug for UintTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name_str())
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
)]
pub enum FloatTy {
F16,
F32,
F64,
F128,
}
impl FloatTy {
pub fn name_str(self) -> &'static str {
match self {
FloatTy::F16 => "f16",
FloatTy::F32 => "f32",
FloatTy::F64 => "f64",
FloatTy::F128 => "f128",
}
}
#[cfg(feature = "nightly")]
pub fn name(self) -> Symbol {
match self {
FloatTy::F16 => sym::f16,
FloatTy::F32 => sym::f32,
FloatTy::F64 => sym::f64,
FloatTy::F128 => sym::f128,
}
}
pub fn bit_width(self) -> u64 {
match self {
FloatTy::F16 => 16,
FloatTy::F32 => 32,
FloatTy::F64 => 64,
FloatTy::F128 => 128,
}
}
}
impl fmt::Debug for FloatTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name_str())
}
}
/// The movability of a coroutine / closure literal:
/// whether a coroutine contains self-references, causing it to be `!Unpin`.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]

View file

@ -45,4 +45,7 @@ tracing = "0.1"
# tidy-alphabetical-end
[features]
# tidy-alphabetical-start
check_only = ["rustc_llvm/check_only"]
# tidy-alphabetical-end

View file

@ -4,8 +4,8 @@ version = "0.0.0"
edition = "2024"
[dependencies]
jiff = { version = "0.2.5", default-features = false, features = ["std"] }
# tidy-alphabetical-start
jiff = { version = "0.2.5", default-features = false, features = ["std"] }
rustc_abi = { path = "../rustc_abi" }
rustc_ast = { path = "../rustc_ast" }
rustc_ast_lowering = { path = "../rustc_ast_lowering" }

View file

@ -2037,9 +2037,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
match prim_ty {
hir::PrimTy::Bool => tcx.types.bool,
hir::PrimTy::Char => tcx.types.char,
hir::PrimTy::Int(it) => Ty::new_int(tcx, ty::int_ty(it)),
hir::PrimTy::Uint(uit) => Ty::new_uint(tcx, ty::uint_ty(uit)),
hir::PrimTy::Float(ft) => Ty::new_float(tcx, ty::float_ty(ft)),
hir::PrimTy::Int(it) => Ty::new_int(tcx, it),
hir::PrimTy::Uint(uit) => Ty::new_uint(tcx, uit),
hir::PrimTy::Float(ft) => Ty::new_float(tcx, ft),
hir::PrimTy::Str => tcx.types.str_,
}
}

View file

@ -1658,8 +1658,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
),
ast::LitKind::Byte(_) => tcx.types.u8,
ast::LitKind::Char(_) => tcx.types.char,
ast::LitKind::Int(_, ast::LitIntType::Signed(t)) => Ty::new_int(tcx, ty::int_ty(t)),
ast::LitKind::Int(_, ast::LitIntType::Unsigned(t)) => Ty::new_uint(tcx, ty::uint_ty(t)),
ast::LitKind::Int(_, ast::LitIntType::Signed(t)) => Ty::new_int(tcx, t),
ast::LitKind::Int(_, ast::LitIntType::Unsigned(t)) => Ty::new_uint(tcx, t),
ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) => {
let opt_ty = expected.to_option(self).and_then(|ty| match ty.kind() {
ty::Int(_) | ty::Uint(_) => Some(ty),
@ -1686,9 +1686,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
});
opt_ty.unwrap_or_else(|| self.next_int_var())
}
ast::LitKind::Float(_, ast::LitFloatType::Suffixed(t)) => {
Ty::new_float(tcx, ty::float_ty(t))
}
ast::LitKind::Float(_, ast::LitFloatType::Suffixed(t)) => Ty::new_float(tcx, t),
ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) => {
let opt_ty = expected.to_option(self).and_then(|ty| match ty.kind() {
ty::Float(_) => Some(ty),

View file

@ -15,8 +15,8 @@ smallvec = "1.8.1"
# tidy-alphabetical-start
default = ["nightly"]
nightly = [
"dep:rustc_serialize",
"dep:rustc_macros",
"dep:rustc_serialize",
"rustc_index_macros/nightly",
]
rustc_randomized_layouts = []

View file

@ -7,9 +7,14 @@ edition = "2024"
proc-macro = true
[dependencies]
syn = { version = "2.0.9", features = ["full", "extra-traits"] }
# tidy-alphabetical-start
proc-macro2 = "1"
quote = "1"
syn = { version = "2.0.9", features = ["full", "extra-traits"] }
# tidy-alphabetical-end
[features]
# tidy-alphabetical-start
nightly = []
# tidy-alphabetical-end

View file

@ -272,7 +272,7 @@ fn lint_int_literal<'tcx>(
cx,
hir_id,
span,
attrs::IntType::SignedInt(ty::ast_int_ty(t)),
attrs::IntType::SignedInt(t),
Integer::from_int_ty(cx, t).size(),
repr_str,
v,
@ -358,7 +358,7 @@ fn lint_uint_literal<'tcx>(
cx,
hir_id,
span,
attrs::IntType::UnsignedInt(ty::ast_uint_ty(t)),
attrs::IntType::UnsignedInt(t),
Integer::from_uint_ty(cx, t).size(),
repr_str,
lit_val,

View file

@ -16,5 +16,7 @@ cc = "=1.2.16"
# tidy-alphabetical-end
[features]
# tidy-alphabetical-start
# Used by ./x.py check --compile-time-deps to skip building C++ code
check_only = []
# tidy-alphabetical-end

View file

@ -2124,59 +2124,6 @@ impl<'tcx> TyCtxt<'tcx> {
}
}
pub fn int_ty(ity: ast::IntTy) -> IntTy {
match ity {
ast::IntTy::Isize => IntTy::Isize,
ast::IntTy::I8 => IntTy::I8,
ast::IntTy::I16 => IntTy::I16,
ast::IntTy::I32 => IntTy::I32,
ast::IntTy::I64 => IntTy::I64,
ast::IntTy::I128 => IntTy::I128,
}
}
pub fn uint_ty(uty: ast::UintTy) -> UintTy {
match uty {
ast::UintTy::Usize => UintTy::Usize,
ast::UintTy::U8 => UintTy::U8,
ast::UintTy::U16 => UintTy::U16,
ast::UintTy::U32 => UintTy::U32,
ast::UintTy::U64 => UintTy::U64,
ast::UintTy::U128 => UintTy::U128,
}
}
pub fn float_ty(fty: ast::FloatTy) -> FloatTy {
match fty {
ast::FloatTy::F16 => FloatTy::F16,
ast::FloatTy::F32 => FloatTy::F32,
ast::FloatTy::F64 => FloatTy::F64,
ast::FloatTy::F128 => FloatTy::F128,
}
}
pub fn ast_int_ty(ity: IntTy) -> ast::IntTy {
match ity {
IntTy::Isize => ast::IntTy::Isize,
IntTy::I8 => ast::IntTy::I8,
IntTy::I16 => ast::IntTy::I16,
IntTy::I32 => ast::IntTy::I32,
IntTy::I64 => ast::IntTy::I64,
IntTy::I128 => ast::IntTy::I128,
}
}
pub fn ast_uint_ty(uty: UintTy) -> ast::UintTy {
match uty {
UintTy::Usize => ast::UintTy::Usize,
UintTy::U8 => ast::UintTy::U8,
UintTy::U16 => ast::UintTy::U16,
UintTy::U32 => ast::UintTy::U32,
UintTy::U64 => ast::UintTy::U64,
UintTy::U128 => ast::UintTy::U128,
}
}
pub fn provide(providers: &mut Providers) {
closure::provide(providers);
context::provide(providers);

View file

@ -6,7 +6,6 @@ edition = "2024"
[dependencies]
# tidy-alphabetical-start
itertools = "0.12"
rustc_abi = { path = "../rustc_abi" }
rustc_apfloat = "0.2.0"
rustc_arena = { path = "../rustc_arena" }

View file

@ -15,6 +15,7 @@ tracing = "0.1"
# tidy-alphabetical-end
[features]
# tidy-alphabetical-start
default = ["nightly"]
nightly = [
"dep:rustc_data_structures",
@ -22,3 +23,4 @@ nightly = [
"rustc_index/nightly",
"rustc_type_ir/nightly",
]
# tidy-alphabetical-end

View file

@ -26,5 +26,8 @@ unicode-width = "0.2.0"
# tidy-alphabetical-end
[dev-dependencies]
# tidy-alphabetical-start
termcolor = "1.2"
# tidy-alphabetical-end

View file

@ -6,7 +6,6 @@ edition = "2024"
[dependencies]
# tidy-alphabetical-start
rustc-hash = "2.0.0"
rustc_abi = { path = "../rustc_abi", optional = true }
rustc_apfloat = "0.2.0"
rustc_arena = { path = "../rustc_arena", optional = true }
@ -24,10 +23,13 @@ tracing = "0.1"
# tidy-alphabetical-end
[dev-dependencies]
# tidy-alphabetical-start
tracing-subscriber = { version = "0.3.3", default-features = false, features = ["fmt", "env-filter", "ansi"] }
tracing-tree = "0.3.0"
# tidy-alphabetical-end
[features]
# tidy-alphabetical-start
default = ["rustc"]
rustc = [
"dep:rustc_abi",
@ -43,3 +45,4 @@ rustc = [
"smallvec/may_dangle",
"rustc_index/nightly",
]
# tidy-alphabetical-end

View file

@ -15,7 +15,11 @@ test = false
doctest = false
[dependencies]
# tidy-alphabetical-start
rustc-literal-escaper = "0.0.5"
# tidy-alphabetical-end
[features]
# tidy-alphabetical-start
rustc-dep-of-std = []
# tidy-alphabetical-end

View file

@ -18,7 +18,9 @@ tracing = "0.1"
# tidy-alphabetical-end
[features]
# tidy-alphabetical-start
# Provides access to APIs that expose internals of the rust compiler.
# APIs enabled by this feature are unstable. They can be removed or modified
# at any point and they are not included in the crate's semantic versioning.
rustc_internal = []
# tidy-alphabetical-end

View file

@ -4,9 +4,8 @@ version = "0.0.0"
edition = "2024"
[dependencies]
# tidy-alphabetical-start
bitflags = "2.5.0"
tracing = "0.1"
twox-hash = "1.6.3"
rustc_abi = { path = "../rustc_abi" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_hir = { path = "../rustc_hir" }
@ -14,3 +13,6 @@ rustc_middle = { path = "../rustc_middle" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
rustc_trait_selection = { path = "../rustc_trait_selection" }
tracing = "0.1"
twox-hash = "1.6.3"
# tidy-alphabetical-end

View file

@ -7,7 +7,6 @@ edition = "2024"
# tidy-alphabetical-start
punycode = "0.4.0"
rustc-demangle = "0.1.21"
rustc_abi = { path = "../rustc_abi" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }

View file

@ -20,9 +20,11 @@ itertools = "0.12"
# tidy-alphabetical-end
[features]
# tidy-alphabetical-start
rustc = [
"dep:rustc_abi",
"dep:rustc_hir",
"dep:rustc_middle",
"dep:rustc_span",
]
# tidy-alphabetical-end

View file

@ -23,14 +23,16 @@ tracing = "0.1"
# tidy-alphabetical-end
[features]
# tidy-alphabetical-start
default = ["nightly"]
nightly = [
"dep:rustc_serialize",
"dep:rustc_span",
"dep:rustc_data_structures",
"dep:rustc_macros",
"dep:rustc_serialize",
"dep:rustc_span",
"rustc_ast_ir/nightly",
"rustc_index/nightly",
"smallvec/may_dangle",
"smallvec/union",
"rustc_index/nightly",
"rustc_ast_ir/nightly",
]
# tidy-alphabetical-end

View file

@ -75,7 +75,7 @@ pub use pattern::*;
pub use predicate::*;
pub use predicate_kind::*;
pub use region_kind::*;
pub use rustc_ast_ir::{Movability, Mutability, Pinnedness};
pub use rustc_ast_ir::{FloatTy, IntTy, Movability, Mutability, Pinnedness, UintTy};
pub use ty_info::*;
pub use ty_kind::*;
pub use upcast::*;

View file

@ -15,7 +15,7 @@ pub use self::closure::*;
use crate::inherent::*;
#[cfg(feature = "nightly")]
use crate::visit::TypeVisitable;
use crate::{self as ty, DebruijnIndex, Interner};
use crate::{self as ty, DebruijnIndex, FloatTy, IntTy, Interner, UintTy};
mod closure;
@ -509,160 +509,6 @@ impl<I: Interner> AliasTy<I> {
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
)]
pub enum IntTy {
Isize,
I8,
I16,
I32,
I64,
I128,
}
impl IntTy {
pub fn name_str(&self) -> &'static str {
match *self {
IntTy::Isize => "isize",
IntTy::I8 => "i8",
IntTy::I16 => "i16",
IntTy::I32 => "i32",
IntTy::I64 => "i64",
IntTy::I128 => "i128",
}
}
pub fn bit_width(&self) -> Option<u64> {
Some(match *self {
IntTy::Isize => return None,
IntTy::I8 => 8,
IntTy::I16 => 16,
IntTy::I32 => 32,
IntTy::I64 => 64,
IntTy::I128 => 128,
})
}
pub fn normalize(&self, target_width: u32) -> Self {
match self {
IntTy::Isize => match target_width {
16 => IntTy::I16,
32 => IntTy::I32,
64 => IntTy::I64,
_ => unreachable!(),
},
_ => *self,
}
}
pub fn to_unsigned(self) -> UintTy {
match self {
IntTy::Isize => UintTy::Usize,
IntTy::I8 => UintTy::U8,
IntTy::I16 => UintTy::U16,
IntTy::I32 => UintTy::U32,
IntTy::I64 => UintTy::U64,
IntTy::I128 => UintTy::U128,
}
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
)]
pub enum UintTy {
Usize,
U8,
U16,
U32,
U64,
U128,
}
impl UintTy {
pub fn name_str(&self) -> &'static str {
match *self {
UintTy::Usize => "usize",
UintTy::U8 => "u8",
UintTy::U16 => "u16",
UintTy::U32 => "u32",
UintTy::U64 => "u64",
UintTy::U128 => "u128",
}
}
pub fn bit_width(&self) -> Option<u64> {
Some(match *self {
UintTy::Usize => return None,
UintTy::U8 => 8,
UintTy::U16 => 16,
UintTy::U32 => 32,
UintTy::U64 => 64,
UintTy::U128 => 128,
})
}
pub fn normalize(&self, target_width: u32) -> Self {
match self {
UintTy::Usize => match target_width {
16 => UintTy::U16,
32 => UintTy::U32,
64 => UintTy::U64,
_ => unreachable!(),
},
_ => *self,
}
}
pub fn to_signed(self) -> IntTy {
match self {
UintTy::Usize => IntTy::Isize,
UintTy::U8 => IntTy::I8,
UintTy::U16 => IntTy::I16,
UintTy::U32 => IntTy::I32,
UintTy::U64 => IntTy::I64,
UintTy::U128 => IntTy::I128,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
feature = "nightly",
derive(Encodable_NoContext, Decodable_NoContext, HashStable_NoContext)
)]
pub enum FloatTy {
F16,
F32,
F64,
F128,
}
impl FloatTy {
pub fn name_str(self) -> &'static str {
match self {
FloatTy::F16 => "f16",
FloatTy::F32 => "f32",
FloatTy::F64 => "f64",
FloatTy::F128 => "f128",
}
}
pub fn bit_width(self) -> u64 {
match self {
FloatTy::F16 => 16,
FloatTy::F32 => 32,
FloatTy::F64 => 64,
FloatTy::F128 => 128,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum IntVarValue {
Unknown,
@ -860,24 +706,6 @@ impl fmt::Display for InferTy {
}
}
impl fmt::Debug for IntTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name_str())
}
}
impl fmt::Debug for UintTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name_str())
}
}
impl fmt::Debug for FloatTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name_str())
}
}
impl fmt::Debug for InferTy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use InferTy::*;

View file

@ -1960,43 +1960,6 @@ impl PrimitiveType {
}
}
impl From<ast::IntTy> for PrimitiveType {
fn from(int_ty: ast::IntTy) -> PrimitiveType {
match int_ty {
ast::IntTy::Isize => PrimitiveType::Isize,
ast::IntTy::I8 => PrimitiveType::I8,
ast::IntTy::I16 => PrimitiveType::I16,
ast::IntTy::I32 => PrimitiveType::I32,
ast::IntTy::I64 => PrimitiveType::I64,
ast::IntTy::I128 => PrimitiveType::I128,
}
}
}
impl From<ast::UintTy> for PrimitiveType {
fn from(uint_ty: ast::UintTy) -> PrimitiveType {
match uint_ty {
ast::UintTy::Usize => PrimitiveType::Usize,
ast::UintTy::U8 => PrimitiveType::U8,
ast::UintTy::U16 => PrimitiveType::U16,
ast::UintTy::U32 => PrimitiveType::U32,
ast::UintTy::U64 => PrimitiveType::U64,
ast::UintTy::U128 => PrimitiveType::U128,
}
}
}
impl From<ast::FloatTy> for PrimitiveType {
fn from(float_ty: ast::FloatTy) -> PrimitiveType {
match float_ty {
ast::FloatTy::F16 => PrimitiveType::F16,
ast::FloatTy::F32 => PrimitiveType::F32,
ast::FloatTy::F64 => PrimitiveType::F64,
ast::FloatTy::F128 => PrimitiveType::F128,
}
}
}
impl From<ty::IntTy> for PrimitiveType {
fn from(int_ty: ty::IntTy) -> PrimitiveType {
match int_ty {

View file

@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::numeric_literal;
use rustc_ast::ast::{self, LitFloatType, LitKind};
use rustc_ast::ast::{LitFloatType, LitKind};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
@ -75,10 +75,10 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
let digits = count_digits(sym_str);
let max = max_digits(fty);
let type_suffix = match lit_float_ty {
LitFloatType::Suffixed(ast::FloatTy::F16) => Some("f16"),
LitFloatType::Suffixed(ast::FloatTy::F32) => Some("f32"),
LitFloatType::Suffixed(ast::FloatTy::F64) => Some("f64"),
LitFloatType::Suffixed(ast::FloatTy::F128) => Some("f128"),
LitFloatType::Suffixed(FloatTy::F16) => Some("f16"),
LitFloatType::Suffixed(FloatTy::F32) => Some("f32"),
LitFloatType::Suffixed(FloatTy::F64) => Some("f64"),
LitFloatType::Suffixed(FloatTy::F128) => Some("f128"),
LitFloatType::Unsuffixed => None,
};
let (is_whole, is_inf, mut float_str) = match fty {

View file

@ -9,6 +9,7 @@ use rustc_hir::{
FnRetTy, HirId, Lit, PatExprKind, PatKind, QPath, StmtKind, StructTailExpr,
};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty::{FloatTy, IntTy, UintTy};
use rustc_session::declare_lint_pass;
use rustc_span::symbol::{Ident, Symbol};
use std::cell::Cell;
@ -337,15 +338,43 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
LitKind::Byte(b) => kind!("Byte({b})"),
LitKind::Int(i, suffix) => {
let int_ty = match suffix {
LitIntType::Signed(int_ty) => format!("LitIntType::Signed(IntTy::{int_ty:?})"),
LitIntType::Unsigned(uint_ty) => format!("LitIntType::Unsigned(UintTy::{uint_ty:?})"),
LitIntType::Signed(int_ty) => {
let t = match int_ty {
IntTy::Isize => "Isize",
IntTy::I8 => "I8",
IntTy::I16 => "I16",
IntTy::I32 => "I32",
IntTy::I64 => "I64",
IntTy::I128 => "I128",
};
format!("LitIntType::Signed(IntTy::{t})")
}
LitIntType::Unsigned(uint_ty) => {
let t = match uint_ty {
UintTy::Usize => "Usize",
UintTy::U8 => "U8",
UintTy::U16 => "U16",
UintTy::U32 => "U32",
UintTy::U64 => "U64",
UintTy::U128 => "U128",
};
format!("LitIntType::Unsigned(UintTy::{t})")
}
LitIntType::Unsuffixed => String::from("LitIntType::Unsuffixed"),
};
kind!("Int({i}, {int_ty})");
},
LitKind::Float(_, suffix) => {
let float_ty = match suffix {
LitFloatType::Suffixed(suffix_ty) => format!("LitFloatType::Suffixed(FloatTy::{suffix_ty:?})"),
LitFloatType::Suffixed(suffix_ty) => {
let t = match suffix_ty {
FloatTy::F16 => "F16",
FloatTy::F32 => "F32",
FloatTy::F64 => "F64",
FloatTy::F128 => "F128",
};
format!("LitFloatType::Suffixed(FloatTy::{t})")
}
LitFloatType::Unsuffixed => String::from("LitFloatType::Unsuffixed"),
};
kind!("Float(_, {float_ty})");

View file

@ -10,7 +10,7 @@ use crate::{clip, is_direct_expn_of, sext, unsext};
use rustc_abi::Size;
use rustc_apfloat::Float;
use rustc_apfloat::ieee::{Half, Quad};
use rustc_ast::ast::{self, LitFloatType, LitKind};
use rustc_ast::ast::{LitFloatType, LitKind};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{
BinOpKind, Block, ConstBlock, Expr, ExprKind, HirId, Item, ItemKind, Node, PatExpr, PatExprKind, QPath, UnOp,
@ -309,10 +309,10 @@ pub fn lit_to_mir_constant<'tcx>(lit: &LitKind, ty: Option<Ty<'tcx>>) -> Constan
LitKind::Int(n, _) => Constant::Int(n.get()),
LitKind::Float(ref is, LitFloatType::Suffixed(fty)) => match fty {
// FIXME(f16_f128): just use `parse()` directly when available for `f16`/`f128`
ast::FloatTy::F16 => Constant::parse_f16(is.as_str()),
ast::FloatTy::F32 => Constant::F32(is.as_str().parse().unwrap()),
ast::FloatTy::F64 => Constant::F64(is.as_str().parse().unwrap()),
ast::FloatTy::F128 => Constant::parse_f128(is.as_str()),
FloatTy::F16 => Constant::parse_f16(is.as_str()),
FloatTy::F32 => Constant::F32(is.as_str().parse().unwrap()),
FloatTy::F64 => Constant::F64(is.as_str().parse().unwrap()),
FloatTy::F128 => Constant::parse_f128(is.as_str()),
},
LitKind::Float(ref is, LitFloatType::Unsuffixed) => match ty.expect("type of float is known").kind() {
ty::Float(FloatTy::F16) => Constant::parse_f16(is.as_str()),