Skip to content

Fix #10137: Emit a warning if generated main class conflicts #10203

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
Nov 6, 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
8 changes: 8 additions & 0 deletions compiler/src/dotty/tools/dotc/ast/MainProxies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ object MainProxies {
mainMethods(stats).flatMap(mainProxy)
}

private def checkNoShadowing(mainFun: Symbol)(using Context) =
val cls = ctx.typer.findRef(mainFun.name.toTypeName, WildcardType, EmptyFlags, mainFun).typeSymbol
if cls.exists && cls.owner != ctx.owner then
report.warning(
i"""The class `${ctx.printer.fullNameString(mainFun)}` generated from `@main` will shadow the existing ${cls.showLocated}.
|The existing definition might no longer be found on recompile.""", mainFun)

import untpd._
def mainProxy(mainFun: Symbol)(using Context): List[TypeDef] = {
val mainAnnotSpan = mainFun.getAnnotation(defn.MainAnnot).get.tree.span
Expand Down Expand Up @@ -86,6 +93,7 @@ object MainProxies {
case _ =>
report.error(s"@main can only annotate a method", pos)
}
checkNoShadowing(mainFun)
val errVar = Ident(nme.error)
val handler = CaseDef(
Typed(errVar, TypeTree(defn.CLP_ParseError.typeRef)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
nameString(if (ctx.property(XprintMode).isEmpty) sym.initial.name else sym.name)

override def fullNameString(sym: Symbol): String =
if (isEmptyPrefix(sym.maybeOwner)) nameString(sym)
if !sym.exists || isEmptyPrefix(sym.effectiveOwner) then nameString(sym)
else super.fullNameString(sym)

override protected def fullNameOwner(sym: Symbol): Symbol = {
Expand Down
11 changes: 6 additions & 5 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2241,11 +2241,12 @@ class Typer extends Namer
val pkg = pid1.symbol
pid1 match
case pid1: RefTree if pkg.is(Package) =>
val packageCtx = ctx.packageContext(tree, pkg)
var stats1 = typedStats(tree.stats, pkg.moduleClass)(using packageCtx)._1
if (!ctx.isAfterTyper)
stats1 = stats1 ++ typedBlockStats(MainProxies.mainProxies(stats1))(using packageCtx)._1
cpy.PackageDef(tree)(pid1, stats1).withType(pkg.termRef)
inContext(ctx.packageContext(tree, pkg)) {
var stats1 = typedStats(tree.stats, pkg.moduleClass)._1
if (!ctx.isAfterTyper)
stats1 = stats1 ++ typedBlockStats(MainProxies.mainProxies(stats1))._1
cpy.PackageDef(tree)(pid1, stats1).withType(pkg.termRef)
}
case _ =>
// Package will not exist if a duplicate type has already been entered, see `tests/neg/1708.scala`
errorTree(tree,
Expand Down
10 changes: 10 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i10137.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Error: tests/neg-custom-args/fatal-warnings/i10137.scala:2:12 -------------------------------------------------------
2 | @main def main(): Unit = println("Hello, World!") // error
| ^
| The class `foo.main` generated from `@main` will shadow the existing class main in package scala.
| The existing definition might no longer be found on recompile.
-- Error: tests/neg-custom-args/fatal-warnings/i10137.scala:4:10 -------------------------------------------------------
4 |@main def List(): Unit = println("List") // error
| ^
| The class `List` generated from `@main` will shadow the existing type List in package scala.
| The existing definition might no longer be found on recompile.
4 changes: 4 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i10137.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package foo:
@main def main(): Unit = println("Hello, World!") // error

@main def List(): Unit = println("List") // error
18 changes: 18 additions & 0 deletions tests/pending/pos/i10161.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
object Incompat2 {
Copy link
Member

Choose a reason for hiding this comment

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

is this meant to be in this PR?

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, it's an open issue, so we can keep it in the test set

trait Context { type Out }

object Context {
def foo(implicit ctx: Context): Option[ctx.Out] = ???

def bar(implicit ctx: Context): (Option[ctx.Out], String) = (foo, "foo")
}
}
object Incompat3 {
trait Context { type Out }

object Context {
given foo(using ctx: Context) as Option[ctx.Out] = ???

given bar(using ctx: Context) as (Option[ctx.Out], String) = (foo, "foo")
}
}