Skip to content

Commit b3501a6

Browse files
committed
do not look in companion in javaFindMember in typedSelect
1 parent 3e2a88e commit b3501a6

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

compiler/src/dotty/tools/dotc/core/ContextOps.scala

+12-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ object ContextOps:
3434
if (elem.name == name) return elem.sym.denot // return self
3535
}
3636
val pre = ctx.owner.thisType
37-
if ctx.isJava then javaFindMember(name, pre, required, excluded)
37+
if ctx.isJava then
38+
// Note: I didn't verify if there exists a code path that would require `lookInCompanion = true`,
39+
// it is just to preserve the original behavior.
40+
javaFindMember(name, pre, lookInCompanion = true, required, excluded)
3841
else pre.findMember(name, pre, required, excluded)
3942
}
4043
else // we are in the outermost context belonging to a class; self is invisible here. See inClassContext.
@@ -43,7 +46,13 @@ object ContextOps:
4346
ctx.scope.denotsNamed(name).filterWithFlags(required, excluded).toDenot(NoPrefix)
4447
}
4548

46-
final def javaFindMember(name: Name, pre: Type, required: FlagSet = EmptyFlags, excluded: FlagSet = EmptyFlags): Denotation =
49+
/** Look in the prefix with Java semantics.
50+
* @param lookInCompanion If true, try in the companion class of a module as a fallback.
51+
* Note: originally this was used to type Select nodes in Java code,
52+
* but that is no longer the case.
53+
* It is preserved in case it is necessary for denotNamed, but this is unverified.
54+
*/
55+
final def javaFindMember(name: Name, pre: Type, lookInCompanion: Boolean, required: FlagSet = EmptyFlags, excluded: FlagSet = EmptyFlags): Denotation =
4756
assert(ctx.isJava)
4857
inContext(ctx) {
4958

@@ -53,7 +62,7 @@ object ContextOps:
5362
val directSearch = pre.findMember(name, pre, required, excluded)
5463

5564
// 2. Try to search in companion class if current is an object.
56-
def searchCompanionClass = if preSym.is(Flags.Module) then
65+
def searchCompanionClass = if lookInCompanion && preSym.is(Flags.Module) then
5766
preSym.companionClass.thisType.findMember(name, pre, required, excluded)
5867
else NoDenotation
5968

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ trait TypeAssigner {
156156
val pre = maybeSkolemizePrefix(qualType, name)
157157
val mbr =
158158
if ctx.isJava then
159-
ctx.javaFindMember(name, pre)
159+
// don't look in the companion class here if qual is a module,
160+
// we use backtracking to instead change the qual to the companion class
161+
// if this fails.
162+
ctx.javaFindMember(name, pre, lookInCompanion = false)
160163
else
161164
qualType.findMember(name, pre)
162165

0 commit comments

Comments
 (0)