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

rustc_metadata: Deduplicate code between encode_info_for_item and encode_info_for_foreign_item #77393

Closed
petrochenkov opened this issue Oct 1, 2020 · 8 comments
Assignees
Labels
A-metadata Area: Crate metadata C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@petrochenkov
Copy link
Contributor

petrochenkov commented Oct 1, 2020

The set of data to record for regular items and foreign items is pretty similar, but slightly different.
When metadata encoding is done independently for them, issues like #77375 arise.

encode_info_for_trait_item and encode_info_for_impl_item are also similar, and also slightly different (all in different directions), so they may be included into the refactoring as well.

@oli-obk oli-obk added C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Oct 1, 2020
@jonas-schievink jonas-schievink added the A-metadata Area: Crate metadata label Oct 1, 2020
@jyn514
Copy link
Member

jyn514 commented Oct 1, 2020

Relevant code:

fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'tcx>) {

fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignItem<'_>) {

fn encode_info_for_trait_item(&mut self, def_id: DefId) {

fn encode_info_for_impl_item(&mut self, def_id: DefId) {

@1c3t3a
Copy link
Contributor

1c3t3a commented Oct 1, 2020

I'd like to give this a try if mentoring is available!

@1c3t3a
Copy link
Contributor

1c3t3a commented Oct 1, 2020

Is it 'just' refactoring the methods all together?

@petrochenkov
Copy link
Contributor Author

The goal is to make the code nicer and less error-prone, but I'm not sure which exactly refactoring to apply to do that.
As I mentioned above all the methods are slightly different. Perhaps some of the called encode_* functions can be tweaked to accommodate for the differences.
The issue is E-easy in the sense that the changes will certainly be easy to implement, but what those changes should be is a question.
It needs experimentation.

@jyn514 jyn514 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 1, 2020
@pitaj
Copy link
Contributor

pitaj commented Oct 18, 2020

Hi I'd like to try my hand at this so I made a small change to align the contents of encode_info_for_item and encode_info_for_foreign_item. I created new conditional helper methods and rearranged things to both have the following calls:

        self.encode_ident_span(def_id, ...);
        record!(self.tables.visibility[def_id] <-
            ty::Visibility::from_hir(...));
        record!(self.tables.attributes[def_id] <- ...);
        self.encode_stability(def_id);
        self.encode_const_stability(def_id);
        self.encode_deprecation(def_id);
        self.encode_item_type_when(def_id, ...);
        self.encode_inherent_implementations(def_id);
        self.encode_function_signature_when(
            def_id,
            matches!(...),
        );
        self.encode_generics(def_id);
        self.encode_variances_when(def_id, matches!(...));
        self.encode_explicit_predicates(def_id);
        self.encode_generics_explicit_predicates_inferred_outlives_when(def_id, ...);

I'd then pull those calls out into a single helper method. Am I headed in the right direction? If so I'll keep going. My strategy is to dedupe _item and _foreign_item, then dedupe _trait_item and _impl_item, then dedupe the result of those if it makes sense to.

Couple questions:

@petrochenkov
Copy link
Contributor Author

@pitaj

Does the order of record!s matter?

No, it shouldn't matter.
(And if it does you'll get test suit failures anyway.)

What is the difference between tcx.def_span() and item.span?

For functions def_span returns the span of its header, not the whole function with body, for other items it will return item.span.
def_span is preferable by default.

@pitaj
Copy link
Contributor

pitaj commented Oct 18, 2020

@rustbot claim

@petrochenkov
Copy link
Contributor Author

This is no longer relevant with the recent metadata encoding changes in #80347 and related PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-metadata Area: Crate metadata C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants