Skip to content
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

fix(#18265): crash on extension method without type nor RHS #18743

Merged
Changes from 1 commit
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
Next Next commit
fix(#18265): crash on extension method without body
This commit fixes a bug that causes Scala compiler to crash due to position error
when an extension method followed by a newline has neither type annotation nor `= <expr>`.

This commit is based on #18445.

Co-Authored-By: @hamzaremmal
i10416 committed Oct 22, 2023
commit caf30e3d1186ed306ba5c8ef93b148ad7db6dc54
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
@@ -2577,6 +2577,8 @@ object Parsers {
parents match {
case parent :: Nil if !in.isNestedStart =>
reposition(if (parent.isType) ensureApplied(wrapNew(parent)) else parent)
case tkn if in.token == INDENT =>
Copy link
Contributor Author

@i10416 i10416 Oct 22, 2023

Choose a reason for hiding this comment

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

With brace syntax, the last token consumed by a parser is }, but with indent syntax, the last token consumed by a parser is OUTDENT, which causes mismatching spans(actual offset is n, but reposition set offset less than n). Template returned from templateBodyOpt has the exact span, so we reuse it.

New(templateBodyOpt(emptyConstructor, parents, Nil))
case _ =>
New(reposition(templateBodyOpt(emptyConstructor, parents, Nil)))
}
6 changes: 6 additions & 0 deletions tests/neg/i18265.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- [E019] Syntax Error: tests/neg/i18265.scala:5:13 --------------------------------------------------------------------
5 | def twice // error
| ^
| Missing return type
|
| longer explanation available when compiling with `-explain`
5 changes: 5 additions & 0 deletions tests/neg/i18265.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait Foo

val foo = new Foo:
extension (s: String)
def twice // error