Skip to content

Commit 7cfdda5

Browse files
committed
Preserve select of exported member in newExpr
1 parent e53d6fc commit 7cfdda5

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ object NamerOps:
223223
companion
224224

225225
def typeConstructorCompanion(tsym: Symbol, prefix: Type, proxy: Symbol)(using Context): TermSymbol =
226-
newSymbol(tsym.owner, tsym.name.toTermName,
227-
ConstructorCompanionFlags | StableRealizable | Method, ExprType(prefix.select(proxy)), coord = tsym.coord)
226+
inline def core = ConstructorCompanionFlags | StableRealizable | Method
227+
inline def flags = if tsym.is(Exported) then core | Exported else core
228+
newSymbol(tsym.owner, tsym.name.toTermName, flags, ExprType(prefix.select(proxy)), coord = tsym.coord)
228229

229230
/** Add all necessary constructor proxy symbols for members of class `cls`. This means:
230231
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,8 +1262,8 @@ class Namer { typer: Typer =>
12621262
n += 1
12631263

12641264
/** Add a forwarder with name `alias` or its type name equivalent to `mbr`,
1265-
* provided `mbr` is accessible and of the right implicit/non-implicit kind.
1266-
*/
1265+
* provided `mbr` is accessible and of the right implicit/non-implicit kind.
1266+
*/
12671267
def addForwarder(alias: TermName, mbr: SingleDenotation, span: Span): Unit =
12681268

12691269
def adaptForwarderParams(acc: List[List[tpd.Tree]], tp: Type, prefss: List[List[tpd.Tree]])

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,10 +1254,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
12541254
typed(cpy.Block(tree)(clsDef :: Nil, New(Ident(anon), Nil)), pt)
12551255
case _ =>
12561256
var tpt1 = typedType(tree.tpt)
1257-
val tsym = tpt1.tpe.underlyingClassRef(refinementOK = false).typeSymbol
12581257
if ctx.mode.isQuotedPattern && tpt1.tpe.typeSymbol.isAllOf(Synthetic | Case) then
12591258
val errorTp = errorType(CannotInstantiateQuotedTypeVar(tpt1.tpe.typeSymbol), tpt1.srcPos)
12601259
return cpy.New(tree)(tpt1).withType(errorTp)
1260+
val tsym = tpt1.tpe.underlyingClassRef(refinementOK = false).typeSymbol
12611261
if tsym.is(Package) then
12621262
report.error(em"$tsym cannot be instantiated", tpt1.srcPos)
12631263
tpt1 = tpt1.withType(ensureAccessible(tpt1.tpe, superAccess = false, tpt1.srcPos))
@@ -1267,7 +1267,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
12671267
report.error(WildcardOnTypeArgumentNotAllowedOnNew(), targ.srcPos)
12681268
case _ =>
12691269
}
1270-
12711270
assignType(cpy.New(tree)(tpt1), tpt1)
12721271
}
12731272

@@ -5009,7 +5008,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
50095008
case _ =>
50105009
}
50115010

5012-
/** If `tree` is a constructor proxy reference, convert it to a `new` expression,
5011+
/** If `tree` is a constructor proxy reference, convert it to a `new` expression;
5012+
* check if it is a reference to an exported type/companion pair;
50135013
* otherwise return EmptyTree.
50145014
*/
50155015
def newExpr(tree: Tree): Tree =
@@ -5025,13 +5025,19 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
50255025
cpy.Ident(qual)(qual.symbol.name.sourceModuleName.toTypeName)
50265026
case _ =>
50275027
errorTree(tree, em"cannot convert from $tree to an instance creation expression")
5028-
val tycon = ctorResultType.underlyingClassRef(refinementOK = Feature.enabled(modularity))
5028+
val tycon =
5029+
val exported =
5030+
if qual.symbol.isAllOf(SyntheticMethod | Exported) then
5031+
qual.symbol.owner.info.memberBasedOnFlags(qual.symbol.name.toTypeName, required = Exported)
5032+
else NoDenotation
5033+
if exported.exists then exported.symbol.typeRef
5034+
else ctorResultType.underlyingClassRef(refinementOK = Feature.enabled(modularity))
50295035
typed(
50305036
untpd.Select(
50315037
untpd.New(untpd.TypedSplice(tpt.withType(tycon))),
50325038
nme.CONSTRUCTOR),
5033-
pt)
5034-
.showing(i"convert creator $tree -> $result", typr)
5039+
pt
5040+
).showing(i"convert creator $tree -> $result", typr)
50355041

50365042
/** If `tree` is a constructor proxy reference, return the type it constructs,
50375043
* otherwise return NoType.
@@ -5050,7 +5056,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
50505056
// begin adapt1
50515057
tree match {
50525058
case _: MemberDef | _: PackageDef | _: Import | _: WithoutTypeOrPos[?] | _: Closure => tree
5053-
case _ => tree.tpe.widen match {
5059+
case _ =>
5060+
tree.tpe.widen match
50545061
case tp: FlexType =>
50555062
ensureReported(tp)
50565063
tree
@@ -5112,7 +5119,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
51125119
if (ctx.mode is Mode.Type) adaptType(tree.tpe)
51135120
else adaptNoArgs(wtp)
51145121
}
5115-
}
51165122
}
51175123
}
51185124

tests/warn/i24562.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//> using options -Wunused:imports
2+
3+
package util:
4+
class Random(s: String):
5+
def this() = this("")
6+
7+
package bar:
8+
export util.Random
9+
10+
package foo:
11+
import bar.Random
12+
def test = Random(null)
13+
14+
package baz:
15+
import bar.*
16+
def test = Random(null)
17+
18+
// cf tests/pos/i12299a.scala
19+
package aliased:
20+
object Outer:
21+
22+
object Wrap:
23+
export Outer.Bar
24+
25+
class Bar
26+
27+
def test =
28+
import Wrap.* // warn! not a naming error because a benign alias
29+
val localBar = Bar() // not Wrap.Bar!
30+
val localNewBar = new Bar

0 commit comments

Comments
 (0)