This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Confused about the "typer phase"; Why all ClassDef.tpe == <notype>?

2 replies
monster
Joined: 2011-10-22,
User offline. Last seen 42 years 45 weeks ago.

If my PluginComponent has:

val runsAfter = List[String]("typer")

and my TypingTransformer has:

override def transform(tree: Tree): Tree = {
super.transform(tree) match {
case cd : ClassDef =>
log("Type of "+cd+" is "+cd.tpe)
cd
case x => x
}
}

What I see is that all ClassDef have a .tpe of Is that not
the job of the typer phase to give a type to everything, and should I
not therefore have a real type when I call .tpe inside a
PluginComponent that runs after the typer phase? Even calling
something like "typer.typed(cd)" makes no difference.

I am trying to use the following code, and it fails because the
*original.tpe* is :

val original: ClassDef = ... // Some trait
val owner = original.symbol.owner
val name = newTypeName("foo")
val myNewClass = owner.newClass(owner.pos, name).setFlag(SYNTHETIC)
val parents = List(definitions.ObjectClass.tpe, *original.tpe*)
myNewClass.setInfo(ClassInfoType(parents, new Scope, myNewClass))

Also, is there some link that not simply list the different phases,
but actually explains *what* each of them does in more detail than
what scalac says?

scalac -Xshow-phases returns:
...
typer 4 the meat and potatoes: type the trees
...

But "type the trees" apparently does not equate "make sure all things
get a type", so I find this description somewhat misleading...

adriaanm
Joined: 2010-02-08,
User offline. Last seen 31 weeks 4 days ago.
Re: Confused about the "typer phase"; Why all ClassDef.tpe == <
Hi,
let's take this to scala-internals

On Sat, Oct 22, 2011 at 3:34 PM, monster <skunkiferous@googlemail.com> wrote:
If my PluginComponent has:

   val runsAfter = List[String]("typer")

and my TypingTransformer has:

       override def transform(tree: Tree): Tree = {
               super.transform(tree) match {
                       case cd : ClassDef =>
                           log("Type of "+cd+" is "+cd.tpe)
                           cd
                       case x => x
               }
       }

What I see is that all ClassDef have a .tpe of <notype>  Is that not
the job of the typer phase to give a type to everything, and should I
not therefore have a real type when I call .tpe inside a
PluginComponent  that runs after the typer phase? Even calling
something like "typer.typed(cd)" makes no difference.
that's because typedClassDef does not give the ClassDef a type(see https://github.com/adriaanm/scala-dev/blob/master/src/compiler/scala/tools/nsc/typechecker/Typers.scala#L1338)
the info you're looking for is in the symbol of the classdef tree  

I am trying to use the following code, and it fails because the
*original.tpe* is <notype>:

val original: ClassDef = ... // Some trait
val owner = original.symbol.owner
val name = newTypeName("foo")
val myNewClass = owner.newClass(owner.pos, name).setFlag(SYNTHETIC)
val parents = List(definitions.ObjectClass.tpe, *original.tpe*)
val parents = List(definitions.ObjectClass.tpe, original.symbol.tpe) // this should work   
myNewClass.setInfo(ClassInfoType(parents, new Scope, myNewClass))

Also, is there some link that not simply list the different phases,
but actually explains *what* each of them does in more detail than
what scalac says?
there's Miguel's compiler corner: http://lamp.epfl.ch/~magarcia/ScalaCompilerCornerReloaded/
just the other day I hacked something that creates a new class (it's just a first cut -- known to be a bit buggy)for what it's worth, here's the relevant commit: https://github.com/adriaanm/scala-dev/commit/c67f74f019c21d2b6832a30be7609c48a9664472
cheersadriaan
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Confused about the "typer phase"; Why all ClassDef.tpe == <

On Sat, Oct 22, 2011 at 6:34 AM, monster wrote:
> But "type the trees" apparently does not equate "make sure all things
> get a type", so I find this description somewhat misleading...

NoType is a type. The absence of a type is represented by null.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland