Stream: t-compiler/const-eval

Topic: array patterns


Taylor Cramer (Jan 09 2019 at 01:52, on Zulip):

@centril and I were chatting about sublice patterns and future-proofing for array patterns

centril (Jan 09 2019 at 01:52, on Zulip):

dear lord there are so many streams.. Zulip is :frown:

Taylor Cramer (Jan 09 2019 at 01:53, on Zulip):

(I'm also not sure this is the right one- "const eval" is more "evaluating consts" than "typechecking constants" Ithink, but seems a reasonable place)

Taylor Cramer (Jan 09 2019 at 01:54, on Zulip):

anyways the question was how hard it would be to support e.g. let [x, y, z @ ..] = [1, 2, 3, 4]; which would result in z: [i32; 2]

Taylor Cramer (Jan 09 2019 at 01:55, on Zulip):

My intuition was that it would be quite difficult, as you'd have to do subtraction from the const in [i32; 4] based on the # of other elements bound in the pattern

Taylor Cramer (Jan 09 2019 at 01:55, on Zulip):

It's not a simple syntactic element count like array length inference is today

centril (Jan 09 2019 at 01:57, on Zulip):

I'm not sure how the compiler is architechted around this.. but ostensibly we already know the type of RHS = [1, 2, 3, 4] so we know the type of the pattern; once we know the type of the pattern xs = [x, y, z, @ ..] we can infer the type of z using the known type of the pattern and the number of elements not in z

centril (Jan 09 2019 at 01:59, on Zulip):

and let [a, b, c, d] = [1, 2, 3, 4] already works but let [a, b, d, e] = [1,2,3,4] does not so unification of the pattern type and the expression already happens here

centril (Jan 09 2019 at 02:00, on Zulip):

typing [a, b, c @ ..] cannot be done without knowing [1, 2, 3, 4] tho

Taylor Cramer (Jan 09 2019 at 02:03, on Zulip):

I'd think doing this properly would require const unification

centril (Jan 09 2019 at 02:12, on Zulip):

@Taylor Cramer something like this:

Γ ⊢tm   N : usize
Γ ⊢tm   M : usize
Γ ⊢size N < M
Γ ⊢ty   σ type
Γ ⊢tm   arr: [σ; N]
---------------------------------------
Γ ⊢pat  [elt_0, .., elt_M, tail] : [σ; N]
Γ ⊢pat  tail : [σ; N - M]
∀ i ∈ M. Γ ⊢ elt_i : σ
centril (Jan 09 2019 at 02:13, on Zulip):

probably requires const unification yeah

Matthew Jasper (Jan 09 2019 at 07:49, on Zulip):

anyways the question was how hard it would be to support e.g. let [x, y, z @ ..] = [1, 2, 3, 4]; which would result in z: [i32; 2]

We already support that using z.., so not very.

Taylor Cramer (Jan 09 2019 at 16:14, on Zulip):

oh well don't i feel foolish

Taylor Cramer (Jan 09 2019 at 16:15, on Zulip):

Here was me thinking that #![feature(slice_patterns)] was just for slices

Taylor Cramer (Jan 09 2019 at 16:15, on Zulip):

that certainly makes things simpler

centril (Jan 09 2019 at 16:16, on Zulip):

@Taylor Cramer I was trying to say it wasn't before on Discord :P -- I think we miscommunicated a lot ^.^

Taylor Cramer (Jan 09 2019 at 16:16, on Zulip):

In my defense, there were no examples of this in the RFC

centril (Jan 09 2019 at 16:17, on Zulip):

very true

Last update: Nov 15 2019 at 20:10UTC