Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StarlarkIndexable.getIndex implementations that return null. #15344

Closed
wants to merge 4 commits into from

Conversation

katre
Copy link
Member

@katre katre commented Apr 26, 2022

They should return Starlark.NONE instead.

Part of Optional Toolchains (#14726).

They should return Starlark.NONE instead.

Part of Optional Toolchains (bazelbuild#14726).
@katre katre requested a review from brandjon April 26, 2022 13:59
@katre
Copy link
Member Author

katre commented Apr 26, 2022

I was originally going to fix this in EvalUtils.index, but the comments there make me think that isn't a preferred change. Let me know if that would be easier.

@katre katre requested review from tetromino and brandjon and removed request for brandjon April 26, 2022 14:02
Copy link
Contributor

@tetromino tetromino left a comment

Choose a reason for hiding this comment

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

I would argue that for consistency with dict (and python), getIndex() should throw an EvalException if the key is missing.

@sgowroji sgowroji added team-Configurability platforms, toolchains, cquery, select(), config transitions awaiting-user-response Awaiting a response from the author labels Apr 27, 2022
@sgowroji
Copy link
Member

Hello @katre, Can you please share the update on the review comments. Thanks!

@katre
Copy link
Member Author

katre commented Apr 27, 2022

@sgowroji: Please note the timestamps: @tetromino commented at about 10pm ET last night, it is currently not even 8am ET now. I appreciate that you are keeping on top of PRs, but please give all contributors at least 12 hours to read and respond.

@tetromino: I will respond once I actually start my work day and can think through your response.

@katre
Copy link
Member Author

katre commented Apr 27, 2022

There are three cases here:

  • ConstraintCollection and the test can just throw the exception.
  • StarlarkToolchainContext is more complicated
    • If the toolchain type (the key) is known and a toolchain exists, return it
    • If the toolchain type is known and there is no toolchain, return None
    • If the toolchain type is unknown, throw an exception

The entire point of the work I am doing is to allow for optional toolchains, and the design we settled on there was that ctx.toolchains[/missing/optional/toolchain/type] would return None.

Does this make sense, @tetromino ?

Copy link
Contributor

@tetromino tetromino left a comment

Choose a reason for hiding this comment

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

@katre - understood. I originally misread the code, but I now see that for both ConstraintCollection and StarlarkToolchainContext, getIndex and containsKey behave consistently.

One nit I suggest fixing is update the docstring in ToolchainContextApi.java to explain when context["//pkg:my_toolchain_type"] can be None.

Besides that, LGTM.

@katre
Copy link
Member Author

katre commented Apr 27, 2022

Good point about the docs, fixed that. Let me know if this is sufficiently clear.

if (result == null) {
result = Starlark.NONE;
Copy link
Contributor

Choose a reason for hiding this comment

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

I am really sorry for being unclear (and for being mistaken in my first comment) - the previous version of the code you hard (returning Starlark.NONE) was correct. For consistency with dict's behavior, we don't want getIndex(x) to throw if containsKey(x) returns true.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I see. Changed it back.

@bazel-io bazel-io closed this in a48e246 Apr 27, 2022
@katre katre deleted the fix-toolchain-none branch April 27, 2022 16:29
@brandjon brandjon removed their request for review April 27, 2022 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-user-response Awaiting a response from the author team-Configurability platforms, toolchains, cquery, select(), config transitions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants