Skip to content
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

Bogus type error with constructor params and anonymous classes #3048

Closed
scabug opened this issue Feb 10, 2010 · 5 comments
Closed

Bogus type error with constructor params and anonymous classes #3048

scabug opened this issue Feb 10, 2010 · 5 comments
Assignees

Comments

@scabug
Copy link

scabug commented Feb 10, 2010

The following code causes bogus type error, when anonymous classes are used as parameters:

class B
object C extends B

class F[T <: B](cons: => T)
class F2[T <: B](cons: => T) extends F(cons)

object D extends F2(C) // works
object E extends F2(new B {})
test.scala:8: error: illegal inheritance;
 self-type ESI-8849.type does not conform to F2SI-8845[BSI-11]'s selftype F2SI-8845[BSI-11]
object E extends F2((new B {}))
                 ^
test.scala:8: error: type mismatch;
 found   : BSI-11
 required: BSI-11
object E extends F2((new B {}))
                     ^
two errors found

Possible workaround at the moment is to provide an explicit type ascription:

object E extends F2(new B {}:B)
@scabug
Copy link
Author

scabug commented Feb 10, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3048?orig=1
Reporter: miasma

@scabug
Copy link
Author

scabug commented May 5, 2010

@adriaanm said:
looks like the underlying cause is type inference

minimal test case:

class A
class B[T <: A](cons: T)

object C extends B/*[A]*/(new A{})

@scabug
Copy link
Author

scabug commented May 5, 2010

@adriaanm said:
Martin, re-assigning to you as I can't figure out what's going wrong. Note that the anonymous class A{} and the absence of type argument to B are essential to reproduce.

First, I thought it was because case (3) of adapt creates a TypeApply tree like [T <: A] B[T] [T]
I [http://github.com/adriaanm/scala/commit/952245ef4a6385443564fccb5aaf25f012d05908#L2R796 changed] this to result in simply B[T]', but that does not fix much. (as an aside, that fix does prevent a very weird substitution. Before this change, the TreeTypeSubstituterininferMethodInstancewould substituteT -> A, which would change the tree new [T <: A] B[T] [T]' -> `new [T <: A] B[T] [anonymous class $$anon]')

Then, I figure typedTypeConstructor' might be at fault for using normalize instead of dealias, which would turn the type constructor Binto[T<:A]B[T]`, so I [http://github.com/adriaanm/scala/commit/952245ef4a6385443564fccb5aaf25f012d05908#L2R4161 changed that to use dealias]

the full patch I cobbled together so far is at:
http://github.com/adriaanm/scala/commit/952245ef4a6385443564fccb5aaf25f012d05908

@scabug
Copy link
Author

scabug commented Dec 16, 2010

@hubertp said:
I will try to track it down using events system.

@scabug
Copy link
Author

scabug commented Jun 27, 2011

Commit Message Bot (anonymous) said:
(extempore in r25161) Don't infer anonymous classes. The type is widened as little as
possible, just far enough to avoid all kinds of undesirable consequences
which accompany the preservation of too much type information. (The
problems are akin to inferring the singleton type too freely.)

// Example of code which did not compile, but now does
class A
class B[T <: A](cons: T)
object C extends B(new A {})

Closes #4110, #3048. I already ran this by moors, so review by odersky.

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

No branches or pull requests

2 participants