Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Track delegate conversions of lambda methods #2863

Closed

Description

@sbomer

For compiler-generated code analysis, we want to analyze lambdas not at the point of declaration, but at the point of invocation. Otherwise, we will miss cases where a lambda is called after some captured state is assigned, creating an analysis hole.

For example, see the following nullable analysis behavior which just analyzes lambdas at the point of declaration. There is a hole because the lambda will cause a NRE when called, but nullable analysis doesn't warn. The local function doesn't have this problem:
https://sharplab.io/#v2:EYLgtghglgdgNAFxFANnAJiA1AHwAIBMAjALABQAxDAK4ooTAoCmABEzA8+XgMwuEsAwiwDe5FhP588AFhYBZABQBKUeMkbBAfhYBjFgF4WMJgHchKgNzqNEvEQIsUhlisMA+NWVs/+RAJyKugB0ACoA9gDKCABOsADmKsrW3r4Avim+LDa2siwAMuG6ECh4AKwIUOEwbgae9oEhEdFxMInKyTkaXZL6RjR0mb4oVj0ShcWlFVU1nakSaeSLZEA=

Activity

  1. sbomer commented on Jun 24, 2022

    @sbomer
    MemberAuthor

    Writing down some thoughts from today's discussion: I don't think we should try to track delegate conversions. If a delegate escapes the method, we would need to assume that the captured state could have any possible state encountered from the point where it escapes.
    For example:

    using DAM = System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute;
    using DAMT = System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes;
    using System;
    public class C {
        public void M() {
            Type t = GetAll();
            var l = () => RequireAll(t);
            Escape(l);
            t = GetUnknown(); // What if the escaping delegate gets invoked after this?
        }
        
        public void Escape(Action action) {}
        
        static Type GetUnknown() => throw null;
    
        [return: DAM(DAMT.All)]
        static Type GetAll() => throw null;
        
        void RequireAll([DAM(DAMT.All)] Type t) {}
    }

    Rather than tracking the points where delegates escape, I think we should just conservatively assume that all delegates escape, and analyze them with the "worst-case" state (all states encountered in the closure of methods). We could technically do better by identifying escaping delegates, and analyzing them with the context encountered after the escape. But I think the conservative approach is good enough and avoids having to track delegates. We can just assume that any lambda or local function that gets converted to a delegate escapes.

    This does unfortunately mean that we won't support precise analysis of lambdas or local functions if they are called within the method with different captured states. We could do better for local functions which don't get converted to delegates, but the proposal for now is to treat them the same as lambdas - always analyze them with the intersection of states encountered during the method closure.

    Also: credit goes to @vitek-karas for the idea to use the conservative set of possible captured states when analyzing lambdas!

  2. sbomer commented on Jul 14, 2022

    @sbomer
    MemberAuthor

    Closing this since we decided not to track delegate values. Instead we assume that all delegate conversions escape and conservatively analyze the captured locals with all states encountered in the closure of methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions