Skip to content

Commit 1b64c60

Browse files
Merge pull request #9553 from dotty-staging/fix-#9529
Fix #9529: Desugar ExtMethods in expression position
2 parents 1ef8e19 + 1e14ba3 commit 1b64c60

File tree

5 files changed

+11
-1
lines changed

5 files changed

+11
-1
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,8 @@ object desugar {
17151715
case PatDef(mods, pats, tpt, rhs) =>
17161716
val pats1 = if (tpt.isEmpty) pats else pats map (Typed(_, tpt))
17171717
flatTree(pats1 map (makePatDef(tree, mods, _, rhs)))
1718+
case ext: ExtMethods =>
1719+
Block(List(ext), Literal(Constant(())).withSpan(ext.span))
17181720
}
17191721
desugared.withSpan(tree.span)
17201722
}

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3654,7 +3654,7 @@ object Parsers {
36543654
newLineOptWhenFollowedBy(LBRACE)
36553655
if in.isNestedStart then inDefScopeBraces(extMethods())
36563656
else { syntaxError("Extension without extension methods"); Nil }
3657-
val result = ExtMethods(tparams, extParams :: givenParamss, methods)
3657+
val result = atSpan(start)(ExtMethods(tparams, extParams :: givenParamss, methods))
36583658
val comment = in.getDocComment(start)
36593659
if comment.isDefined then
36603660
for meth <- methods do

tests/neg/i9529.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
given Int = { // should have been `given AnyRef {`
2+
extension (n: Int) // error
3+
def plus(m: Int): Int = n + m
4+
}

tests/neg/i9529b.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def f1: Int = { def g = 0 } // error: Found: Unit Required: Int
2+
def f2: Int = { extension (x: Int) def g = 0 } // error: Found: Unit Required: Int

tests/pos/i9529.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def f1: Unit = { def g = 0 }
2+
def f2: Unit = { extension (x: Int) def g = 0 }

0 commit comments

Comments
 (0)