@@ -135,12 +135,16 @@ class Typer extends Namer
135135 * @param name the name of the identifier
136136 * @param pt the expected type
137137 * @param required flags the result's symbol must have
138+ * @param excluded flags the result's symbol must not have
138139 * @param pos indicates position to use for error reporting
139140 */
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 = {
141142 val refctx = ctx
142143 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)
144148
145149 /** A symbol qualifies if it really exists and is not a package class.
146150 * In addition, if we are in a constructor of a pattern, we ignore all definitions
@@ -204,7 +208,7 @@ class Typer extends Namer
204208 imp.sym.info match
205209 case ImportType (expr) =>
206210 val pre = expr.tpe
207- var denot = pre.memberBasedOnFlags(name, required, EmptyFlags )
211+ var denot = pre.memberBasedOnFlags(name, required, excluded )
208212 .accessibleFrom(pre)(using refctx)
209213 // Pass refctx so that any errors are reported in the context of the
210214 // reference instead of the context of the import scope
@@ -332,7 +336,7 @@ class Typer extends Namer
332336 val scope = if owner.isClass then owner.info.decls else outer.scope
333337 if scope.lookup(name).exists then
334338 val symsMatch = scope.lookupAll(name).exists(denot.containsSym)
335- if ! symsMatch then
339+ if ! symsMatch && ! suppressErrors then
336340 report.errorOrMigrationWarning(
337341 AmbiguousReference (name, Definition , Inheritance , prevCtx)(using outer),
338342 pos)
@@ -344,7 +348,7 @@ class Typer extends Namer
344348 checkNoOuterDefs(denot, outer, prevCtx)
345349
346350 if isNewDefScope then
347- val defDenot = ctx.denotNamed(name, required)
351+ val defDenot = ctx.denotNamed(name, required, excluded )
348352 if (qualifies(defDenot)) {
349353 val found =
350354 if (isSelfDenot(defDenot)) curOwner.enclosingClass.thisType
@@ -462,7 +466,7 @@ class Typer extends Namer
462466 unimported = Set .empty
463467 foundUnderScala2 = NoType
464468 try
465- val found = findRef(name, pt, EmptyFlags , tree.srcPos)
469+ val found = findRef(name, pt, EmptyFlags , EmptyFlags , tree.srcPos)
466470 if foundUnderScala2.exists && ! (foundUnderScala2 =:= found) then
467471 report.migrationWarning(
468472 ex """ Name resolution will change.
@@ -474,7 +478,17 @@ class Typer extends Namer
474478 unimported = saved1
475479 foundUnderScala2 = saved2
476480
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+
477490 def setType (ownType : Type ): Tree =
491+ checkNotShadowed(ownType)
478492 val tree1 = ownType match
479493 case ownType : NamedType if ! prefixIsElidable(ownType) =>
480494 ref(ownType).withSpan(tree.span)
@@ -3484,10 +3498,10 @@ class Typer extends Namer
34843498 case selProto @ SelectionProto (selName : TermName , mbrType, _, _) =>
34853499 def tryExtension (using Context ): Tree =
34863500 try
3487- findRef(selName, WildcardType , ExtensionMethod , tree.srcPos) match
3501+ findRef(selName, WildcardType , ExtensionMethod , EmptyFlags , tree.srcPos) match
34883502 case ref : TermRef =>
34893503 extMethodApply(untpd.ref(ref).withSpan(tree.span), tree, mbrType)
3490- case _ => findRef(selProto.extensionName, WildcardType , ExtensionMethod , tree.srcPos) match
3504+ case _ => findRef(selProto.extensionName, WildcardType , ExtensionMethod , EmptyFlags , tree.srcPos) match
34913505 case ref : TermRef =>
34923506 extMethodApply(untpd.ref(ref).withSpan(tree.span), tree, mbrType)
34933507 case _ => EmptyTree
0 commit comments