-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adjust ContainingSymbol
for substituted local function
#75636
Conversation
938f2ed
to
6a27f97
Compare
6a27f97
to
39b4a97
Compare
@@ -190,7 +190,9 @@ public sealed override Symbol ContainingSymbol | |||
{ | |||
get | |||
{ | |||
return _containingType; | |||
return MethodKind is MethodKind.LocalFunction | |||
? OriginalDefinition.ContainingSymbol |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be valid only when we are dealing with local function symbol with container that is not substituted itself. At the moment this assumption is probably true because we never create such a symbol for a local function, not that it is technically impossible. At the same time we have an invariant that a constructed generic symbol must maintain ContainingSymbol === ConstructedFrom.ContainingSymbol
. It is not obvious that the conditional logic around local function is going to maintain the invariant under all possible scenarios now and in the fututre. Consider if it would be more intuitive and more maintainable in the long run to do the following instead:
- Override
ContainingSymbol
inConstructedMethodSymbol
as follows:
public override Symbol ContainingSymbol
{
get
{
return ConstructedFrom.ContainingSymbol;
}
}
- Add the following assert to
protected SubstitutedMethodSymbol(NamedTypeSymbol containingSymbol, TypeMap map, MethodSymbol originalDefinition, MethodSymbol constructedFrom)
:
Debug.Assert(this is ConstructedMethodSymbol || originalDefinition.MethodKind is not (MethodKind.LocalFunction or MethodKind.Lambda));
``` #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is another even more general alternative approach:
- Replace
NamedTypeSymbol _containingType
withSymbol _containingSymbol
- Change signature of the second constructor to
protected SubstitutedMethodSymbol(Symbol containingSymbol, TypeMap map, MethodSymbol originalDefinition, MethodSymbol constructedFrom)
- Adjust implementation as follows:
public sealed override Symbol ContainingSymbol
{
get
{
return _containingSymbol;
}
}
public override NamedTypeSymbol ContainingType
{
get
{
return _containingSymbol is NamedTypeSymbol nt ? nt : containingSymbol.ContainingType;
}
}
- Adjust constructor of
ConstructedMethodSymbol
to do the following:
base(containingSymbol: constructedFrom.ContainingSymbol, ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks much for the alternative proposals
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (commit 2)
Fixes #75543