Skip to content

Commit

Permalink
Rollup merge of rust-lang#79951 - LeSeulArtichaut:ty-ir, r=nikomatsakis
Browse files Browse the repository at this point in the history
Refractor a few more types to `rustc_type_ir`

In the continuation of rust-lang#79169, ~~blocked on that PR~~.

This PR:
 - moves `IntVarValue`, `FloatVarValue`, `InferTy` (and friends) and `Variance`
 - creates the `IntTy`, `UintTy` and `FloatTy` enums in `rustc_type_ir`, based on their `ast` and `chalk_ir` equilavents, and uses them for types in the rest of the compiler.

~~I will split up that commit to make this easier to review and to have a better commit history.~~
EDIT: done, I split the PR in commits of 200-ish lines each

r? `````@nikomatsakis````` cc `````@jackh726`````
  • Loading branch information
JohnTitor committed Jan 28, 2021
2 parents 025a850 + e0d64b9 commit 446edd1
Show file tree
Hide file tree
Showing 46 changed files with 818 additions and 565 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4345,6 +4345,7 @@ dependencies = [
"bitflags",
"rustc_data_structures",
"rustc_index",
"rustc_macros",
"rustc_serialize",
]

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,14 @@ mod vtable;
mod prelude {
pub(crate) use std::convert::{TryFrom, TryInto};

pub(crate) use rustc_ast::ast::{FloatTy, IntTy, UintTy};
pub(crate) use rustc_span::Span;

pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
pub(crate) use rustc_middle::bug;
pub(crate) use rustc_middle::mir::{self, *};
pub(crate) use rustc_middle::ty::layout::{self, TyAndLayout};
pub(crate) use rustc_middle::ty::{
self, FnSig, Instance, InstanceDef, ParamEnv, Ty, TyCtxt, TypeAndMut, TypeFoldable,
self, FloatTy, FnSig, Instance, InstanceDef, IntTy, ParamEnv, Ty, TyCtxt, TypeAndMut, TypeFoldable, UintTy,
};
pub(crate) use rustc_target::abi::{Abi, LayoutOf, Scalar, Size, VariantIdx};

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,8 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
lhs: Self::Value,
rhs: Self::Value,
) -> (Self::Value, Self::Value) {
use rustc_ast::IntTy::*;
use rustc_ast::UintTy::*;
use rustc_middle::ty::{Int, Uint};
use rustc_middle::ty::{IntTy::*, UintTy::*};

let new_kind = match ty.kind() {
Int(t @ Isize) => Int(t.normalize(self.tcx.sess.target.pointer_width)),
Expand Down
35 changes: 17 additions & 18 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::llvm::debuginfo::{
};
use crate::value::Value;

use rustc_ast as ast;
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::const_cstr;
use rustc_data_structures::fingerprint::Fingerprint;
Expand Down Expand Up @@ -830,37 +829,37 @@ trait MsvcBasicName {
fn msvc_basic_name(self) -> &'static str;
}

impl MsvcBasicName for ast::IntTy {
impl MsvcBasicName for ty::IntTy {
fn msvc_basic_name(self) -> &'static str {
match self {
ast::IntTy::Isize => "ptrdiff_t",
ast::IntTy::I8 => "__int8",
ast::IntTy::I16 => "__int16",
ast::IntTy::I32 => "__int32",
ast::IntTy::I64 => "__int64",
ast::IntTy::I128 => "__int128",
ty::IntTy::Isize => "ptrdiff_t",
ty::IntTy::I8 => "__int8",
ty::IntTy::I16 => "__int16",
ty::IntTy::I32 => "__int32",
ty::IntTy::I64 => "__int64",
ty::IntTy::I128 => "__int128",
}
}
}

impl MsvcBasicName for ast::UintTy {
impl MsvcBasicName for ty::UintTy {
fn msvc_basic_name(self) -> &'static str {
match self {
ast::UintTy::Usize => "size_t",
ast::UintTy::U8 => "unsigned __int8",
ast::UintTy::U16 => "unsigned __int16",
ast::UintTy::U32 => "unsigned __int32",
ast::UintTy::U64 => "unsigned __int64",
ast::UintTy::U128 => "unsigned __int128",
ty::UintTy::Usize => "size_t",
ty::UintTy::U8 => "unsigned __int8",
ty::UintTy::U16 => "unsigned __int16",
ty::UintTy::U32 => "unsigned __int32",
ty::UintTy::U64 => "unsigned __int64",
ty::UintTy::U128 => "unsigned __int128",
}
}
}

impl MsvcBasicName for ast::FloatTy {
impl MsvcBasicName for ty::FloatTy {
fn msvc_basic_name(self) -> &'static str {
match self {
ast::FloatTy::F32 => "float",
ast::FloatTy::F64 => "double",
ty::FloatTy::F32 => "float",
ty::FloatTy::F64 => "double",
}
}
}
Expand Down
37 changes: 18 additions & 19 deletions compiler/rustc_codegen_llvm/src/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ use crate::llvm;
use crate::llvm::{Bool, False, True};
use crate::type_of::LayoutLlvmExt;
use crate::value::Value;
use rustc_ast as ast;
use rustc_codegen_ssa::common::TypeKind;
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_middle::bug;
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::Ty;
use rustc_middle::ty::{self, Ty};
use rustc_target::abi::call::{CastTarget, FnAbi, Reg};
use rustc_target::abi::{AddressSpace, Align, Integer, Size};

Expand Down Expand Up @@ -80,32 +79,32 @@ impl CodegenCx<'ll, 'tcx> {
self.type_i8()
}

crate fn type_int_from_ty(&self, t: ast::IntTy) -> &'ll Type {
crate fn type_int_from_ty(&self, t: ty::IntTy) -> &'ll Type {
match t {
ast::IntTy::Isize => self.type_isize(),
ast::IntTy::I8 => self.type_i8(),
ast::IntTy::I16 => self.type_i16(),
ast::IntTy::I32 => self.type_i32(),
ast::IntTy::I64 => self.type_i64(),
ast::IntTy::I128 => self.type_i128(),
ty::IntTy::Isize => self.type_isize(),
ty::IntTy::I8 => self.type_i8(),
ty::IntTy::I16 => self.type_i16(),
ty::IntTy::I32 => self.type_i32(),
ty::IntTy::I64 => self.type_i64(),
ty::IntTy::I128 => self.type_i128(),
}
}

crate fn type_uint_from_ty(&self, t: ast::UintTy) -> &'ll Type {
crate fn type_uint_from_ty(&self, t: ty::UintTy) -> &'ll Type {
match t {
ast::UintTy::Usize => self.type_isize(),
ast::UintTy::U8 => self.type_i8(),
ast::UintTy::U16 => self.type_i16(),
ast::UintTy::U32 => self.type_i32(),
ast::UintTy::U64 => self.type_i64(),
ast::UintTy::U128 => self.type_i128(),
ty::UintTy::Usize => self.type_isize(),
ty::UintTy::U8 => self.type_i8(),
ty::UintTy::U16 => self.type_i16(),
ty::UintTy::U32 => self.type_i32(),
ty::UintTy::U64 => self.type_i64(),
ty::UintTy::U128 => self.type_i128(),
}
}

crate fn type_float_from_ty(&self, t: ast::FloatTy) -> &'ll Type {
crate fn type_float_from_ty(&self, t: ty::FloatTy) -> &'ll Type {
match t {
ast::FloatTy::F32 => self.type_f32(),
ast::FloatTy::F64 => self.type_f64(),
ty::FloatTy::F32 => self.type_f32(),
ty::FloatTy::F64 => self.type_f64(),
}
}

Expand Down
20 changes: 8 additions & 12 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,20 +875,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
ty::Uint(_) => value.to_string(),
ty::Int(int_ty) => {
match int_ty.normalize(bx.tcx().sess.target.pointer_width) {
ast::IntTy::I8 => (value as i8).to_string(),
ast::IntTy::I16 => (value as i16).to_string(),
ast::IntTy::I32 => (value as i32).to_string(),
ast::IntTy::I64 => (value as i64).to_string(),
ast::IntTy::I128 => (value as i128).to_string(),
ast::IntTy::Isize => unreachable!(),
ty::IntTy::I8 => (value as i8).to_string(),
ty::IntTy::I16 => (value as i16).to_string(),
ty::IntTy::I32 => (value as i32).to_string(),
ty::IntTy::I64 => (value as i64).to_string(),
ty::IntTy::I128 => (value as i128).to_string(),
ty::IntTy::Isize => unreachable!(),
}
}
ty::Float(ast::FloatTy::F32) => {
f32::from_bits(value as u32).to_string()
}
ty::Float(ast::FloatTy::F64) => {
f64::from_bits(value as u64).to_string()
}
ty::Float(ty::FloatTy::F32) => f32::from_bits(value as u32).to_string(),
ty::Float(ty::FloatTy::F64) => f64::from_bits(value as u64).to_string(),
_ => span_bug!(span, "asm const has bad type {}", ty),
};
InlineAsmOperandRef::Const { string }
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_infer/src/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use super::{InferCtxt, MiscVariable, TypeTrace};

use crate::traits::{Obligation, PredicateObligations};

use rustc_ast as ast;
use rustc_data_structures::sso::SsoHashMap;
use rustc_hir::def_id::DefId;
use rustc_middle::traits::ObligationCause;
Expand Down Expand Up @@ -281,7 +280,7 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
&self,
vid_is_expected: bool,
vid: ty::FloatVid,
val: ast::FloatTy,
val: ty::FloatTy,
) -> RelateResult<'tcx, Ty<'tcx>> {
self.inner
.borrow_mut()
Expand Down
60 changes: 37 additions & 23 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,25 @@ fn lint_overflowing_range_endpoint<'tcx>(

// For `isize` & `usize`, be conservative with the warnings, so that the
// warnings are consistent between 32- and 64-bit platforms.
fn int_ty_range(int_ty: ast::IntTy) -> (i128, i128) {
fn int_ty_range(int_ty: ty::IntTy) -> (i128, i128) {
match int_ty {
ast::IntTy::Isize => (i64::MIN.into(), i64::MAX.into()),
ast::IntTy::I8 => (i8::MIN.into(), i8::MAX.into()),
ast::IntTy::I16 => (i16::MIN.into(), i16::MAX.into()),
ast::IntTy::I32 => (i32::MIN.into(), i32::MAX.into()),
ast::IntTy::I64 => (i64::MIN.into(), i64::MAX.into()),
ast::IntTy::I128 => (i128::MIN, i128::MAX),
ty::IntTy::Isize => (i64::MIN.into(), i64::MAX.into()),
ty::IntTy::I8 => (i8::MIN.into(), i8::MAX.into()),
ty::IntTy::I16 => (i16::MIN.into(), i16::MAX.into()),
ty::IntTy::I32 => (i32::MIN.into(), i32::MAX.into()),
ty::IntTy::I64 => (i64::MIN.into(), i64::MAX.into()),
ty::IntTy::I128 => (i128::MIN, i128::MAX),
}
}

fn uint_ty_range(uint_ty: ast::UintTy) -> (u128, u128) {
fn uint_ty_range(uint_ty: ty::UintTy) -> (u128, u128) {
let max = match uint_ty {
ast::UintTy::Usize => u64::MAX.into(),
ast::UintTy::U8 => u8::MAX.into(),
ast::UintTy::U16 => u16::MAX.into(),
ast::UintTy::U32 => u32::MAX.into(),
ast::UintTy::U64 => u64::MAX.into(),
ast::UintTy::U128 => u128::MAX,
ty::UintTy::Usize => u64::MAX.into(),
ty::UintTy::U8 => u8::MAX.into(),
ty::UintTy::U16 => u16::MAX.into(),
ty::UintTy::U32 => u32::MAX.into(),
ty::UintTy::U64 => u64::MAX.into(),
ty::UintTy::U128 => u128::MAX,
};
(0, max)
}
Expand Down Expand Up @@ -258,8 +258,8 @@ fn report_bin_hex_error(
//
// No suggestion for: `isize`, `usize`.
fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static str> {
use rustc_ast::IntTy::*;
use rustc_ast::UintTy::*;
use ty::IntTy::*;
use ty::UintTy::*;
macro_rules! find_fit {
($ty:expr, $val:expr, $negative:expr,
$($type:ident => [$($utypes:expr),*] => [$($itypes:expr),*]),+) => {
Expand Down Expand Up @@ -302,7 +302,7 @@ fn lint_int_literal<'tcx>(
type_limits: &TypeLimits,
e: &'tcx hir::Expr<'tcx>,
lit: &hir::Lit,
t: ast::IntTy,
t: ty::IntTy,
v: u128,
) {
let int_type = t.normalize(cx.sess().target.pointer_width);
Expand All @@ -314,7 +314,14 @@ fn lint_int_literal<'tcx>(
// avoiding use of -min to prevent overflow/panic
if (negative && v > max + 1) || (!negative && v > max) {
if let Some(repr_str) = get_bin_hex_repr(cx, lit) {
report_bin_hex_error(cx, e, attr::IntType::SignedInt(t), repr_str, v, negative);
report_bin_hex_error(
cx,
e,
attr::IntType::SignedInt(ty::ast_int_ty(t)),
repr_str,
v,
negative,
);
return;
}

Expand Down Expand Up @@ -351,7 +358,7 @@ fn lint_uint_literal<'tcx>(
cx: &LateContext<'tcx>,
e: &'tcx hir::Expr<'tcx>,
lit: &hir::Lit,
t: ast::UintTy,
t: ty::UintTy,
) {
let uint_type = t.normalize(cx.sess().target.pointer_width);
let (min, max) = uint_ty_range(uint_type);
Expand Down Expand Up @@ -391,7 +398,14 @@ fn lint_uint_literal<'tcx>(
}
}
if let Some(repr_str) = get_bin_hex_repr(cx, lit) {
report_bin_hex_error(cx, e, attr::IntType::UnsignedInt(t), repr_str, lit_val, false);
report_bin_hex_error(
cx,
e,
attr::IntType::UnsignedInt(ty::ast_uint_ty(t)),
repr_str,
lit_val,
false,
);
return;
}
cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
Expand Down Expand Up @@ -430,8 +444,8 @@ fn lint_literal<'tcx>(
ty::Float(t) => {
let is_infinite = match lit.node {
ast::LitKind::Float(v, _) => match t {
ast::FloatTy::F32 => v.as_str().parse().map(f32::is_infinite),
ast::FloatTy::F64 => v.as_str().parse().map(f64::is_infinite),
ty::FloatTy::F32 => v.as_str().parse().map(f32::is_infinite),
ty::FloatTy::F64 => v.as_str().parse().map(f64::is_infinite),
},
_ => bug!(),
};
Expand Down Expand Up @@ -984,7 +998,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
help: Some("consider using `u32` or `libc::wchar_t` instead".into()),
},

ty::Int(ast::IntTy::I128) | ty::Uint(ast::UintTy::U128) => FfiUnsafe {
ty::Int(ty::IntTy::I128) | ty::Uint(ty::UintTy::U128) => FfiUnsafe {
ty,
reason: "128-bit integers don't currently have a known stable ABI".into(),
help: None,
Expand Down
Loading

0 comments on commit 446edd1

Please sign in to comment.