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 d6a9497ccc
3 changed files with 42 additions and 13 deletions

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()),