Skip to content

Accessing an array out of bounds, should generates an exception like on .NET - #3748

Merged
MangelMaxime merged 7 commits into
mainfrom
fix/array_out_of_bounds
Feb 20, 2024
Merged

MangelMaxime merged 7 commits into
mainfrom
fix/array_out_of_bounds

Conversation

@MangelMaxime

@MangelMaxime MangelMaxime commented Feb 10, 2024 •

Copy link
Copy Markdown
Member

Fix #3729

@ncave Could you please have a look at what I did?

By some trial and error, I found that these places are where we handle array index access + Array.item which have the same behaviour.

@MangelMaxime
MangelMaxime requested a review from ncave February 10, 2024 20:14
@MangelMaxime

Copy link
Copy Markdown
Member Author

Oups I didn't test TypeScript locally, I will have a look at it.

Comment thread src/Fable.Transforms/Replacements.fs Resolved
Comment thread src/fable-library/Array.fs Resolved
@MangelMaxime
MangelMaxime force-pushed the fix/array_out_of_bounds branch from 315c251 to 357b96c Compare February 12, 2024 20:57
@MangelMaxime

MangelMaxime commented Feb 12, 2024 •

Copy link
Copy Markdown
Member Author

Failing test is this one:

#if FABLE_COMPILER
#if !FABLE_COMPILER_JAVASCRIPT
testCase "Fable.JsonProvider works" <| fun _ ->
let parsed = LiteralJson(ANOTHER_JSON)
parsed.widget.debug |> equal false
parsed.widget.text.data |> equal "lots of"
parsed.widget.text.size.[1].height |> equal 80.
parsed.widget.text.vOffset |> equal 500.
#endif

Failing on this line equal<float64>(80, item(1, ((parsed["widget"])["text"])["size"])["height"]); because TypeScript infers the type of item<'T> to item<unknown>.

Should we provide the generic type to item ? Also looking at the compilers directives for that test they feels strange. Is it expected that this test is only for TypeScript or should it run for JavaScript too ?

Also this tests is kind of really specific because it is using a TypeProviders, so perhaps we should disable it or adapt the TypeProvider if needed.

I think that for now, we should probably disable the test and create an issue to keep track of it. If the same problem occurs in other situations.

@ncave

@ncave

ncave commented Feb 12, 2024 •

Copy link
Copy Markdown
Collaborator

@MangelMaxime Yes, it's strange, it should probably run for JS too.
I'm not sure what the type of that array actually is, coming from the type provider.
It may be ok to disable that test for now, but I hope it's not indicative of other potential issues like this.
Can you paste the TS code that was generated for the test?

@ncave

ncave commented Feb 12, 2024 •

Copy link
Copy Markdown
Collaborator

@MangelMaxime Also, on an unrelated note, recently we tend to merge a lot of commits with every PR.
It may be a good idea to always "squash and merge" the PRs instead, otherwise the commit history becomes somewhat unusable to figure out what changed and when.

@MangelMaxime

Copy link
Copy Markdown
Member Author

Can you paste the TS code that was generated for the test?

Generated code looks like this:

testCase("Fable.JsonProvider works", (): void => {
    const parsed: any = JSON.parse(ANOTHER_JSON);
    equal<boolean>(false, (parsed["widget"])["debug"]);
    equal<string>("lots of", ((parsed["widget"])["text"])["data"]);
    equal<float64>(80, item(1, ((parsed["widget"])["text"])["size"])["height"]);
    equal<float64>(500, ((parsed["widget"])["text"])["vOffset"]);
})

@MangelMaxime Also, on an unrelated note, recently we tend to merge a lot of commits with every PR.
It may be a good idea to always "squash and merge" the PRs instead, otherwise the commit history becomes somewhat unusable to figure out what changed and when.

Noted, I agree and it will avoid commits like Update changelog... I will give it a try and we will see how it looks like.

@goswinr

goswinr commented Feb 13, 2024

Copy link
Copy Markdown
Contributor

How about setters?
I think this should fail too, it fills up the array with undefined:

let x = [| "0" |]
x[5] <- "0"
printfn $"{x.Length}"
printfn $"{x[2]}"

REPL

@MangelMaxime

Copy link
Copy Markdown
Member Author

I didn't think about it.

I will also have a look at the setter code.

@MangelMaxime
MangelMaxime force-pushed the fix/array_out_of_bounds branch from 357b96c to 6d7df02 Compare February 19, 2024 22:17
@MangelMaxime
MangelMaxime requested a review from ncave February 19, 2024 22:17
@MangelMaxime

MangelMaxime commented Feb 19, 2024 •

Copy link
Copy Markdown
Member Author

I managed to add the fix for Array.set and ar[idx] <- value syntax.

For the second one, I add to unwrap fableExpr in transformSet quite a bit but I didn't see any other places to capture the ExprSet. And it seems like some other module do make some call to libCall even if this is not the norm in that file.

// system. We suppose this is probably due to the TypeProvider generation
// so for now we just ignore that situation hoping this is not
// indicative of other potential issues like this
// See: https://github.com/fable-compiler/Fable/pull/3748

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MangelMaxime Looks like that test is failing for JS too

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be because I forgot to update the compiler directive for the open statement.

I have it working locally now, let's hope this is the final CI run and we can merge this PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like not, the test was disable for JavaScript because when testing using fable-standalone we don't support TypeProviders.

But ok, that's not related to my PR changes will fix it with an additional compiler directive.

@MangelMaxime
MangelMaxime merged commit 1f4001b into main Feb 20, 2024
@MangelMaxime
MangelMaxime deleted the fix/array_out_of_bounds branch February 20, 2024 11:33
}

#if !FABLE_COMPILER_TYPESCRIPT
#if FABLE_COMPILER && !FABLE_COMPILER_TYPESCRIPT && !NPM_PACKAGE_FABLE_COMPILER_JAVASCRIPT

@ncave ncave Feb 20, 2024 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MangelMaxime I wonder if we can simplify this to be just:

#if !FABLE_COMPILER

instead of introducing yet another define:

#if FABLE_COMPILER && !FABLE_COMPILER_TYPESCRIPT && !NPM_PACKAGE_FABLE_COMPILER_JAVASCRIPT

I understand why we need this test to run on JS/TS, but it's an example of a test that has a dependency on an external assembly.
Perhaps this test can be moved to the integration tests, or somewhere else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IndexOutOfRangeException for bad index?

3 participants