mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[mojo-stdlib] Added fn to normalize index calculation

Open StandinKP opened this issue 10 months ago • 5 comments

Fixes #2251

StandinKP avatar Apr 23 '24 15:04 StandinKP

@JoeLoser @soraros I am facing this issue right now after rebasing with nightly.

❯ ./stdlib/scripts/run-tests.sh
Creating build directory for building the stdlib running the tests in.
Packaging up the Standard Library.
Included from /Users/kaushalphulgirkar/Documents/Projects/mojo/stdlib/src/collections/__init__.mojo:1:
/Users/kaushalphulgirkar/Documents/Projects/mojo/stdlib/src/collections/list.mojo:573:39: error: invalid initialization: could not deduce parameter 'type' of parent struct 'Reference'
        var normalized_idx = Reference(self)._normalize_index(i)
                             ~~~~~~~~~^~~~~~
/Users/kaushalphulgirkar/Documents/Projects/mojo/stdlib/src/__init__.mojo:1:1: note:  struct declared here
# ===----------------------------------------------------------------------=== #
^
Included from /Users/kaushalphulgirkar/Documents/Projects/mojo/stdlib/src/memory/__init__.mojo:1:
/Users/kaushalphulgirkar/Documents/Projects/mojo/stdlib/src/memory/reference.mojo:222:8: note: function declared here
    fn __init__(inout self, value: Self._mlir_type):
       ^
mojo: error: failed to parse the provided Mojo source module

Not able to figure how to exactly fix this. Any suggestions? Did we change the Reference initializations?

StandinKP avatar May 07 '24 04:05 StandinKP

Hi @StandinKP, indeed you are right :) there was a change with how Reference works right now, see https://github.com/modularml/mojo/commit/0fe4cb7c9610d0c9623f5b0becd2d9af94835b7c

I think that in your case, you don't need to do Reference(self) since self is already a reference. The error message from the compiler is not very clear. Let's hope it will get better in the future!

gabrieldemarmiesse avatar May 07 '24 09:05 gabrieldemarmiesse

Makes sense. Thanks @gabrieldemarmiesse

StandinKP avatar May 07 '24 10:05 StandinKP

@gabrieldemarmiesse if I use self[]._normalize_index(i) it gives a very nasty error and crashes all the test suite trying to figure out this but if you have any idea let me know Thanks

StandinKP avatar May 08 '24 09:05 StandinKP

@soraros can you provide some guidance here? Thanks

StandinKP avatar May 16 '24 05:05 StandinKP

@StandinKP I think we should merge https://github.com/modularml/mojo/pull/2677 first and then you can open one pull request per method to change. This will ensure the easy changes will be merged to nightly quickly and the harder changes (I know there are some out of bounds bugs in the tests that you will hit) can be merged afterwards. What do you think?

gabrieldemarmiesse avatar May 16 '24 18:05 gabrieldemarmiesse

Closing as this is now implemented at https://github.com/modularml/mojo/blob/3cfdd29453d1bddd6d6dbde5fd13c3cf64eff825/stdlib/src/collections/_index_normalization.mojo#L48. Thanks for pushing on this @StandinKP and @gabrieldemarmiesse!

JoeLoser avatar May 29 '24 17:05 JoeLoser