-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
librustc_middle: change query functions to accept impl Into<$K> type #70956
Conversation
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.
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. |
r? @Zoxc cc @rust-lang/compiler Not sure |
impl From<LocalDefId> for DefId { | ||
fn from(local_def_id: LocalDefId) -> DefId { | ||
local_def_id.to_def_id() | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()
.
There was a problem hiding this comment.
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.
I opened #70961, which implements the approach that I had in mind for this. Notably, only queries that accept a If the additional macro complexity required for my PR is not acceptable, we should go with some version of this one. |
|
Agreed, I just don't think that |
@ecstatic-morse Heh, for me it's not about understanding but rather refactorability. |
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. |
r? @eddyb |
…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.
…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.
Lets close this in favor of #70961 |
Take `impl Into<DefId>` for query methods on `TyCtxt` Alternative implementation of rust-lang#70956. cc rust-lang#70853.
This should help to increase the usage of LocalDefId instead of DefId.