diff options
author | Jade <[email protected]> | 2021-05-16 02:51:18 +0100 |
---|---|---|
committer | Jade <[email protected]> | 2021-05-16 02:51:18 +0100 |
commit | de0ed9860d86c3b905a967b1a7b5243499d32d67 (patch) | |
tree | 6c98701c319b32a52fdd6d39ce85c590aaf254f9 /crates/hir_ty/src/consteval.rs | |
parent | 78d6b88f211cc9faf88815ce7fb1a91546cfce15 (diff) |
Address final feedback
* rename ConstExtension->ConstExt
* refactor a manual construction of a Const
Diffstat (limited to 'crates/hir_ty/src/consteval.rs')
-rw-r--r-- | crates/hir_ty/src/consteval.rs | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/crates/hir_ty/src/consteval.rs b/crates/hir_ty/src/consteval.rs index a4a430f30..e3ceb3d62 100644 --- a/crates/hir_ty/src/consteval.rs +++ b/crates/hir_ty/src/consteval.rs | |||
@@ -11,12 +11,12 @@ use hir_def::{ | |||
11 | use crate::{Const, ConstData, ConstValue, Interner, TyKind}; | 11 | use crate::{Const, ConstData, ConstValue, Interner, TyKind}; |
12 | 12 | ||
13 | /// Extension trait for [`Const`] | 13 | /// Extension trait for [`Const`] |
14 | pub trait ConstExtension { | 14 | pub trait ConstExt { |
15 | /// Is a [`Const`] unknown? | 15 | /// Is a [`Const`] unknown? |
16 | fn is_unknown(&self) -> bool; | 16 | fn is_unknown(&self) -> bool; |
17 | } | 17 | } |
18 | 18 | ||
19 | impl ConstExtension for Const { | 19 | impl ConstExt for Const { |
20 | fn is_unknown(&self) -> bool { | 20 | fn is_unknown(&self) -> bool { |
21 | match self.data(&Interner).value { | 21 | match self.data(&Interner).value { |
22 | // interned Unknown | 22 | // interned Unknown |
@@ -35,20 +35,12 @@ impl ConstExtension for Const { | |||
35 | } | 35 | } |
36 | } | 36 | } |
37 | 37 | ||
38 | /// Extension trait for [`Expr`] | 38 | // FIXME: support more than just evaluating literals |
39 | pub trait ExprEval { | 39 | pub fn eval_usize(expr: &Expr) -> Option<u64> { |
40 | /// Attempts to evaluate the expression as a target usize. | 40 | match expr { |
41 | fn eval_usize(&self) -> Option<u64>; | 41 | Expr::Literal(Literal::Uint(v, None)) |
42 | } | 42 | | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(), |
43 | 43 | _ => None, | |
44 | impl ExprEval for Expr { | ||
45 | // FIXME: support more than just evaluating literals | ||
46 | fn eval_usize(&self) -> Option<u64> { | ||
47 | match self { | ||
48 | Expr::Literal(Literal::Uint(v, None)) | ||
49 | | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(), | ||
50 | _ => None, | ||
51 | } | ||
52 | } | 44 | } |
53 | } | 45 | } |
54 | 46 | ||