Skip to content

Handling of byref return values and locals in data flow analysis #2158

@vitek-karas

Description

@vitek-karas

This issue is now tracking all the various cases of ref locals and return values for both the linker an analyzer.
It's referred to from some tests, so just search the codebase for the issue number to find some of the known cases where it's problematic (definitely not all).

			[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
			static Type _annotatedField;

			static ref Type GetAnnotatedRefType () { return ref _annotatedField; } // No warning currently

			static void ByRefReturnValue ()
			{
				ref Type typeShouldHaveAllMethods = GetAnnotatedRefType();
				typeShouldHaveAllMethods = typeof(TestType); // Doesn't apply annotations
				_annotatedField.GetMethods (); // Doesn't warn, but now contains typeof(TestType)
			}

We will need to think this through, how the system should work around byref values.
In a way handling of byref values needs to effectively work "in reverse". Meaning that we need to enforce when assigning to a byref value that the byref value has at least the same annotations as the source value. But then assigning a known type needs to apply annotations from the byref to the type... so it's like writing to an annotated field.

There's also a reflection based test which also needs to correctly warn:

			[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
			static Type _annotatedField;

			[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
			static ref Type GetAnnotatedRefType () { return ref _annotatedField; }

			delegate ref Type DelegateOverGetRefType ();
			static void ByRefReturnValue ()
			{
				var d = (DelegateOverGetRefType)typeof (AnnotatedMethodReturnValue)
					.GetMethod (nameof (GetAnnotatedRefType))
					.CreateDelegate (typeof (DelegateOverGetRefType));

				ref Type typeShouldHaveAllMethods = ref d ();
				typeShouldHaveAllMethods = typeof (TestType); // Doesn't apply annotations
				_annotatedField.GetMethods (); // Doesn't warn, but now contains typeof(TestType)
			}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions