Skip to content

Stabilize unit tests with non-() return type #48854

Closed
@nikomatsakis

Description

@nikomatsakis

CURRENT STATUS:

Awaiting someone to write the stabilization PR! Mentoring instructions here.


This is a sub-issue of the larger tracking issue devoted to the ? in main RFC. This issue corresponds to stabilizing the use of unit tests whose return type is something other than () -- it basically an extension of #48453, which was discussing the same thing but for the main function.

What would be stabilized

As before, unit tests that return ():

  • Succeed unless they panic
  • Can be annotated with #[should_panic]

However, unit tests can now have other return types:

  • The return type must implement the Termination trait
  • The test passes if the return value is considered a "success" (e.g., an Ok from Result)
  • If the type is not (), then #[should_panic] is disallowed

Unknowns

  • Question: what's up with #[should_panic] and #[bench] anyway? (Example)
  • Can we use this with rustdoc yet?
Older proposal

Possible changes needed before stabilizing

  • Adjust libtest runner to distinguish #[should_panic] from #[should_error], as suggested by @scottmcm?
  • Successful examples of using with rustdoc

What would be stabilized

Unit tests (and #[bench] functions) would join the main function in being allowed to return any value that implements the Termination trait.

This commits us to the following (each link is to a test):

  • tests that return Result and other Termination types. In the case of Result:
  • benchmarks work the same way (Ok, Err)
    • note that #[bench] is still effectively unstable though in general

What remains unstable

  • The library details: where the trait is and its methods
    • Which methods the test runner invokes on the trait -- this is unstable as the trait itself is unstable

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-dev-toolsRelevant to the dev-tools subteam, which will review and decide on the PR/issue.T-langRelevant to the language teamfinished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions