Skip to content
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

Fix wrong symbol comparisons #65430

Merged
merged 4 commits into from
Dec 12, 2022
Merged

Fix wrong symbol comparisons #65430

merged 4 commits into from
Dec 12, 2022

Conversation

Youssef1313
Copy link
Member

Fixes #62368
Fixes #58966
Fixes #65428

cc @333fred @jcouv @AlekseyTs

Keeping draft until the behavior of ConsiderEverything (and the fact that we create ConstructedNamedTypeSymbol under #nullable enable, and SourceNamedTypeSymbol otherwise) is confirmed as a correct behavior. Otherwise, the fix will be much more involving.

@ghost ghost added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Nov 15, 2022
@@ -1090,7 +1090,7 @@ private static bool IsInaccessibleBecauseOfConstruction(NamedTypeSymbol type, Na
NamedTypeSymbol containingType;
if (containingTypes.TryGetValue(contextBaseType.OriginalDefinition, out containingType))
{
return !TypeSymbol.Equals(containingType, contextBaseType, TypeCompareKind.ConsiderEverything2);
return !TypeSymbol.Equals(containingType, contextBaseType, TypeCompareKind.AllNullableIgnoreOptions);
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 15, 2022

Choose a reason for hiding this comment

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

AllNullableIgnoreOptions

Can we use AllIgnoreOptions here? What are we trying to detect? #Closed

Copy link
Contributor

@AlekseyTs AlekseyTs Nov 17, 2022

Choose a reason for hiding this comment

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

It is not obvious to me what we are trying to detect here. And we need to understand that before picking the TypeCompareKind. For example, should dynamic vs. object make a difference here? If you have an understanding of our intent here, please share it. Otherwise, we need to get this understanding.

Copy link
Member Author

Choose a reason for hiding this comment

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

@AlekseyTs I don't have too much info here. I thought that we might not be caring about object vs dynamic since they are both System.Object in the resulting IL. I think CLS compliance is more about "is this assembly (resulting IL) compliant with some rules?" though I'm not sure if there might be something in these rules that might be concerned with the extra "DynamicAttribute" produced.

Revisiting the behavior when nullable is disabled:

#nullable disable

[assembly: System.CLSCompliant(true)]

public class Base<T>
{
    protected class Nested { }
}

public class C<T> : Base<object>
{
    protected void M(Base<dynamic>.Nested c) // CLS compliance warning
    {
    }
}

I haven't confirmed yet, but I think AllIgnoreOptions might affect this scenario and remove the warning.

I guess we could use AllNullableIgnoreOptions. I know that this won't be revisited since it's no longer ConsiderEverything2, but I also think ConsiderEverything2 was introduced specifically when doing nullability work. So AllNullableIgnoreOptions should just be good.

What do you think @AlekseyTs ?

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think ?

  1. I would start with understanding the rule that we are trying to implement. What does it say exactly? Let's find this out.
  2. I would try to figure out whether tuple names and dynamic differences matter for the purpose of the rule. It doesn't look like other options should matter, or corresponding differences could be detected by native compilers. But I could be wrong.
  3. I would explicitly include options that we care about rather than using AllNullableIgnoreOptions.
  4. The legacy native compiler supported dynamic. It would be good to check its behavior around dynamic vs. object difference.

Copy link
Member Author

@Youssef1313 Youssef1313 Nov 18, 2022

Choose a reason for hiding this comment

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

  • I would start with understanding the rule that we are trying to implement. What does it say exactly? Let's find this out.

@AlekseyTs Where can I find the whole rules spec'ed precisely? The rule intended to be implemented here per the doc comment is "Rule 46":

/// This check (the only one that uses the "context" parameter is based on CLS Rule 46,

But I don't know where can I see what the "rule 46" says exactly. The brief about the rule in the comment is that a constructed generic type C<type1> can't access protected members of a constructed generic type C<type2>. I think C<object> and C<dynamic> are both the exact same type from the point of view of another language consuming this assembly (they are both C<System.Object>). It doesn't seem like the attribute on the type parameter will matter for the ability to access members in other languages.


The legacy native compiler supported dynamic. It would be good to check its behavior around dynamic vs. object difference.

I'll see if I can download VS 2013 and test that. Is there an easier way to get the native compiler other than using VS 2013?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. I am comfortable going with AllIgnoreOptions here. Assuming legacy compiler didn't care about object/dynamic difference. Let's confirm that.

Copy link
Contributor

Choose a reason for hiding this comment

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

@MadsTorgersen, @dotnet/roslyn-compiler

I am comfortable going with AllIgnoreOptions here. Assuming legacy compiler didn't care about object/dynamic difference. Let's confirm that

It looks like legacy compiler cares about object/dynamic difference.

[assembly: System.CLSCompliant(true)]

public class C<T>
{
    protected class N { }
}

public class D : C<object>
{
    protected void M4(C<dynamic>.N n) { }
}

Observed:

(10,23): warning CS3001: Argument type 'C<dynamic>.N' is not CLS-compliant

Should we preserve this behavior?

Copy link
Member

Choose a reason for hiding this comment

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

CLS compliance is not considered by any modern framework, and is broken in a number of ways today. Preserving that warning does not concern me.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Youssef1313 We discussed this issue internally and reached consensus that it would be fine dropping this warning. Let's add test for this scenario, add test suggested at #65430 (comment) and move this PR forward.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry for missing this for long. I added the tests.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 1)

@jcouv jcouv marked this pull request as ready for review November 16, 2022 00:45
@jcouv jcouv requested a review from a team as a code owner November 16, 2022 00:45
var source = @"
internal class Test<T> : ICloneable<Test<T>>
{
public Test<T> Clone() => new();
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 17, 2022

Choose a reason for hiding this comment

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

T

Consider explicitly testing scenario with T? here. #Closed

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 3)

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 4)

@AlekseyTs
Copy link
Contributor

@dotnet/roslyn-compiler For the second review.

@jcouv jcouv self-assigned this Dec 12, 2022
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 (iteration 4)

@jcouv jcouv merged commit 92b7f1c into dotnet:main Dec 12, 2022
@ghost ghost added this to the Next milestone Dec 12, 2022
@jcouv
Copy link
Member

jcouv commented Dec 12, 2022

Thanks @Youssef1313 !

@Youssef1313 Youssef1313 deleted the symbol-comparison branch December 12, 2022 18:14
@Cosifne Cosifne modified the milestones: Next, 17.5 P3 Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Community The pull request was submitted by a contributor who is not a Microsoft employee.
Projects
None yet
5 participants