-
Notifications
You must be signed in to change notification settings - Fork 1k
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
This-types are inferred and passed to blackbox macros outside of the valid scope for a this-type #16107
Comments
This seems to not only be an issue with macros. object ThisTypeObj:
type T
def thisTypeFn: T = ???
def test1 =
infer(ThisTypeObj.thisTypeFn)
infer(??? : ThisTypeObj.T)
infer[ThisTypeObj.T](???)
infer(??? : T)
infer[T](???)
def infer[A](a: A): Unit = ???
def test2: Unit =
infer(ThisTypeObj.thisTypeFn) // leaks ThisTypeObj$.this.T
infer(??? : ThisTypeObj.T)
infer[ThisTypeObj.T](???) Compiling this code with package <root>.this.<empty> {
final lazy module val ThisTypeObj: <empty>.this.ThisTypeObj$ =
new <empty>.this.ThisTypeObj$.<init>()
final module class ThisTypeObj$() extends Object.<init>() {
this: <empty>.this.ThisTypeObj.type =>
type T >: scala.this.Nothing <: scala.this.Any
def thisTypeFn: ThisTypeObj$.this.T = scala.this.Predef.???
def test1: scala.this.Unit(inf) =
{
<empty>.this.Test_2$package.infer[ThisTypeObj$.this.T^(inf)](
<empty>.this.ThisTypeObj.thisTypeFn)
<empty>.this.Test_2$package.infer[<empty>.this.ThisTypeObj.T^(inf)](
scala.this.Predef.??? :<empty>.this.ThisTypeObj.T)
<empty>.this.Test_2$package.infer[<empty>.this.ThisTypeObj.T](
scala.this.Predef.???)
<empty>.this.Test_2$package.infer[ThisTypeObj$.this.T^(inf)](
scala.this.Predef.??? :ThisTypeObj$.this.T)
<empty>.this.Test_2$package.infer[ThisTypeObj$.this.T](
scala.this.Predef.???)
}
}
final lazy module val Test_2$package: <empty>.this.Test_2$package$ =
new <empty>.this.Test_2$package$.<init>()
final module class Test_2$package$() extends Object.<init>() {
this: <empty>.this.Test_2$package.type =>
def infer[A >: scala.this.Nothing <: scala.this.Any](a: A):
<root>.this.scala.Unit = scala.this.Predef.???
def test2: <root>.this.scala.Unit =
{
<empty>.this.Test_2$package.infer[ThisTypeObj$.this.T^(inf)](
<empty>.this.ThisTypeObj.thisTypeFn)
<empty>.this.Test_2$package.infer[<empty>.this.ThisTypeObj.T^(inf)](
scala.this.Predef.??? :<empty>.this.ThisTypeObj.T)
<empty>.this.Test_2$package.infer[<empty>.this.ThisTypeObj.T](
scala.this.Predef.???)
}
}
} @smarter is this an issue with avoidance or something else? |
Just jotting down some notes. Those type variables are instantiated when Typer calls Ultimately I don't see this as much of a bug. A "This" of a module class, when not in the module, isn't ambiguous. It just needs to be converted. Might come back to seeing if this is possible. |
…la3#16107 - check if this-type prefix is present in the owner chain
* Fix 'weak' abstract type detection on Scala 3 - work around scala/scala3#16107 - check if this-type prefix is present in the owner chain * fix 2.11 build
As I mentioned, I'm not too convinced this is much of a bug. There two shapes for the same type - the module. So, rather than trying to make this come out of the compiler, I'd say the consuming code should "just" handle them both. |
Compiler version
3.2.1-RC2 and 3.1.3
Minimized code
Output
Expectation
Expected the following output:
Essentially the
printTypeOf
macro receives an inferred TypeRef for a typeThisTypeObj.this.T
that's unwriteable and invalid at the point of use, becausedef example
is not inside the definition ofThisTypeObj
. Only writing an explicit type ascription obtains a more valid TypeRef for a writeable typeThisTypeObj.T
This behavior is unexpected, as we assume that this-types can only be inferred/found in scopes where they are valid/writeable. Scala 2 behaved that way, where an expression passed to the macro could not contain ThisType's for a foreign
this
that is not the outer scope of the expression.This code is a minimized version of real library code in https://github.com/izumi/izumi which we're trying to port to Scala 3
The text was updated successfully, but these errors were encountered: