File tree 5 files changed +48
-35
lines changed
compiler/src/dotty/tools/dotc/transform
5 files changed +48
-35
lines changed Original file line number Diff line number Diff line change @@ -407,8 +407,11 @@ class TailRec extends MiniPhase {
407
407
assert(false , " We should never have gotten inside a pattern" )
408
408
tree
409
409
410
- case _ : ValDef | _ : DefDef | _ : Super | _ : This |
411
- _ : Literal | _ : TypeTree | _ : TypeDef | EmptyTree =>
410
+ case tree : ValOrDefDef =>
411
+ if (isMandatory) noTailTransform(tree.rhs)
412
+ tree
413
+
414
+ case _ : Super | _ : This | _ : Literal | _ : TypeTree | _ : TypeDef | EmptyTree =>
412
415
tree
413
416
414
417
case Labeled (bind, expr) =>
Original file line number Diff line number Diff line change @@ -43,12 +43,12 @@ object Test1772B {
43
43
44
44
// the `liftedTree` local method will prevent a tail call here.
45
45
@ tailrec
46
- def bar (i : Int ) : Int = { // error: TailRec optimisation not applicable
46
+ def bar (i : Int ) : Int = {
47
47
if (i == 0 ) 0
48
48
else 1 + (try {
49
49
throw new RuntimeException
50
50
} catch {
51
- case _ : Throwable => bar(i - 1 ) // old error: cannot rewrite recursive call
51
+ case _ : Throwable => bar(i - 1 ) // error: it is not in tail position
52
52
})
53
53
}
54
54
}
Original file line number Diff line number Diff line change
1
+ import annotation .tailrec
2
+
3
+ object Test {
4
+ def foo (x : => Int ) = 1
5
+ def bar (x : Int => Int ) = 1
6
+
7
+ @ tailrec def rec1 : Int =
8
+ foo(rec1) // error: not in tail position
9
+
10
+ @ tailrec def rec2 : Int =
11
+ bar(_ => rec2) // error: not in tail position
12
+
13
+ @ tailrec def rec3 : Int =
14
+ 1 + (try ??? catch {
15
+ case _ : Throwable =>
16
+ rec3 // error: not in tail position
17
+ })
18
+
19
+ @ tailrec def rec4 : Unit = {
20
+ def local = rec4 // error: not in tail position
21
+ }
22
+
23
+ @ tailrec def rec5 : Int = {
24
+ val x = {
25
+ rec5 // error: not in tail position
26
+ 1
27
+ }
28
+ x
29
+ }
30
+ }
31
+
32
+ object Test4649 {
33
+ @ tailrec
34
+ def lazyFilter [E ](s : Stream [E ], p : E => Boolean ): Stream [E ] = s match {
35
+ case h #:: t =>
36
+ if (p(h)) h #:: lazyFilter(t, p) // error: not in tail position
37
+ else lazyFilter(t, p)
38
+ case _ =>
39
+ Stream .empty
40
+ }
41
+ }
Original file line number Diff line number Diff line change 1
- import scala .annotation .tailrec
2
1
trait A { self : B =>
3
- @ tailrec
4
2
private def foo (arg1 : Int , arg2 : Int ): Int = {
5
3
def bar = this .foo(arg1, arg2)
6
4
foo(arg1, arg2)
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments