Skip to content

Refactor rustc_trans::save to allow other backends than CSV #31838

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

Merged
merged 1 commit into from
Mar 16, 2016
Merged

Refactor rustc_trans::save to allow other backends than CSV #31838

merged 1 commit into from
Mar 16, 2016

Conversation

aochagavia
Copy link
Contributor

r? @nrc

Things done:

  • Moved (.*)Data structs to an own module, so they can be imported easily (use data::*).
  • Created a Dump trait with callbacks for dumping items.
  • Refactored DumpCsvVisitor to use an implementor of Dump instead of dumping as CSV. Renamed it to DumpVisitor.
  • Created a DumpCsv struct that implements Dump and serializes items as CSV.

I tried to extract some of the logic contained in FmtStr and Recorder, such as normalization of ids (I put it in DumpVisitor). I think it makes sense to provide the same information to other implementors of Dump, instead of normalizing only for DumpCsv. However, there is still some logic related to spans implemented only for DumpCsv. I just thought it would be better to merge this as soon as possible, since there are so much changes, and fix this afterwards.

@aochagavia
Copy link
Contributor Author

Update: I think I know the cause of the ICE. I hope to fix it tomorrow.

@aochagavia
Copy link
Contributor Author

Hmmm... it seems that it will be more difficult to find the bug than I had thought. I'll post here when I fix it.

use std::hash::Hasher;
use syntax::ast::{self, NodeId};
use syntax::codemap::Span;

Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment to the file stating what is here? And perhaps move normalize_node_id lower in the file, since it is a utility function

@nrc
Copy link
Member

nrc commented Feb 28, 2016

Nice! Another test you should run is the save-analysis test in run-make - you should ensure that you get the same output before and after this refactoring (the automated test only checks that we don't crash while making the output).

@aochagavia
Copy link
Contributor Author

Thanks for the feedback! I'll let you know when I have addressed your comments and fixed the remaining bugs.

@bors
Copy link
Collaborator

bors commented Mar 3, 2016

☔ The latest upstream changes (presumably #31979) made this pull request unmergeable. Please resolve the merge conflicts.

@aochagavia
Copy link
Contributor Author

@nrc I think this is almost ready to be merged... I managed to remove Recorder and FmtStr, splitting their logic into CsvDumper and DumpVisitor. The only problem I see is that I created two structs in data.rs, namely MethodCallData2 and ImplData2. I would like to remove them, but I need them in order to pass the right information to implementors of Dump (the original MethodCallData and ImplData don't exactly match the data that is dumped, see Dump::method_call and Dump::impl_data). Any thoughts on how to solve this?

@nrc
Copy link
Member

nrc commented Mar 13, 2016

@aochagavia hmm, yeah the ...2 structs are pretty ugly. Why do we sometimes have a guarantee about the ref_id and sometimes not? That looks like the only difference - could you not just call unwrap in Dump::method_call?

@aochagavia
Copy link
Contributor Author

@nrc I will try to explain the reasoning behind the ...2 structs.

Instances of MethodCallData2 are currently a copy of MethodCallData, with the only change that the ref_id field is unwrapped using unwrap_or(CRATE_ROOT_DEF_ID) (see this and this). We could let the implementors of Dump do this, but it seems hacky.

The case of ImplData2 is a little different. Instances of it are a copy of ImplData as well, but the trait_ref and self_ref fields are unwrapped using .map(|data| data.ref_id.unwrap()).unwrap_or(CRATE_ROOT_DEF_ID) (see dump_visitor.rs). Again, we could let the implementors of Dump do this, but it seems wrong since every implementor would need to duplicate the code.

I think the real question here is: Could we just adapt the old ImplData and MethodCallData? The problem is, both structs are used by the current API exposed by SaveContext, so it is not completely straightforward to change them. Therefore I would prefer to land this with FIXMEs and deal with that later.

@nrc
Copy link
Member

nrc commented Mar 14, 2016

@aochagavia I think we should change the fields in ImplData with type Option to Option, then everything is in the same form (and I was never super happy with the nested setup).

Why do you use CRATE_ROOT_DEF_ID? We use zero def ids, but the fact that the crate root is also zero should be treated like a coincidence, I think.

I think the problem here is that the nice API stores 'maybe' refs as Option, but the CSV representation has no such concept, so uses 0. A JSON representation should use null. So each serialisation needs a function Option -> T (where T is String for csv and json, but might be binary data for something) which serialises these 'maybe' values. That should probably be literally a function.

That seems like not too big a thing to fix now. If you'd rather land now, I'd like to at least address the CRATE_ROOT_DEF_ID issue and to summarise this conversation in the FIXME comments.

@aochagavia
Copy link
Contributor Author

IIRC, CRATE_ROOT_DEF_ID was used in the CSV generator, so I just copied it
in order to ensure that the semantics where the same (I didn't know it was
equal to 0). I will make the changes you said and let you know when I have pushed them.

@aochagavia
Copy link
Contributor Author

@nrc I tried to change Option<TypeRefData> to Option<DefId> in ImplData. However, that didn't work out because the TypeRefData is needed in dump_visitor (see this and this). I pushed a new commit with comments clarifying the problem in a FIXME (see ImplData2 in data.rs).

Unless you can see an easy solution for this, I woud like to land it now and figure it out later.

@nrc
Copy link
Member

nrc commented Mar 14, 2016

@aochagavia I don't understand - it looks in the commit like you have done the replacement of Option to Option?

When you dump the impl, you should be able to use the def if to lookup the self/trait data from the save context. If that needs some work, then happy to land now.

@aochagavia
Copy link
Contributor Author

@nrc As far as I know, there are no functions for SaveContext that allow lookups based on a DefId (I looked in the docs). What am I missing?

@aochagavia
Copy link
Contributor Author

r? @nrc

Move rustc_trans::*Data to own module
Add new data to rustc_trans::save
Create a Dump trait implemented by structs that dump save analysis data
Split dump_csv into a DumpVisitor and a CsvDumper
@nrc
Copy link
Member

nrc commented Mar 15, 2016

@bors: r+

@bors
Copy link
Collaborator

bors commented Mar 15, 2016

📌 Commit 67395d8 has been approved by nrc

@bors
Copy link
Collaborator

bors commented Mar 15, 2016

⌛ Testing commit 67395d8 with merge c66d238...

bors added a commit that referenced this pull request Mar 15, 2016
Refactor rustc_trans::save to allow other backends than CSV

r? @nrc

Things done:
* Moved `(.*)Data` structs to an own module, so they can be imported easily (`use data::*`).
* Created a `Dump` trait with callbacks for dumping items.
* Refactored `DumpCsvVisitor` to use an implementor of `Dump` instead of dumping as CSV. Renamed it to `DumpVisitor`.
* Created a `DumpCsv` struct that implements `Dump` and serializes items as CSV.

I tried to extract some of the logic contained in `FmtStr` and `Recorder`, such as normalization of ids (I put it in `DumpVisitor`). I think it makes sense to provide the same information to other implementors of `Dump`, instead of normalizing only for `DumpCsv`. However, there is still some logic related to spans implemented only for `DumpCsv`. I just thought it would be better to merge this as soon as possible, since there are so much changes, and fix this afterwards.
@bors
Copy link
Collaborator

bors commented Mar 15, 2016

💔 Test failed - auto-win-gnu-64-opt

@alexcrichton
Copy link
Member

@bors: retry

@bors bors merged commit 67395d8 into rust-lang:master Mar 16, 2016
@aochagavia aochagavia deleted the trans branch April 5, 2016 07:04
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.

4 participants