Skip to content

compile-time assert numControllers == len(controllersArray)#3924

Merged
knative-prow-robot merged 7 commits into
knative:masterfrom
syedriko:compile_time_assert
May 29, 2019
Merged

compile-time assert numControllers == len(controllersArray)#3924
knative-prow-robot merged 7 commits into
knative:masterfrom
syedriko:compile_time_assert

Conversation

@syedriko
Copy link
Copy Markdown
Contributor

Replace a runtime check with a compile-time assert in controller mainline
Sister PR in eventing knative/eventing#1112

@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label Apr 27, 2019
@knative-prow-robot knative-prow-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Apr 27, 2019
@knative-prow-robot
Copy link
Copy Markdown
Contributor

Hi @syedriko. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@knative-prow-robot knative-prow-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. area/API API objects and controllers labels Apr 27, 2019
@vagababov
Copy link
Copy Markdown
Contributor

/ok-to-test

@knative-prow-robot knative-prow-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 27, 2019
@vagababov
Copy link
Copy Markdown
Contributor

While this does nicely exercise the language gymnastics, I am not a big fan of those checks because Go specifically has no assert by design: https://golang.org/doc/faq#assertions
And this just circumvents the language design.

@syedriko
Copy link
Copy Markdown
Contributor Author

syedriko commented Apr 27, 2019

My motivation was in early detection of a violation, instead of leaving it until runtime. I don't think this check is very different from the very widespread one, that checks for interface conformance, like here

// Verify that Revision adheres to the appropriate interfaces.

Go specifically has no assert by design: https://golang.org/doc/faq#assertions

That paragraph is a bit too generic IMO, it seems like it's talking about an equivalent of assert() from C/C++, which can be pretty evil. The Go language spec is fine with type assertions though https://golang.org/ref/spec#Type_assertions.

IMO, the API in this area needs to be reworked to eliminate the circular dependency with the number of controllers and remove the need for this check in the first place.

@vagababov
Copy link
Copy Markdown
Contributor

Well, type assertion is just a downcast :-)
Anyway, I am not strictly against it, though I am unsure how much there's benefit vs readability loss.
I'll let others decide.

@n3wscott
Copy link
Copy Markdown
Contributor

I find this pretty hard to read. We have e2e tests that will catch the error.

@knative-prow-robot knative-prow-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Apr 29, 2019
@syedriko
Copy link
Copy Markdown
Contributor Author

@n3wscott, could you take another look?

@grantr
Copy link
Copy Markdown
Contributor

grantr commented Apr 29, 2019

I approved the equivalent change in knative/eventing#1112. The "assertion" is unusual but good insurance to avoid debugging an incorrect numControllers using E2E tests. That could take hours. With the expanded comment added in 5925a8d, the purpose of this line is easy to understand.

We are already circumventing language design every time we assert an interface type and every time we use interface{} 😉

I agree with @syedriko that the real fix is to remove the need to specify numControllers. Anyone who finds this line unbearably ugly can work on that :)

@syedriko
Copy link
Copy Markdown
Contributor Author

syedriko commented May 2, 2019

/cc @markusthoemmes , could you take a look at this?

Copy link
Copy Markdown
Contributor

@markusthoemmes markusthoemmes left a comment

Choose a reason for hiding this comment

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

I agree with @grantr

/approve

Let's let it burn our eyes until we decide to fix it for good. I don't want to overrule the other opinions here though.

@vagababov strong opinion on your end?

@markusthoemmes
Copy link
Copy Markdown
Contributor

Hmm I can't approve that. doh.

/lgtm

then!

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label May 2, 2019
@syedriko
Copy link
Copy Markdown
Contributor Author

syedriko commented May 2, 2019

/cc @tcnghia, what's your take?

@matzew
Copy link
Copy Markdown
Member

matzew commented May 3, 2019

/lgtm

Comment thread cmd/controller/main.go
// Build all of our controllers, with the clients constructed above.
// Add new controllers to this array.
controllers := []*controller.Impl{
controllers := [...]*controller.Impl{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we're only concerned about the upper bound then you could just have this line be[numControllers]*controller.Impl

Then you can drop the compile time assertion below - since the compiler will complain if this array has more that numController elements

ie. https://play.golang.org/p/Q3Coj5TecnI

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But it won't if there're less: knative/eventing#1112 (comment)

@knative-prow-robot knative-prow-robot removed the lgtm Indicates that a PR is ready to be merged. label May 3, 2019
Copy link
Copy Markdown
Contributor

@markusthoemmes markusthoemmes left a comment

Choose a reason for hiding this comment

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

/approve
/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label May 29, 2019
@knative-prow-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: markusthoemmes, syedriko

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 29, 2019
@markusthoemmes
Copy link
Copy Markdown
Contributor

/test pull-knative-serving-upgrade-tests

@knative-prow-robot knative-prow-robot merged commit 44d9045 into knative:master May 29, 2019
@syedriko syedriko deleted the compile_time_assert branch May 29, 2019 15:32
JRBANCEL pushed a commit to JRBANCEL/serving that referenced this pull request May 29, 2019
…#3924)

* compile-time assert numControllers == len(controllersArray)

* Incorporated review feedback from @grantr
joshrider pushed a commit to joshrider/serving that referenced this pull request Jun 10, 2019
…#3924)

* compile-time assert numControllers == len(controllersArray)

* Incorporated review feedback from @grantr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. area/API API objects and controllers cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants