Skip to content

Commit a0c6c98

Browse files
Use the typical MethodTable not the instantiated one. (#107154)
1 parent 2694613 commit a0c6c98

File tree

2 files changed

+63
-18
lines changed

2 files changed

+63
-18
lines changed

src/coreclr/vm/prestub.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,7 @@ namespace
12941294
Instantiation targetMethodInst;
12951295
if (targetType->HasInstantiation())
12961296
{
1297-
declClassInst = declaration->GetMethodTable()->GetInstantiation();
1297+
declClassInst = declaration->GetMethodTable()->GetTypicalMethodTable()->GetInstantiation();
12981298
targetClassInst = targetType->GetTypicalMethodTable()->GetInstantiation();
12991299
}
13001300
if (targetMethod->HasMethodInstantiation())

src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111

1212
struct Struct { }
1313

14+
interface I1 { }
15+
16+
interface I2<M> { }
17+
18+
class ClassWithI1 : I1 { }
19+
20+
class ClassWithI1I2 : I1, I2<ClassWithI1I2> { }
21+
1422
public static unsafe class UnsafeAccessorsTestsGenerics
1523
{
1624
class ClassWithEnum<T>
@@ -396,45 +404,82 @@ public static void Verify_Generic_GenericTypeGenericStaticMethod()
396404
}
397405
}
398406

399-
class ClassWithConstraints
407+
class MethodWithConstraints
400408
{
401-
private string M<T, U>() where T : U, IEquatable<T>
409+
private string M<T, U>() where T : U, I2<T>
402410
=> $"{typeof(T)}|{typeof(U)}";
403411

404-
private static string SM<T, U>() where T : U, IEquatable<T>
412+
private static string SM<T, U>() where T : U, I2<T>
405413
=> $"{typeof(T)}|{typeof(U)}";
406414
}
407415

408416
[Fact]
409417
[ActiveIssue("https://github.com/dotnet/runtime/issues/102942", TestRuntimes.Mono)]
410-
public static void Verify_Generic_ConstraintEnforcement()
418+
public static void Verify_Generic_MethodConstraintEnforcement()
411419
{
412-
Console.WriteLine($"Running {nameof(Verify_Generic_ConstraintEnforcement)}");
420+
Console.WriteLine($"Running {nameof(Verify_Generic_MethodConstraintEnforcement)}");
413421

414-
Assert.Equal($"{typeof(string)}|{typeof(object)}", CallMethod<string, object>(new ClassWithConstraints()));
415-
Assert.Equal($"{typeof(string)}|{typeof(object)}", CallStaticMethod<string, object>(null));
416-
Assert.Throws<InvalidProgramException>(() => CallMethod_NoConstraints<string, object>(new ClassWithConstraints()));
417-
Assert.Throws<InvalidProgramException>(() => CallMethod_MissingConstraint<string, object>(new ClassWithConstraints()));
418-
Assert.Throws<InvalidProgramException>(() => CallStaticMethod_NoConstraints<string, object>(null));
419-
Assert.Throws<InvalidProgramException>(() => CallStaticMethod_MissingConstraint<string, object>(null));
422+
Assert.Equal($"{typeof(ClassWithI1I2)}|{typeof(I1)}", CallMethod<ClassWithI1I2, I1>(new MethodWithConstraints()));
423+
Assert.Equal($"{typeof(ClassWithI1I2)}|{typeof(I1)}", CallStaticMethod<ClassWithI1I2, I1>(null));
424+
Assert.Throws<InvalidProgramException>(() => CallMethod_NoConstraints<ClassWithI1I2, I1>(new MethodWithConstraints()));
425+
Assert.Throws<InvalidProgramException>(() => CallMethod_MissingConstraint<ClassWithI1I2, I1>(new MethodWithConstraints()));
426+
Assert.Throws<InvalidProgramException>(() => CallStaticMethod_NoConstraints<ClassWithI1I2, I1>(null));
427+
Assert.Throws<InvalidProgramException>(() => CallStaticMethod_MissingConstraint<ClassWithI1I2, I1>(null));
420428

421429
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "M")]
422-
extern static string CallMethod<V,W>(ClassWithConstraints c) where V : W, IEquatable<V>;
430+
extern static string CallMethod<V,W>(MethodWithConstraints c) where V : W, I2<V>;
423431

424432
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "M")]
425-
extern static string CallMethod_NoConstraints<V,W>(ClassWithConstraints c);
433+
extern static string CallMethod_NoConstraints<V,W>(MethodWithConstraints c);
426434

427435
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "M")]
428-
extern static string CallMethod_MissingConstraint<V,W>(ClassWithConstraints c) where V : W;
436+
extern static string CallMethod_MissingConstraint<V,W>(MethodWithConstraints c) where V : W;
429437

430438
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "SM")]
431-
extern static string CallStaticMethod<V,W>(ClassWithConstraints c) where V : W, IEquatable<V>;
439+
extern static string CallStaticMethod<V,W>(MethodWithConstraints c) where V : W, I2<V>;
432440

433441
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "SM")]
434-
extern static string CallStaticMethod_NoConstraints<V,W>(ClassWithConstraints c);
442+
extern static string CallStaticMethod_NoConstraints<V,W>(MethodWithConstraints c);
435443

436444
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "SM")]
437-
extern static string CallStaticMethod_MissingConstraint<V,W>(ClassWithConstraints c) where V : W;
445+
extern static string CallStaticMethod_MissingConstraint<V,W>(MethodWithConstraints c) where V : W;
446+
}
447+
448+
class ClassWithConstraints<T, U> where T : U, I2<T>
449+
{
450+
private string M<W>() where W : I1
451+
=> $"{typeof(T)}|{typeof(U)}|{typeof(W)}";
452+
453+
private static string SM<X>() where X : I1
454+
=> $"{typeof(T)}|{typeof(U)}|{typeof(X)}";
455+
}
456+
457+
class AccessorsWithConstraints<A, B> where A : B, I2<A>
458+
{
459+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "M")]
460+
public extern static string CallMethod<C>(ClassWithConstraints<A, B> c) where C: I1;
461+
462+
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "SM")]
463+
public extern static string CallStaticMethod<D>(ClassWithConstraints<A, B> c) where D: I1;
464+
465+
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "M")]
466+
public extern static string CallMethod_MissingMethodConstraint<E>(ClassWithConstraints<A, B> c);
467+
468+
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "SM")]
469+
public extern static string CallStaticMethod_MissingMethodConstraint<F>(ClassWithConstraints<A, B> c);
470+
}
471+
472+
[Fact]
473+
[ActiveIssue("https://github.com/dotnet/runtime/issues/102942", TestRuntimes.Mono)]
474+
public static void Verify_Generic_ClassConstraintEnforcement()
475+
{
476+
Console.WriteLine($"Running {nameof(Verify_Generic_ClassConstraintEnforcement)}");
477+
478+
Assert.Equal($"{typeof(ClassWithI1I2)}|{typeof(I1)}|{typeof(ClassWithI1)}", AccessorsWithConstraints<ClassWithI1I2, I1>.CallMethod<ClassWithI1>(new ClassWithConstraints<ClassWithI1I2, I1>()));
479+
Assert.Equal($"{typeof(ClassWithI1I2)}|{typeof(I1)}|{typeof(ClassWithI1)}", AccessorsWithConstraints<ClassWithI1I2, I1>.CallStaticMethod<ClassWithI1>(null));
480+
481+
Assert.Throws<InvalidProgramException>(() => AccessorsWithConstraints<ClassWithI1I2, I1>.CallMethod_MissingMethodConstraint<ClassWithI1>(new ClassWithConstraints<ClassWithI1I2, I1>()));
482+
Assert.Throws<InvalidProgramException>(() => AccessorsWithConstraints<ClassWithI1I2, I1>.CallStaticMethod_MissingMethodConstraint<ClassWithI1>(null));
438483
}
439484

440485
class Invalid

0 commit comments

Comments
 (0)