mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-02 09:47:57 +00:00
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. The one annoying wrinkle is that the old version had differences in their `Debug` impls, e.g. one printed `u32` while the other printed `U32`. Some compiler error messages rely on the former (yuk), and some clippy output depends on the latter. So the commit also changes clippy to not rely on `Debug` and just implement what it needs itself.
This commit is contained in:
@@ -3417,6 +3417,7 @@ dependencies = [
|
||||
"rustc_data_structures",
|
||||
"rustc_macros",
|
||||
"rustc_serialize",
|
||||
"rustc_span",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,12 +8,14 @@ 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]
|
||||
default = ["nightly"]
|
||||
nightly = [
|
||||
"dep:rustc_serialize",
|
||||
"dep:rustc_span",
|
||||
"dep:rustc_data_structures",
|
||||
"dep:rustc_macros",
|
||||
]
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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_,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -2198,59 +2198,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);
|
||||
|
||||
@@ -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::*;
|
||||
|
||||
@@ -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::*;
|
||||
|
||||
@@ -1963,43 +1963,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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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})");
|
||||
|
||||
@@ -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()),
|
||||
|
||||
Reference in New Issue
Block a user