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

Fix #12128: Add test #12592

Merged
merged 3 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions compiler/src/dotty/tools/dotc/core/NamerOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import ast.untpd
/** Operations that are shared between Namer and TreeUnpickler */
object NamerOps:

/** The given type, unless `sym` is a constructor, in which case the
* type of the constructed instance is returned
/** The type of the constructed instance is returned
*
* @param ctor the constructor
*/
def effectiveResultType(sym: Symbol, paramss: List[List[Symbol]], givenTp: Type)(using Context): Type =
if sym.name == nme.CONSTRUCTOR then
paramss match
case TypeSymbols(tparams) :: _ => sym.owner.typeRef.appliedTo(tparams.map(_.typeRef))
case _ => sym.owner.typeRef
else givenTp
def effectiveResultType(ctor: Symbol, paramss: List[List[Symbol]])(using Context): Type =
paramss match
case TypeSymbols(tparams) :: _ => ctor.owner.typeRef.appliedTo(tparams.map(_.typeRef))
case _ => ctor.owner.typeRef

/** if isConstructor, make sure it has one leading non-implicit parameter list */
def normalizeIfConstructor(paramss: List[List[Symbol]], isConstructor: Boolean)(using Context): List[List[Symbol]] =
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ class TreePickler(pickler: TastyPickler) {

def pickleDef(tag: Int, mdef: MemberDef, tpt: Tree, rhs: Tree = EmptyTree, pickleParams: => Unit = ())(using Context): Unit = {
val sym = mdef.symbol

assert(symRefs(sym) == NoAddr, sym)
registerDef(sym)
writeByte(tag)
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,11 @@ class TreeUnpickler(reader: TastyReader,
val tpt = readTpt()(using localCtx)
val paramss = normalizeIfConstructor(
paramDefss.nestedMap(_.symbol), name == nme.CONSTRUCTOR)
val resType = effectiveResultType(sym, paramss, tpt.tpe)
val resType =
if name == nme.CONSTRUCTOR then
effectiveResultType(sym, paramss)
else
tpt.tpe
sym.info = methodType(paramss, resType)
DefDef(paramDefss, tpt)
case VALDEF =>
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class TreeChecker extends Phase with SymTransformer {
// until erasure, see the comment above `Compiler#phases`.
if (ctx.phaseId <= erasurePhase.id) {
val initial = symd.initial
assert(symd.signature == initial.signature,
assert(symd == initial || symd.signature == initial.signature,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(symd == initial || symd.signature == initial.signature,
assert((symd eq initial) || symd.signature == initial.signature,

Nitpick: is reference equality more appropriate here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, using eq is a good idea, semantically they are the same for Dotty. BTW, we usually use backquote for non-symbolic infix operators.

i"""Signature of ${sym.showLocated} changed at phase ${ctx.base.fusedContaining(ctx.phase.prev)}
|Initial info: ${initial.info}
|Initial sig : ${initial.signature}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ class Semantic {

// follow constructor
if cls.hasSource then
printer.println("init super class " + cls.show)
val res2 = thisV.call(ctor, superType = NoType, source)(using ctx, trace.add(source))
errorBuffer ++= res2.errors

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,7 @@ class Namer { typer: Typer =>
if (isConstructor) {
// set result type tree to unit, but take the current class as result type of the symbol
typedAheadType(ddef.tpt, defn.UnitType)
wrapMethType(effectiveResultType(sym, paramSymss, NoType))
wrapMethType(effectiveResultType(sym, paramSymss))
}
else valOrDefDefSig(ddef, sym, paramSymss, wrapMethType)
}
Expand Down
21 changes: 21 additions & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,27 @@ class CompilationTests {
compileFilesInDir("tests/init/neg", options).checkExpectedErrors()
compileFilesInDir("tests/init/pos", options).checkCompile()
compileFilesInDir("tests/init/crash", options.without("-Xfatal-warnings")).checkCompile()

// The regression test for i12128 has some atypical classpath requirements.
// The test consists of three files: (a) Reflect_1 (b) Macro_2 (c) Test_3
// which must be compiled separately. In addition:
// - the output from (a) must be on the classpath while compiling (b)
// - the output from (b) must be on the classpath while compiling (c)
// - the output from (a) _must not_ be on the classpath while compiling (c)
locally {
val i12128Group = TestGroup("checkInit/i12128")
val i12128Options = options.without("-Xfatal-warnings")
val outDir1 = defaultOutputDir + i12128Group + "/Reflect_1/i12128/Reflect_1"
val outDir2 = defaultOutputDir + i12128Group + "/Macro_2/i12128/Macro_2"

val tests = List(
compileFile("tests/init/special/i12128/Reflect_1.scala", i12128Options)(i12128Group),
compileFile("tests/init/special/i12128/Macro_2.scala", i12128Options.withClasspath(outDir1))(i12128Group),
compileFile("tests/init/special/i12128/Test_3.scala", options.withClasspath(outDir2))(i12128Group)
).map(_.keepOutput.checkCompile())

tests.foreach(_.delete())
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/init/special/i12128/Macro_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import scala.quoted._
import scala.language.experimental.macros

class Location(val line: Int)

object MacroCompat {
trait LocationMacro {
inline implicit def generate: Location = ${ locationImpl() }
implicit def generate: Location = macro MacroCompatScala2.locationImpl
}

def locationImpl()(using Quotes): Expr[Location] = '{ new Location(${Expr(0)}) }
}

object MacroCompatScala2 {
def locationImpl(c: Context): c.Tree = {
import c.universe._
val line = Literal(Constant(c.enclosingPosition.line))
New(c.mirror.staticClass(classOf[Location].getName()), line)
}
}
30 changes: 30 additions & 0 deletions tests/init/special/i12128/Reflect_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
trait Context: // Dummy scala.reflect.macros.Context
type Tree = universe.Tree
val universe: Universe

trait Universe {
type Tree >: Null <: AnyRef with TreeApi
type Literal >: Null <: LiteralApi with TermTree
type TermTree >: Null <: TermTreeApi with Tree

trait TermTreeApi extends TreeApi { this: TermTree => }
trait LiteralApi extends TermTreeApi { this: Literal => }
trait TreeApi extends Product { this: Tree => }

type Constant

type Type

def Literal(const: Constant): Tree
def Constant(i: Int): Constant
def New(tpe: Type, arg: Tree): Tree
}

def enclosingPosition: Position

trait Mirror {
def staticClass(name: String): universe.Type
}
val mirror: Mirror

class Position(val line: Int)
1 change: 1 addition & 0 deletions tests/init/special/i12128/Test_3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
object Test extends MacroCompat.LocationMacro