Skip to content

Commit ce77e47

Browse files
committed
Fix #10295: Require import qualifiers to be idempotent
The use case for contextual abstractions is still supported if the function returning an implicit parameter is declared `inline`.
1 parent f1872e5 commit ce77e47

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
@@ -723,6 +723,8 @@ trait Checking {
723723
def checkLegalImportPath(path: Tree)(using Context): Unit = {
724724
checkStable(path.tpe, path.srcPos, "import prefix")
725725
if (!ctx.isAfterTyper) Checking.checkRealizable(path.tpe, path.srcPos)
726+
if !isIdempotentExpr(path) then
727+
report.error(em"import prefix is not a pure expression", path.srcPos)
726728
}
727729

728730
/** Check that `tp` is a class type.

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)