Skip to content

Commit a24bf8c

Browse files
Constraint checking (#20)
1 parent a58cc3c commit a24bf8c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/coreclr/vm/typedesc.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,30 @@ BOOL TypeVarTypeDesc::SatisfiesConstraints(SigTypeContext *pTypeContextOfConstra
19031903
// if a concrete type can be cast to the constraint, then this constraint will be satisifed
19041904
if (thElem.CanCastTo(thConstraint))
19051905
{
1906+
// Static virtual methods need an extra check when an abstract type is used for instantiation
1907+
// to ensure that the implementation of the constraint is complete
1908+
if (!thElem.IsTypeDesc() &&
1909+
thElem.AsMethodTable()->IsAbstract() &&
1910+
thConstraint.IsInterface() &&
1911+
thConstraint.AsMethodTable()->HasVirtualStaticMethods())
1912+
{
1913+
MethodTable *pInterfaceMT = thConstraint.AsMethodTable();
1914+
bool virtualStaticResolutionCheckFailed = false;
1915+
for (MethodTable::MethodIterator it(pInterfaceMT); it.IsValid(); it.Next())
1916+
{
1917+
MethodDesc *pMD = it.GetMethodDesc();
1918+
if (pMD->IsVirtual() &&
1919+
pMD->IsStatic() &&
1920+
!thElem.AsMethodTable()->ResolveVirtualStaticMethod(pInterfaceMT, pMD, /* allowNullResult */ TRUE, /* checkDuplicates */ TRUE))
1921+
{
1922+
virtualStaticResolutionCheckFailed = true;
1923+
break;
1924+
}
1925+
}
1926+
1927+
if (virtualStaticResolutionCheckFailed)
1928+
continue;
1929+
}
19061930
fCanCast = TRUE;
19071931
break;
19081932
}

src/tests/Loader/classloader/StaticVirtualMethods/NegativeTestCases/UnimplementedAbstractMethodOnAbstractClass.il

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ ConstrainedCallOnAbstractClassToAbstractMethodOnSharedGenericDone: nop
131131
.try {
132132
call void TestEntrypoint::TestScenario2Wrapper()
133133
ldstr "ConstraintCheckShouldFail"
134-
ldstr "TypeLoadException"
134+
ldstr "VerificationException"
135135
ldstr "Did not throw exception"
136136
call void [TypeHierarchyCommonCs]Statics::CheckForFailure(string,string,string)
137137
leave.s ConstraintCheckShouldFailDone
138138
} catch [System.Runtime]System.Exception {
139139
stloc.0
140140
ldstr "ConstraintCheckShouldFail"
141-
ldstr "TypeLoadException"
141+
ldstr "VerificationException"
142142
ldloc.0
143143
callvirt instance class [System.Runtime]System.Type [System.Runtime]System.Exception::GetType()
144144
callvirt instance string [System.Runtime]System.Reflection.MemberInfo::get_Name()

0 commit comments

Comments
 (0)