Skip to content

Fix 2403: Tweak places where we look for new definition #2414

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 1 commit into from
May 29, 2017
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
17 changes: 12 additions & 5 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1222,17 +1222,24 @@ object messages {
case class AmbiguousImport(name: Names.Name, newPrec: Int, prevPrec: Int, prevCtx: Context)(implicit ctx: Context)
extends Message(AmbiguousImportID) {

import typer.Typer.BindingPrec._
import typer.Typer.BindingPrec

/** A string which explains how something was bound; Depending on `prec` this is either
* imported by <tree>
* or defined in <symbol>
*/
private def bindingString(prec: Int, whereFound: Context, qualifier: String = "") =
if (isImportPrec(prec)) {
ex"""imported$qualifier by ${hl"${whereFound.importInfo}"}"""
private def bindingString(prec: Int, whereFound: Context, qualifier: String = "") = {
val howVisible = prec match {
case BindingPrec.definition => "defined"
case BindingPrec.namedImport => "imported by name"
case BindingPrec.wildImport => "imported"
case BindingPrec.packageClause => "found"
}
if (BindingPrec.isImportPrec(prec)) {
ex"""$howVisible$qualifier by ${hl"${whereFound.importInfo}"}"""
} else
ex"""defined$qualifier in ${hl"${whereFound.owner.toString}"}"""
ex"""$howVisible$qualifier in ${hl"${whereFound.owner}"}"""
}


val msg =
Expand Down
18 changes: 14 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,21 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
else {
var result: Type = NoType

// find definition
if ((lastCtx eq ctx) || (ctx.scope ne lastCtx.scope) || (ctx.owner ne lastCtx.owner)) {
val curOwner = ctx.owner

// Can this scope contain new definitions? This is usually the first
// context where either the scope or the owner changes wrt the
// context immediately nested in it. But for package contexts, it's
// the opposite: the last context before the package changes. This distinction
// is made so that top-level imports following a package clause are
// logically nested in that package clause.
val isNewDefScope =
if (curOwner is Package) curOwner ne ctx.outer.owner
else (ctx.scope ne lastCtx.scope) || (curOwner ne lastCtx.owner)

if (isNewDefScope) {
val defDenot = ctx.denotNamed(name)
if (qualifies(defDenot)) {
val curOwner = ctx.owner
val found =
if (isSelfDenot(defDenot)) curOwner.enclosingClass.thisType
else curOwner.thisType.select(name, defDenot)
Expand Down Expand Up @@ -297,7 +307,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
}

// begin findRef
loop(ctx)(ctx)
loop(NoContext)(ctx)
}

// begin typedIdent
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/i1641.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bar { object bippy extends (Double => String) { def apply(x: Double): St
package object println { def bippy(x: Int, y: Int, z: Int) = "(Int, Int, Int)" }
object Test {
def main(args: Array[String]): Unit = {
println(bar.bippy(5.5)) // error
println(bar.bippy(5.5))
Copy link
Member

@smarter smarter May 11, 2017

Choose a reason for hiding this comment

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

Isn't it concerning that this is now different from what scalac does? Is there some more subtlety in the way definitions are looked up that we're missing?

println(bar.bippy(1, 2, 3)) // error
}
}