diff options
author | Felix Mulder <felix.mulder@gmail.com> | 2016-09-15 18:04:03 +0200 |
---|---|---|
committer | Felix Mulder <felix.mulder@gmail.com> | 2016-10-10 13:25:32 +0200 |
commit | c9a8bb8d783d288ae2c165b8eefb3ca47c372c4e (patch) | |
tree | 15944309b9fdbbf27df32872cc4e98d9de7b3621 /src | |
parent | 66f7f7d23264374f9fbc581538fa1f47de16433c (diff) | |
download | dotty-c9a8bb8d783d288ae2c165b8eefb3ca47c372c4e.tar.gz dotty-c9a8bb8d783d288ae2c165b8eefb3ca47c372c4e.tar.bz2 dotty-c9a8bb8d783d288ae2c165b8eefb3ca47c372c4e.zip |
Add more examples to Typer
Diffstat (limited to 'src')
-rw-r--r-- | src/dotty/tools/dotc/reporting/Examples.scala | 32 | ||||
-rw-r--r-- | src/dotty/tools/dotc/typer/Typer.scala | 24 |
2 files changed, 45 insertions, 11 deletions
diff --git a/src/dotty/tools/dotc/reporting/Examples.scala b/src/dotty/tools/dotc/reporting/Examples.scala index 9759d39e8..652620e29 100644 --- a/src/dotty/tools/dotc/reporting/Examples.scala +++ b/src/dotty/tools/dotc/reporting/Examples.scala @@ -2,8 +2,10 @@ package dotty.tools package dotc package reporting -import dotc.core.Contexts.Context +import dotc.core._ +import Contexts.Context, Decorators._, Symbols._ import dotc.printing.SyntaxHighlighting._ +import util.{SourcePosition, NoSourcePosition} object ErrorExplanations { import dotc.ast.Trees._ @@ -73,4 +75,32 @@ object ErrorExplanations { val msg = hl"""A ${"try"} without ${"catch"} or ${"finally"} is equivalent to putting its body in a block; no exceptions are handled.""" } + + case class DuplicateBind(bind: untpd.Bind, tree: untpd.CaseDef)(implicit ctx: Context) extends Explanation { + val msg = + em"duplicate pattern variable: `${bind.name}`" + + val explanation = { + val pat = tree.pat.show + val guard = tree.guard match { + case untpd.EmptyTree => "" + case guard => s"if ${guard.show}" + } + + val body = tree.body match { + case Block(Nil, untpd.EmptyTree) => "" + case body => s" ${body.show}" + } + + val caseDef = s"case $pat$guard => $body" + + hl"""|Explanation + |=========== + |For each ${"case"} bound variable names have to be unique. In: + | + |$caseDef + | + |`${bind.name}` is not unique. Rename one of the binds!""".stripMargin + } + } } diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 3e3bb32f5..19297f9ec 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -37,6 +37,8 @@ import rewrite.Rewrites.patch import NavigateAST._ import transform.SymUtils._ import language.implicitConversions +import printing.SyntaxHighlighting._ +import reporting.ErrorExplanations._ object Typer { @@ -141,9 +143,11 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit * imported by <tree> * or defined in <symbol> */ - def bindingString(prec: Int, whereFound: Context, qualifier: String = "")(implicit ctx: Context) = - if (prec == wildImport || prec == namedImport) ex"imported$qualifier by ${whereFound.importInfo}" - else ex"defined$qualifier in ${whereFound.owner}" + def bindingString(prec: Int, whereFound: Context, qualifier: String = "") = + if (prec == wildImport || prec == namedImport) { + ex"""imported$qualifier by ${hl"${whereFound.importInfo.toString}"}""" + } else + ex"""defined$qualifier in ${hl"${whereFound.owner.toString}"}""" /** Check that any previously found result from an inner context * does properly shadow the new one from an outer context. @@ -166,9 +170,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit else { if (!scala2pkg && !previous.isError && !found.isError) { error( - ex"""reference to $name is ambiguous; - |it is both ${bindingString(newPrec, ctx, "")} - |and ${bindingString(prevPrec, prevCtx, " subsequently")}""", + ex"""|reference to `$name` is ambiguous + |it is both ${bindingString(newPrec, ctx, "")} + |and ${bindingString(prevPrec, prevCtx, " subsequently")}""", tree.pos) } previous @@ -181,7 +185,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit def checkUnambiguous(found: Type) = { val other = namedImportRef(site, selectors.tail) if (other.exists && found.exists && (found != other)) - error(em"reference to $name is ambiguous; it is imported twice in ${ctx.tree}", + error(em"reference to `$name` is ambiguous; it is imported twice in ${ctx.tree}", tree.pos) found } @@ -839,11 +843,11 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit mapOver(t) } } - override def transform(tree: Tree)(implicit ctx: Context) = - super.transform(tree.withType(elimWildcardSym(tree.tpe))) match { + override def transform(trt: Tree)(implicit ctx: Context) = + super.transform(trt.withType(elimWildcardSym(trt.tpe))) match { case b: Bind => if (ctx.scope.lookup(b.name) == NoSymbol) ctx.enter(b.symbol) - else ctx.error(em"duplicate pattern variable: ${b.name}", b.pos) + else ctx.explainError(DuplicateBind(b, tree), b.pos) b.symbol.info = elimWildcardSym(b.symbol.info) b case t => t |