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

librustc_middle: change query functions to accept impl Into<$K> type #70956

Closed

Conversation

marmeladema
Copy link
Contributor

This should help to increase the usage of LocalDefId instead of DefId.

This should help to increase the usage of LocalDefId instead of DefId.
Some now useless calls to `.to_def_id()` have been removed as
example of usefulness of the change.
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @petrochenkov (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 9, 2020
@mark-i-m
Copy link
Member

mark-i-m commented Apr 9, 2020

cc @eddyb @ecstatic-morse

@eddyb
Copy link
Member

eddyb commented Apr 9, 2020

r? @Zoxc cc @rust-lang/compiler

Not sure Into is the right trait (we might want to use our own trait to control it better), but I've wanted this before. We should also check if this affects building the compiler (time/size).

@rust-highfive rust-highfive assigned Zoxc and unassigned petrochenkov Apr 9, 2020
Comment on lines +198 to +202
impl From<LocalDefId> for DefId {
fn from(local_def_id: LocalDefId) -> DefId {
local_def_id.to_def_id()
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Another reason to not use From/Into is that we kind of want people to write .to_def_id() instead of .into()... or maybe we don't.

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 am new to all of this and my opinion probably does not matter much but using Into is useful because we don't have to import a new trait everywhere.
Also, if i understand correctly, that new trait would only be implemented for LocalDefId/DefId. What about other key types? Using Into makes it more general, which may or may not be good.

Copy link
Member

Choose a reason for hiding this comment

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

useful because we don't have to import a new trait everywhere.

I was imagining an IntoQueryKey trait which would be private/sealed so that we couldn't import it anywhere, i.e. these aren't conversions we'd let people do by calling .into_query_key() (which has all the same issues .into() does).

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think to_def_id() adds anything over into(), since anecdotally most variables of type LocalDefId are named def_id anyways. We could always start with a custom trait and later migrate to Into if you still have concerns. We would keep an inherent method on LocalDefId for converting to a DefId so additional trait imports shouldn't be a factor.

pub fn $name(self, key: $K) {
ensure_query::<queries::$name<'_>, _>(self.tcx, key)
pub fn $name(self, key: impl Into<$K>) {
ensure_query::<queries::$name<'_>, _>(self.tcx, key.into())
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd make some new methods that call the existing ones in this file to avoid making these generic. We want to make sure that ensure_query and get_query is only instantiated in rustc_middle.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just that i understand; the proposal here is to add new methods only for methods where the key is a DefId and give them a name similar to the original one but with a prefix (for example). We would end up with methods like local_generics_of for LocalDefId instead of generics_of for DefId?

If so, all callers that have a LocalDefId would have to use the new methods, and then there is not much benefit compared to calling .to_def_id() everywhere. All code will have to be changed anyway, either to call the new methods or to call .to_def_id().

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe they want the caller of ensure_query to remain monomorphic and for you to introduce a generic wrapper around it that calls .into() on the query param before passing it to the monomorphic version.

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented Apr 9, 2020

I opened #70961, which implements the approach that I had in mind for this. Notably, only queries that accept a DefId become generic. My hope is that this makes using Into instead of a hypothetical IntoQueryParam more feasible. I also made sure that the caller of ensure_query was monomorphic to address Zoxc's concern.

If the additional macro complexity required for my PR is not acceptable, we should go with some version of this one.

@eddyb
Copy link
Member

eddyb commented Apr 9, 2020

My hope is that this makes using Into instead of a hypothetical IntoQueryParam more feasible

Into doesn't stop people from writing .into() everywhere though, in cases where we might want the conversion to be explicit.

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented Apr 9, 2020

Agreed, I just don't think that def_id.into() is less understandable than def_id.to_def_id(). See my reply above. I get types on hover though, so maybe I'm biased.

@eddyb
Copy link
Member

eddyb commented Apr 9, 2020

@ecstatic-morse Heh, for me it's not about understanding but rather refactorability.
You can search for .to_def_id() now, you can't easily search for .into() applied on a LocalDefId.

@ecstatic-morse
Copy link
Contributor

That's fair. Let's go with a custom trait for now and we can re-evaluate later. I'm hoping that the series of PRs addressing #70853 are the last time we will need to do such a refactoring, but I could be wrong.

@nikomatsakis
Copy link
Contributor

r? @eddyb

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2020
…e-local-def-id, r=eddyb

rustc_middle: return `LocalDefId` where possible in hir::map module

This changes the return type of the following functions to return a `LocalDefId` instead of a `DefId`:
* opt_local_def_id_from_node_id
* opt_local_def_id
* body_owner_def_id
* local_def_id_from_node_id
* get_parent_id

This is another step in the right direction for rust-lang#70853

This pull request will be followed by another (substantial one) which changes the return type of `local_def_id` function but this change being more invasive, we might want to wait for rust-lang#70956 or rust-lang#70961 (or some other form it) to land first.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Apr 10, 2020
…dle-local-def-id, r=eddyb

rustc_middle: return `LocalDefId` where possible in hir::map module

This changes the return type of the following functions to return a `LocalDefId` instead of a `DefId`:
* opt_local_def_id_from_node_id
* opt_local_def_id
* body_owner_def_id
* local_def_id_from_node_id
* get_parent_id

This is another step in the right direction for rust-lang#70853

This pull request will be followed by another (substantial one) which changes the return type of `local_def_id` function but this change being more invasive, we might want to wait for rust-lang#70956 or rust-lang#70961 (or some other form it) to land first.
@marmeladema
Copy link
Contributor Author

Lets close this in favor of #70961

@marmeladema marmeladema deleted the queries/impl-into-def-id branch April 12, 2020 14:28
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 13, 2020
Take `impl Into<DefId>` for query methods on `TyCtxt`

Alternative implementation of rust-lang#70956. cc rust-lang#70853.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants