-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix #3540: Implicit class with two arguments does not fail compilation #3641
Conversation
There was a problem hiding this 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:
- Separate subject from body with a blank line
- When fixing an issue, start your commit message with
Fix #<ISSUE-NBR>:
- Limit the subject line to 72 characters
- Capitalize the subject line
- Do not end the subject line with a period
- Use the imperative mood in the subject line ("Added" instead of "Add")
- Wrap the body at 80 characters
- Use the body to explain what and why vs. how
adapted from https://chris.beams.io/posts/git-commit
Have an awesome day! ☀️
tests/neg/i3540.scala
Outdated
@@ -0,0 +1,3 @@ | |||
object Test { | |||
implicit class Foo(i: Int, s: String) // error: Implicit classes must accept exactly one primary constructor parameter | |||
} |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Added.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this 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 |
There was a problem hiding this comment.
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
}
There was a problem hiding this comment.
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 | ||
|
There was a problem hiding this comment.
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 | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
b16682f
to
46c9495
Compare
This PR aims #3540 and #2464.
Where:
And
Compilation must fail providing
Implicit classes must accept exactly one primary constructor parameter
error message and further explanation.