1 //! HIR for references to types. Paths in these are not yet resolved. They can
2 //! be directly created from an ast::TypeRef, without further queries.
4 use hir_expand::{name::Name, AstId, InFile};
5 use std::convert::TryInto;
8 use crate::{body::LowerCtx, path::Path};
10 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
17 pub fn from_mutable(mutable: bool) -> Mutability {
25 pub fn as_keyword_for_ref(self) -> &'static str {
27 Mutability::Shared => "",
28 Mutability::Mut => "mut ",
32 pub fn as_keyword_for_ptr(self) -> &'static str {
34 Mutability::Shared => "const ",
35 Mutability::Mut => "mut ",
40 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
47 pub fn from_raw(is_raw: bool) -> Rawness {
56 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
62 /// Converts an `ast::PathType` to a `hir::TraitRef`.
63 pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Option<Self> {
64 // FIXME: Use `Path::from_src`
66 ast::Type::PathType(path) => {
67 path.path().and_then(|it| ctx.lower_path(it)).map(|path| TraitRef { path })
75 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
81 RawPtr(Box<TypeRef>, Mutability),
82 Reference(Box<TypeRef>, Option<LifetimeRef>, Mutability),
83 // FIXME: for full const generics, the latter element (length) here is going to have to be an
84 // expression that is further lowered later in hir_ty.
85 Array(Box<TypeRef>, ConstScalar),
87 /// A fn pointer. Last element of the vector is the return type.
88 Fn(Vec<TypeRef>, bool /*varargs*/),
90 ImplTrait(Vec<TypeBound>),
91 DynTrait(Vec<TypeBound>),
92 Macro(AstId<ast::MacroCall>),
96 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
97 pub struct LifetimeRef {
102 pub(crate) fn new_name(name: Name) -> Self {
106 pub(crate) fn new(lifetime: &ast::Lifetime) -> Self {
107 LifetimeRef { name: Name::new_lifetime(lifetime) }
110 pub fn missing() -> LifetimeRef {
111 LifetimeRef { name: Name::missing() }
115 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
118 // ForLifetime(Vec<LifetimeRef>, Path), FIXME ForLifetime
119 Lifetime(LifetimeRef),
124 /// Converts an `ast::TypeRef` to a `hir::TypeRef`.
125 pub fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Self {
127 ast::Type::ParenType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()),
128 ast::Type::TupleType(inner) => {
129 TypeRef::Tuple(inner.fields().map(|it| TypeRef::from_ast(ctx, it)).collect())
131 ast::Type::NeverType(..) => TypeRef::Never,
132 ast::Type::PathType(inner) => {
133 // FIXME: Use `Path::from_src`
136 .and_then(|it| ctx.lower_path(it))
138 .unwrap_or(TypeRef::Error)
140 ast::Type::PtrType(inner) => {
141 let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty());
142 let mutability = Mutability::from_mutable(inner.mut_token().is_some());
143 TypeRef::RawPtr(Box::new(inner_ty), mutability)
145 ast::Type::ArrayType(inner) => {
146 // FIXME: This is a hack. We should probably reuse the machinery of
147 // `hir_def::body::lower` to lower this into an `Expr` and then evaluate it at the
148 // `hir_ty` level, which would allow knowing the type of:
149 // let v: [u8; 2 + 2] = [0u8; 4];
152 .map(ConstScalar::usize_from_literal_expr)
153 .unwrap_or(ConstScalar::Unknown);
155 TypeRef::Array(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty())), len)
157 ast::Type::SliceType(inner) => {
158 TypeRef::Slice(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty())))
160 ast::Type::RefType(inner) => {
161 let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty());
162 let lifetime = inner.lifetime().map(|lt| LifetimeRef::new(<));
163 let mutability = Mutability::from_mutable(inner.mut_token().is_some());
164 TypeRef::Reference(Box::new(inner_ty), lifetime, mutability)
166 ast::Type::InferType(_inner) => TypeRef::Placeholder,
167 ast::Type::FnPtrType(inner) => {
170 .and_then(|rt| rt.ty())
171 .map(|it| TypeRef::from_ast(ctx, it))
172 .unwrap_or_else(|| TypeRef::Tuple(Vec::new()));
173 let mut is_varargs = false;
174 let mut params = if let Some(pl) = inner.param_list() {
175 if let Some(param) = pl.params().last() {
176 is_varargs = param.dotdotdot_token().is_some();
179 pl.params().map(|p| p.ty()).map(|it| TypeRef::from_ast_opt(&ctx, it)).collect()
184 TypeRef::Fn(params, is_varargs)
186 // for types are close enough for our purposes to the inner type for now...
187 ast::Type::ForType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()),
188 ast::Type::ImplTraitType(inner) => {
189 TypeRef::ImplTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
191 ast::Type::DynTraitType(inner) => {
192 TypeRef::DynTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
194 ast::Type::MacroType(mt) => match mt.macro_call() {
197 .map(|mc| TypeRef::Macro(InFile::new(ctx.file_id(), mc)))
198 .unwrap_or(TypeRef::Error),
199 None => TypeRef::Error,
204 pub(crate) fn from_ast_opt(ctx: &LowerCtx, node: Option<ast::Type>) -> Self {
205 if let Some(node) = node {
206 TypeRef::from_ast(ctx, node)
212 pub(crate) fn unit() -> TypeRef {
213 TypeRef::Tuple(Vec::new())
216 pub fn walk(&self, f: &mut impl FnMut(&TypeRef)) {
219 fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) {
222 TypeRef::Fn(types, _) | TypeRef::Tuple(types) => {
223 types.iter().for_each(|t| go(t, f))
225 TypeRef::RawPtr(type_ref, _)
226 | TypeRef::Reference(type_ref, ..)
227 | TypeRef::Array(type_ref, _)
228 | TypeRef::Slice(type_ref) => go(&type_ref, f),
229 TypeRef::ImplTrait(bounds) | TypeRef::DynTrait(bounds) => {
230 for bound in bounds {
232 TypeBound::Path(path) => go_path(path, f),
233 TypeBound::Lifetime(_) | TypeBound::Error => (),
237 TypeRef::Path(path) => go_path(path, f),
238 TypeRef::Never | TypeRef::Placeholder | TypeRef::Macro(_) | TypeRef::Error => {}
242 fn go_path(path: &Path, f: &mut impl FnMut(&TypeRef)) {
243 if let Some(type_ref) = path.type_anchor() {
246 for segment in path.segments().iter() {
247 if let Some(args_and_bindings) = segment.args_and_bindings {
248 for arg in &args_and_bindings.args {
250 crate::path::GenericArg::Type(type_ref) => {
253 crate::path::GenericArg::Lifetime(_) => {}
256 for binding in &args_and_bindings.bindings {
257 if let Some(type_ref) = &binding.type_ref {
260 for bound in &binding.bounds {
262 TypeBound::Path(path) => go_path(path, f),
263 TypeBound::Lifetime(_) | TypeBound::Error => (),
273 pub(crate) fn type_bounds_from_ast(
274 lower_ctx: &LowerCtx,
275 type_bounds_opt: Option<ast::TypeBoundList>,
276 ) -> Vec<TypeBound> {
277 if let Some(type_bounds) = type_bounds_opt {
278 type_bounds.bounds().map(|it| TypeBound::from_ast(lower_ctx, it)).collect()
285 pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::TypeBound) -> Self {
287 ast::TypeBoundKind::PathType(path_type) => {
288 let path = match path_type.path() {
290 None => return TypeBound::Error,
293 let path = match ctx.lower_path(path) {
295 None => return TypeBound::Error,
297 TypeBound::Path(path)
299 ast::TypeBoundKind::ForType(_) => TypeBound::Error, // FIXME ForType
300 ast::TypeBoundKind::Lifetime(lifetime) => {
301 TypeBound::Lifetime(LifetimeRef::new(&lifetime))
306 pub fn as_path(&self) -> Option<&Path> {
308 TypeBound::Path(p) => Some(p),
314 /// A concrete constant value
315 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
316 pub enum ConstScalar {
317 // for now, we only support the trivial case of constant evaluating the length of an array
318 // Note that this is u64 because the target usize may be bigger than our usize
321 /// Case of an unknown value that rustc might know but we don't
322 // FIXME: this is a hack to get around chalk not being able to represent unevaluatable
324 // https://github.com/rust-analyzer/rust-analyzer/pull/8813#issuecomment-840679177
325 // https://rust-lang.zulipchat.com/#narrow/stream/144729-wg-traits/topic/Handling.20non.20evaluatable.20constants'.20equality/near/238386348
329 impl std::fmt::Display for ConstScalar {
330 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
332 ConstScalar::Usize(us) => write!(fmt, "{}", us),
333 ConstScalar::Unknown => write!(fmt, "_"),
339 // FIXME: as per the comments on `TypeRef::Array`, this evaluation should not happen at this
341 fn usize_from_literal_expr(expr: ast::Expr) -> ConstScalar {
343 ast::Expr::Literal(lit) => {
344 let lkind = lit.kind();
346 ast::LiteralKind::IntNumber(num)
347 if num.suffix() == None || num.suffix() == Some("usize") =>
349 num.value().and_then(|v| v.try_into().ok())
356 .map(ConstScalar::Usize)
357 .unwrap_or(ConstScalar::Unknown)