Skip to content

Commit 1fdf650

Browse files
committed
Fallback to Type.member for ParamRef
1 parent a69cc97 commit 1fdf650

File tree

2 files changed

+27
-49
lines changed

2 files changed

+27
-49
lines changed

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

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,23 @@ class TypeOps:
2323
given typeOps: TypeOps = this
2424

2525
extension [T <: Type](symtab: mutable.Map[(T, Name), Symbol])
26-
private def getOrErr(key: (T, Name), parent: Symbol)(using Context): Option[Symbol] =
27-
val sym = symtab.get(key)
28-
if sym.isEmpty then
29-
symbolNotFound(key._1, key._2, parent)
30-
sym
26+
private def getOrErr(binder: T, name: Name, parent: Symbol)(using Context): Option[Symbol] =
27+
// In case refinement or type param cannot be accessed from traverser and
28+
// no symbols are registered to the symbol table, fall back to Type.member
29+
val sym = symtab.getOrElse(
30+
(binder, name),
31+
binder.member(name).symbol
32+
)
33+
if sym.exists then
34+
Some(sym)
35+
else
36+
symbolNotFound(binder, name, parent)
37+
None
3138

3239
private def symbolNotFound(binder: Type, name: Name, parent: Symbol)(using ctx: Context): Unit =
33-
// Known issue: for exports
34-
// ```
35-
// // in Codec.scala
36-
// trait Encoder[T] ...
37-
//
38-
// // in ExportCodec.scala
39-
// export Encoder
40-
// ```
41-
// There's type argument TypeParamRef("T") for Codec in `ExportCodec.scala` whose binder is
42-
//
43-
// HKTypeLambda(
44-
// List(T),
45-
// List(TypeBounds(...)),
46-
// AppliedType(
47-
// TypeRef(... trait Decoder),
48-
// List(TypeParamRef(T))
49-
// )
50-
// )
51-
// where this HKTypeLambda never appears in the source code of `ExportCodec.scala`
52-
val suppress = parent.is(Flags.Exported)
53-
if !suppress then
54-
report.warning(
55-
s"""Internal error in extracting SemanticDB while compiling ${ctx.compilationUnit.source}: Ignoring ${name} of type ${binder}"""
56-
)
40+
report.warning(
41+
s"""Internal error in extracting SemanticDB while compiling ${ctx.compilationUnit.source}: Ignoring ${name} of type ${binder}"""
42+
)
5743

5844
extension (tpe: Type)
5945
def toSemanticSig(using LinkMode, Context, SemanticSymbolBuilder)(sym: Symbol): s.Signature =
@@ -144,12 +130,12 @@ class TypeOps:
144130
): (Type, List[List[Symbol]], List[Symbol]) = t match {
145131
case mt: MethodType =>
146132
val syms = mt.paramNames.flatMap { paramName =>
147-
paramRefSymtab.getOrErr((mt, paramName), sym)
133+
paramRefSymtab.getOrErr(mt, paramName, sym)
148134
}
149135
flatten(mt.resType, paramss :+ syms, tparams)
150136
case pt: PolyType =>
151137
val syms = pt.paramNames.flatMap { paramName =>
152-
paramRefSymtab.getOrErr((pt, paramName), sym)
138+
paramRefSymtab.getOrErr(pt, paramName, sym)
153139
}
154140
// there shouldn't multiple type params
155141
flatten(pt.resType, paramss, tparams ++ syms)
@@ -183,7 +169,7 @@ class TypeOps:
183169
val wildcardSym = newSymbol(NoSymbol, tpnme.WILDCARD, Flags.EmptyFlags, bounds)
184170
Some(wildcardSym)
185171
else
186-
paramRefSymtab.getOrErr((lambda, paramName), sym)
172+
paramRefSymtab.getOrErr(lambda, paramName, sym)
187173
}
188174
(lambda.resType, paramSyms)
189175
case _ => (tpe, Nil)
@@ -221,15 +207,16 @@ class TypeOps:
221207
s.SingleType(spre, ssym)
222208

223209
case tref: ParamRef =>
224-
val key = (tref.binder, tref.paramName)
225-
paramRefSymtab.getOrErr(key, sym) match {
210+
paramRefSymtab.getOrErr(
211+
tref.binder, tref.paramName, sym
212+
) match {
226213
case Some(ref) =>
227214
val ssym = ref.symbolName
228215
tref match {
229216
case _: TypeParamRef => s.TypeRef(s.Type.Empty, ssym, Seq.empty)
230217
case _: TermParamRef => s.SingleType(s.Type.Empty, ssym)
231218
}
232-
case None => // shouldn't happen
219+
case None =>
233220
s.Type.Empty
234221
}
235222

@@ -279,17 +266,8 @@ class TypeOps:
279266
val (parent, refinedInfos) = flatten(rt, List.empty)
280267
val stpe = s.IntersectionType(flattenParent(parent))
281268

282-
val decls = refinedInfos.map { (name, info) =>
283-
// In case refinement cannot be accessed from traverser and
284-
// no symbols are registered to the symtab
285-
// fall back to Type.member
286-
val decl = refinementSymtab.getOrElse(
287-
(rt, name),
288-
rt.member(name).symbol
289-
)
290-
if decl == NoSymbol then
291-
symbolNotFound(rt, name, sym)
292-
decl
269+
val decls = refinedInfos.flatMap { (name, info) =>
270+
refinementSymtab.getOrErr(rt, name, sym)
293271
}
294272
val sdecls = decls.sscopeOpt(using LinkMode.HardlinkChildren)
295273
s.StructuralType(stpe, sdecls)

tests/semanticdb/metac.expect

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3762,9 +3762,9 @@ Occurrences => 5 entries
37623762

37633763
Symbols:
37643764
exports/`exports-package$package`. => final package object exports extends Object { self: exports.type => +4 decls }
3765-
exports/`exports-package$package`.Codec# => final type Codec = Codec[<?>]
3766-
exports/`exports-package$package`.Decoder# => final type Decoder = Decoder[<?>]
3767-
exports/`exports-package$package`.Encoder# => final type Encoder = Encoder[<?>]
3765+
exports/`exports-package$package`.Codec# => final type Codec [unknown T: <?>] = Codec[T]
3766+
exports/`exports-package$package`.Decoder# => final type Decoder [unknown T: <?>] = Decoder[T]
3767+
exports/`exports-package$package`.Encoder# => final type Encoder [unknown T: <?>] = Encoder[T]
37683768

37693769
Occurrences:
37703770
[0:8..0:15): exports <- exports/

0 commit comments

Comments
 (0)