Skip to content

Avoid dereferencing null CheckConstraintsArgs.CurrentCompilation #78729

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AlekseyTs
Copy link
Contributor

Fixes #78430.

@AlekseyTs AlekseyTs requested a review from a team as a code owner May 28, 2025 15:56
var testStruct = comp.GetTypeByMetadataName("TestStruct");
var extensionMethodSymbol = comp.GetMember<MethodSymbol>("TestClass.TestExtensionMethod");

AssertEx.Equal("void TestStruct.TestExtensionMethod<TestStruct>()", extensionMethodSymbol.ReduceExtensionMethod(testStruct, null).ToTestDisplayString());
Copy link
Member

@jcouv jcouv May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on MethodSymbol.ReduceExtensionMethod may need to be updated. It says:

        /// <summary>
        /// If this is an extension method that can be applied to a receiver of the given type,
        /// returns a reduced extension method symbol thus formed. Otherwise, returns null.
        /// </summary>
        /// <param name="compilation">The compilation in which constraints should be checked.
        /// Should not be null, but if it is null we treat constraints as we would in the latest
        /// language version.</param>
        public MethodSymbol ReduceExtensionMethod(TypeSymbol receiverType, CSharpCompilation compilation, out bool wasFullyInferred)

But in fact, we're going to skip some constraint checks when the compilation is null.
That means we'll return symbols that violate some constraints. That may be worth adding a test to observe. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in fact, we're going to skip some constraint checks when the compilation is null.

What constraint checks are getting skipped?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're skipping a check in CheckConstraintsSingleType

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're skipping a check in CheckConstraintsSingleType

It doesn't look like Binder.CheckManagedAddr actually checks any constraints. This helper used in many other settings, I guess it was convenient to include it here as well, as we do with some other checks that aren't primarily related to constraint checking. Besides, it doesn't look like CheckConstraintsSingleType is reachable from ReduceExtensionMethod

@jcouv jcouv self-assigned this May 28, 2025
Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks (commit 1)

@AlekseyTs
Copy link
Contributor Author

@dotnet/roslyn-compiler For another review

2 similar comments
@AlekseyTs
Copy link
Contributor Author

@dotnet/roslyn-compiler For another review

@AlekseyTs
Copy link
Contributor Author

@dotnet/roslyn-compiler For another review

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

Successfully merging this pull request may close these issues.

ReduceExtensionMethod throw NullReferenceException when specifying ref struct symbol as receiverType
3 participants