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

Misleading constant diagnostics #18933

Merged

Conversation

elijah6
Copy link
Contributor

@elijah6 elijah6 commented Apr 23, 2017

Bugs this fixes:

#18750

Replaced WRN_FilterIsConstant warning with three separate warnings with different suggestions.

  • WRN_FilterIsConstantTrue: suggest removing the filter
  • WRN_FilterIsConstantFalse: suggest removing the catch clause
  • WRN_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.

@elijah6
Copy link
Contributor Author

elijah6 commented Apr 23, 2017

It seems there was a memory issue with perf_correctness_prtest. Is it possible to re-run?

@BattleRush
Copy link
Contributor

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

@elijah6
Copy link
Contributor Author

elijah6 commented Apr 23, 2017

@BattleRush Thank you! It doesn't seem to have been triggered, so I will try the comment on its own.

@elijah6
Copy link
Contributor Author

elijah6 commented Apr 23, 2017

retest perf_correctness_prtest please

@elijah6
Copy link
Contributor Author

elijah6 commented Apr 23, 2017

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
Copy link
Member

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.

Copy link
Contributor Author

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,
Copy link
Member

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.

  1. Between FilterIsConstantFalse and FilterIsConstantRedundantTryCatch, I don't foresee developers getting confused by the former but suddenly knowing how to proceed with the latter.
  2. Distinguishing the cases introduces additional complexity in the compiler.
  3. 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).

Copy link
Contributor Author

@elijah6 elijah6 Apr 23, 2017

Choose a reason for hiding this comment

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

Thank you, @sharwell .
Should I then just distinguish True and False cases, and suggest consider removing the catch clause for all False cases? I agree it would reduce unnecessary overhead for the two false cases.
I will wait for additional comment from @gafter who originally raised #18750

Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Contributor Author

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.

@sharwell sharwell added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Apr 23, 2017
@sharwell
Copy link
Member

sharwell commented Apr 23, 2017

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.

@sharwell
Copy link
Member

sharwell commented May 9, 2017

@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);
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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).

@gafter gafter self-assigned this May 9, 2017
@gafter gafter added Area-Compilers Bug Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. labels May 9, 2017
@elijah6 elijah6 force-pushed the misleadingConstantDiagnostics branch from a945044 to fc5be2f Compare May 10, 2017 07:04
@sharwell
Copy link
Member

@gafter It seems like this was reasonably well-approached and hopefully low risk. Can you take a look today to see if it would be a candidate for 15.3, and if so let @elijah6 know what (if anything) you'd be looking for in order to call it complete?

@sharwell
Copy link
Member

@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. 👍

@elijah6
Copy link
Contributor Author

elijah6 commented May 18, 2017

Sure! 😄 Thank you for tracking these.

@elijah6 elijah6 force-pushed the misleadingConstantDiagnostics branch from fc5be2f to 25b8fea Compare May 20, 2017 08:21
@gafter
Copy link
Member

gafter commented Sep 25, 2017

@elijah6 Can you please bring this PR up-to-date with master?

@elijah6 elijah6 force-pushed the misleadingConstantDiagnostics branch from 3e65f68 to ce80269 Compare October 1, 2017 15:10
@elijah6
Copy link
Contributor Author

elijah6 commented Oct 1, 2017

@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
Copy link
Member

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.

Copy link
Contributor Author

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,
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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));
}

Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

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.

@gafter gafter added this to the 15.5 milestone Oct 1, 2017
@gafter
Copy link
Member

gafter commented Oct 2, 2017

@dotnet/roslyn-compiler Can we please have a second review of this community submission?

@elijah6 elijah6 force-pushed the misleadingConstantDiagnostics branch from ce80269 to a487c48 Compare October 4, 2017 17:34
@elijah6 elijah6 force-pushed the misleadingConstantDiagnostics branch from bc09f71 to 9090635 Compare November 12, 2017 16:17
@elijah6
Copy link
Contributor Author

elijah6 commented Nov 13, 2017

retest windows_debug_unit32_prtest please

@gafter gafter merged commit 5408538 into dotnet:post-dev15.5-contrib Nov 13, 2017
@gafter
Copy link
Member

gafter commented Nov 13, 2017

@elijah6 Thank you so much for the contribution! Sorry it took so long to be processed.

@gafter gafter added the Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented label Nov 13, 2017
@sharwell sharwell modified the milestones: 15.7, 15.6 Nov 13, 2017
@elijah6 elijah6 deleted the misleadingConstantDiagnostics branch November 14, 2017 19:29
@elijah6
Copy link
Contributor Author

elijah6 commented Nov 14, 2017

@gafter Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Community The pull request was submitted by a contributor who is not a Microsoft employee. Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants