You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
Track delegate conversions of lambda methods #2863
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.
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:
usingDAM=System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute;usingDAMT=System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes;usingSystem;publicclassC{publicvoidM(){Typet=GetAll();varl=()=>RequireAll(t);Escape(l);t=GetUnknown();// What if the escaping delegate gets invoked after this?}publicvoidEscape(Actionaction){}staticTypeGetUnknown()=>thrownull;[return:DAM(DAMT.All)]staticTypeGetAll()=>thrownull;voidRequireAll([DAM(DAMT.All)]Typet){}}
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!
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.
Activity
sbomer commentedon Jun 24, 2022
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:
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!
sbomer commentedon Jul 14, 2022
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.