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

Add lint warning for inner function marked as #[test] #2471

Merged
merged 2 commits into from
Sep 2, 2018

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Jun 10, 2018

Rendered
Tracking issue

Add a lint that warns when marking an inner function as #[test].

#[test] is used to mark functions to be run as part of a test suite. The
functions being marked need to be addressable for this to work. Currently,
marking an inner function as #[test] will not raise any errors or warnings,
but the test will silently not be run. By adding a lint that identifies these
cases, users are less likely to fail to notice.

Implemented in rust-lang/rust#51450.

@warlord500
Copy link

I have a quick question, why do inner test functions not run?

a good use case for inner test function is the outer function you have a const you dont want to expose to the world. but in the inner function, you need test the outer function with the const as input for example something like sin function with pi as input but obviously the const is supposed to be hidden.

@estebank
Copy link
Contributor Author

@warlord500 they cannot run because test functions need to be addressable, and inner functions aren't.

cc rust-lang/rust#36629

@Centril Centril added the T-lang Relevant to the language team, which will review and decide on the RFC. label Jun 11, 2018
@mark-i-m
Copy link
Member

Does this need an RFC? It sounds like a bug fix to me.

@estebank
Copy link
Contributor Author

CC @rust-lang/lang

@sfackler
Copy link
Member

sfackler commented Jun 12, 2018 via email

@eddyb
Copy link
Member

eddyb commented Jun 12, 2018

What if we use hygiene to force name resolution of the harness paths (well, they can be just single identifiers then) to occur in the definition scope of the test functions?
It could even make the harness neatly pluggable - I mean, is it different than injecting a single macro invocation with all of those hygiene-tagged names, at the top-level of the crate?

cc @petrochenkov @alexcrichton @Manishearth

@petrochenkov
Copy link
Contributor

Looks like for each fn marked with #[test] we need to

  • create a fresh "macro 2.0" macro
  • register that macro as coming from the same location as fn
  • mark all fn's tokens as produced by that macro
  • copypaste the marked tokens into whatever place the test harness needs

Sounds possible if expansion of #[test] has access to full compiler APIs and runs at correct phase, not sure about details.

@eddyb
Copy link
Member

eddyb commented Jun 14, 2018

@petrochenkov That's an interesting approach, but wouldn't it suffice to only have a single ExprPath that resolves to a specific function without being anywhere near it?

@petrochenkov
Copy link
Contributor

Ah, I see, then it should look something like:

  • create a fresh "macro 2.0" macro
  • register that macro as coming from the same location as fn
  • mark the ExprPath as produced by that macro
  • copypaste the ExprPath into whatever place the test harness needs

@petrochenkov
Copy link
Contributor

petrochenkov commented Jun 14, 2018

However, if we can do what I wrote above, then we can probably directly assign resolution to the ExprPath's node id as well.

@eddyb
Copy link
Member

eddyb commented Jun 14, 2018

@petrochenkov Yeah, the main reason I wanted to avoid dealing with resolution directly is for moving the test harness into a library macro, so it would literally only get one ident token per fn.

@estebank
Copy link
Contributor Author

We would still meet to make sure there are no rest functions nested inside another test function, right?

@eddyb
Copy link
Member

eddyb commented Jun 14, 2018

@estebank I don't see how that would be a problem.

@joshtriplett
Copy link
Member

We discussed this in the lang team meeting. Our conclusion: because this doesn't work today, we should have a lint about it. If someone wants to propose a separate RFC that defines a behavior for this, they can also propose a change to the lint.

@rfcbot fcp merge

@rfcbot
Copy link
Collaborator

rfcbot commented Jun 14, 2018

Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. labels Jun 14, 2018
@joshtriplett
Copy link
Member

As an aside, perhaps things like putting #[test] on a trait method should emit the same lint?

@scottmcm
Copy link
Member

One possible way it wouldn't need an RFC might be if it's a fix-a-false-negative extension of the existing unused_attributes lint, as that's already emitted by

trait Foo {
    #[test]
    fn foo() {}
}
warning: unused attribute
 --> src/main.rs:2:5
  |
2 |     #[test]
  |     ^^^^^^^
  |
  = note: #[warn(unused_attributes)] on by default

@eddyb
Copy link
Member

eddyb commented Jun 14, 2018

I agree with @scottmcm, if we can make the unused_attribute lint fire, organically.
Otherwise, I strongly prefer a proper solution, along the lines of what @petrochenkov described.

@scottmcm
Copy link
Member

Per rust-lang/rust#36629 (comment), this was done without the RFC being accepted?

@Centril
Copy link
Contributor

Centril commented Aug 20, 2018

(This is currently not implemented on stable but it is implemented in the beta compiler, which becomes stable on 2018-09-13)

@estebank
Copy link
Contributor Author

It can safely be reverted. I forgot about the RFCS I'd opened when I pinged on the PR and it got approved.

@scottmcm
Copy link
Member

I still think this should be unused_attribute, but linting it is what's really important.

@rfcbot reviewed

@rfcbot rfcbot added proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. labels Aug 23, 2018
@eddyb
Copy link
Member

eddyb commented Aug 23, 2018

Hmm, does unused_attributes not fire because of the #[cfg(test)] gating, or because of the fact that the nested functions are still items and some part of the traversal hits them?

For either, I think we can move the bit that looks at #[test] attributes to look only at item children of modules, which is the only place where #[test] fn works.
I could look into it or mentor someone through it.

@rfcbot rfcbot added the final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. label Aug 23, 2018
@rfcbot
Copy link
Collaborator

rfcbot commented Aug 23, 2018

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. label Aug 23, 2018
@rfcbot
Copy link
Collaborator

rfcbot commented Sep 2, 2018

The final comment period, with a disposition to merge, as per the review above, is now complete.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this RFC. and removed final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. labels Sep 2, 2018
@Centril Centril merged commit 26a847f into rust-lang:master Sep 2, 2018
@Centril
Copy link
Contributor

Centril commented Sep 2, 2018

Huzzah! This RFC has been merged!

Tracking issue: rust-lang/rust#53911

@Centril Centril added A-lint Proposals relating to lints / warnings / clippy. A-test Proposals relating to testing. labels Nov 23, 2018
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 4, 2023
…trochenkov

Make test harness lint about unnnameable tests.

Implementation of rust-lang#113734 (comment)

About the options suggested in rust-lang#36629 (comment): adding this case to unused_attribute was just more complicated. I'll try to understand a bit more what you had in mind in rust-lang/rfcs#2471 (comment)

This was just simpler to do in a standalone PR. I'll remove the corresponding changes from rust-lang#113734 later.

r? `@petrochenkov`
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Aug 5, 2023
Make test harness lint about unnnameable tests.

Implementation of rust-lang/rust#113734 (comment)

About the options suggested in rust-lang/rust#36629 (comment): adding this case to unused_attribute was just more complicated. I'll try to understand a bit more what you had in mind in rust-lang/rfcs#2471 (comment)

This was just simpler to do in a standalone PR. I'll remove the corresponding changes from rust-lang/rust#113734 later.

r? `@petrochenkov`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Proposals relating to lints / warnings / clippy. A-test Proposals relating to testing. disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this RFC. T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants