Rollup merge of #130618 - m-ou-se:skip-query, r=compiler-errors

Skip query in get_parent_item when possible.

For HirIds with a non-zero item local id, `self.parent_owner_iter(hir_id).next()` just returns the same HirId with the item local id set to 0, but also does a query to retrieve the Node which is ignored here, which seems wasteful.
This commit is contained in:
Michael Goulet 2024-09-23 23:49:11 -04:00 committed by GitHub
commit c0f1a69229
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -598,7 +598,10 @@ impl<'hir> Map<'hir> {
/// in the HIR which is recorded by the map and is an item, either an item /// in the HIR which is recorded by the map and is an item, either an item
/// in a module, trait, or impl. /// in a module, trait, or impl.
pub fn get_parent_item(self, hir_id: HirId) -> OwnerId { pub fn get_parent_item(self, hir_id: HirId) -> OwnerId {
if let Some((def_id, _node)) = self.parent_owner_iter(hir_id).next() { if hir_id.local_id != ItemLocalId::ZERO {
// If this is a child of a HIR owner, return the owner.
hir_id.owner
} else if let Some((def_id, _node)) = self.parent_owner_iter(hir_id).next() {
def_id def_id
} else { } else {
CRATE_OWNER_ID CRATE_OWNER_ID