Skip to content

Commit d6a9497

Browse files
authored
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`
2 parents c78d589 + 618a90d commit d6a9497

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

clippy_lints/src/float_literal.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::numeric_literal;
3-
use rustc_ast::ast::{self, LitFloatType, LitKind};
3+
use rustc_ast::ast::{LitFloatType, LitKind};
44
use rustc_errors::Applicability;
55
use rustc_hir as hir;
66
use rustc_lint::{LateContext, LateLintPass};
@@ -75,10 +75,10 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
7575
let digits = count_digits(sym_str);
7676
let max = max_digits(fty);
7777
let type_suffix = match lit_float_ty {
78-
LitFloatType::Suffixed(ast::FloatTy::F16) => Some("f16"),
79-
LitFloatType::Suffixed(ast::FloatTy::F32) => Some("f32"),
80-
LitFloatType::Suffixed(ast::FloatTy::F64) => Some("f64"),
81-
LitFloatType::Suffixed(ast::FloatTy::F128) => Some("f128"),
78+
LitFloatType::Suffixed(FloatTy::F16) => Some("f16"),
79+
LitFloatType::Suffixed(FloatTy::F32) => Some("f32"),
80+
LitFloatType::Suffixed(FloatTy::F64) => Some("f64"),
81+
LitFloatType::Suffixed(FloatTy::F128) => Some("f128"),
8282
LitFloatType::Unsuffixed => None,
8383
};
8484
let (is_whole, is_inf, mut float_str) = match fty {

clippy_lints/src/utils/author.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_hir::{
99
FnRetTy, HirId, Lit, PatExprKind, PatKind, QPath, StmtKind, StructTailExpr,
1010
};
1111
use rustc_lint::{LateContext, LateLintPass, LintContext};
12+
use rustc_middle::ty::{FloatTy, IntTy, UintTy};
1213
use rustc_session::declare_lint_pass;
1314
use rustc_span::symbol::{Ident, Symbol};
1415
use std::cell::Cell;
@@ -337,15 +338,43 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
337338
LitKind::Byte(b) => kind!("Byte({b})"),
338339
LitKind::Int(i, suffix) => {
339340
let int_ty = match suffix {
340-
LitIntType::Signed(int_ty) => format!("LitIntType::Signed(IntTy::{int_ty:?})"),
341-
LitIntType::Unsigned(uint_ty) => format!("LitIntType::Unsigned(UintTy::{uint_ty:?})"),
341+
LitIntType::Signed(int_ty) => {
342+
let t = match int_ty {
343+
IntTy::Isize => "Isize",
344+
IntTy::I8 => "I8",
345+
IntTy::I16 => "I16",
346+
IntTy::I32 => "I32",
347+
IntTy::I64 => "I64",
348+
IntTy::I128 => "I128",
349+
};
350+
format!("LitIntType::Signed(IntTy::{t})")
351+
}
352+
LitIntType::Unsigned(uint_ty) => {
353+
let t = match uint_ty {
354+
UintTy::Usize => "Usize",
355+
UintTy::U8 => "U8",
356+
UintTy::U16 => "U16",
357+
UintTy::U32 => "U32",
358+
UintTy::U64 => "U64",
359+
UintTy::U128 => "U128",
360+
};
361+
format!("LitIntType::Unsigned(UintTy::{t})")
362+
}
342363
LitIntType::Unsuffixed => String::from("LitIntType::Unsuffixed"),
343364
};
344365
kind!("Int({i}, {int_ty})");
345366
},
346367
LitKind::Float(_, suffix) => {
347368
let float_ty = match suffix {
348-
LitFloatType::Suffixed(suffix_ty) => format!("LitFloatType::Suffixed(FloatTy::{suffix_ty:?})"),
369+
LitFloatType::Suffixed(suffix_ty) => {
370+
let t = match suffix_ty {
371+
FloatTy::F16 => "F16",
372+
FloatTy::F32 => "F32",
373+
FloatTy::F64 => "F64",
374+
FloatTy::F128 => "F128",
375+
};
376+
format!("LitFloatType::Suffixed(FloatTy::{t})")
377+
}
349378
LitFloatType::Unsuffixed => String::from("LitFloatType::Unsuffixed"),
350379
};
351380
kind!("Float(_, {float_ty})");

clippy_utils/src/consts.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{clip, is_direct_expn_of, sext, unsext};
1010
use rustc_abi::Size;
1111
use rustc_apfloat::Float;
1212
use rustc_apfloat::ieee::{Half, Quad};
13-
use rustc_ast::ast::{self, LitFloatType, LitKind};
13+
use rustc_ast::ast::{LitFloatType, LitKind};
1414
use rustc_hir::def::{DefKind, Res};
1515
use rustc_hir::{
1616
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
309309
LitKind::Int(n, _) => Constant::Int(n.get()),
310310
LitKind::Float(ref is, LitFloatType::Suffixed(fty)) => match fty {
311311
// FIXME(f16_f128): just use `parse()` directly when available for `f16`/`f128`
312-
ast::FloatTy::F16 => Constant::parse_f16(is.as_str()),
313-
ast::FloatTy::F32 => Constant::F32(is.as_str().parse().unwrap()),
314-
ast::FloatTy::F64 => Constant::F64(is.as_str().parse().unwrap()),
315-
ast::FloatTy::F128 => Constant::parse_f128(is.as_str()),
312+
FloatTy::F16 => Constant::parse_f16(is.as_str()),
313+
FloatTy::F32 => Constant::F32(is.as_str().parse().unwrap()),
314+
FloatTy::F64 => Constant::F64(is.as_str().parse().unwrap()),
315+
FloatTy::F128 => Constant::parse_f128(is.as_str()),
316316
},
317317
LitKind::Float(ref is, LitFloatType::Unsuffixed) => match ty.expect("type of float is known").kind() {
318318
ty::Float(FloatTy::F16) => Constant::parse_f16(is.as_str()),

0 commit comments

Comments
 (0)