@@ -135,12 +135,16 @@ class Typer extends Namer
135
135
* @param name the name of the identifier
136
136
* @param pt the expected type
137
137
* @param required flags the result's symbol must have
138
+ * @param excluded flags the result's symbol must not have
138
139
* @param pos indicates position to use for error reporting
139
140
*/
140
- def findRef (name : Name , pt : Type , required : FlagSet , pos : SrcPos )(using Context ): Type = {
141
+ def findRef (name : Name , pt : Type , required : FlagSet , excluded : FlagSet , pos : SrcPos )(using Context ): Type = {
141
142
val refctx = ctx
142
143
val noImports = ctx.mode.is(Mode .InPackageClauseName )
143
- def fail (msg : Message ) = report.error(msg, pos)
144
+ def suppressErrors = excluded.is(ConstructorProxy )
145
+ // when searching for references shadowed by a constructor proxy, do not report errors
146
+ def fail (msg : Message ) =
147
+ if ! suppressErrors then report.error(msg, pos)
144
148
145
149
/** A symbol qualifies if it really exists and is not a package class.
146
150
* In addition, if we are in a constructor of a pattern, we ignore all definitions
@@ -204,7 +208,7 @@ class Typer extends Namer
204
208
imp.sym.info match
205
209
case ImportType (expr) =>
206
210
val pre = expr.tpe
207
- var denot = pre.memberBasedOnFlags(name, required, EmptyFlags )
211
+ var denot = pre.memberBasedOnFlags(name, required, excluded )
208
212
.accessibleFrom(pre)(using refctx)
209
213
// Pass refctx so that any errors are reported in the context of the
210
214
// reference instead of the context of the import scope
@@ -332,7 +336,7 @@ class Typer extends Namer
332
336
val scope = if owner.isClass then owner.info.decls else outer.scope
333
337
if scope.lookup(name).exists then
334
338
val symsMatch = scope.lookupAll(name).exists(denot.containsSym)
335
- if ! symsMatch then
339
+ if ! symsMatch && ! suppressErrors then
336
340
report.errorOrMigrationWarning(
337
341
AmbiguousReference (name, Definition , Inheritance , prevCtx)(using outer),
338
342
pos)
@@ -344,7 +348,7 @@ class Typer extends Namer
344
348
checkNoOuterDefs(denot, outer, prevCtx)
345
349
346
350
if isNewDefScope then
347
- val defDenot = ctx.denotNamed(name, required)
351
+ val defDenot = ctx.denotNamed(name, required, excluded )
348
352
if (qualifies(defDenot)) {
349
353
val found =
350
354
if (isSelfDenot(defDenot)) curOwner.enclosingClass.thisType
@@ -462,7 +466,7 @@ class Typer extends Namer
462
466
unimported = Set .empty
463
467
foundUnderScala2 = NoType
464
468
try
465
- val found = findRef(name, pt, EmptyFlags , tree.srcPos)
469
+ val found = findRef(name, pt, EmptyFlags , EmptyFlags , tree.srcPos)
466
470
if foundUnderScala2.exists && ! (foundUnderScala2 =:= found) then
467
471
report.migrationWarning(
468
472
ex """ Name resolution will change.
@@ -474,7 +478,17 @@ class Typer extends Namer
474
478
unimported = saved1
475
479
foundUnderScala2 = saved2
476
480
481
+ def checkNotShadowed (ownType : Type ) = ownType match
482
+ case ownType : TermRef if ownType.symbol.is(ConstructorProxy ) =>
483
+ val shadowed = findRef(name, pt, EmptyFlags , ConstructorProxy , tree.srcPos)
484
+ if shadowed.exists then
485
+ report.error(
486
+ em """ Reference to creator proxy for ${ownType.symbol.companionClass.showLocated}
487
+ |shadows outer reference to ${shadowed.termSymbol.showLocated}""" , tree.srcPos)
488
+ case _ =>
489
+
477
490
def setType (ownType : Type ): Tree =
491
+ checkNotShadowed(ownType)
478
492
val tree1 = ownType match
479
493
case ownType : NamedType if ! prefixIsElidable(ownType) =>
480
494
ref(ownType).withSpan(tree.span)
@@ -3475,10 +3489,10 @@ class Typer extends Namer
3475
3489
case selProto @ SelectionProto (selName : TermName , mbrType, _, _) =>
3476
3490
def tryExtension (using Context ): Tree =
3477
3491
try
3478
- findRef(selName, WildcardType , ExtensionMethod , tree.srcPos) match
3492
+ findRef(selName, WildcardType , ExtensionMethod , EmptyFlags , tree.srcPos) match
3479
3493
case ref : TermRef =>
3480
3494
extMethodApply(untpd.ref(ref).withSpan(tree.span), tree, mbrType)
3481
- case _ => findRef(selProto.extensionName, WildcardType , ExtensionMethod , tree.srcPos) match
3495
+ case _ => findRef(selProto.extensionName, WildcardType , ExtensionMethod , EmptyFlags , tree.srcPos) match
3482
3496
case ref : TermRef =>
3483
3497
extMethodApply(untpd.ref(ref).withSpan(tree.span), tree, mbrType)
3484
3498
case _ => EmptyTree
0 commit comments