Skip to content

Fix #3540: Implicit class with two arguments does not fail compilation #3641

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

Conversation

vitorsvieira
Copy link
Contributor

This PR aims #3540 and #2464.

Where:

object Test {
  implicit class Foo(i: Int, s: String)
}

And

object Foo {
  object Bar
  implicit class Bar
}

Compilation must fail providing Implicit classes must accept exactly one primary constructor parameter error message and further explanation.

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Commit Messages

We want to keep history, but for that to actually be useful we have
some rules on how to format our commit messages (relevant xkcd).

Please stick to these guidelines for commit messages:

  1. Separate subject from body with a blank line
  2. When fixing an issue, start your commit message with Fix #<ISSUE-NBR>:
  3. Limit the subject line to 72 characters
  4. Capitalize the subject line
  5. Do not end the subject line with a period
  6. Use the imperative mood in the subject line ("Added" instead of "Add")
  7. Wrap the body at 80 characters
  8. Use the body to explain what and why vs. how

adapted from https://chris.beams.io/posts/git-commit

Have an awesome day! ☀️

@@ -0,0 +1,3 @@
object Test {
implicit class Foo(i: Int, s: String) // error: Implicit classes must accept exactly one primary constructor parameter
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add more tests here:

implicit class Foo0
implicit class Foo1()
implicit class Foo2()(x: Int)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also add

implicit case class Bar0
implicit case class Bar1()
implicit case class Bar2()(x: Int)

Copy link
Contributor Author

@vitorsvieira vitorsvieira Dec 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. When adding these tests, the number does not match, as it expects only one failure.

Wrong number of errors encountered when compiling ../out/compileNeg/neg/i3540, 

expected: 1, actual: 7

Should I change compileNeg?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You simply need to add // error next to the lines that do not compile

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Added.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add the error messages emitted by the compiler:

implicit class Foo0 // error: <the error message here>

You can get the error messages by running in sbt:

dotc tests/neg/i3540.scala

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added error messages to the comments.

@allanrenucci allanrenucci self-assigned this Dec 11, 2017
Copy link
Contributor

@allanrenucci allanrenucci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a pos test under tests/pos/i3540.scala:

object Test {
  implicit class Foo(x: Int)(implicit y: Int)
  implicit class Foo(x: Int)(y: Int)(implicit z: Int) // OK but not used during implicit lookup
  implicit class Foo(x: Int)(y: Int) // OK but not used during implicit lookup
}

|
|It’s possible to create an implicit class with more than one
|non-implicit argument, but such classes are not used during implicit lookup.
|""" + implicitClassRestrictionsText
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say instead:

val explanation = {
  val example = "implicit class RichDate(date: java.util.Date)"
  hl"""Implicit classes may only take one non-implicit argument in their constructor. For example:
      |
      | $example
      |
      |While it’s possible to create an implicit class with more than one non-implicit argument,
      |such classes aren’t used during implicit lookup.
      |""" + implicitClassRestrictionsText
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Updated.


assertMessageCount(1, messages)
val err :: Nil = messages

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this white line

implicit case class Bar0 // error: A case class must have at least one parameter list
implicit case class Bar1() // error: A case class may not be defined as implicit
implicit case class Bar2()(x: Int) // error: A case class may not be defined as implicit
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line at the end of a file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

…on-implicit arguments in primary constructor.
@vitorsvieira vitorsvieira force-pushed the fix-#3540-implicit-class-with-two-arguments-does-not-fail-compilation branch from b16682f to 46c9495 Compare December 11, 2017 17:31
@allanrenucci allanrenucci merged commit a1c20d5 into scala:master Dec 11, 2017
@vitorsvieira vitorsvieira deleted the fix-#3540-implicit-class-with-two-arguments-does-not-fail-compilation branch December 16, 2017 12:57
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

Successfully merging this pull request may close these issues.

4 participants