-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix type test for trait parameter arguments #12041
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
Merged
Changes from all commits
Commits
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
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
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 |
---|---|---|
|
@@ -26,7 +26,7 @@ import Constants.Constant | |
import NullOpsDecorator._ | ||
|
||
object RefChecks { | ||
import tpd.{Tree, MemberDef, NamedArg, Literal, Template, DefDef} | ||
import tpd._ | ||
|
||
val name: String = "refchecks" | ||
|
||
|
@@ -133,6 +133,27 @@ object RefChecks { | |
false | ||
} | ||
|
||
/** Check that arguments passed to trait parameters conform to the parameter types | ||
* in the current class. This is necessary since parameter types might be narrowed | ||
* through intersection with other parent traits. See neg/i11018.scala. | ||
*/ | ||
def checkParamInits(app: Apply): Unit = | ||
val parentCls = app.tpe.classSymbol | ||
if parentCls.is(Trait) then | ||
val params = parentCls.asClass.paramGetters | ||
val args = termArgss(app).flatten | ||
for (param, arg) <- params.lazyZip(args) do | ||
if !param.is(Private) then // its type can be narrowed through intersection -> a check is needed | ||
val paramType = cls.thisType.memberInfo(param) | ||
if !(arg.tpe <:< paramType) then | ||
val argTypes = args.tpes | ||
// it could still be OK but we might need to substitute arguments for parameters | ||
// to account for dependent parameters. See pos/i11993.scala | ||
if !(arg.tpe.subst(params, argTypes) <:< paramType.subst(params, argTypes)) | ||
then | ||
report.error(IllegalParameterInit(arg.tpe, paramType, param, cls), arg.srcPos) | ||
|
||
for case app: Apply <- parentTrees do checkParamInits(app) | ||
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. yes, it's also a better idea to walk through the constructors. |
||
case _ => | ||
} | ||
|
||
|
@@ -801,34 +822,7 @@ object RefChecks { | |
report.error(problem(), clazz.srcPos) | ||
} | ||
|
||
// check that basetype and subtype agree on types of trait parameters | ||
// | ||
// I.e. trait and class parameters not only need to conform to the expected | ||
// type of the corresponding base-trait, but also to the type as seen by the | ||
// inheriting subtype. | ||
def checkTraitParametersOK() = for { | ||
parent <- clazz.info.parents | ||
parentSym = parent.classSymbol | ||
if parentSym.isClass | ||
cls = parentSym.asClass | ||
if cls.paramAccessors.nonEmpty | ||
param <- cls.paramAccessors | ||
} { | ||
val tpeFromParent = parent.memberInfo(param) | ||
val tpeFromClazz = clazz.thisType.memberInfo(param) | ||
if (!(tpeFromParent <:< tpeFromClazz)) { | ||
val msg = | ||
em"""illegal parameter: The types of $param do not match. | ||
| | ||
| $param in $cls has type: $tpeFromParent | ||
| but $clazz expects $param to have type: $tpeFromClazz""" | ||
|
||
report.error(msg, clazz.srcPos) | ||
} | ||
} | ||
|
||
checkParameterizedTraitsOK() | ||
checkTraitParametersOK() | ||
} | ||
|
||
/** Check that `site` does not inherit conflicting generic instances of `baseCls`, | ||
|
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
object test1: | ||
class Foo( | ||
val x: String, | ||
val y: Option[x.type] | ||
) | ||
|
||
class Bar extends Foo("bar", Some("bar")) | ||
|
||
object test2: | ||
trait Foo( | ||
val x: String, | ||
val y: Option[x.type] | ||
) | ||
|
||
class Bar extends Foo("bar", Some("bar")) |
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.
It is a good idea to check this one here, I didn't do that in #11059.