Skip to content

Commit 986c2df

Browse files
committed
Refix #4936: Move check from Parsers back to Desugar
This follows again the original strategy in 1ecf67b. This check is at the latest possible point: a few lines later, `RetainedModuleValFlags` excludes `Abstract` and `Sealed` while `ModuleValCreationFlags` adds `Final`. Unlike in Parsers, at this point in Desugar we get Synthetic objects. We also get objects with flags but without corresponding mods; they might all be synthetic, but let's harden flagSpan anyway.
1 parent 7044e39 commit 986c2df

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,17 @@ object desugar {
701701
val impl = mdef.impl
702702
val mods = mdef.mods
703703
def isEnumCase = mods.isEnumCase
704+
705+
def flagSourcePos(flag: FlagSet) = mods.mods.find(_.flags == flag).fold(mdef.sourcePos)(_.sourcePos)
706+
707+
if (mods.is(Abstract))
708+
ctx.error(hl"""$Abstract modifier cannot be used for objects""", flagSourcePos(Abstract))
709+
if (mods.is(Sealed))
710+
ctx.error(hl"""$Sealed modifier is redundant for objects""", flagSourcePos(Sealed))
711+
// Maybe this should be an error; see https://github.com/scala/bug/issues/11094.
712+
if (mods.is(Final) && !mods.is(Synthetic))
713+
ctx.warning(hl"""$Final modifier is redundant for objects""", flagSourcePos(Final))
714+
704715
if (mods is Package)
705716
PackageDef(Ident(moduleName), cpy.ModuleDef(mdef)(nme.PACKAGE, impl).withMods(mods &~ Package) :: Nil)
706717
else if (isEnumCase)

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,16 +2415,6 @@ object Parsers {
24152415

24162416
def objectDefRest(start: Offset, mods: Modifiers, name: TermName): ModuleDef = {
24172417
val template = templateOpt(emptyConstructor)
2418-
2419-
def flagSpan(flag: FlagSet) = mods.mods.find(_.flags == flag).get.span
2420-
if (mods is Abstract)
2421-
syntaxError(hl"""${Abstract} modifier cannot be used for objects""", flagSpan(Abstract))
2422-
if (mods is Sealed)
2423-
syntaxError(hl"""${Sealed} modifier is redundant for objects""", flagSpan(Sealed))
2424-
// Maybe this should be an error; see https://github.com/scala/bug/issues/11094.
2425-
if (mods is Final)
2426-
warning(hl"""${Final} modifier is redundant for objects""", source atSpan flagSpan(Final))
2427-
24282418
finalizeDef(ModuleDef(name, template), mods, start)
24292419
}
24302420

0 commit comments

Comments
 (0)