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

Introduce Requirements struct to clarify code #4683

Merged
merged 8 commits into from
Oct 31, 2017
Merged

Conversation

djc
Copy link
Contributor

@djc djc commented Oct 30, 2017

cargo::core::resolver::build_requirements() previously, in this order:

  • Defined local variables to track state
  • Called a function to mutate the local variables
  • Defined the aforementioned function
  • Returned two out of three local variables as a tuple

This PR changes the code so that this is a recast as a struct (appropriately called Requirements), which is mutated in a more fine-grained way by its methods and acts as the return type for build_requirements(). To me, this seems a lot easier to understand.

This work was done in the context of #1286, but I figured it was easier to start landing some of the refactoring to avoid bitrot and get early (well, earlier) feedback.

@rust-highfive
Copy link

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

@djc
Copy link
Contributor Author

djc commented Oct 30, 2017

@alexcrichton this is what I started with at RustFest. I have some more work done on using features as a type, but that requires yet more untangling of all the related code passing around features as strings.

Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Looks great to me, thanks so much again for taking this on!


fn require_dependency(&mut self, pkg: &'r str) {
if self.seen(pkg) {
return;
Copy link
Member

Choose a reason for hiding this comment

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

I think this early return may cause the boolean in the deps entry to be false when it should unconditionally be true if this function is called, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's also what happens in the old version, right? I tried to keep it functionally the same; reviewing it now, this seems to be the behavior of the old code, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(Also, wouldn't you say that the boolean in deps must already be true if pkg is self.seen() here?)

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok I think I see that now, makes sense! I find this code always pretty hard to follow...

I completely forget at this point why these all exist as they are, this has changed a fair bit since first written.

}

fn require_crate_feature(&mut self, package: &'r str, feat: &'r str) {
self.used.insert(package);
Copy link
Member

Choose a reason for hiding this comment

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

I'd naively expect this to call require_dependency and then fill in the deps map list, but that also seems like the kind of thing that may break tests...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm still a little fuzzy on what used means exactly, and how it's different from the deps boolean being true. This is different at least in that it leaves the deps boolean at false, which seems meaningful because setting a "forwarded" feature does not mean that we should actually pull that dependency in.

Copy link
Member

Choose a reason for hiding this comment

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

I think it may be related to #4364? I think that changed a few things in this area

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Collaborator

bors commented Oct 31, 2017

📌 Commit abf4122 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Oct 31, 2017

⌛ Testing commit abf4122 with merge d2d7b30...

bors added a commit that referenced this pull request Oct 31, 2017
Introduce Requirements struct to clarify code

`cargo::core::resolver::build_requirements()` previously, in this order:

* Defined local variables to track state
* Called a function to mutate the local variables
* Defined the aforementioned function
* Returned two out of three local variables as a tuple

This PR changes the code so that this is a recast as a struct (appropriately called `Requirements`), which is mutated in a more fine-grained way by its methods and acts as the return type for `build_requirements()`. To me, this seems a lot easier to understand.

This work was done in the context of #1286, but I figured it was easier to start landing some of the refactoring to avoid bitrot and get early (well, earlier) feedback.
@bors
Copy link
Collaborator

bors commented Oct 31, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: alexcrichton
Pushing d2d7b30 to master...

@bors bors merged commit abf4122 into rust-lang:master Oct 31, 2017
@djc djc deleted the requirements branch April 5, 2018 15:17
@ehuss ehuss added this to the 1.23.0 milestone Feb 6, 2022
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.

5 participants