diff options
author | Jade <[email protected]> | 2021-05-11 13:06:33 +0100 |
---|---|---|
committer | Jade <[email protected]> | 2021-05-11 13:25:19 +0100 |
commit | dc63fea427280ff278bf89a8b9c78df606009910 (patch) | |
tree | 5fb6eae3df58a7c5bc3bc5a1220dd9a5d6d3ea86 /crates/hir_ty/src/consts.rs | |
parent | 77f0c92fd8311bccc001ddaf9eb72662d35e9836 (diff) |
Add basic support for array lengths in types
This recognizes `let a = [1u8, 2, 3]` as having type `[u8; 3]` instead
of the previous `[u8; _]`. Byte strings and `[0u8; 2]` kinds of range
array declarations are unsupported as before.
I don't know why a bunch of our rustc tests had single quotes inside
strings un-escaped by `UPDATE_EXPECT=1 cargo t`, but I don't think it's
bad? Maybe something in a nightly?
Diffstat (limited to 'crates/hir_ty/src/consts.rs')
-rw-r--r-- | crates/hir_ty/src/consts.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/crates/hir_ty/src/consts.rs b/crates/hir_ty/src/consts.rs new file mode 100644 index 000000000..77d2a7a05 --- /dev/null +++ b/crates/hir_ty/src/consts.rs | |||
@@ -0,0 +1,20 @@ | |||
1 | //! Handling of concrete const values | ||
2 | |||
3 | /// A concrete constant value | ||
4 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
5 | pub enum ConstScalar { | ||
6 | // for now, we only support the trivial case of constant evaluating the length of an array | ||
7 | Usize(usize), | ||
8 | |||
9 | /// Case of an unknown value that rustc might know but we don't | ||
10 | Unknown, | ||
11 | } | ||
12 | |||
13 | impl std::fmt::Display for ConstScalar { | ||
14 | fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { | ||
15 | match self { | ||
16 | ConstScalar::Usize(us) => write!(fmt, "{}", us), | ||
17 | ConstScalar::Unknown => write!(fmt, "_"), | ||
18 | } | ||
19 | } | ||
20 | } | ||