Skip to content

Make private inline accessors final #15324

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
Jun 4, 2022
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
9 changes: 5 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/AccessProxies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,18 @@ abstract class AccessProxies {
}

/** A fresh accessor symbol */
private def newAccessorSymbol(owner: Symbol, name: TermName, info: Type, span: Span)(using Context): TermSymbol = {
val sym = newSymbol(owner, name, Synthetic | Method, info, coord = span).entered
if (sym.allOverriddenSymbols.exists(!_.is(Deferred))) sym.setFlag(Override)
private def newAccessorSymbol(owner: Symbol, name: TermName, info: Type, accessed: Symbol)(using Context): TermSymbol = {
val sym = newSymbol(owner, name, Synthetic | Method, info, coord = accessed.span).entered
if accessed.is(Private) then sym.setFlag(Final)
else if sym.allOverriddenSymbols.exists(!_.is(Deferred)) then sym.setFlag(Override)
sym
}

/** An accessor symbol, create a fresh one unless one exists already */
protected def accessorSymbol(owner: Symbol, accessorName: TermName, accessorInfo: Type, accessed: Symbol)(using Context): Symbol = {
def refersToAccessed(sym: Symbol) = accessedBy.get(sym).contains(accessed)
owner.info.decl(accessorName).suchThat(refersToAccessed).symbol.orElse {
val acc = newAccessorSymbol(owner, accessorName, accessorInfo, accessed.span)
val acc = newAccessorSymbol(owner, accessorName, accessorInfo, accessed)
accessedBy(acc) = accessed
acc
}
Expand Down
11 changes: 11 additions & 0 deletions tests/neg/i15323.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class A:
private var x = 0
inline def f1(): Int =
x += 1;
x

class B extends A:
private var x = 0 // error
inline def f2(): Int =
x += 1
x