Skip to content

Commit

Permalink
compiler: enhance Normalize.insertSuperCtor to consider inherited cto…
Browse files Browse the repository at this point in the history
…r visibility

Previously when selecting implicit chained super ctor, we short circuited if
we found more than one constructor in superclass.  With this change we now
filter out those constructors that aren't visible.  For example if a superclass
in another pod has both a public make and an internal makeFoo, we will now
implicitly bind the ctor chain to the public make instead of raising an error
in CheckErrors.
  • Loading branch information
briansfrank committed Feb 18, 2024
1 parent 2cd3c18 commit 67365c5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/compiler/fan/namespace/CSlot.fan
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ mixin CSlot
**
abstract CBridge? usesBridge()

**
** Return if this slot is visible to the given type
**
Bool isVisibleTo(CType curType)
{
if (parent == curType) return true
if (isPrivate) return false
if (isInternal) return parent.pod == curType.pod
if (isProtected) return parent.pod == curType.pod || curType.fits(parent)
return true
}

}

**************************************************************************
Expand Down
7 changes: 6 additions & 1 deletion src/compiler/fan/steps/Normalize.fan
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,14 @@ class Normalize : CompilerStep
if (parent.isMixin) return
if (base.isObj) return

// check if the base class has exactly one available
// check if the base class has exactly one available, visible
// constructor with no parameters
superCtors := base.instanceCtors
if (superCtors.size > 1)
{
// if there are more than one, then only find ctors visible to me
superCtors = superCtors.findAll |ctor| { ctor.isVisibleTo(curType) }
}
if (superCtors.size != 1) return
superCtor := superCtors.first
if (superCtor.isPrivate) return
Expand Down
24 changes: 24 additions & 0 deletions src/testCompiler/fan/MiscTest.fan
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ class MiscTest : CompilerTest
])
}


//////////////////////////////////////////////////////////////////////////
// Normalize Ctor Visibility
//////////////////////////////////////////////////////////////////////////

Void testCtorVisibility()
{
compile(
"class A
{
Int x
new make() { x = 10 }
private new makeAlt() { x = 20 }
}
class B : A {}
")

t := pod.types[1]
b := t.make
verifyEq(t.name, "B")
verifyEq(b->x, 10)
}

//////////////////////////////////////////////////////////////////////////
// Normalize
//////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 67365c5

Please sign in to comment.