Skip to content

Fix #9529: Desugar ExtMethods in expression position #9553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,8 @@ object desugar {
case PatDef(mods, pats, tpt, rhs) =>
val pats1 = if (tpt.isEmpty) pats else pats map (Typed(_, tpt))
flatTree(pats1 map (makePatDef(tree, mods, _, rhs)))
case ext: ExtMethods =>
Block(List(ext), Literal(Constant(())).withSpan(ext.span))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this can only happen in case of error, maybe the parser should be changed to not try to parse extension methods in such positions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we special case extensions? Isn't it better to handle them the same as defs? I would expect that the following would befave similarly

def f1: Unit = { def g = 0 }
def f2: Unit = { extension (x: Int) def g = 0 }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, same error should be emitted for

def f1: Int = { def g = 0 } // error: Found: Unit   Required: Int
def f2: Int = { extension (x: Int) def g = 0 } // error: Found: Unit   Required: Int

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok then is it possible to have a pos test for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, ill add the test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added these tests

}
desugared.withSpan(tree.span)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3654,7 +3654,7 @@ object Parsers {
newLineOptWhenFollowedBy(LBRACE)
if in.isNestedStart then inDefScopeBraces(extMethods())
else { syntaxError("Extension without extension methods"); Nil }
val result = ExtMethods(tparams, extParams :: givenParamss, methods)
val result = atSpan(start)(ExtMethods(tparams, extParams :: givenParamss, methods))
val comment = in.getDocComment(start)
if comment.isDefined then
for meth <- methods do
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i9529.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
given Int = { // should have been `given AnyRef {`
extension (n: Int) // error
def plus(m: Int): Int = n + m
}
2 changes: 2 additions & 0 deletions tests/neg/i9529b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def f1: Int = { def g = 0 } // error: Found: Unit Required: Int
def f2: Int = { extension (x: Int) def g = 0 } // error: Found: Unit Required: Int
2 changes: 2 additions & 0 deletions tests/pos/i9529.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def f1: Unit = { def g = 0 }
def f2: Unit = { extension (x: Int) def g = 0 }