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

using assert(x.size(1) == 3) in a test, triggers a wrong compiler warning #2229

Open
jimka2001 opened this issue Mar 3, 2023 · 5 comments
Open

Comments

@jimka2001
Copy link

When I compile the code belo using scalatest 2.3.15 in scala version 2.13.10, I get an annoying and wrong warning. Could it be that assert is expanding to code which contains something.size without the parens?

Auto-application to `()` is deprecated. Supply the empty argument list `()` explicitly to invoke method size,
or remove the empty argument list from its definition (Java-defined methods are exempt).
In Scala 3, an unapplied method like this will be eta-expanded into a function.
    assert(MyObj.size() == 3) // how to get rid of this warning?
import org.scalatest.funsuite.AnyFunSuite

object MyObj  {

  def size():Int = {
    3
  }
}

class MyTestSuite2 extends AnyFunSuite {
  test("test3") {
    ()
  }
  test("size") {
    assert(MyObj.size() == 3) // how to get rid of this warning?
  }
}
@bvenners
Copy link
Contributor

bvenners commented Mar 3, 2023

@jimka2001 That is possible. We'll check into it. For now to get rid of the warning you can use Predef.assert instead of ScalaTest's assert:

  Predef.assert(MyObj.size() == 3)

@som-snytt
Copy link

With -Vmacro, the missing application is evident at $org_scalatest_assert_macro_left.size. I added the WARN output to make sure what was tested, as the macro output is copious.

It would also be nice if the expansion preserved positions so the error message caret is in the correct column.

macroImplArgs: List(Expr[Nothing](MyObj.size().==(3)), Expr[Nothing](scalactic.this.Prettifier.default), Expr[Nothing]((Position.apply("ParenTest.scala", "Please set the environment variable SCALACTIC_FILL_FILE_PATHNAMES to yes at compile time to enable this feature.", 9): org.scalactic.source.Position)))
typechecking {
  val $org_scalatest_assert_macro_expr = {
    val $org_scalatest_assert_macro_left = MyObj;
    val $org_scalatest_assert_macro_right = 3;
    _root_.org.scalactic.Bool.lengthSizeMacroBool($org_scalatest_assert_macro_left, "size", $org_scalatest_assert_macro_left.size, $org_scalatest_assert_macro_right, scalactic.this.Prettifier.default)
  };
  _root_.org.scalatest.Assertions.assertionsHelper.macroAssert($org_scalatest_assert_macro_expr, "", scalactic.this.Prettifier.default, (Position.apply("ParenTest.scala", "Please set the environment variable SCALACTIC_FILL_FILE_PATHNAMES to yes at compile time to enable this feature.", 9): org.scalactic.source.Position))
} with expected type ?, implicit views = true, macros = true
WARN on $org_scalatest_assert_macro_left.size
{
  val $org_scalatest_assert_macro_expr: org.scalactic.Bool = {
    val $org_scalatest_assert_macro_left: MyObj.type = MyObj;
    val $org_scalatest_assert_macro_right: Int = 3;
    org.scalactic.Bool.lengthSizeMacroBool($org_scalatest_assert_macro_left, "size", $org_scalatest_assert_macro_left.size(), $org_scalatest_assert_macro_right, scalactic.this.Prettifier.default)
  };
  org.scalatest.Assertions.assertionsHelper.macroAssert($org_scalatest_assert_macro_expr, "", scalactic.this.Prettifier.default, (Position.apply("ParenTest.scala", "Please set the environment variable SCALACTIC_FILL_FILE_PATHNAMES to yes at compile time to enable this feature.", 9): org.scalactic.source.Position))
}

@cheeseng
Copy link
Contributor

cheeseng commented Mar 5, 2023

@jimka2001 @som-snytt @bvenners I submitted the following PR for this problem:

#2230

Thanks for reporting it!

@som-snytt
Copy link

Thanks! And they say never call a plumber on the weekend.

@som-snytt
Copy link

The PR was merged, ticket should be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants