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

Compiler often infers java.lang.Object instead of scala.AnyRef #10025

Open
scabug opened this issue Nov 3, 2016 · 5 comments
Open

Compiler often infers java.lang.Object instead of scala.AnyRef #10025

scabug opened this issue Nov 3, 2016 · 5 comments
Labels
dealias compiler isn't dealiasing when it should, or vice versa
Milestone

Comments

@scabug
Copy link

scabug commented Nov 3, 2016

This is a terrible example of using Lower Bounds but it does reveal a behaviour that seemed unexpected.

trait Animal
case class Foo()
case class Cage[A >: Animal](animal: A)
val cage = Cage(Foo())

In this example, I would expect that that the compiler would infer:

cage: Cage[AnyRef] = Cage(Foo())

But the compiler actually infers:

cage: Cage[Object] = Cage(Foo())
@scabug
Copy link
Author

scabug commented Nov 3, 2016

Imported From: https://issues.scala-lang.org/browse/SI-10025?orig=1
Reporter: Wade Waldron (wade.waldron)
Affected Versions: 2.11.8, 2.12.0

@scabug
Copy link
Author

scabug commented Nov 3, 2016

Wade Waldron (wade.waldron) said:
With a bit more investigation, we have learned that this seems to not be limited to Lower Bounds situations.

scala> new AnyRef
res3: Object = java.lang.Object@33f8bccc

@scabug
Copy link
Author

scabug commented Nov 4, 2016

@soc said (edited on Nov 4, 2016 2:13:05 PM UTC):
Think of AnyRef as an alias for Object:

scala> type Foo = String
defined type alias Foo

scala> new Foo
res0: String = ""

scala> :power

scala> val x = symbolOf[AnyRef]
x: $r.intp.global.TypeSymbol = type AnyRef

scala> x.isAliasType
res6: Boolean = true

scala> x.tpe.dealias
res7: $r.intp.global.Type = Object

Assuming that you didn't find any case where something compiled with AnyRef, but was rejected when using Object (or the other way around), I'd say that this isn't a bug.

I guess the main question is: Should we prefer displaying AnyRef over Object? Always, or are there any exceptions?

@scabug scabug added the quickfix label Apr 7, 2017
@scabug scabug added this to the Backlog milestone Apr 7, 2017
@dwijnand
Copy link
Member

Yeah, doesn't look like a bug to me either.

@SethTisue SethTisue added the dealias compiler isn't dealiasing when it should, or vice versa label Nov 11, 2020
@SethTisue
Copy link
Member

simpler reproducer is

scala 2.13.11> class A; class B; List(new A, new B)
                                 ^
               warning: a type was inferred to be `Object`; this may indicate a programming error.
class A
class B
val res0: List[Object] = List(A@1bb172dd, B@1cdd31a4)

not sure whether to label this "fixed in Scala 3" — Scala 3 infers a union type instead. (And, not sure how dealiasing and printing interact with other in Scala 3.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dealias compiler isn't dealiasing when it should, or vice versa
Projects
None yet
Development

No branches or pull requests

3 participants