-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Discourage default arg for extension receiver #22492
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
Merged
KacperFKorban
merged 5 commits into
scala:main
from
som-snytt:issue/12460-extension-checks
Feb 20, 2025
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fd9ebe0
Discourage default arg for extension receiver
som-snytt f7d1bc2
Fix span of default getter
som-snytt 6f925ce
Check HasDefaultParams, compute explicit info once
som-snytt 97bceda
Massage the message
som-snytt 3816ded
Revert prettification
som-snytt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Discourage default arg for extension receiver
- Loading branch information
commit fd9ebe0ea563a02f42bd72792a2495e3aebdbabc
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1163,22 +1163,20 @@ object RefChecks { | |
* that are either both opaque types or both not. | ||
*/ | ||
def checkExtensionMethods(sym: Symbol)(using Context): Unit = | ||
if sym.is(Extension) && !sym.nextOverriddenSymbol.exists then | ||
if sym.is(Extension) then | ||
extension (tp: Type) | ||
def strippedResultType = Applications.stripImplicit(tp.stripPoly, wildcardOnly = true).resultType | ||
def firstExplicitParamTypes = Applications.stripImplicit(tp.stripPoly, wildcardOnly = true).firstParamTypes | ||
def explicit = Applications.stripImplicit(tp.stripPoly, wildcardOnly = true) | ||
def hasImplicitParams = tp.stripPoly match { case mt: MethodType => mt.isImplicitMethod case _ => false } | ||
val target = sym.info.firstExplicitParamTypes.head // required for extension method, the putative receiver | ||
val methTp = sym.info.strippedResultType // skip leading implicits and the "receiver" parameter | ||
val target = sym.info.explicit.firstParamTypes.head // required for extension method, the putative receiver | ||
val methTp = sym.info.explicit.resultType // skip leading implicits and the "receiver" parameter | ||
def hidden = | ||
target.nonPrivateMember(sym.name) | ||
.filterWithPredicate: | ||
member => | ||
.filterWithPredicate: member => | ||
member.symbol.isPublic && { | ||
val memberIsImplicit = member.info.hasImplicitParams | ||
val paramTps = | ||
if memberIsImplicit then methTp.stripPoly.firstParamTypes | ||
else methTp.firstExplicitParamTypes | ||
else methTp.explicit.firstParamTypes | ||
|
||
paramTps.isEmpty || memberIsImplicit && !methTp.hasImplicitParams || { | ||
val memberParamTps = member.info.stripPoly.firstParamTypes | ||
|
@@ -1190,7 +1188,13 @@ object RefChecks { | |
} | ||
} | ||
.exists | ||
if !target.typeSymbol.isOpaqueAlias && hidden | ||
val receiverName = sym.info.explicit.firstParamNames.head | ||
val num = sym.info.paramNamess.flatten.indexWhere(_ == receiverName) | ||
val getterName = DefaultGetterName(sym.name.toTermName, num = num) | ||
val getterDenot = sym.owner.info.member(getterName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Can you move that into a local function? The logic might be slightly clearer then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also leverage |
||
if getterDenot.exists | ||
then report.warning(ExtensionHasDefault(sym), getterDenot.symbol.srcPos) | ||
if !target.typeSymbol.isOpaqueAlias && !sym.nextOverriddenSymbol.exists && hidden | ||
then report.warning(ExtensionNullifiedByMember(sym, target.typeSymbol), sym.srcPos) | ||
end checkExtensionMethods | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-- [E208] Potential Issue Warning: tests/warn/i12460.scala:3:23 -------------------------------------------------------- | ||
3 |extension (s: String = "hello, world") def invert = s.reverse.toUpperCase // warn | ||
| ^ | ||
| Extension method invert should not have a default argument for its receiver. | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| Although extensions are ordinary methods, they must be invoked as a selection. | ||
| Therefore, the receiver cannot be omitted. A default argument for that parameter would never be used. | ||
--------------------------------------------------------------------------------------------------------------------- | ||
-- [E208] Potential Issue Warning: tests/warn/i12460.scala:5:17 -------------------------------------------------------- | ||
5 |extension (using String)(s: String = "hello, world") def revert = s.reverse.toUpperCase // warn | ||
| ^ | ||
| Extension method revert should not have a default argument for its receiver. | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
| Explanation (enabled by `-explain`) | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| Although extensions are ordinary methods, they must be invoked as a selection. | ||
| Therefore, the receiver cannot be omitted. A default argument for that parameter would never be used. | ||
--------------------------------------------------------------------------------------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//> using options -explain | ||
|
||
extension (s: String = "hello, world") def invert = s.reverse.toUpperCase // warn | ||
|
||
extension (using String)(s: String = "hello, world") def revert = s.reverse.toUpperCase // warn | ||
|
||
extension (s: String) def divert(m: String = "hello, world") = (s+m).reverse.toUpperCase // ok |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
question: I think that this message might be confusing, since it's technically false. They don't have to be invoked as a selection.
I can agree on adding a warning here, since it in most cases it's undesired to have a default value for a receiver of an extension method, but I we should make the message conditional -- e.g.
When invoking an extension method as a selection, the receiver cannot be omitted.