Skip to content

Symbol#source correct for all phases #6757

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
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
30 changes: 21 additions & 9 deletions compiler/src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -626,15 +626,27 @@ object Symbols {
final def symbol(implicit ev: DontUseSymbolOnSymbol): Nothing = unsupported("symbol")
type DontUseSymbolOnSymbol

final def source(implicit ctx: Context): SourceFile =
if (!defTree.isEmpty && !ctx.erasedTypes) defTree.source
else this match {
case cls: ClassSymbol => cls.sourceOfClass
case _ =>
if (denot.is(Module)) denot.moduleClass.source
else if (denot.exists) denot.owner.source
else NoSource
}
final def source(implicit ctx: Context): SourceFile = {
def valid(src: SourceFile): SourceFile =
if (src.exists && src.file.extension != "class") src
else NoSource

if (!denot.exists) NoSource
else
valid(defTree.source) match {
case NoSource =>
valid(denot.owner.source) match {
case NoSource =>
this match {
case cls: ClassSymbol => valid(cls.sourceOfClass)
case _ if denot.is(Module) => valid(denot.moduleClass.source)
case _ => NoSource
}
case src => src
}
case src => src
}
}

/** A symbol related to `sym` that is defined in source code.
*
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ object Inliner {
case tree: SeqLiteral => finalize(tree, untpd.SeqLiteral(transform(tree.elems), transform(tree.elemtpt))(curSource))
case tree: TypeTree => tpd.TypeTree(tree.tpe)(ctx.withSource(curSource)).withSpan(tree.span)
case tree: Bind => finalize(tree, untpd.Bind(tree.name, transform(tree.body))(curSource))
case tree: DefTree => super.transform(tree).setDefTree
case _ => super.transform(tree)
})
assert(transformed.isInstanceOf[EmptyTree[_]] || transformed.isInstanceOf[EmptyValDef[_]] || transformed.source == curSource)
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/inline-joint/defn.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Foo {
inline def foo = new { bar(0) }

def bar(i: => Int): Unit = ()
}
3 changes: 3 additions & 0 deletions tests/pos/inline-joint/use.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Test {
val v = Foo.foo
}