-
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
Replacing obsolete type with var shouldn't suppress deprecation warning. #40004
Comments
I'll give this a go |
same problem in VB Imports System
<Obsolete>
Class A
End Class
Class B
#Disable Warning
Public ReadOnly Property A As A = New A()
#Enable Warning
End Class
Class C
Public Sub Test()
Dim b = New B()
Dim a1 = b.A
Dim a2 As A = b.A
End Sub
End Class I'm interested in whether this actually a bug in VB. If it is then the location of the warning should be moved to the DIM. |
I'm interested in getting your assessment as to whether this is even a bug, and if so what the bug actually is. Consider the following three code examples: A a = b.A; // should there be a warning on the first A
return a.Prop;
...
var a = b.A; // should there be a warning on var
return a.Prop;
...
return b.A.Prop; // should there be a warning on b.A? It feels like they should be consistent. Making a temporary local shouldn't introduce a warning, and changing from var to a named type shouldn't introduce a warning. I feel like the most consistent approach is not to have a warning here at all. cc @jnm2 |
I think a good rule is: There should be an error on usage of obsolete types in signatures. |
We have a similar difference between using System;
[Obsolete]
class A {
}
class B {
#pragma warning disable CS0612
public static void M(A a){}
#pragma warning restore CS0612
}
class C {
public void Test() {
B.M(default);
B.M(default(A));
}
} I'm not sure this is a bug, so leaving this for now till a decision is made. |
It's not about whether A is statically inferable; it's about the direct cause that is introducing A. With With Reflecting the causal structure of the code seems super important to me. Accountability should match control. This is the same kind of thing that bothered me so much about #38638 (comment) (now fixed). Therefore, every example I've seen from the C# compiler so far seems right to me. |
Also, consider something similar to the using System;
class C<TFoo> where TFoo : A // CS0612 here
{
public C(Func<TFoo> getFoo)
{
// No CS0612 here
TFoo foo = getFoo();
// Or here
var foo2 = getFoo();
}
}
[Obsolete]
class A { } |
I think the rule is that naming an obsolete type in source triggers the diagnostic. Inferring it does not. In other words, I suspect we should keep the current behavior. |
Version Used: 16.4.0 Preview 5
Steps to Reproduce:
Expected Behavior:
Both
var
to the left ofa1
andA
to the left ofa2
produce warning:'A' is obsolete.
Actual Behavior:
var
to the left ofa1
doesn't produce the warning.A
to the left ofa2
produces the warning.The text was updated successfully, but these errors were encountered: