Refactor array bracket access to dedicated AST node#8168
Refactor array bracket access to dedicated AST node#8168shulhi wants to merge 20 commits intorescript-lang:masterfrom
Conversation
|
Hmm, any reason you went with one new node to represent two different things? |
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
I had it split initially, but thought it would be better to represent this as a single node to be consistent with record update. | Pexp_record of expression record_element list * expression option
(* { l1=P1; ...; ln=Pn } (None)
{ E0 with l1=P1; ...; ln=Pn } (Some E0) *)but, you could also make a counter argument with Splitting |
|
This is not currently changing the parser. Guess wip. Just, it means it's not been tested yet. |
It was just my initial unfiltered response. With records it feels different somehow: |
|
Okay this is probably closer to record construction vs mutable record field assignment (two different nodes) rather than record update. This is still in draft though, it is still subject to change and I don't mind changing it to two different nodes. |
|
I also think 2 separate nodes are cleaner for this. Great work btw, this will be a good cleanup/enhancement. |
6a52ad2 to
de2dc18
Compare
TLDRThis PR introduces Bracket access now has fixed semantics as a language primitive:
This also removes the implicit reliance on module name. The parser previously had a FIXME about this: Even the built-in DetailsPrevious behaviorThe parser desugared
The return type is the same as before ( GenType impactSince bracket access is now a language primitive, open ImmutableArray
arr[3]On master, this resolved to If // arr[3] — always array<'a>, gentype sees T1[]
let result = arr[3]
// Explicit call — preserves ImmutableArray.t, gentype sees ReadonlyArray<T1>
let result = ImmutableArray.get(arr, 3)
// Explicit call — preserves Belt types
let result = Belt.Array.get(arr, 3) |
|
@nojaf can you please review again. I've made some changes to the analysis related section to handle completion. See changes in |
| Completable: Cpath Value[people][]-> | ||
| Package opens Stdlib.place holder Pervasives.JsxModules.place holder | ||
| Resolved opens 1 Stdlib | ||
| ContextPath Value[people][]-> |
There was a problem hiding this comment.
The [] represents the CPIndex
nojaf
left a comment
There was a problem hiding this comment.
I think this is fine, though would like to know why the examples changed.
| let testImmutableArrayGet = arr => { | ||
| open ImmutableArray | ||
| arr[3] | ||
| ImmutableArray.Array.get(arr, 3) |
There was a problem hiding this comment.
Was this existing code?
Why did this change?
There was a problem hiding this comment.
This is to match the expected output of the gentype. I mentioned the reason here, #8168 (comment).
Essentially, arr[3] is transformed into Array.get(arr, 3) and depending on what module is in scope, the behavior of the gentype can be changed.
Since arr[3] no longer transformed to Array.get(arr, 3) internally, to get the same gentype behavior you need to be explicit.
There was a problem hiding this comment.
Hmm, not sure if updating the test to avoid the problem is the best thing to do here. I have never used gen type so I have no idea what the implications are but this seems quite user impacting? If so, is this the right play then?
There was a problem hiding this comment.
I mentioned before that this is going to impact gentype. I don't use gentype/typescript though, maybe @cometkim can chime in? Anyway, this is going to be in v13, we can expect to have some breaking changes.
There's also this note I found in the codebase that this needs to be fixed. This PR essentially fixes this issue.
(* FIXME: Do not implicitly rely on specific module name, even primitive one
This can be abused like
module Array = MyModule
Find better mechanism to support it
*)
| ~kind: | ||
| (Completion.ExtractedType (Toption (env, ExtractedType typ), `Type)); | ||
| ]) | ||
| | CPIndex cp -> ( |
cristianoc
left a comment
There was a problem hiding this comment.
I would have expected the syntax tests to look a little different as they would show the new internal representation of array index.
Not sure why there's no diff evident in this PR.
We're still checking in the reacts of parsing as printing of the parsed AST right?
Before With this PR, |
|
Can we clarify the gentype discussion. The intended breaking change is that one cannot redefine Array anymore, as far as indexing operations are concerned. |
|
That's correct, the changes in gentype is the side effect of not allowing Array to be redefined. Take this for example, open ImmutableArray
arr[3]will generate typescript definition for open Belt
arr[3]This is because previously, index access is translated to module Foo = {
module Array = {
let get = (_, _) => Some(42)
}
}
let arr = [1,2,3,4]
open Foo
Console.log(arr[3]) // print 42 instead of 4 |
|
OK all clear. This looks good to me. |
Refactors array bracket access from
Pexp_apply(Array.get/set, ...)to a dedicatedPexp_indexAST node. This is an internal refactoring with no user-facing changes.Changes:
arr[0]now represented asPexp_index(arr, 0, None)in ASTarr[0] = valnow represented asPexp_index(arr, 0, Some(val))in ASTWhy: