-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
refactor lint handling to be more "eager" #42511
Comments
Another problem with the existing deferred lints is I don't think there is a way to make the messages rich (complex diagnostics) while emitting the diagnostics on the spot would make that trivial. |
@eddyb I actually added support for rich lints, but yes, it was a pain, and it shouldn't be. |
Cc @Manishearth , @llogiq , @mcarton We have a few lints which use a custom visitor because they need additional info. With these lints we've had problems with the attributes not working on the actual item which is linted. The way I understood this new system, such a check could become way simpler. |
@nikomatsakis - I'd like to have a go at this if you're still willing to mentor it. I've started familiarising myself with the existing code and I am preparing to have a go at the "step one" you suggest. |
I've added a function to the HIR map that naively calculates a If I understand the code correctly, there are four phases where we try to lint about things:
Which would mean the plan in the issue description expands to:
Is that still what you had in mind @nikomatsakis? |
@mjkillough That seems fine, expect that walking up the HIR every time can be expensive - or did you mean that HIR lowering records the information for some nodes (items?) and the HIR maps walks up for anything in between that and the node it was queried about? |
@eddyb - Yeah, once we calculate the levels at each node during HIR lowering, we'd just walk up from the current node to whichever node had the calculated lint levels. As I haven't touched the HIR lowering part yet, I'm currently doing the easier thing and walking all the way up to the crate each time. I don't think it'll just be items that we store the calculated lint levels on. Based on what the visitors in I also haven't put much thought into how we'd store the calculated lint levels on the HIR nodes yet - I'd planned to do that once I had something working. |
@mjkillough I'd do only |
@eddyb - OK, that makes sense. I'll have a go at doing that. Thanks! |
@mjkillough any progress on this issue? I'm excited to see you were poking at it! |
@nikomatsakis - Ah, sorry for going quiet on this! I did start making progress, but ran out of free time. I managed to get a protoype working, doing roughly what I described in my previous comment. It wasn't doing any smart caching - just walking up the HIR for every node to calculate lint levels. (It was tricky to see whether my changes broke anything, as they caused around 100 failures in the compile-fail tests because a bunch of lints are now more eager). My next steps were going to be to rejig some of the structures in I should be able to start looking at this again from the middle of next week. If someone needs to start making progress with this in the meantime, just let me know and I can push what I have. |
@alexcrichton is keen to pick this up, so I've pushed what I had here and wrote my planned next-steps in the commit message: master...mjkillough:eager_linting_prototype It may not make sense to base changes off that branch, as it is quite rough around the edges. It'll at least give you an indication of the direction I was heading in. :) |
I've opened a PR for this at #43522 |
rustc: Rearchitect lints to be emitted more eagerly In preparation for incremental compilation this commit refactors the lint handling infrastructure in the compiler to be more "eager" and overall more incremental-friendly. Many passes of the compiler can emit lints at various points but before this commit all lints were buffered in a table to be emitted at the very end of compilation. This commit changes these lints to be emitted immediately during compilation using pre-calculated lint level-related data structures. Linting today is split into two phases, one set of "early" lints run on the `syntax::ast` and a "late" set of lints run on the HIR. This commit moves the "early" lints to running as late as possible in compilation, just before HIR lowering. This notably means that we're catching resolve-related lints just before HIR lowering. The early linting remains a pass very similar to how it was before, maintaining context of the current lint level as it walks the tree. Post-HIR, however, linting is structured as a method on the `TyCtxt` which transitively executes a query to calculate lint levels. Each request to lint on a `TyCtxt` will query the entire crate's 'lint level data structure' and then go from there about whether the lint should be emitted or not. The query depends on the entire HIR crate but should be very quick to calculate (just a quick walk of the HIR) and the red-green system should notice that the lint level data structure rarely changes, and should hopefully preserve incrementality. Overall this resulted in a pretty big change to the test suite now that lints are emitted much earlier in compilation (on-demand vs only at the end). This in turn necessitated the addition of many `#![allow(warnings)]` directives throughout the compile-fail test suite and a number of updates to the UI test suite. Closes #42511
Lint handling at present works like this:
NodeId
that identifies the AST/HIR node that the lint is attached to, along with the lint name, message, etc.This is not a very good setup. Among other things:
#[allow]
. Wasteful.@eddyb proposed an alternative architecture that I think is better.
The best way to store this lint table is not 100% clear. Something sparse seems obviously like a good idea, since most nodes in the HIR do not have lint-related attributes on them (and hence just inherit the settings from their parent). I think maybe having each item store a link to the "current" settings for each lint makes sense; this can be easily shared between items. Then, within an item, if we need to compute the settings for some particular node, we would walk up the parents in the HIR map, looking for lint-related attributes and -- when we reach the item -- just using the settings stored on the item itself.
Alternatively, we could just cache nothing and compute everything this way, at least to start. If more caching is desired, maybe have passes keep a local cache that would then be populated within the things within a particular item.
I'm available to mentor this. It is a bit of a "design needed" sort of job, so I'm not sure what the immediate instructions would be. The first step is obviously familiarizing yourself with the current setup. After that, I would probably try to make a commit that adds a method to the HIR map that will compute the innermost applicable lint setting for a particular node (just by walking up the parents). We could then use this and remove all the existing lint code for tracking. That should work, but be slow (O(n^2) lookups). Presumably we can then adopt some sort of caching strategy.
@eddyb also mentioned that for bonus points, we should probably "decode" attributes during HIR lowering more generally, so that we are not walking the general attribute data structures, but rather something specific to the stuff the compiler cares about.
The text was updated successfully, but these errors were encountered: