From 5b0c48481971802278e42b898ddfc14d670984be Mon Sep 17 00:00:00 2001 From: Kacper Korban Date: Fri, 1 Nov 2024 12:31:06 +0100 Subject: [PATCH] Refactor checking for symbol references in signatures, when infering tracked --- after-printing.txt | 151 ++++++++++++++++++ .../src/dotty/tools/dotc/config/Feature.scala | 2 +- .../src/dotty/tools/dotc/typer/Namer.scala | 42 ++--- 3 files changed, 167 insertions(+), 28 deletions(-) create mode 100644 after-printing.txt diff --git a/after-printing.txt b/after-printing.txt new file mode 100644 index 000000000000..602cdb4a58b2 --- /dev/null +++ b/after-printing.txt @@ -0,0 +1,151 @@ +Header: + version: + tooling: + UUID: + +Names (351 bytes, starting from ): + 0: ASTs + 1: + 2: scala + 3: reflect + 4: scala[Qualified . reflect] + 5: ClassTag + 6: Foo + 7: + 8: java + 9: lang + 10: java[Qualified . lang] + 11: Object + 12: java[Qualified . lang][Qualified . Object] + 13: [Signed Signature(List(),java.lang.Object) @] + 14: Unit + 15: mkArray + 16: T + 17: Nothing + 18: Any + 19: WitnessNames + 20: annotation + 21: scala[Qualified . annotation] + 22: internal + 23: scala[Qualified . annotation][Qualified . internal] + 24: scala[Qualified . annotation][Qualified . internal][Qualified . WitnessNames] + 25: collection + 26: scala[Qualified . collection] + 27: immutable + 28: scala[Qualified . collection][Qualified . immutable] + 29: Seq + 30: scala[Qualified . collection][Qualified . immutable][Qualified . Seq] + 31: [Signed Signature(List(scala.collection.immutable.Seq),scala.annotation.internal.WitnessNames) @] + 32: String + 33: evidence$1 + 34: evidence$ + 35: [Unique evidence$ 1] + 36: ??? + 37: Predef + 38: SourceFile + 39: scala[Qualified . annotation][Qualified . internal][Qualified . SourceFile] + 40: java[Qualified . lang][Qualified . String] + 41: [Signed Signature(List(java.lang.String),scala.annotation.internal.SourceFile) @] + 42: + 43: Positions + 44: Comments + 45: Attributes + +Trees (122 bytes, starting from ): + 0: PACKAGE(120) + 2: TERMREFpkg 1 [] + 4: IMPORT(4) + 6: TERMREFpkg 4 [scala[Qualified . reflect]] + 8: IMPORTED 5 [ClassTag] + 10: TYPEDEF(110) 6 [Foo] + 13: TEMPLATE(89) + 15: APPLY(10) + 17: SELECTin(8) 13 [[Signed Signature(List(),java.lang.Object) @]] + 20: NEW + 21: TYPEREF 11 [Object] + 23: TERMREFpkg 10 [java[Qualified . lang]] + 25: SHAREDtype 21 + 27: DEFDEF(7) 7 [] + 30: EMPTYCLAUSE + 31: TYPEREF 14 [Unit] + 33: TERMREFpkg 2 [scala] + 35: STABLE + 36: DEFDEF(66) 15 [mkArray] + 39: TYPEPARAM(35) 16 [T] + 42: TYPEBOUNDStpt(8) + 44: TYPEREF 17 [Nothing] + 46: SHAREDtype 33 + 48: TYPEREF 18 [Any] + 50: SHAREDtype 33 + 52: ANNOTATION(22) + 54: TYPEREF 19 [WitnessNames] + 56: TERMREFpkg 23 [scala[Qualified . annotation][Qualified . internal]] + 58: APPLY(16) + 60: SELECTin(6) 31 [[Signed Signature(List(scala.collection.immutable.Seq),scala.annotation.internal.WitnessNames) @]] + 63: NEW + 64: SHAREDtype 54 + 66: SHAREDtype 54 + 68: REPEATED(6) + 70: TYPEREF 32 [String] + 72: SHAREDtype 23 + 74: STRINGconst 33 [evidence$1] + 76: PARAM(14) 35 [[Unique evidence$ 1]] + 79: APPLIEDtpt(10) + 81: IDENTtpt 5 [ClassTag] + 83: TYPEREF 5 [ClassTag] + 85: SHAREDtype 6 + 87: IDENTtpt 16 [T] + 89: TYPEREFdirect 39 + 91: GIVEN + 92: IDENTtpt 17 [Nothing] + 94: TYPEREF 17 [Nothing] + 96: TERMREFpkg 2 [scala] + 98: TERMREF 36 [???] + 100: TERMREF 37 [Predef] + 102: SHAREDtype 33 + 104: ANNOTATION(16) + 106: TYPEREF 38 [SourceFile] + 108: SHAREDtype 56 + 110: APPLY(10) + 112: SELECTin(6) 41 [[Signed Signature(List(java.lang.String),scala.annotation.internal.SourceFile) @]] + 115: NEW + 116: SHAREDtype 106 + 118: SHAREDtype 106 + 120: STRINGconst 42 [] + 122: + +Positions (81 bytes, starting from ): + lines: 7 + line sizes: + 38, 0, 23, 0, 10, 41, 0 + positions: + 0: 40 .. 117 + 4: 40 .. 63 + 6: 47 .. 54 + 8: 55 .. 63 + 10: 65 .. 117 + 13: 78 .. 117 + 21: 71 .. 71 + 27: 78 .. 78 + 31: 78 .. 78 + 36: 78 .. 117 + 39: 90 .. 101 + 44: 93 .. 93 + 48: 93 .. 93 + 64: 90 .. 90 + 70: 90 .. 90 + 74: 90 .. 90 + 76: 93 .. 101 + 81: 93 .. 101 + 87: 93 .. 101 + 92: 104 .. 111 + 98: 114 .. 117 + 110: 65 .. 117 + 116: 65 .. 65 + 120: 65 .. 65 + + source paths: + 0: 42 [] + +Attributes (2 bytes, starting from ): + SOURCEFILEattr 42 [] diff --git a/compiler/src/dotty/tools/dotc/config/Feature.scala b/compiler/src/dotty/tools/dotc/config/Feature.scala index c70d4f9e9da6..ad20bab46c1e 100644 --- a/compiler/src/dotty/tools/dotc/config/Feature.scala +++ b/compiler/src/dotty/tools/dotc/config/Feature.scala @@ -111,7 +111,7 @@ object Feature: * feature is defined. */ def enabled(feature: TermName)(using Context): Boolean = - enabledBySetting(feature) || enabledByImport(feature) || feature == modularity + enabledBySetting(feature) || enabledByImport(feature) /** Is auto-tupling enabled? */ def autoTuplingEnabled(using Context): Boolean = !enabled(nme.noAutoTupling) diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index bb1998be8b59..bcd6015b21fb 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -2003,13 +2003,9 @@ class Namer { typer: Typer => /** Try to infer if the parameter needs a `tracked` modifier */ def needsTracked(psym: Symbol, param: ValDef, owningSym: Symbol)(using Context) = - // println(i"Checking if $psym needs tracked") lazy val abstractContextBound = isContextBoundWitnessWithAbstractMembers(psym, param, owningSym) lazy val isRefInSignatures = psym.maybeOwner.isPrimaryConstructor - // && !psym.flags.is(Synthetic) - // && !psym.maybeOwner.flags.is(Synthetic) - // && !psym.maybeOwner.maybeOwner.flags.is(Synthetic) && isReferencedInPublicSignatures(psym) !psym.is(Tracked) && psym.isTerm @@ -2046,29 +2042,21 @@ class Namer { typer: Typer => case _ => false checkOwnerMemberSignatures(owner) - private def namedTypeWithPrefixContainsSymbolRef(tpe: Type, syms: List[Symbol])(using Context): Boolean = tpe match - case tpe: NamedType => tpe.prefix.exists && tpeContainsSymbolRef(tpe.prefix, syms) - case _ => false - - private def tpeContainsSymbolRef(tpe0: Type, syms: List[Symbol])(using Context): Boolean = - val tpe = tpe0.dropAlias.safeDealias - tpe match - case ExprType(resType) => tpeContainsSymbolRef(resType, syms) - case m : MethodOrPoly => - m.paramInfos.exists(tpeContainsSymbolRef(_, syms)) - || tpeContainsSymbolRef(m.resultType, syms) - case r @ RefinedType(parent, _, refinedInfo) => tpeContainsSymbolRef(parent, syms) || tpeContainsSymbolRef(refinedInfo, syms) - case TypeBounds(lo, hi) => tpeContainsSymbolRef(lo, syms) || tpeContainsSymbolRef(hi, syms) - case t: Type => - tpe.termSymbol.exists && syms.contains(tpe.termSymbol) - || tpe.argInfos.exists(tpeContainsSymbolRef(_, syms)) - || namedTypeWithPrefixContainsSymbolRef(tpe, syms) - - private def maybeParamAccessors(owner: Symbol, sym: Symbol)(using Context): List[Symbol] = - owner.infoOrCompleter match - case info: ClassInfo => - info.decls.lookupAll(sym.name).filter(d => d.is(ParamAccessor)).toList - case _ => List.empty + /** Check if any of syms are referenced in tpe */ + private def tpeContainsSymbolRef(tpe: Type, syms: List[Symbol])(using Context): Boolean = + val acc = new ExistsAccumulator( + { tpe => tpe.termSymbol.exists && syms.contains(tpe.termSymbol) }, + StopAt.Static, + forceLazy = false + ) { + override def apply(acc: Boolean, tpe: Type): Boolean = super.apply(acc, tpe.safeDealias) + } + acc(false, tpe) + + private def maybeParamAccessors(owner: Symbol, sym: Symbol)(using Context): List[Symbol] = owner.infoOrCompleter match + case info: ClassInfo => + info.decls.lookupAll(sym.name).filter(d => d.is(ParamAccessor)).toList + case _ => List.empty /** Under x.modularity, set every context bound evidence parameter of a class to be tracked, * provided it has a type that has an abstract type member. Reset private and local flags