@@ -2614,7 +2614,7 @@ protected internal override TranslatedExpression VisitBlockContainer(BlockContai
26142614 }
26152615
26162616 internal TranslatedExpression TranslateTarget ( ILInstruction target , bool nonVirtualInvocation ,
2617- bool memberStatic , IType memberDeclaringType )
2617+ bool memberStatic , IType memberDeclaringType , IType constrainedTo = null )
26182618 {
26192619 // If references are missing member.IsStatic might not be set correctly.
26202620 // Additionally check target for null, in order to avoid a crash.
@@ -2630,8 +2630,8 @@ internal TranslatedExpression TranslateTarget(ILInstruction target, bool nonVirt
26302630 }
26312631 else
26322632 {
2633- IType targetTypeHint = memberDeclaringType ;
2634- if ( CallInstruction . ExpectedTypeForThisPointer ( memberDeclaringType ) == StackType . Ref )
2633+ IType targetTypeHint = constrainedTo ?? memberDeclaringType ;
2634+ if ( CallInstruction . ExpectedTypeForThisPointer ( memberDeclaringType , constrainedTo ) == StackType . Ref )
26352635 {
26362636 if ( target . ResultType == StackType . Ref )
26372637 {
@@ -2643,13 +2643,13 @@ internal TranslatedExpression TranslateTarget(ILInstruction target, bool nonVirt
26432643 }
26442644 }
26452645 var translatedTarget = Translate ( target , targetTypeHint ) ;
2646- if ( CallInstruction . ExpectedTypeForThisPointer ( memberDeclaringType ) == StackType . Ref )
2646+ if ( CallInstruction . ExpectedTypeForThisPointer ( memberDeclaringType , constrainedTo ) == StackType . Ref )
26472647 {
26482648 // When accessing members on value types, ensure we use a reference of the correct type,
26492649 // and not a pointer or a reference to a different type (issue #1333)
2650- if ( ! ( translatedTarget . Type is ByReferenceType brt && NormalizeTypeVisitor . TypeErasure . EquivalentTypes ( brt . ElementType , memberDeclaringType ) ) )
2650+ if ( ! ( translatedTarget . Type is ByReferenceType brt && NormalizeTypeVisitor . TypeErasure . EquivalentTypes ( brt . ElementType , constrainedTo ?? memberDeclaringType ) ) )
26512651 {
2652- translatedTarget = translatedTarget . ConvertTo ( new ByReferenceType ( memberDeclaringType ) , this ) ;
2652+ translatedTarget = translatedTarget . ConvertTo ( new ByReferenceType ( constrainedTo ?? memberDeclaringType ) , this ) ;
26532653 }
26542654 }
26552655 if ( translatedTarget . Expression is DirectionExpression )
@@ -2675,9 +2675,9 @@ internal TranslatedExpression TranslateTarget(ILInstruction target, bool nonVirt
26752675 }
26762676 else
26772677 {
2678- return new TypeReferenceExpression ( ConvertType ( memberDeclaringType ) )
2678+ return new TypeReferenceExpression ( ConvertType ( constrainedTo ?? memberDeclaringType ) )
26792679 . WithoutILInstruction ( )
2680- . WithRR ( new TypeResolveResult ( memberDeclaringType ) ) ;
2680+ . WithRR ( new TypeResolveResult ( constrainedTo ?? memberDeclaringType ) ) ;
26812681 }
26822682
26832683 bool ShouldUseBaseReference ( )
@@ -2686,7 +2686,7 @@ bool ShouldUseBaseReference()
26862686 return false ;
26872687 if ( ! MatchLdThis ( target ) )
26882688 return false ;
2689- if ( memberDeclaringType . GetDefinition ( ) == resolver . CurrentTypeDefinition )
2689+ if ( ( constrainedTo ?? memberDeclaringType ) . GetDefinition ( ) == resolver . CurrentTypeDefinition )
26902690 return false ;
26912691 return true ;
26922692 }
0 commit comments