Skip to content

run-make-support: use a CommandWrapper trait instead of impl_helper_methods macro #125747

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
1 task
jieyouxu opened this issue May 30, 2024 · 5 comments
Closed
1 task
Labels
A-run-make Area: port run-make Makefiles to rmake.rs A-testsuite Area: The testsuite used to check the correctness of rustc C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jieyouxu
Copy link
Member

jieyouxu commented May 30, 2024

impl_helper_methods worked reasonably well while prototyping, but now I realized a trait with default provided methods is a better fit for making it easier and less verbose to implement command wrappers. This is because command_output can be a required method checked by rustc, while we can provide helper methods but still allow individual command wrapper instances to override as desired.

pub trait CommandWrapper {
    // required method
    fn command_output(self) -> std::command::Output;

    // provided helper methods
    fn run(self) -> std::command::Output { ... }
    fn run_fail(self, expected_exit_code: i32) -> std::command::Output { ... }
}

then implementors can write

pub struct Rustc { cmd: Command }

impl CommandWrapper for Rustc {
    fn command_output(self) -> Output { ... }
}

impl Rustc {
    // ... other helper methods
}

Unresolved questions

  • If these helper methods are moved on to a trait, then test writers will have to bring the trait in scope in order to use the helper methods. Is it worth the extra typing?
@jieyouxu jieyouxu added C-cleanup Category: PRs that clean code up or issues documenting cleanup. A-testsuite Area: The testsuite used to check the correctness of rustc T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 30, 2024
@jieyouxu jieyouxu self-assigned this May 30, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 30, 2024
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 30, 2024
@jieyouxu
Copy link
Member Author

Drawback: test writer would need to bring the trait into scope to use the helper methods...

@ChrisDenton
Copy link
Member

There could be inherent methods that simply forward to the trait methods. That's more boilerplate though.

@jieyouxu jieyouxu removed their assignment May 31, 2024
@jieyouxu jieyouxu moved this to In progress in Oxidizing run-make tests Jun 6, 2024
@jieyouxu jieyouxu moved this from In progress to Ready / Needs Design in Oxidizing run-make tests Jun 6, 2024
@Kobzol
Copy link
Contributor

Kobzol commented Jun 7, 2024

We don't need command_output on each wrapper struct anymore as of #126121. I don't think that a trait pulls its weight here, due the tests having to import it (it's a bit annoying since run-make tests are typically not very friendly with IDEs). It would be nice to remove the macro, but we can do that also with other approaches than a trait (e.g. Deref).

bors added a commit to rust-lang-ci/rust that referenced this issue Jun 8, 2024
Add a custom Command wrapper to `run-make-support`

This should make it easier to make sure that we check process exit codes, and it should also make checking of stdout/stderr less verbose and more explicit in run-make tests. I prefer the `run()/run_fail().assert(...)` style to something like `run_fail_assert_exit_code`, because the former is more composable.

Regarding rust-lang#125747, I'm not sure if we really need a custom trait, I think that we can get far enough with just `Deref` on the `Cc/Clang/Rustc/Rustdoc/...` structs. But now that these structs don't even need `command_output` anymore, I think that they are fine-ish as they are with the macro.

Related issues: rust-lang#125617, rust-lang#125747

Fixes: rust-lang#125617 (because `command_output` is no longer a public method)

r? `@jieyouxu`
@Kobzol
Copy link
Contributor

Kobzol commented Jun 9, 2024

FWIW, I tried to remove the macro by implementing Deref/DerefMut<Target=Command> for the wrappers instead. It would have worked.. were it not for the builder pattern :) Because if we implement the helper functions (like env) on Command directly, then they have to return &mut Self, which means that you would have to use all of the top-level command wrapper methods before using any of the Command methods, which is annoying. For example, rustc().crate_type().env() would work, but rustc().env().crate_type() wouldn't work, because env() would return Command, and not Rustc.

I'm not aware of any way of resolving this without using a macro. That being said, I think that the current macro is fine, it just adds a bunch of methods, and does not have any specific requirements on the wrapper apart from having a cmd field. The documentation needs to be updated though (#126188).

@jieyouxu
Copy link
Member Author

jieyouxu commented Jun 9, 2024

I'm going to close this issue, because I think using a trait here unfortunately just makes the API worse.

@jieyouxu jieyouxu closed this as completed Jun 9, 2024
@github-project-automation github-project-automation bot moved this from Ready / Needs Design to Done in Oxidizing run-make tests Jun 9, 2024
@jieyouxu jieyouxu moved this from Done to Abandoned in Oxidizing run-make tests Jun 9, 2024
@Kobzol Kobzol closed this as not planned Won't fix, can't repro, duplicate, stale Jun 9, 2024
@github-project-automation github-project-automation bot moved this from Abandoned to Done in Oxidizing run-make tests Jun 9, 2024
@Kobzol Kobzol moved this from Done to Abandoned in Oxidizing run-make tests Jun 9, 2024
@jieyouxu jieyouxu added the A-run-make Area: port run-make Makefiles to rmake.rs label Jun 9, 2024
@insu-pl insu-pl mentioned this issue Jun 24, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-run-make Area: port run-make Makefiles to rmake.rs A-testsuite Area: The testsuite used to check the correctness of rustc C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
No open projects
Status: Abandoned
Development

No branches or pull requests

4 participants