mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 21:35:20 +00:00
Merge #2225
2225: Reduce duplication between uncertain floats & ints r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
6ca0d79cff
7 changed files with 55 additions and 70 deletions
|
@ -77,9 +77,7 @@ pub use crate::{
|
||||||
source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
|
source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
|
||||||
ty::{
|
ty::{
|
||||||
display::HirDisplay,
|
display::HirDisplay,
|
||||||
primitive::{
|
primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness, Uncertain},
|
||||||
FloatBitness, FloatTy, IntBitness, IntTy, Signedness, UncertainFloatTy, UncertainIntTy,
|
|
||||||
},
|
|
||||||
ApplicationTy, CallableDef, Substs, TraitRef, Ty, TypeCtor, TypeWalk,
|
ApplicationTy, CallableDef, Substs, TraitRef, Ty, TypeCtor, TypeWalk,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -21,7 +21,7 @@ use crate::{
|
||||||
expr::ExprId,
|
expr::ExprId,
|
||||||
generics::{GenericParams, HasGenericParams},
|
generics::{GenericParams, HasGenericParams},
|
||||||
util::make_mut_slice,
|
util::make_mut_slice,
|
||||||
Adt, Crate, DefWithBody, Mutability, Name, Trait, TypeAlias,
|
Adt, Crate, DefWithBody, FloatTy, IntTy, Mutability, Name, Trait, TypeAlias, Uncertain,
|
||||||
};
|
};
|
||||||
use display::{HirDisplay, HirFormatter};
|
use display::{HirDisplay, HirFormatter};
|
||||||
|
|
||||||
|
@ -47,10 +47,10 @@ pub enum TypeCtor {
|
||||||
Char,
|
Char,
|
||||||
|
|
||||||
/// A primitive integer type. For example, `i32`.
|
/// A primitive integer type. For example, `i32`.
|
||||||
Int(primitive::UncertainIntTy),
|
Int(Uncertain<IntTy>),
|
||||||
|
|
||||||
/// A primitive floating-point type. For example, `f64`.
|
/// A primitive floating-point type. For example, `f64`.
|
||||||
Float(primitive::UncertainFloatTy),
|
Float(Uncertain<FloatTy>),
|
||||||
|
|
||||||
/// Structures, enumerations and unions.
|
/// Structures, enumerations and unions.
|
||||||
Adt(Adt),
|
Adt(Adt),
|
||||||
|
|
|
@ -31,10 +31,10 @@ use ra_prof::profile;
|
||||||
use test_utils::tested_by;
|
use test_utils::tested_by;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
lower, primitive,
|
lower,
|
||||||
traits::{Guidance, Obligation, ProjectionPredicate, Solution},
|
traits::{Guidance, Obligation, ProjectionPredicate, Solution},
|
||||||
ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypableDef,
|
ApplicationTy, InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypableDef,
|
||||||
TypeCtor, TypeWalk,
|
TypeCtor, TypeWalk, Uncertain,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
adt::VariantDef,
|
adt::VariantDef,
|
||||||
|
@ -43,7 +43,7 @@ use crate::{
|
||||||
expr::{BindingAnnotation, Body, ExprId, PatId},
|
expr::{BindingAnnotation, Body, ExprId, PatId},
|
||||||
resolve::{Resolver, TypeNs},
|
resolve::{Resolver, TypeNs},
|
||||||
ty::infer::diagnostics::InferenceDiagnostic,
|
ty::infer::diagnostics::InferenceDiagnostic,
|
||||||
Adt, AssocItem, ConstData, DefWithBody, FnData, Function, Path, StructField,
|
Adt, AssocItem, ConstData, DefWithBody, FloatTy, FnData, Function, IntTy, Path, StructField,
|
||||||
};
|
};
|
||||||
|
|
||||||
macro_rules! ty_app {
|
macro_rules! ty_app {
|
||||||
|
@ -358,14 +358,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty {
|
fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty {
|
||||||
match ty {
|
match ty {
|
||||||
Ty::Unknown => self.new_type_var(),
|
Ty::Unknown => self.new_type_var(),
|
||||||
Ty::Apply(ApplicationTy {
|
Ty::Apply(ApplicationTy { ctor: TypeCtor::Int(Uncertain::Unknown), .. }) => {
|
||||||
ctor: TypeCtor::Int(primitive::UncertainIntTy::Unknown),
|
self.new_integer_var()
|
||||||
..
|
}
|
||||||
}) => self.new_integer_var(),
|
Ty::Apply(ApplicationTy { ctor: TypeCtor::Float(Uncertain::Unknown), .. }) => {
|
||||||
Ty::Apply(ApplicationTy {
|
self.new_float_var()
|
||||||
ctor: TypeCtor::Float(primitive::UncertainFloatTy::Unknown),
|
}
|
||||||
..
|
|
||||||
}) => self.new_float_var(),
|
|
||||||
_ => ty,
|
_ => ty,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -684,12 +682,8 @@ impl InferTy {
|
||||||
fn fallback_value(self) -> Ty {
|
fn fallback_value(self) -> Ty {
|
||||||
match self {
|
match self {
|
||||||
InferTy::TypeVar(..) => Ty::Unknown,
|
InferTy::TypeVar(..) => Ty::Unknown,
|
||||||
InferTy::IntVar(..) => {
|
InferTy::IntVar(..) => Ty::simple(TypeCtor::Int(Uncertain::Known(IntTy::i32()))),
|
||||||
Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(primitive::IntTy::i32())))
|
InferTy::FloatVar(..) => Ty::simple(TypeCtor::Float(Uncertain::Known(FloatTy::f64()))),
|
||||||
}
|
|
||||||
InferTy::FloatVar(..) => Ty::simple(TypeCtor::Float(
|
|
||||||
primitive::UncertainFloatTy::Known(primitive::FloatTy::f64()),
|
|
||||||
)),
|
|
||||||
InferTy::MaybeNeverTypeVar(..) => Ty::simple(TypeCtor::Never),
|
InferTy::MaybeNeverTypeVar(..) => Ty::simple(TypeCtor::Never),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,10 @@
|
||||||
use std::iter::{repeat, repeat_with};
|
use std::iter::{repeat, repeat_with};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use hir_def::path::{GenericArg, GenericArgs};
|
use hir_def::{
|
||||||
|
builtin_type::Signedness,
|
||||||
|
path::{GenericArg, GenericArgs},
|
||||||
|
};
|
||||||
use hir_expand::name;
|
use hir_expand::name;
|
||||||
|
|
||||||
use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
|
use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
|
||||||
|
@ -12,8 +15,9 @@ use crate::{
|
||||||
expr::{self, Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
|
expr::{self, Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
|
||||||
generics::{GenericParams, HasGenericParams},
|
generics::{GenericParams, HasGenericParams},
|
||||||
ty::{
|
ty::{
|
||||||
autoderef, method_resolution, op, primitive, CallableDef, InferTy, Mutability, Namespace,
|
autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace,
|
||||||
Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk,
|
Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk,
|
||||||
|
Uncertain,
|
||||||
},
|
},
|
||||||
Adt, Name,
|
Adt, Name,
|
||||||
};
|
};
|
||||||
|
@ -337,13 +341,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
UnaryOp::Neg => {
|
UnaryOp::Neg => {
|
||||||
match &inner_ty {
|
match &inner_ty {
|
||||||
Ty::Apply(a_ty) => match a_ty.ctor {
|
Ty::Apply(a_ty) => match a_ty.ctor {
|
||||||
TypeCtor::Int(primitive::UncertainIntTy::Unknown)
|
TypeCtor::Int(Uncertain::Unknown)
|
||||||
| TypeCtor::Int(primitive::UncertainIntTy::Known(
|
| TypeCtor::Int(Uncertain::Known(IntTy {
|
||||||
primitive::IntTy {
|
signedness: Signedness::Signed,
|
||||||
signedness: primitive::Signedness::Signed,
|
..
|
||||||
..
|
}))
|
||||||
},
|
|
||||||
))
|
|
||||||
| TypeCtor::Float(..) => inner_ty,
|
| TypeCtor::Float(..) => inner_ty,
|
||||||
_ => Ty::Unknown,
|
_ => Ty::Unknown,
|
||||||
},
|
},
|
||||||
|
@ -428,9 +430,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
);
|
);
|
||||||
self.infer_expr(
|
self.infer_expr(
|
||||||
*repeat,
|
*repeat,
|
||||||
&Expectation::has_type(Ty::simple(TypeCtor::Int(
|
&Expectation::has_type(Ty::simple(TypeCtor::Int(Uncertain::Known(
|
||||||
primitive::UncertainIntTy::Known(primitive::IntTy::usize()),
|
IntTy::usize(),
|
||||||
))),
|
)))),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -443,9 +445,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
|
Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
|
||||||
}
|
}
|
||||||
Literal::ByteString(..) => {
|
Literal::ByteString(..) => {
|
||||||
let byte_type = Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(
|
let byte_type = Ty::simple(TypeCtor::Int(Uncertain::Known(IntTy::u8())));
|
||||||
primitive::IntTy::u8(),
|
|
||||||
)));
|
|
||||||
let slice_type = Ty::apply_one(TypeCtor::Slice, byte_type);
|
let slice_type = Ty::apply_one(TypeCtor::Slice, byte_type);
|
||||||
Ty::apply_one(TypeCtor::Ref(Mutability::Shared), slice_type)
|
Ty::apply_one(TypeCtor::Ref(Mutability::Shared), slice_type)
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ use crate::{
|
||||||
generics::{GenericDef, WherePredicate},
|
generics::{GenericDef, WherePredicate},
|
||||||
resolve::{Resolver, TypeNs},
|
resolve::{Resolver, TypeNs},
|
||||||
ty::{
|
ty::{
|
||||||
primitive::{FloatTy, IntTy, UncertainFloatTy, UncertainIntTy},
|
primitive::{FloatTy, IntTy, Uncertain},
|
||||||
Adt,
|
Adt,
|
||||||
},
|
},
|
||||||
util::make_mut_slice,
|
util::make_mut_slice,
|
||||||
|
@ -674,20 +674,20 @@ impl From<BuiltinFloat> for FloatTy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Option<BuiltinInt>> for UncertainIntTy {
|
impl From<Option<BuiltinInt>> for Uncertain<IntTy> {
|
||||||
fn from(t: Option<BuiltinInt>) -> Self {
|
fn from(t: Option<BuiltinInt>) -> Self {
|
||||||
match t {
|
match t {
|
||||||
None => UncertainIntTy::Unknown,
|
None => Uncertain::Unknown,
|
||||||
Some(t) => UncertainIntTy::Known(t.into()),
|
Some(t) => Uncertain::Known(t.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Option<BuiltinFloat>> for UncertainFloatTy {
|
impl From<Option<BuiltinFloat>> for Uncertain<FloatTy> {
|
||||||
fn from(t: Option<BuiltinFloat>) -> Self {
|
fn from(t: Option<BuiltinFloat>) -> Self {
|
||||||
match t {
|
match t {
|
||||||
None => UncertainFloatTy::Unknown,
|
None => Uncertain::Unknown,
|
||||||
Some(t) => UncertainFloatTy::Known(t.into()),
|
Some(t) => Uncertain::Known(t.into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,16 +8,17 @@ use arrayvec::ArrayVec;
|
||||||
use hir_def::CrateModuleId;
|
use hir_def::CrateModuleId;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use super::{autoderef, lower, Canonical, InEnvironment, TraitEnvironment, TraitRef};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
impl_block::{ImplBlock, ImplId},
|
impl_block::{ImplBlock, ImplId},
|
||||||
resolve::Resolver,
|
resolve::Resolver,
|
||||||
ty::primitive::{FloatBitness, UncertainFloatTy, UncertainIntTy},
|
ty::primitive::{FloatBitness, Uncertain},
|
||||||
ty::{Ty, TypeCtor},
|
ty::{Ty, TypeCtor},
|
||||||
AssocItem, Crate, Function, Module, Mutability, Name, Trait,
|
AssocItem, Crate, Function, Module, Mutability, Name, Trait,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::{autoderef, lower, Canonical, InEnvironment, TraitEnvironment, TraitRef};
|
||||||
|
|
||||||
/// This is used as a key for indexing impls.
|
/// This is used as a key for indexing impls.
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum TyFingerprint {
|
pub enum TyFingerprint {
|
||||||
|
@ -140,14 +141,12 @@ fn def_crates(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option<ArrayV
|
||||||
TypeCtor::Adt(def_id) => Some(std::iter::once(def_id.krate(db)?).collect()),
|
TypeCtor::Adt(def_id) => Some(std::iter::once(def_id.krate(db)?).collect()),
|
||||||
TypeCtor::Bool => lang_item_crate!(db, cur_crate, "bool"),
|
TypeCtor::Bool => lang_item_crate!(db, cur_crate, "bool"),
|
||||||
TypeCtor::Char => lang_item_crate!(db, cur_crate, "char"),
|
TypeCtor::Char => lang_item_crate!(db, cur_crate, "char"),
|
||||||
TypeCtor::Float(UncertainFloatTy::Known(f)) => match f.bitness {
|
TypeCtor::Float(Uncertain::Known(f)) => match f.bitness {
|
||||||
// There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
|
// There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
|
||||||
FloatBitness::X32 => lang_item_crate!(db, cur_crate, "f32", "f32_runtime"),
|
FloatBitness::X32 => lang_item_crate!(db, cur_crate, "f32", "f32_runtime"),
|
||||||
FloatBitness::X64 => lang_item_crate!(db, cur_crate, "f64", "f64_runtime"),
|
FloatBitness::X64 => lang_item_crate!(db, cur_crate, "f64", "f64_runtime"),
|
||||||
},
|
},
|
||||||
TypeCtor::Int(UncertainIntTy::Known(i)) => {
|
TypeCtor::Int(Uncertain::Known(i)) => lang_item_crate!(db, cur_crate, i.ty_to_string()),
|
||||||
lang_item_crate!(db, cur_crate, i.ty_to_string())
|
|
||||||
}
|
|
||||||
TypeCtor::Str => lang_item_crate!(db, cur_crate, "str_alloc", "str"),
|
TypeCtor::Str => lang_item_crate!(db, cur_crate, "str_alloc", "str"),
|
||||||
TypeCtor::Slice => lang_item_crate!(db, cur_crate, "slice_alloc", "slice"),
|
TypeCtor::Slice => lang_item_crate!(db, cur_crate, "slice_alloc", "slice"),
|
||||||
TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!(db, cur_crate, "const_ptr"),
|
TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!(db, cur_crate, "const_ptr"),
|
||||||
|
|
|
@ -4,44 +4,38 @@ use std::fmt;
|
||||||
|
|
||||||
pub use hir_def::builtin_type::{FloatBitness, IntBitness, Signedness};
|
pub use hir_def::builtin_type::{FloatBitness, IntBitness, Signedness};
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
|
||||||
pub enum UncertainIntTy {
|
pub enum Uncertain<T> {
|
||||||
Unknown,
|
Unknown,
|
||||||
Known(IntTy),
|
Known(T),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<IntTy> for UncertainIntTy {
|
impl From<IntTy> for Uncertain<IntTy> {
|
||||||
fn from(ty: IntTy) -> Self {
|
fn from(ty: IntTy) -> Self {
|
||||||
UncertainIntTy::Known(ty)
|
Uncertain::Known(ty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for UncertainIntTy {
|
impl fmt::Display for Uncertain<IntTy> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
UncertainIntTy::Unknown => write!(f, "{{integer}}"),
|
Uncertain::Unknown => write!(f, "{{integer}}"),
|
||||||
UncertainIntTy::Known(ty) => write!(f, "{}", ty),
|
Uncertain::Known(ty) => write!(f, "{}", ty),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
impl From<FloatTy> for Uncertain<FloatTy> {
|
||||||
pub enum UncertainFloatTy {
|
|
||||||
Unknown,
|
|
||||||
Known(FloatTy),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<FloatTy> for UncertainFloatTy {
|
|
||||||
fn from(ty: FloatTy) -> Self {
|
fn from(ty: FloatTy) -> Self {
|
||||||
UncertainFloatTy::Known(ty)
|
Uncertain::Known(ty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for UncertainFloatTy {
|
impl fmt::Display for Uncertain<FloatTy> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
UncertainFloatTy::Unknown => write!(f, "{{float}}"),
|
Uncertain::Unknown => write!(f, "{{float}}"),
|
||||||
UncertainFloatTy::Known(ty) => write!(f, "{}", ty),
|
Uncertain::Known(ty) => write!(f, "{}", ty),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue