diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 04ddc52c2e47..82abd29e4dbc 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -550,18 +550,23 @@ object Parsers { } /** Accept identifier and return Ident with its name as a term name. */ - def termIdent(): Ident = atSpan(in.offset) { - makeIdent(in.token, ident()) - } + def termIdent(): Ident = + makeIdent(in.token, in.offset, ident()) /** Accept identifier and return Ident with its name as a type name. */ - def typeIdent(): Ident = atSpan(in.offset) { - makeIdent(in.token, ident().toTypeName) - } + def typeIdent(): Ident = + makeIdent(in.token, in.offset, ident().toTypeName) - private def makeIdent(tok: Token, name: Name) = - if (tok == BACKQUOTED_IDENT) BackquotedIdent(name) - else Ident(name) + private def makeIdent(tok: Token, offset: Offset, name: Name) = { + val tree = + if (tok == BACKQUOTED_IDENT) BackquotedIdent(name) + else Ident(name) + + // Make sure that even trees with parsing errors have a offset that is within the offset + val errorOffset = offset min (in.lastOffset - 1) + if (tree.name == nme.ERROR && tree.span == NoSpan) tree.withSpan(Span(errorOffset, errorOffset)) + else atSpan(offset)(tree) + } def wildcardIdent(): Ident = atSpan(accept(USCORE)) { Ident(nme.WILDCARD) } diff --git a/tests/neg/i5032a.scala b/tests/neg/i5032a.scala new file mode 100644 index 000000000000..2b6e8d31bdc6 --- /dev/null +++ b/tests/neg/i5032a.scala @@ -0,0 +1,2 @@ +class i1@ +// error \ No newline at end of file diff --git a/tests/neg/i5032b.scala b/tests/neg/i5032b.scala new file mode 100644 index 000000000000..68539e742bcb --- /dev/null +++ b/tests/neg/i5032b.scala @@ -0,0 +1,2 @@ +class a@ +class b@ // error // error