Skip to content

Preserve type bounds for inlined definitions in posttyper #17190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Mode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,7 @@ object Mode {
* Type `Null` becomes a subtype of non-primitive value types in TypeComparer.
*/
val RelaxedOverriding: Mode = newMode(30, "RelaxedOverriding")

/** We are checking the original call of an Inlined node */
val InlinedCall: Mode = newMode(31, "InlinedCall")
}
14 changes: 8 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,14 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
if tree.isType then
checkNotPackage(tree)
else
if tree.symbol.is(Inline) && !Inlines.inInlineMethod then
ctx.compilationUnit.needsInlining = true
checkNoConstructorProxy(tree)
registerNeedsInlining(tree)
tree.tpe match {
case tpe: ThisType => This(tpe.cls).withSpan(tree.span)
case _ => tree
}
case tree @ Select(qual, name) =>
if tree.symbol.is(Inline) then
ctx.compilationUnit.needsInlining = true
registerNeedsInlining(tree)
if name.isTypeName then
Checking.checkRealizable(qual.tpe, qual.srcPos)
withMode(Mode.Type)(super.transform(checkNotPackage(tree)))
Expand Down Expand Up @@ -344,8 +342,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
case tree: TypeApply =>
if tree.symbol.isQuote then
ctx.compilationUnit.needsStaging = true
if tree.symbol.is(Inline) then
ctx.compilationUnit.needsInlining = true
registerNeedsInlining(tree)
val tree1 @ TypeApply(fn, args) = normalizeTypeArgs(tree)
for arg <- args do
checkInferredWellFormed(arg)
Expand All @@ -363,6 +360,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
case Inlined(call, bindings, expansion) if !call.isEmpty =>
val pos = call.sourcePos
CrossVersionChecks.checkExperimentalRef(call.symbol, pos)
withMode(Mode.InlinedCall)(transform(call))
val callTrace = Inlines.inlineCallTrace(call.symbol, pos)(using ctx.withSource(pos.source))
cpy.Inlined(tree)(callTrace, transformSub(bindings), transform(expansion)(using inlineContext(call)))
case templ: Template =>
Expand Down Expand Up @@ -504,6 +502,10 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
private def normalizeErasedRhs(rhs: Tree, sym: Symbol)(using Context) =
if (sym.isEffectivelyErased) dropInlines.transform(rhs) else rhs

private def registerNeedsInlining(tree: Tree)(using Context): Unit =
if tree.symbol.is(Inline) && !Inlines.inInlineMethod && !ctx.mode.is(Mode.InlinedCall) then
ctx.compilationUnit.needsInlining = true

/** Check if the definition has macro annotation and sets `compilationUnit.hasMacroAnnotations` if needed. */
private def registerIfHasMacroAnnotations(tree: DefTree)(using Context) =
if !Inlines.inInlineMethod && MacroAnnotations.hasMacroAnnotation(tree.symbol) then
Expand Down
3 changes: 3 additions & 0 deletions tests/neg/i17168.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type F[X <: String] = X

val a = summon[F[Int] =:= Int] // error
14 changes: 0 additions & 14 deletions tests/pos/i13332super.scala

This file was deleted.

6 changes: 3 additions & 3 deletions tests/run/anon-mirror-gen-local.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Outer5 { self =>
}
}

lazy val o = new Outer5() // infinite init
final lazy val o = new Outer5() // infinite init

}

Expand Down Expand Up @@ -142,7 +142,7 @@ def locally3 = {
class Bar extends Foo {

def hello =
val mQux = summon[Mirror.Of[Bar.super.foo.type]]
val mQux = summon[Mirror.Of[foo.type]]
assert(mQux.fromProduct(EmptyTuple) == Qux)
}

Expand All @@ -157,4 +157,4 @@ def locally3 = {
testOuter6
locally1
locally2
locally3
locally3
2 changes: 1 addition & 1 deletion tests/run/i13332a.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class SubUniverse extends Universe {

trait Whole {
trait MixinA {
lazy val mixinB = new MixinB() {}
final lazy val mixinB = new MixinB() {}
}
trait MixinB {
object A extends MixinB { // by inheriting `MixinB`, we should not check for inheritance from the right
Expand Down