Closed
Description
At the moment #[inline]
attributes are validated by the compiler during translation because that's the first time they are actually used. However, as the compiler gets more lazy, an inline function might be exported without the #[inline]
attribute ever being validate. This can lead to the case of getting an error message only when the function is used in a downstream crate.
To solve this issue, modify the compiler so that compile-fail/E0534.rs passes if the inline function is not called from anywhere.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
nikomatsakis commentedon Nov 3, 2017
Here are some mentoring instructions:
Currently, the
inline
attributes are parsed by this call here, inlibrustc_trans
. Conveniently, the real logic is done by a functionfind_inline_attr
that lives inlibsyntax
-- one of the base crates of rustc (see the README for details). This is convenient because it means that these attributes are available much earlier.(Actually, a good first task is to make a failing test. We should be able to create one based on the changes from #45575.)
Probably the best place to handle this would be in the
ast_validation
pass, which runs earlier in the compiler. It just walks the tree checking for various sanity checks.I think what I would do is the following. These changes also have the drive-by effect of a fixing a few other shortcomings.
find_inline_attr
helper so that it doesn't directly report an error, but rather returns aResult
with the span and other information in the event of an error.find_inline_attr
fromvisit_item
,visit_trait_item
andvisit_impl_item
, with the list of attributes from each item-like thing.Err
, report an error.inline
attribute from being listed asWhitelisted
toNormal
infeature_gate.rs
unused_attribute
lint to fire if#[inline]
is used in an inappropriate place, such as on a struct. You should add acompile-fail
orui
test showing that we now get a lint in those scenarios (add#![deny(unused_attrs)]
to the test to force it to fail).find_inline_attr
in the same way, but just ignore anyErr
returns.find_inline_attr
had already been marked asused
-- themark_used
call is our way of tracking which attributes are used and which are not. The idea behind this assertion would be that we would be ensuring that the "ast validation" pass did indeed visit the attribute and validate its form (since that pass would mark it as used).OK, that's a bit rough, but hopefully good to get started, please feel free to ask questions -- though asking on gitter will likely yield faster responses.
jgouly commentedon Nov 3, 2017
I'd like to have a go at fixing this.
zackmdavis commentedon Nov 3, 2017
(This is also #40792, and will likely surface some error-index doctest failures.)
#[inline]
attributes earlier #40792nikomatsakis commentedon Nov 3, 2017
@zackmdavis thanks, I closed that bug in favor of this one since this one seems to have more
jgouly commentedon Nov 4, 2017
Trying to create a test case for the various issues here:
I've been extending check_inline (
rust/src/librustc/hir/check_attr.rs
Line 61 in 200c4d0
rust/src/libsyntax/attr.rs
Line 532 in 9f3b091
This currently diagnoses the first 3 errors, but the last one gives:
@nikomatsakis is that warning ok for the last one, or do you want to actually diagnose it as 'error[E0518]: attribute should be applied to function'.
jgouly commentedon Nov 5, 2017
I pushed some progress to: jgouly@9a8927b
One of the issues I'm seeing with asserting that the inline attribute is used after hir validation is this:
But that line doesn't have an inline attribute: https://github.com/jgouly/rust/blob/gh45738/src/libstd/sync/mpsc/mod.rs#L1838
Any ideas?
nikomatsakis commentedon Nov 6, 2017
@jgouly
I think that warning is OK. In particular, we can't really make this a hard-error without going through a big warning period and so forth -- it would break existing code! -- which means we would need a warning anyhow. Let's start with just the warning, and decide late if we ever want it to be a hard error. (The main problem I see is that if we ever wanted
#[inline]
to have some semantics there, it would affect how existing code works, but in any#[inline]
happens to be the kind of attribute whose effects the user shouldn't really be able to "observe" anyway.)nikomatsakis commentedon Nov 6, 2017
@jgouly hmm, I wonder if we synthesize fake inline attributes for closures or something. (@eddyb on IRC says no.) Seems curious.
nikomatsakis commentedon Nov 6, 2017
Ah, I suspect that the problem is that the attribute is actually on the
send
orunwrap
function that is being called -- when we create a local, monomorphized copy in this crate, we presumably make an#[attr]
that was never visited. You might fix it @jgouly by adding some check for whether the attribute is attached to something in theLOCAL_CRATE
or not.jgouly commentedon Nov 9, 2017
Been very busy and unable to make progress the last few days. Hoping to get this near finished at the weekend.
jgouly commentedon Nov 19, 2017
@nikomatsakis I have two problems now.
One is that the signature for check_attribute was changed to take an ast::Item, in my code I added visit_trait_item and visit_impl_item, so I have ast::TraitItem and ast::ImplItem, what's the best way to abstract over those items? jgouly@a3fcbad
The other problem is the LOCAL_CRATE bit, that seems to come from hir::def_id, which I don't have yet as this is at the AST stage. How can I check at this point?
jgouly commentedon Nov 22, 2017
After talking with @arielb1 and @nikomatsakis on IRC, I'm going to remove the assert, it seems more hassle than it's worth.
For the check_attribute I'm just going to pass the item's span, than the item itself.
jgouly commentedon Mar 4, 2018
If it wasn't obvious, someone else should take this over! My code is here as a starting point: https://github.com/jgouly/rust/tree/gh45738
michaelwoerister commentedon Jun 17, 2021
@wesleywiser Is it possible that you fixed this a while ago? I remember you did some work around querifying attributes.
jackh726 commentedon Jan 29, 2022
Closing as it looks like this has been fixed.