File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff 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 */
Original file line number Diff line number Diff line change 1+ def get : 1 = { println(" hi" ); 1 }
2+ import get ._ // error: import prefix is not a pure expression
3+ val x = get.toLong
4+
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments