Skip to content

Commit 38116bf

Browse files
authored
Merge pull request #10594 from dotty-staging/fix-#10295
Fix #10295: Require import qualifiers to be idempotent
2 parents a575f9c + ce77e47 commit 38116bf

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ trait Checking {
722722
private def checkLegalImportOrExportPath(path: Tree, kind: String)(using Context): Unit = {
723723
checkStable(path.tpe, path.srcPos, kind)
724724
if (!ctx.isAfterTyper) Checking.checkRealizable(path.tpe, path.srcPos)
725+
if !isIdempotentExpr(path) then
726+
report.error(em"import prefix is not a pure expression", path.srcPos)
725727
}
726728

727729
/** Check that `path` is a legal prefix for an import clause */

tests/neg/i10295.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def get: 1 = { println("hi"); 1 }
2+
import get._ // error: import prefix is not a pure expression
3+
val x = get.toLong
4+

tests/pos/i10295.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
trait M:
2+
type X
3+
object X:
4+
def foo(): X = ???
5+
6+
inline def m(using m: M): m.type = m
7+
8+
def doSomething(body: M ?=> Unit) = body(using new M{})
9+
10+
11+
def Test1 =
12+
given M = new M{}
13+
import m._
14+
val x: X = X.foo()
15+
println(x)
16+
17+
def Test2 =
18+
19+
doSomething {
20+
val x: m.X = m.X.foo()
21+
println(x)
22+
}
23+
// or with an import
24+
doSomething {
25+
import m._ // Concise and clear import of the same stable path `m`
26+
val x: X = X.foo()
27+
println(x)
28+
}
29+
// without this feature we would need an extra line in each call site
30+
doSomething {
31+
// not ideal
32+
val myM = m // or summon[M]
33+
import myM._
34+
val x: X = X.foo()
35+
println(x)
36+
}

0 commit comments

Comments
 (0)