Skip to content

Fix #10295: Require import qualifiers to be idempotent #10594

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
Dec 6, 2020
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
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ trait Checking {
def checkLegalImportPath(path: Tree)(using Context): Unit = {
checkStable(path.tpe, path.srcPos, "import prefix")
if (!ctx.isAfterTyper) Checking.checkRealizable(path.tpe, path.srcPos)
if !isIdempotentExpr(path) then
report.error(em"import prefix is not a pure expression", path.srcPos)
}

/** Check that `tp` is a class type.
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i10295.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def get: 1 = { println("hi"); 1 }
import get._ // error: import prefix is not a pure expression
val x = get.toLong

36 changes: 36 additions & 0 deletions tests/pos/i10295.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
trait M:
type X
object X:
def foo(): X = ???

inline def m(using m: M): m.type = m

def doSomething(body: M ?=> Unit) = body(using new M{})


def Test1 =
given M = new M{}
import m._
val x: X = X.foo()
println(x)

def Test2 =

doSomething {
val x: m.X = m.X.foo()
println(x)
}
// or with an import
doSomething {
import m._ // Concise and clear import of the same stable path `m`
val x: X = X.foo()
println(x)
}
// without this feature we would need an extra line in each call site
doSomething {
// not ideal
val myM = m // or summon[M]
import myM._
val x: X = X.foo()
println(x)
}