Skip to content

Fix/returns #302

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

Merged
merged 4 commits into from
Dec 17, 2014
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
4 changes: 3 additions & 1 deletion src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ object SymDenotations {
}

/** Is this a user defined "def" method? Excluded are accessors. */
final def isSourceMethod(implicit ctx: Context) = this is (Method, butNot = Accessor)
final def isSourceMethod(implicit ctx: Context) = this is (Method, butNot = AccessorOrLabel)

/** Is this a setter? */
final def isGetter(implicit ctx: Context) =
Expand Down Expand Up @@ -1664,4 +1664,6 @@ object SymDenotations {
private final val NumBits = NumWords << WordSizeLog
private final val Mask = NumBits - 1
}

private val AccessorOrLabel = Accessor | Label
}
21 changes: 11 additions & 10 deletions src/dotty/tools/dotc/reporting/ConsoleReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ class ConsoleReporter(
}
}

override def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match {
case d: Error =>
printMessageAndPos(s"error: ${d.msg}", d.pos)
if (ctx.settings.prompt.value) displayPrompt()
case d: ConditionalWarning if !d.enablingOption.value =>
case d: Warning =>
printMessageAndPos(s"warning: ${d.msg}", d.pos)
case _ =>
printMessageAndPos(d.msg, d.pos)
}
override def doReport(d: Diagnostic)(implicit ctx: Context): Unit =
if (!d.isSuppressed) d match {
case d: Error =>
printMessageAndPos(s"error: ${d.msg}", d.pos)
if (ctx.settings.prompt.value) displayPrompt()
case d: ConditionalWarning if !d.enablingOption.value =>
case d: Warning =>
printMessageAndPos(s"warning: ${d.msg}", d.pos)
case _ =>
printMessageAndPos(d.msg, d.pos)
}

def displayPrompt(): Unit = {
writer.print("\na)bort, s)tack, r)esume: ")
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/reporting/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ abstract class Reporter {

def report(d: Diagnostic)(implicit ctx: Context): Unit = if (!isHidden(d)) {
doReport(d)
if (!d.isSuppressed) d match {
d match {
case d: ConditionalWarning if !d.enablingOption.value => unreportedWarnings(d.enablingOption.name) += 1
case d: Warning => warningCount += 1
case d: Error => errorCount += 1
Expand Down
6 changes: 5 additions & 1 deletion src/dotty/tools/dotc/transform/TreeTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,11 @@ object TreeTransforms {
if (mutatedInfo eq null) tree
else {
val expr = transform(tree.expr, mutatedInfo, cur)
val from = transform(tree.from, mutatedInfo, cur)
val from = tree.from
// don't thansform the `from` part, as this is not a normal ident, but
// a pointer to the enclosing method. Transforming this as a normal ident
// can go wrong easily. If a transformation is needed, it should be
// the responsibility of the transformReturn method to handle this also.
goReturn(cpy.Return(tree)(expr, from), mutatedInfo.nx.nxTransReturn(cur))
}
case tree: Try =>
Expand Down
47 changes: 29 additions & 18 deletions src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -669,25 +669,36 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
}

def typedReturn(tree: untpd.Return)(implicit ctx: Context): Return = track("typedReturn") {
def returnProto(owner: Symbol) =
if (owner.isConstructor) defn.UnitType else owner.info.finalResultType
def enclMethInfo(cx: Context): (Tree, Type) =
if (tree.from.isEmpty) {
val owner = cx.owner
if (cx == NoContext || owner.isType) {
ctx.error("return outside method definition", tree.pos)
(EmptyTree, WildcardType)
} else if (owner.isSourceMethod)
if (owner.isCompleted) {
val from = Ident(TermRef(NoPrefix, owner.asTerm))
val proto = returnProto(owner)
(from, proto)
} else (EmptyTree, errorType(d"$owner has return statement; needs result type", tree.pos))
else enclMethInfo(cx.outer)
def returnProto(owner: Symbol, locals: Scope): Type =
if (owner.isConstructor) defn.UnitType
else owner.info match {
case info: PolyType =>
val tparams = locals.toList.takeWhile(_ is TypeParam)
assert(info.paramNames.length == tparams.length,
i"return mismatch from $owner, tparams = $tparams, locals = ${locals.toList}%, %")
info.instantiate(tparams.map(_.typeRef)).finalResultType
case info =>
info.finalResultType
}
else
(tree.from.asInstanceOf[tpd.Tree], returnProto(tree.from.symbol))
val (from, proto) = enclMethInfo(ctx)
def enclMethInfo(cx: Context): (Tree, Type) = {
val owner = cx.owner
if (cx == NoContext || owner.isType) {
ctx.error("return outside method definition", tree.pos)
(EmptyTree, WildcardType)
}
else if (owner != cx.outer.owner && owner.isSourceMethod) {
if (owner.isCompleted) {
val from = Ident(TermRef(NoPrefix, owner.asTerm))
val proto = returnProto(owner, cx.scope)
(from, proto)
}
else (EmptyTree, errorType(d"$owner has return statement; needs result type", tree.pos))
}
else enclMethInfo(cx.outer)
}
val (from, proto) =
if (tree.from.isEmpty) enclMethInfo(ctx)
else (tree.from.asInstanceOf[tpd.Tree], WildcardType)
val expr1 = typedExpr(tree.expr orElse untpd.unitLiteral.withPos(tree.pos), proto)
assignType(cpy.Return(tree)(expr1, from))
}
Expand Down