-
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
Misleading constant diagnostics #18933
Misleading constant diagnostics #18933
Conversation
It seems there was a memory issue with perf_correctness_prtest. Is it possible to re-run? |
retest perf_correctness_prtest please not sure if only members can use this command, but writing "test perf_correctness_prtest please" or "retest perf_correctness_prtest please" as a comment in the pull request should trigger a new run |
@BattleRush Thank you! It doesn't seem to have been triggered, so I will try the comment on its own. |
retest perf_correctness_prtest please |
retest windows_release_vs-integration_prtest please |
@@ -1232,17 +1232,20 @@ internal enum ErrorCode | |||
ERR_FixedBufferTooManyDimensions = 7092, | |||
ERR_CantReadConfigFile = 7093, | |||
ERR_BadAwaitInCatchFilter = 7094, | |||
WRN_FilterIsConstant = 7095, | |||
// WRN_FilterIsConstant = 7095, // replaced by FilterIsConstantFalse and FilterIsConstantTrue |
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.
💭 It seems like FilterIsConstantTrue
should still use this ID.
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.
Sure, I will keep the ID value 7095, and use the new name FilterIsConstantTrue
.
My thinking was to keep related warnings close together, but I guess keeping the orignal warning ID for the sake of backward compatibility (for warning disable pragma) is more important.
ERR_EncNoPIAReference = 7096, | ||
//ERR_EncNoDynamicOperation = 7097, // dynamic operations are now allowed | ||
ERR_LinkedNetmoduleMetadataMustProvideFullPEImage = 7098, | ||
ERR_MetadataReferencesNotSupported = 7099, | ||
ERR_InvalidAssemblyCulture = 7100, | ||
ERR_EncReferenceToAddedMember = 7101, | ||
ERR_MutuallyExclusiveOptions = 7102, | ||
WRN_FilterIsConstantFalse = 7103, | ||
WRN_FilterIsConstantTrue = 7104, | ||
WRN_FilterIsConstantRedundantTryCatch = 7105, |
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.
💭 I question the value of this specifically.
- Between
FilterIsConstantFalse
andFilterIsConstantRedundantTryCatch
, I don't foresee developers getting confused by the former but suddenly knowing how to proceed with the latter. - Distinguishing the cases introduces additional complexity in the compiler.
- Distinguishing the cases introduces maintainability overhead since upgrade scenarios consider cases where warnings are treated as errors.
It boils down to paying a cost (complexity, maintainability) for a feature that no one will actually need in the end.
💡 If you keep this one, it should probably be renamed to WRN_FilterIsConstantFalseRedundantTryCatch
(added False
).
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.
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.
Hi @gafter , do you have any preference/suggestion?
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.
I'm not sure why we report this warning in the first place. Who is helped by this?
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.
The answer to that question would set the direction of this pull request (may need to close and have a new one to remove the filter altogether?). I doubt this kind of code will be written in a practical application, but still seems like a nice hint if #19294 is to be merged as it skips redundant catch clause emission if unreachable.
Also, I am trying to think if any sort of automatic refactoring done by IDE could potentially write a dummy boolean value in the filter expression. In that case, if the developer forgets to replace the dummy value, it would at least come up as a warning in compilation.
By the way, welcome to the project @elijah6 ! 🎉 Keep in mind I am not the expert in this section of the code (far from it). That's why I marked my comments with 💭 ... they show what I was thinking but it would make sense to wait for someone like @gafter or @jaredpar to provide additional details/comments before taking action based on something I said. |
@elijah6 I know a bunch of the best qualified reviewers for changes like this are busy preparing for Build. Even though it's not my area, I like what you're getting done here. I'm tracking this to try and get follow-up on it in a timely manner. |
: ErrorCode.WRN_FilterIsConstantFalse; | ||
|
||
// Since the expression is a constant, the name can be retrieved from the first token | ||
Error(diagnostics, errorCode, filter.FilterExpression, boundFilter.Syntax.GetFirstToken().Text); |
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.
No, the name true
is not available from the first token of (1 + 1) = 2
.
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.
That is a good point... It might be best to use the constant value to say ...filter is constant 'false'...
and ...filter is constant 'true'...
instead of specifying the name (e.g. if default
is used, then it may be more natural to report ...filter is constant 'false'...
rather than ...filter is constant 'default'...
since the compiler already knows what it evaluates to. Similarly for (1+1)==2
or (1+1)!=2
, it would then report as true
and false
respectively.
No need to pass around the name either in that case.
// we suggest different actions | ||
var errorCode = boundFilter.ConstantValue.BooleanValue | ||
? ErrorCode.WRN_FilterIsConstantTrue | ||
: hasSingleCatch |
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.
Rather than passing around hasSingleCatch, I think it would be cleaner to compute it here (from filter.Parent.Parent
).
a945044
to
fc5be2f
Compare
@elijah6 It looks like this one may get retargeted to 15.6, but that's fine. 🙂 Your other PR will make a very nice addition for 15.3. 👍 |
Sure! 😄 Thank you for tracking these. |
fc5be2f
to
25b8fea
Compare
@elijah6 Can you please bring this PR up-to-date with master? |
3e65f68
to
ce80269
Compare
@gafter branch brought up-to-date. |
var errorCode = boundFilter.ConstantValue.BooleanValue | ||
? ErrorCode.WRN_FilterIsConstantTrue | ||
: (filter.Parent.Parent as TryStatementSyntax).Catches.Count == 1 | ||
? ErrorCode.WRN_FilterIsConstantFalseRedundantTryCatch |
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.
It would not be redundant if there is also a finally
clause.
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.
That is true. It should be FilterIsConstantFalse in that case. I will make the fix.
@@ -1241,9 +1241,11 @@ internal enum ErrorCode | |||
ERR_EncReferenceToAddedMember = 7101, | |||
ERR_MutuallyExclusiveOptions = 7102, | |||
ERR_InvalidDebugInfo = 7103, | |||
WRN_FilterIsConstantFalse = 7104, |
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.
These are in the wrong section; they were not, as the #region title describes, introduced in C# 6. Please move it to near the bottom.
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.
I put them in that section only because they are bug fixes for the error code 7095 ("FilterIsConstant") introduced in C# 6. Isn't that the right approach for error code bug fixes? If not, should it go in a new section at the bottom?
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.
It will be easier for the software archaeologists if we mostly add to the end.
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.
They have been moved to the bottom.
// catch (A) when (false) { } | ||
Diagnostic(ErrorCode.WRN_FilterIsConstantFalseRedundantTryCatch, "(1+1)!=2").WithLocation(10, 25)); | ||
} | ||
|
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.
I would like to see what happens when the filter is a constant, but it is not boolean. What errors occur? Can you please add a test?
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.
In that case, it will be handled by ERR_NoImplicitConv diagnostic. It has its own tests actually.
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.
And that causes these diagnostics not to be produced?
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.
yeah. I have added a test case to make it clearer.
@dotnet/roslyn-compiler Can we please have a second review of this community submission? |
ce80269
to
a487c48
Compare
bc09f71
to
9090635
Compare
retest windows_debug_unit32_prtest please |
@elijah6 Thank you so much for the contribution! Sorry it took so long to be processed. |
@gafter Thank you! |
Bugs this fixes:
#18750
Replaced WRN_FilterIsConstant warning with three separate warnings with different suggestions.
WRN_FilterIsConstantTrue
: suggest removing the filterWRN_FilterIsConstantFalse
: suggest removing the catch clauseWRN_FilterIsConstantRedundantTryCatch
: suggest removing the try-catch block, if the catch filter expression is 'false'/'default' and there is only one catch clause for the try-catch block.