mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
add ModuleDef::BuiltInType
This commit is contained in:
parent
6b88735fe6
commit
e6545cc647
5 changed files with 61 additions and 10 deletions
|
@ -9,7 +9,7 @@ use crate::{
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
nameres::{ModuleScope, Namespace, ImportId, CrateModuleId},
|
nameres::{ModuleScope, Namespace, ImportId, CrateModuleId},
|
||||||
expr::{Body, BodySourceMap, validation::ExprValidator},
|
expr::{Body, BodySourceMap, validation::ExprValidator},
|
||||||
ty::{TraitRef, InferenceResult},
|
ty::{TraitRef, InferenceResult, primitive::{IntTy, FloatTy}},
|
||||||
adt::{EnumVariantId, StructFieldId, VariantDef},
|
adt::{EnumVariantId, StructFieldId, VariantDef},
|
||||||
generics::HasGenericParams,
|
generics::HasGenericParams,
|
||||||
docs::{Documentation, Docs, docs_from_ast},
|
docs::{Documentation, Docs, docs_from_ast},
|
||||||
|
@ -75,6 +75,15 @@ pub struct Module {
|
||||||
pub(crate) module_id: CrateModuleId,
|
pub(crate) module_id: CrateModuleId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum BuiltinType {
|
||||||
|
Char,
|
||||||
|
Bool,
|
||||||
|
Str,
|
||||||
|
Int(IntTy),
|
||||||
|
Float(FloatTy),
|
||||||
|
}
|
||||||
|
|
||||||
/// The defs which can be visible in the module.
|
/// The defs which can be visible in the module.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub enum ModuleDef {
|
pub enum ModuleDef {
|
||||||
|
@ -89,6 +98,7 @@ pub enum ModuleDef {
|
||||||
Static(Static),
|
Static(Static),
|
||||||
Trait(Trait),
|
Trait(Trait),
|
||||||
TypeAlias(TypeAlias),
|
TypeAlias(TypeAlias),
|
||||||
|
BuiltinType(BuiltinType),
|
||||||
}
|
}
|
||||||
impl_froms!(
|
impl_froms!(
|
||||||
ModuleDef: Module,
|
ModuleDef: Module,
|
||||||
|
@ -100,7 +110,8 @@ impl_froms!(
|
||||||
Const,
|
Const,
|
||||||
Static,
|
Static,
|
||||||
Trait,
|
Trait,
|
||||||
TypeAlias
|
TypeAlias,
|
||||||
|
BuiltinType
|
||||||
);
|
);
|
||||||
|
|
||||||
pub enum ModuleSource {
|
pub enum ModuleSource {
|
||||||
|
|
|
@ -80,5 +80,6 @@ pub use self::code_model::{
|
||||||
Function, FnSignature,
|
Function, FnSignature,
|
||||||
StructField, FieldSource,
|
StructField, FieldSource,
|
||||||
Static, Const, ConstSignature,
|
Static, Const, ConstSignature,
|
||||||
Trait, TypeAlias, MacroDef, Container
|
Trait, TypeAlias, MacroDef, Container,
|
||||||
|
BuiltinType,
|
||||||
};
|
};
|
||||||
|
|
|
@ -649,7 +649,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
| TypableDef::Function(_)
|
| TypableDef::Function(_)
|
||||||
| TypableDef::Enum(_)
|
| TypableDef::Enum(_)
|
||||||
| TypableDef::Const(_)
|
| TypableDef::Const(_)
|
||||||
| TypableDef::Static(_) => (Ty::Unknown, None),
|
| TypableDef::Static(_)
|
||||||
|
| TypableDef::BuiltinType(_) => (Ty::Unknown, None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ use std::iter;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Function, Struct, Union, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static,
|
Function, Struct, Union, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static,
|
||||||
HirDatabase,
|
HirDatabase, BuiltinType,
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
name::KnownName,
|
name::KnownName,
|
||||||
nameres::Namespace,
|
nameres::Namespace,
|
||||||
|
@ -66,7 +66,7 @@ impl Ty {
|
||||||
|
|
||||||
pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self {
|
pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self {
|
||||||
if let Some(name) = path.as_ident() {
|
if let Some(name) = path.as_ident() {
|
||||||
// FIXME handle primitive type names in resolver as well?
|
// TODO: remove this
|
||||||
if let Some(int_ty) = primitive::IntTy::from_type_name(name) {
|
if let Some(int_ty) = primitive::IntTy::from_type_name(name) {
|
||||||
return Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(int_ty)));
|
return Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(int_ty)));
|
||||||
} else if let Some(float_ty) = primitive::FloatTy::from_type_name(name) {
|
} else if let Some(float_ty) = primitive::FloatTy::from_type_name(name) {
|
||||||
|
@ -128,7 +128,7 @@ impl Ty {
|
||||||
TypableDef::Enum(e) => Some(e.into()),
|
TypableDef::Enum(e) => Some(e.into()),
|
||||||
TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()),
|
TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()),
|
||||||
TypableDef::TypeAlias(t) => Some(t.into()),
|
TypableDef::TypeAlias(t) => Some(t.into()),
|
||||||
TypableDef::Const(_) | TypableDef::Static(_) => None,
|
TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::BuiltinType(_) => None,
|
||||||
};
|
};
|
||||||
substs_from_path_segment(db, resolver, segment, def_generic, false)
|
substs_from_path_segment(db, resolver, segment, def_generic, false)
|
||||||
}
|
}
|
||||||
|
@ -149,7 +149,8 @@ impl Ty {
|
||||||
| TypableDef::Enum(_)
|
| TypableDef::Enum(_)
|
||||||
| TypableDef::Const(_)
|
| TypableDef::Const(_)
|
||||||
| TypableDef::Static(_)
|
| TypableDef::Static(_)
|
||||||
| TypableDef::TypeAlias(_) => last,
|
| TypableDef::TypeAlias(_)
|
||||||
|
| TypableDef::BuiltinType(_) => last,
|
||||||
TypableDef::EnumVariant(_) => {
|
TypableDef::EnumVariant(_) => {
|
||||||
// the generic args for an enum variant may be either specified
|
// the generic args for an enum variant may be either specified
|
||||||
// on the segment referring to the enum, or on the segment
|
// on the segment referring to the enum, or on the segment
|
||||||
|
@ -299,6 +300,7 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace
|
||||||
(TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t),
|
(TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t),
|
||||||
(TypableDef::Const(c), Namespace::Values) => type_for_const(db, c),
|
(TypableDef::Const(c), Namespace::Values) => type_for_const(db, c),
|
||||||
(TypableDef::Static(c), Namespace::Values) => type_for_static(db, c),
|
(TypableDef::Static(c), Namespace::Values) => type_for_static(db, c),
|
||||||
|
(TypableDef::BuiltinType(t), Namespace::Types) => type_for_builtin(t),
|
||||||
|
|
||||||
// 'error' cases:
|
// 'error' cases:
|
||||||
(TypableDef::Function(_), Namespace::Types) => Ty::Unknown,
|
(TypableDef::Function(_), Namespace::Types) => Ty::Unknown,
|
||||||
|
@ -308,6 +310,7 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace
|
||||||
(TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown,
|
(TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown,
|
||||||
(TypableDef::Const(_), Namespace::Types) => Ty::Unknown,
|
(TypableDef::Const(_), Namespace::Types) => Ty::Unknown,
|
||||||
(TypableDef::Static(_), Namespace::Types) => Ty::Unknown,
|
(TypableDef::Static(_), Namespace::Types) => Ty::Unknown,
|
||||||
|
(TypableDef::BuiltinType(_), Namespace::Values) => Ty::Unknown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -399,6 +402,17 @@ fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty {
|
||||||
Ty::from_hir(db, &resolver, signature.type_ref())
|
Ty::from_hir(db, &resolver, signature.type_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Build the declared type of a static.
|
||||||
|
fn type_for_builtin(def: BuiltinType) -> Ty {
|
||||||
|
Ty::simple(match def {
|
||||||
|
BuiltinType::Char => TypeCtor::Char,
|
||||||
|
BuiltinType::Bool => TypeCtor::Bool,
|
||||||
|
BuiltinType::Str => TypeCtor::Str,
|
||||||
|
BuiltinType::Int(ty) => TypeCtor::Int(ty.into()),
|
||||||
|
BuiltinType::Float(ty) => TypeCtor::Float(ty.into()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig {
|
fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig {
|
||||||
let var_data = def.variant_data(db);
|
let var_data = def.variant_data(db);
|
||||||
let fields = match var_data.fields() {
|
let fields = match var_data.fields() {
|
||||||
|
@ -477,8 +491,19 @@ pub enum TypableDef {
|
||||||
TypeAlias(TypeAlias),
|
TypeAlias(TypeAlias),
|
||||||
Const(Const),
|
Const(Const),
|
||||||
Static(Static),
|
Static(Static),
|
||||||
|
BuiltinType(BuiltinType),
|
||||||
}
|
}
|
||||||
impl_froms!(TypableDef: Function, Struct, Union, Enum, EnumVariant, TypeAlias, Const, Static);
|
impl_froms!(
|
||||||
|
TypableDef: Function,
|
||||||
|
Struct,
|
||||||
|
Union,
|
||||||
|
Enum,
|
||||||
|
EnumVariant,
|
||||||
|
TypeAlias,
|
||||||
|
Const,
|
||||||
|
Static,
|
||||||
|
BuiltinType
|
||||||
|
);
|
||||||
|
|
||||||
impl From<ModuleDef> for Option<TypableDef> {
|
impl From<ModuleDef> for Option<TypableDef> {
|
||||||
fn from(def: ModuleDef) -> Option<TypableDef> {
|
fn from(def: ModuleDef) -> Option<TypableDef> {
|
||||||
|
@ -491,6 +516,7 @@ impl From<ModuleDef> for Option<TypableDef> {
|
||||||
ModuleDef::TypeAlias(t) => t.into(),
|
ModuleDef::TypeAlias(t) => t.into(),
|
||||||
ModuleDef::Const(v) => v.into(),
|
ModuleDef::Const(v) => v.into(),
|
||||||
ModuleDef::Static(v) => v.into(),
|
ModuleDef::Static(v) => v.into(),
|
||||||
|
ModuleDef::BuiltinType(t) => t.into(),
|
||||||
ModuleDef::Module(_) | ModuleDef::Trait(_) => return None,
|
ModuleDef::Module(_) | ModuleDef::Trait(_) => return None,
|
||||||
};
|
};
|
||||||
Some(res)
|
Some(res)
|
||||||
|
|
|
@ -30,6 +30,12 @@ pub enum UncertainIntTy {
|
||||||
Known(IntTy),
|
Known(IntTy),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<IntTy> for UncertainIntTy {
|
||||||
|
fn from(ty: IntTy) -> Self {
|
||||||
|
UncertainIntTy::Known(ty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for UncertainIntTy {
|
impl fmt::Display for UncertainIntTy {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
|
@ -45,6 +51,12 @@ pub enum UncertainFloatTy {
|
||||||
Known(FloatTy),
|
Known(FloatTy),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<FloatTy> for UncertainFloatTy {
|
||||||
|
fn from(ty: FloatTy) -> Self {
|
||||||
|
UncertainFloatTy::Known(ty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for UncertainFloatTy {
|
impl fmt::Display for UncertainFloatTy {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue