Skip to content

Commit 99006e1

Browse files
committed
test throw and inlined defs
1 parent 8ac0e64 commit 99006e1

File tree

6 files changed

+136
-12
lines changed

6 files changed

+136
-12
lines changed

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,40 @@ class ExtractSemanticDB extends Phase:
7272
/** The symbol occurrences generated so far, as a set */
7373
private val generated = new mutable.HashSet[SymbolOccurrence]
7474

75-
/** Definitions of this symbol should be excluded from semanticdb */
75+
/** Definitions of this symbol should be excluded from semanticdb, assumes sym exists */
7676
private def excludeDef(sym: Symbol)(using Context): Boolean =
77-
!sym.exists
78-
|| sym.isLocalDummy
77+
sym.isLocalDummy
7978
|| sym.is(Synthetic)
8079
|| sym.isSetter
8180
|| excludeDefOrUse(sym)
8281

82+
/** assumes sym exists */
8383
private def excludeDefOrUse(sym: Symbol)(using Context): Boolean =
8484
sym.name.is(NameKinds.DefaultGetterName)
8585
|| sym.isConstructor && (sym.owner.is(ModuleClass) || !sym.isGlobal)
8686
|| excludeSymbol(sym)
8787

88+
/** assumes sym exists */
8889
private def excludeSymbol(sym: Symbol)(using Context): Boolean =
8990
sym.name.isWildcard
9091
|| excludeQual(sym)
9192

93+
/** assumes sym exists */
9294
private def excludeQual(sym: Symbol)(using Context): Boolean =
9395
sym.isAnonymousFunction
9496
|| sym.isAnonymousModuleVal
9597
|| sym.name.isEmptyNumbered
9698

99+
/** assumes sym exists */
97100
private def excludeChildren(sym: Symbol)(using Context): Boolean =
98101
sym.isAllOf(HigherKinded | Param)
99102

100-
/** Uses of this symbol where the reference has given span should be excluded from semanticdb */
103+
/** Uses of this symbol where the reference has given span should be excluded from semanticdb, assumes sym and qualifier exist */
101104
private def excludeUse(qualifier: Option[Symbol], sym: Symbol)(using Context): Boolean =
102105
excludeDefOrUse(sym)
103106
|| sym.isConstructor && sym.owner.isAnnotation
104107
|| sym == defn.Any_typeCast
108+
|| sym.owner == defn.OpsPackageClass
105109
|| qualifier.exists(excludeQual)
106110

107111
private def traverseAnnotsOfDefinition(sym: Symbol)(using Context): Unit =
@@ -112,6 +116,15 @@ class ExtractSemanticDB extends Phase:
112116
case tree: Typed => () // hack for inline code
113117
case tree => traverse(tree)
114118

119+
120+
// guarded entrypoints
121+
private inline def excludeSymbol0(sym: Symbol)(using Context) = !sym.exists || excludeSymbol(sym)
122+
private inline def excludeChildren0(sym: Symbol)(using Context) = !sym.exists || excludeChildren(sym)
123+
private inline def excludeDef0(sym: Symbol)(using Context) = !sym.exists || excludeDef(sym)
124+
125+
/** assumes qualSym has been checked to exist already */
126+
private inline def excludeUse0(qualSym: Option[Symbol], sym: Symbol)(using Context) = !sym.exists || excludeUse(qualSym, sym)
127+
115128
override def traverse(tree: Tree)(using Context): Unit =
116129

117130
tree match
@@ -122,7 +135,7 @@ class ExtractSemanticDB extends Phase:
122135

123136
tree match
124137
case tree: PackageDef =>
125-
if !excludeDef(tree.pid.symbol)
138+
if !excludeDef0(tree.pid.symbol)
126139
&& tree.pid.span.hasLength
127140
tree.pid match
128141
case tree: Select =>
@@ -133,13 +146,13 @@ class ExtractSemanticDB extends Phase:
133146
case tree: NamedDefTree =>
134147
if tree.symbol.isAllOf(ModuleValCreationFlags)
135148
return
136-
if !excludeDef(tree.symbol)
149+
if !excludeDef0(tree.symbol)
137150
&& tree.span.hasLength
138151
registerDefinition(tree.symbol, tree.adjustedNameSpan, symbolKinds(tree), tree.source)
139152
val privateWithin = tree.symbol.privateWithin
140153
if privateWithin.exists
141154
registerUseGuarded(None, privateWithin, spanOfSymbol(privateWithin, tree.span, tree.source), tree.source)
142-
else if !excludeSymbol(tree.symbol)
155+
else if !excludeSymbol0(tree.symbol)
143156
registerSymbol(tree.symbol, symbolName(tree.symbol), symbolKinds(tree))
144157
tree match
145158
case tree: ValDef
@@ -170,11 +183,11 @@ class ExtractSemanticDB extends Phase:
170183
traverse(rhs)
171184
PatternValDef.collectPats(pat).foreach(traverse)
172185
case tree =>
173-
if !excludeChildren(tree.symbol)
186+
if !excludeChildren0(tree.symbol)
174187
traverseChildren(tree)
175188
case tree: Template =>
176189
val ctorSym = tree.constr.symbol
177-
if !excludeDef(ctorSym)
190+
if !excludeDef0(ctorSym)
178191
traverseAnnotsOfDefinition(ctorSym)
179192
registerDefinition(ctorSym, tree.constr.span, Set.empty, tree.source)
180193
ctorParams(tree.constr.vparamss, tree.body)
@@ -198,7 +211,7 @@ class ExtractSemanticDB extends Phase:
198211
case _ => traverse(arg)
199212
case tree: Assign =>
200213
val qualSym = condOpt(tree.lhs) { case Select(qual, _) if qual.symbol.exists => qual.symbol }
201-
if !excludeUse(qualSym, tree.lhs.symbol)
214+
if !excludeUse0(qualSym, tree.lhs.symbol)
202215
val lhs = tree.lhs.symbol
203216
val setter = lhs.matchingSetter.orElse(lhs)
204217
tree.lhs match
@@ -467,7 +480,7 @@ class ExtractSemanticDB extends Phase:
467480
generated += occ
468481

469482
private def registerUseGuarded(qualSym: Option[Symbol], sym: Symbol, span: Span, treeSource: SourceFile)(using Context) =
470-
if !excludeUse(qualSym, sym) then
483+
if !excludeUse0(qualSym, sym) then
471484
registerUse(sym, span, treeSource)
472485

473486
private def registerUse(sym: Symbol, span: Span, treeSource: SourceFile)(using Context): Unit =
@@ -571,7 +584,7 @@ class ExtractSemanticDB extends Phase:
571584
vparams <- vparamss
572585
vparam <- vparams
573586
do
574-
if !excludeSymbol(vparam.symbol)
587+
if !excludeSymbol0(vparam.symbol)
575588
traverseAnnotsOfDefinition(vparam.symbol)
576589
val symkinds =
577590
getters.get(vparam.name).fold(SymbolKind.emptySet)(getter =>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package inlineconsume
2+
3+
import inlinedefs.FakePredef/*->inlinedefs::FakePredef.*/.assert/*->inlinedefs::FakePredef.assert().*/
4+
5+
class Foo/*<-inlineconsume::Foo#*/:
6+
def test/*<-inlineconsume::Foo#test().*/ = assert/*->inlinedefs::FakePredef.assert().*/(3 >/*->scala::Int#`>`(+3).*/ 2)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package inlineconsume
2+
3+
import inlinedefs.FakePredef.assert
4+
5+
class Foo:
6+
def test = assert(3 > 2)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package inlinedefs
2+
3+
object FakePredef/*<-inlinedefs::FakePredef.*/:
4+
5+
def assertFail/*<-inlinedefs::FakePredef.assertFail().*/(): Nothing/*->scala::Nothing#*/ = throw new java.lang.AssertionError/*->java::lang::AssertionError#*//*->java::lang::AssertionError#`<init>`(+2).*/("assertion failed")
6+
7+
/** Super long padded documentation
8+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit,
9+
* sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
10+
* Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
11+
* nisi ut aliquip ex ea commodo consequat.
12+
* Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
13+
* dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
14+
* sunt in culpa qui officia deserunt mollit anim id est laborum.
15+
*/
16+
transparent inline final def assert/*<-inlinedefs::FakePredef.assert().*/(inline assertion/*<-inlinedefs::FakePredef.assert().(assertion)*/: Boolean/*->scala::Boolean#*/): Unit/*->scala::Unit#*/ = {
17+
if (!assertion/*->inlinedefs::FakePredef.assert().(assertion)*//*->scala::Boolean#`unary_!`().*/)
18+
assertFail/*->inlinedefs::FakePredef.assertFail().*/()
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package inlinedefs
2+
3+
object FakePredef:
4+
5+
def assertFail(): Nothing = throw new java.lang.AssertionError("assertion failed")
6+
7+
/** Super long padded documentation
8+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit,
9+
* sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
10+
* Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
11+
* nisi ut aliquip ex ea commodo consequat.
12+
* Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
13+
* dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
14+
* sunt in culpa qui officia deserunt mollit anim id est laborum.
15+
*/
16+
transparent inline final def assert(inline assertion: Boolean): Unit = {
17+
if (!assertion)
18+
assertFail()
19+
}

tests/semanticdb/metac.expect

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,67 @@ Occurrences:
29992999
[2:0..2:0): <- example/FilenameWithSpaces#`<init>`().
30003000
[2:6..2:24): FilenameWithSpaces <- example/FilenameWithSpaces#
30013001

3002+
expect/inlineconsume.scala
3003+
--------------------------
3004+
3005+
Summary:
3006+
Schema => SemanticDB v4
3007+
Uri => inlineconsume.scala
3008+
Text => empty
3009+
Language => Scala
3010+
Symbols => 3 entries
3011+
Occurrences => 9 entries
3012+
3013+
Symbols:
3014+
inlineconsume/Foo# => class Foo
3015+
inlineconsume/Foo#`<init>`(). => primary ctor <init>
3016+
inlineconsume/Foo#test(). => method test
3017+
3018+
Occurrences:
3019+
[0:8..0:21): inlineconsume <- inlineconsume/
3020+
[2:7..2:17): inlinedefs -> inlinedefs/
3021+
[2:18..2:28): FakePredef -> inlinedefs/FakePredef.
3022+
[2:29..2:35): assert -> inlinedefs/FakePredef.assert().
3023+
[4:6..4:9): Foo <- inlineconsume/Foo#
3024+
[5:2..5:2): <- inlineconsume/Foo#`<init>`().
3025+
[5:6..5:10): test <- inlineconsume/Foo#test().
3026+
[5:13..5:19): assert -> inlinedefs/FakePredef.assert().
3027+
[5:22..5:23): > -> scala/Int#`>`(+3).
3028+
3029+
expect/inlinedefs.scala
3030+
-----------------------
3031+
3032+
Summary:
3033+
Schema => SemanticDB v4
3034+
Uri => inlinedefs.scala
3035+
Text => empty
3036+
Language => Scala
3037+
Symbols => 4 entries
3038+
Occurrences => 15 entries
3039+
3040+
Symbols:
3041+
inlinedefs/FakePredef. => final object FakePredef
3042+
inlinedefs/FakePredef.assert(). => final macro assert
3043+
inlinedefs/FakePredef.assert().(assertion) => param assertion
3044+
inlinedefs/FakePredef.assertFail(). => method assertFail
3045+
3046+
Occurrences:
3047+
[0:8..0:18): inlinedefs <- inlinedefs/
3048+
[2:7..2:17): FakePredef <- inlinedefs/FakePredef.
3049+
[4:6..4:16): assertFail <- inlinedefs/FakePredef.assertFail().
3050+
[4:20..4:27): Nothing -> scala/Nothing#
3051+
[4:40..4:44): java -> java/
3052+
[4:45..4:49): lang -> java/lang/
3053+
[4:50..4:64): AssertionError -> java/lang/AssertionError#
3054+
[4:64..4:64): -> java/lang/AssertionError#`<init>`(+2).
3055+
[15:31..15:37): assert <- inlinedefs/FakePredef.assert().
3056+
[15:45..15:54): assertion <- inlinedefs/FakePredef.assert().(assertion)
3057+
[15:56..15:63): Boolean -> scala/Boolean#
3058+
[15:66..15:70): Unit -> scala/Unit#
3059+
[16:9..16:18): assertion -> inlinedefs/FakePredef.assert().(assertion)
3060+
[16:18..16:18): -> scala/Boolean#`unary_!`().
3061+
[17:6..17:16): assertFail -> inlinedefs/FakePredef.assertFail().
3062+
30023063
expect/local-file.scala
30033064
-----------------------
30043065

0 commit comments

Comments
 (0)