# Why does sty::TyKind::Array have Unresolved const when given a literal length?

**URL:** <https://internals.rust-lang.org/t/why-does-sty-array-have-unresolved-const-when-given-a-literal-length/10934>\
**Category:** Uncategorized\
**Created:** [September 11, 2019, 5:38am UTC](https://internals.rust-lang.org/t/why-does-sty-array-have-unresolved-const-when-given-a-literal-length/10934 "2019-09-11T05:38:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhynes](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nhynes/32/5886_2.png) [@nhynes](https://internals.rust-lang.org/u/nhynes)\
**Post date:** [September 11, 2019, 5:38am UTC](https://internals.rust-lang.org/t/why-does-sty-array-have-unresolved-const-when-given-a-literal-length/10934/1 "2019-09-11T05:38:49Z")

</div>

Example code:

```rust
struct The { array: [u8; 1] }

```

When accessing `field.ty(tcx, substs)` of `The::array`, the result is

```rust
Const {
    ty: usize,
    val: Unevaluated(
        DefId(0:123 ~ path[0]::{{constant}}[0]),
        [],
    ),
}

```

which is surprising since the length is provided as a literal. I'm sure there's a good reason for this, but I can't quite figure out what it is! It's especially confusing because [`Unevaluated`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/interpret/enum.ConstValue.html#variant.Unevaluated) is said to be for pre-monomorphization, but the struct isn't even generic.

Note: the context of this is in an `after_analysis` rustc hook.

Update: if anyone else runs into this issue, the simple workaround is to get the snippet for the span and then parse the length manually. I'd bet a shiny nickel that nobody else will have this issue though 😉

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [September 16, 2019, 12:22pm UTC](https://internals.rust-lang.org/t/why-does-sty-array-have-unresolved-const-when-given-a-literal-length/10934/2 "2019-09-16T12:22:06Z")

</div>

_All_ array sizes start out as "unevaluated". After all, they can be arbitrarily complex CTFE expressions in general. There is no analysis that tries to determine if the constant is "simple enough" (that analysis would basically be _yet another_ const evaluator).

Usually you can call [`eval_usize`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.Const.html#method.eval_usize) on the constant to get its integer value. Not sure if that works in an `after_analysis` hook already... @oli-obk might know.

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [September 16, 2019, 2:06pm UTC](https://internals.rust-lang.org/t/why-does-sty-array-have-unresolved-const-when-given-a-literal-length/10934/3 "2019-09-16T14:06:25Z")

</div>

Yes, there's a bunch of methods on constants that you can use: [https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.Const.html](https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.Const.html)

In contrast to parsing the snippet, these have the advantage that they'll work with named constants or even arbitrary expressions.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [December 15, 2019, 2:06pm UTC](https://internals.rust-lang.org/t/why-does-sty-array-have-unresolved-const-when-given-a-literal-length/10934/4 "2019-12-15T14:06:26Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
