Skip to content

Conversation

@SamCarlberg
Copy link
Member

@SamCarlberg SamCarlberg commented Oct 14, 2025

CoroutineYieldInLoopDetector

This checks for while loops where coroutines are in scope but without calling a blocking method on at least one of those coroutines:

drivetrain.run(theCoroutine -> {
  while (drivetrain.getDistance() < 10) { // ERROR: "Missing call to `theCoroutine.yield()` inside loop"
    drivetrain.setSpeed(1);
  }
});

Note that, because we assume most looping constructs in commands will use while loops, we don't check for-loops, for-each loops, or do-while loops.

This check can be disabled with @SuppressWarnings("CoroutineYieldInLoop")

CodeAfterCoroutineParkDetector

Essentially acts like the Java compiler's check for code after a while (true) loop, but for coroutine.park():

drivetrain.run(theCoroutine -> {
  drivetrain.setSpeed(1.0);
  theCoroutine.park();
  drivetrain.setSpeed(0.0); // ERROR: "Unreachable statement: `theCoroutine.park()` will never exit"
});

This check can be disabled with @SuppressWarnings("CodeAfterCoroutinePark")

IncorrectCoroutineUseDetector

Checks for usage of captured (outer) coroutine parameters and assignments to fields.

drivetrain.run(outer -> {
  outer.await(arm.run(inner -> {
    outer.yield(); // ERROR: "Coroutine `outer` may not be in scope. Consider using `inner`"
  }))
});

This check can be disabled with @SuppressWarnings("CoroutineMayNotBeInScope")

private Coroutine coroutineField;
drivetrain.run(co -> coroutineField = co); // ERROR: "Captured coroutines may not be stored in fields"

This check can be disabled with @SuppressWarnings("CoroutineCapture")

@SamCarlberg SamCarlberg requested a review from a team as a code owner October 14, 2025 21:51
@github-actions github-actions bot added the 2027 2027 target label Oct 14, 2025
Only locally bound coroutine objects are valid to yield on; eg, with nested lambdas, a loop in the innermost lambda may only use coroutines defined by that innermost lambda

Maybe we should also check for inner lambdas invoking methods on captured coroutines from the enclosing scope, since those coroutines may not be mounted when the inner lambda runs
@SamCarlberg SamCarlberg force-pushed the compiler-plugin-coroutine-yield-check branch from 5f26745 to b65df19 Compare October 14, 2025 22:07
@SamCarlberg SamCarlberg added component: javac plugin Java compiler plugin component: commands v3 WPILib commands library version 3 labels Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2027 2027 target component: commands v3 WPILib commands library version 3 component: javac plugin Java compiler plugin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants