]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/type_ref.rs
Add more tests, refactor array lengths/consteval work
[rust.git] / crates / hir_def / src / type_ref.rs
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.
3
4 use hir_expand::{name::Name, AstId, InFile};
5 use std::convert::TryInto;
6 use syntax::ast;
7
8 use crate::{body::LowerCtx, path::Path};
9
10 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
11 pub enum Mutability {
12     Shared,
13     Mut,
14 }
15
16 impl Mutability {
17     pub fn from_mutable(mutable: bool) -> Mutability {
18         if mutable {
19             Mutability::Mut
20         } else {
21             Mutability::Shared
22         }
23     }
24
25     pub fn as_keyword_for_ref(self) -> &'static str {
26         match self {
27             Mutability::Shared => "",
28             Mutability::Mut => "mut ",
29         }
30     }
31
32     pub fn as_keyword_for_ptr(self) -> &'static str {
33         match self {
34             Mutability::Shared => "const ",
35             Mutability::Mut => "mut ",
36         }
37     }
38 }
39
40 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
41 pub enum Rawness {
42     RawPtr,
43     Ref,
44 }
45
46 impl Rawness {
47     pub fn from_raw(is_raw: bool) -> Rawness {
48         if is_raw {
49             Rawness::RawPtr
50         } else {
51             Rawness::Ref
52         }
53     }
54 }
55
56 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
57 pub struct TraitRef {
58     pub path: Path,
59 }
60
61 impl TraitRef {
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`
65         match node {
66             ast::Type::PathType(path) => {
67                 path.path().and_then(|it| ctx.lower_path(it)).map(|path| TraitRef { path })
68             }
69             _ => None,
70         }
71     }
72 }
73
74 /// Compare ty::Ty
75 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
76 pub enum TypeRef {
77     Never,
78     Placeholder,
79     Tuple(Vec<TypeRef>),
80     Path(Path),
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),
86     Slice(Box<TypeRef>),
87     /// A fn pointer. Last element of the vector is the return type.
88     Fn(Vec<TypeRef>, bool /*varargs*/),
89     // For
90     ImplTrait(Vec<TypeBound>),
91     DynTrait(Vec<TypeBound>),
92     Macro(AstId<ast::MacroCall>),
93     Error,
94 }
95
96 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
97 pub struct LifetimeRef {
98     pub name: Name,
99 }
100
101 impl LifetimeRef {
102     pub(crate) fn new_name(name: Name) -> Self {
103         LifetimeRef { name }
104     }
105
106     pub(crate) fn new(lifetime: &ast::Lifetime) -> Self {
107         LifetimeRef { name: Name::new_lifetime(lifetime) }
108     }
109
110     pub fn missing() -> LifetimeRef {
111         LifetimeRef { name: Name::missing() }
112     }
113 }
114
115 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
116 pub enum TypeBound {
117     Path(Path),
118     // ForLifetime(Vec<LifetimeRef>, Path), FIXME ForLifetime
119     Lifetime(LifetimeRef),
120     Error,
121 }
122
123 impl TypeRef {
124     /// Converts an `ast::TypeRef` to a `hir::TypeRef`.
125     pub fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Self {
126         match node {
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())
130             }
131             ast::Type::NeverType(..) => TypeRef::Never,
132             ast::Type::PathType(inner) => {
133                 // FIXME: Use `Path::from_src`
134                 inner
135                     .path()
136                     .and_then(|it| ctx.lower_path(it))
137                     .map(TypeRef::Path)
138                     .unwrap_or(TypeRef::Error)
139             }
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)
144             }
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];
150                 let len = inner
151                     .expr()
152                     .map(ConstScalar::usize_from_literal_expr)
153                     .unwrap_or(ConstScalar::Unknown);
154
155                 TypeRef::Array(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty())), len)
156             }
157             ast::Type::SliceType(inner) => {
158                 TypeRef::Slice(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty())))
159             }
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(&lt));
163                 let mutability = Mutability::from_mutable(inner.mut_token().is_some());
164                 TypeRef::Reference(Box::new(inner_ty), lifetime, mutability)
165             }
166             ast::Type::InferType(_inner) => TypeRef::Placeholder,
167             ast::Type::FnPtrType(inner) => {
168                 let ret_ty = inner
169                     .ret_type()
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();
177                     }
178
179                     pl.params().map(|p| p.ty()).map(|it| TypeRef::from_ast_opt(&ctx, it)).collect()
180                 } else {
181                     Vec::new()
182                 };
183                 params.push(ret_ty);
184                 TypeRef::Fn(params, is_varargs)
185             }
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()))
190             }
191             ast::Type::DynTraitType(inner) => {
192                 TypeRef::DynTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
193             }
194             ast::Type::MacroType(mt) => match mt.macro_call() {
195                 Some(mc) => ctx
196                     .ast_id(&mc)
197                     .map(|mc| TypeRef::Macro(InFile::new(ctx.file_id(), mc)))
198                     .unwrap_or(TypeRef::Error),
199                 None => TypeRef::Error,
200             },
201         }
202     }
203
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)
207         } else {
208             TypeRef::Error
209         }
210     }
211
212     pub(crate) fn unit() -> TypeRef {
213         TypeRef::Tuple(Vec::new())
214     }
215
216     pub fn walk(&self, f: &mut impl FnMut(&TypeRef)) {
217         go(self, f);
218
219         fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) {
220             f(type_ref);
221             match type_ref {
222                 TypeRef::Fn(types, _) | TypeRef::Tuple(types) => {
223                     types.iter().for_each(|t| go(t, f))
224                 }
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 {
231                         match bound {
232                             TypeBound::Path(path) => go_path(path, f),
233                             TypeBound::Lifetime(_) | TypeBound::Error => (),
234                         }
235                     }
236                 }
237                 TypeRef::Path(path) => go_path(path, f),
238                 TypeRef::Never | TypeRef::Placeholder | TypeRef::Macro(_) | TypeRef::Error => {}
239             };
240         }
241
242         fn go_path(path: &Path, f: &mut impl FnMut(&TypeRef)) {
243             if let Some(type_ref) = path.type_anchor() {
244                 go(type_ref, f);
245             }
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 {
249                         match arg {
250                             crate::path::GenericArg::Type(type_ref) => {
251                                 go(type_ref, f);
252                             }
253                             crate::path::GenericArg::Lifetime(_) => {}
254                         }
255                     }
256                     for binding in &args_and_bindings.bindings {
257                         if let Some(type_ref) = &binding.type_ref {
258                             go(type_ref, f);
259                         }
260                         for bound in &binding.bounds {
261                             match bound {
262                                 TypeBound::Path(path) => go_path(path, f),
263                                 TypeBound::Lifetime(_) | TypeBound::Error => (),
264                             }
265                         }
266                     }
267                 }
268             }
269         }
270     }
271 }
272
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()
279     } else {
280         vec![]
281     }
282 }
283
284 impl TypeBound {
285     pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::TypeBound) -> Self {
286         match node.kind() {
287             ast::TypeBoundKind::PathType(path_type) => {
288                 let path = match path_type.path() {
289                     Some(p) => p,
290                     None => return TypeBound::Error,
291                 };
292
293                 let path = match ctx.lower_path(path) {
294                     Some(p) => p,
295                     None => return TypeBound::Error,
296                 };
297                 TypeBound::Path(path)
298             }
299             ast::TypeBoundKind::ForType(_) => TypeBound::Error, // FIXME ForType
300             ast::TypeBoundKind::Lifetime(lifetime) => {
301                 TypeBound::Lifetime(LifetimeRef::new(&lifetime))
302             }
303         }
304     }
305
306     pub fn as_path(&self) -> Option<&Path> {
307         match self {
308             TypeBound::Path(p) => Some(p),
309             _ => None,
310         }
311     }
312 }
313
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
319     Usize(u64),
320
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
323     // constants
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
326     Unknown,
327 }
328
329 impl std::fmt::Display for ConstScalar {
330     fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
331         match self {
332             ConstScalar::Usize(us) => write!(fmt, "{}", us),
333             ConstScalar::Unknown => write!(fmt, "_"),
334         }
335     }
336 }
337
338 impl ConstScalar {
339     // FIXME: as per the comments on `TypeRef::Array`, this evaluation should not happen at this
340     // parse stage.
341     fn usize_from_literal_expr(expr: ast::Expr) -> ConstScalar {
342         match expr {
343             ast::Expr::Literal(lit) => {
344                 let lkind = lit.kind();
345                 match lkind {
346                     ast::LiteralKind::IntNumber(num)
347                         if num.suffix() == None || num.suffix() == Some("usize") =>
348                     {
349                         num.value().and_then(|v| v.try_into().ok())
350                     }
351                     _ => None,
352                 }
353             }
354             _ => None,
355         }
356         .map(ConstScalar::Usize)
357         .unwrap_or(ConstScalar::Unknown)
358     }
359 }