enum size lint#14300
Conversation
src/librustc/middle/trans/base.rs
Outdated
There was a problem hiding this comment.
A full sort is unnecessary. You can just iterate the sizes once and track the two largest values.
let (a,b) = sizes.iter().fold((0, 0), |(a,b), &(_,sz)| if sz > a { (sz,a) } else if sz > b { (a,sz) } else { (a,b) });
if b > 0 && a > b*3 {
// ...There was a problem hiding this comment.
Yeah, but that's, like, work!
src/librustc/middle/lint.rs
Outdated
There was a problem hiding this comment.
Maybe this could be something like
node_levels: HashMap<(ast::NodeId, Lint), (level, LintSource)>to allow other trans/post-trans lints naturally. (This could easily be a case of YAGNI though.)
|
@kballard @huonw My first iteration of this PR was a separate lint pass that re-walked the AST, but with the trans context. It ended up being pretty gnarly and with lots of duplication. With the suggested node_levels, which nodes are you going to track, and for which lints? There are over 150000 nodes in a given build of rustc, and you need to decide what you are going to track. Of course, it could be done per-lint... it might not be awful. |
|
Yeah, I was thinking only those lints that trans is actually interested in (so ATM it would just be storing |
|
@cmr I'm interested in having a lint that enforces that a given enum (marked with an attribute) always benefits from the null pointer optimization. And by that I mean it's an enum that takes a type parameter and I want to flag any uses of the enum that result in a concrete type that's not null-pointer-optimized. I would then add this attribute to More specifically, I'd want to ensure that it maps to Note that this lint also requires knowing which enums to consider. Implementing this on top of your current hacky work (assuming |
|
Something to consider is that, if we ever get pluggable lints (which I'd really like to have), then special-casing the specific lints that need post-trans info won't work very well (i.e. the pluggable lints won't be able to be special-cased). |
src/librustc/middle/trans/base.rs
Outdated
There was a problem hiding this comment.
You seem to be doing the work of testing every single enum even if the level is allow, and you only check to see if it's actually allow down on line 1587. You should make sure it's not allow right here.
|
@kballard it's true that pluggable lints are going to have a painful life. But, they can always do a separate walk through the AST. This is really just an optimization. |
src/librustc/middle/lint.rs
Outdated
|
Does this handle generic enums? At the very least, it shouldn't ICE (i.e. testcase please). |
|
It runs on all of the current rust source (it was warn by default) runs On Mon, May 19, 2014 at 10:53 PM, Huon Wilson notifications@github.comwrote:
|
|
I believe I've addressed the concerns above, re-review please? |
src/librustc/middle/trans/base.rs
Outdated
There was a problem hiding this comment.
The default is allow, so why are you assuming that no entry at all means you should care about the size? You're just going to end up deciding it's allow later and not emitting the lint.
|
You're still sorting when you could just be folding to find the two largest sizes. I even gave you the code for it. You're also still inserting an entry in the node map for every enum. That just seems like a waste of memory, when you could only insert non- |
|
Ah yes, sorry. |
|
@kballard updated |
src/librustc/middle/trans/base.rs
Outdated
There was a problem hiding this comment.
You don't need this count. If slargest > 0 then you obviously have at least 2 non-zero sizes.
|
BTW, there's actually already |
|
I want the index of the largest element, so that doesn't seem super useful? On Wed, May 21, 2014 at 6:22 AM, Huon Wilson notifications@github.comwrote:
|
|
Not just the index, you also want the two largest values, not the largest and smallest. |
See commits for details.