Skip to content

Test functions should work when defined inside other functions #3532

Description

@brson

This doesn't work, but I frequently want to do it

fn foo() {

  ...

  #[test]
  fn test1() { }
}

The generated code for running test functions uses paths to the tests, and it's not possible to generate a path to test1. The test runner already breaks resolution rules to run private tests, so maybe we can break yet more.

We would want to consider though how this could work with reflection-based test runners - the way we currently break visibility rules to run tests is already bad news for reflection.

Activity

  1. graydon commented on May 8, 2013

    @graydon
    Contributor

    I would not be wholly opposed to permitting references to items inside functions this way. It's not like they do dynamic environment capture. But I suspect there's a namespace-theoretic reason why we can't (i.e. the reason for the type/module and value namespaces to be separate still exists?)

    Alternatives to that seem to me like they'd be difficult to express in AST code without some even-more-odious magic (naming by defid?) and in any case, obvious workarounds exist and this bug is entirely backwards compatible if we ever make it work. I think this is probably far-future if anything.

    I'm curious what you mean by reflection-based runners. Did you want to switch the test-running strategy to do that at some point? We don't really have a bug open for crate-structure reflection. Maybe we should!

  2. msullivan commented on Jul 12, 2013

    @msullivan
    Contributor

    Still unimplemented, still backwards compatible, still not pressing at all.

  3. brson commented on Jul 19, 2013

    @brson
    ContributorAuthor

    @graydon I would like to be able to load tests via reflection, yes, but at this point the model we've got is ok so I don't feel any pressing need.

  4. pnkfelix commented on Oct 1, 2013

    @pnkfelix
    Contributor

    visiting for triage, email from 2013 sep 30.

    nominating for milestone "far future."

  5. catamorphism commented on Oct 17, 2013

    @catamorphism
    Contributor

    de-nominated

  6. steveklabnik commented on Aug 12, 2014

    @steveklabnik
    Contributor

    What is the use-case for this feature? I'm curious.

  7. steveklabnik commented on Jan 21, 2015

    @steveklabnik
    Contributor

    I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.

    This issue has been moved to the RFCs repo: rust-lang/rfcs#612

  8. added a commit that references this issue on May 4, 2024
  9. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-frontendArea: Compiler frontend (errors, parsing and HIR)A-testsuiteArea: The testsuite used to check the correctness of rustcE-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions