Repository navigation
Conversation
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🔵 Needs a closer look
It changes compiler diagnostics for previously accepted source, and CI remains in progress.
Pull request overview
Fixes file-based apps incorrectly honoring #: directives inside disabled conditional regions.
Changes:
- Diagnoses inactive
#:directives in file-based programs. - Adds coverage for disabled and conditional regions across parse modes.
File summaries
| File | Description |
|---|---|
DirectiveParser.cs |
Validates inactive file-based directives. |
IgnoredDirectiveParsingTests.cs |
Adds regression and branch coverage. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Balanced
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
c1b4e7c to
58560a8
Compare
58560a8 to
c1b4e7c
Compare
| { | ||
| if (isActive) | ||
| // Tooling may not know the conditional compilation symbols when reading file-based directives. | ||
| if (isActive || lexer.Options.FileBasedProgram) |
There was a problem hiding this comment.
If this is a regression from #78158, why the fix isn't a simple revert (i.e. simple removal of the if)?
There was a problem hiding this comment.
Because I think we can both fix the regression and preserve the intent of that PR.
The intent of that PR was to avoid errors in situations like this outside file-based apps:
#if false
System.Console.WriteLine("""
#:sdk test
""");
#endifI've preserved that fix in this PR. Plus this PR disallows something like this in file-based apps:
#if false
#:package System.Threading.RateLimiting@10.0.11
#endifOn the other hand, I can see this being a bit confusing (basically the same syntax structure works differently in vs out of file-based apps).
But simply reverting would also introduce a larger breaking change. This PR does not introduce a breaking change in my opinion, since the behavior change needs a feature flag, and the resulting file-based app would never work in dotnet run file.cs end-to-end scenarios anyway.
There was a problem hiding this comment.
On the other hand, I can see this being a bit confusing (basically the same syntax structure works differently in vs out of file-based apps).
I think I can simplify the logic though to avoid this as well.
If we go with this change, I will clarify the speclet.
| var source = """ | ||
| #if X | ||
| #:a | ||
| #elif true |
| { | ||
| if (isActive) | ||
| // Tooling may not know the conditional compilation symbols when reading file-based directives. | ||
| if (isActive || lexer.Options.FileBasedProgram) |
There was a problem hiding this comment.
Yes, the added test InDisabledRegion tests with and without the feature flag FileBasedProgram which directly affects this condition.
|
Done with review pass (commit 1) |
There was a problem hiding this comment.
🟡 Changes recommended
The parser change reintroduces a false-positive diagnostic for raw-string content in disabled file-based code.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
Two diagnostic assertions must verify the active branch’s source location.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (1)
src/Compilers/CSharp/Test/Syntax/Parsing/IgnoredDirectiveParsingTests.cs:583
- This assertion does not verify that the non-file-based-program diagnostic is attached to the active branch: both candidate directives have the same squiggled text, and no location is required. Add the conditional location so the
defineXcombinations detect an active/inactive mix-up.
// error CS9298: '#:' directives can be only used in file-based programs ('-features:FileBasedProgram')
// #:a
Diagnostic(ErrorCode.ERR_PPIgnoredNeedsFileBasedProgram, ":"),
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Balanced
| private DirectiveTriviaSyntax ParseDefineOrUndefDirective(SyntaxToken hash, SyntaxToken keyword, bool isActive, bool isFollowingToken) | ||
| { | ||
| if (isFollowingToken) | ||
| if (isFollowingToken && isActive) |
There was a problem hiding this comment.
Yes, there are preexisting tests, e.g., PreprocessorTests.TestDefineInIfExclusionAfterFirstToken.
There was a problem hiding this comment.
Yes, there are preexisting tests, e.g.,
I do not see any preexisting test observing a behavior change. I also do not see any new test added to target this code path. At the same time, I assume, the new condition is added for a reason. What is it?
There was a problem hiding this comment.
This is not a behavior change. The added condition is needed because the semantics of isFollowingToken flag changed - we are now propagating it through disabled regions where previously it was always false in disabled regions.
| .. commonDiagnostics, | ||
| // error CS9298: '#:' directives can be only used in file-based programs ('-features:FileBasedProgram') | ||
| // #:b | ||
| Diagnostic(ErrorCode.ERR_PPIgnoredNeedsFileBasedProgram, ":"), |
There was a problem hiding this comment.
Diagnostic(ErrorCode.ERR_PPIgnoredNeedsFileBasedProgram, ":")
While, perhaps, it is not the primary goal of the test to verify which branch is considered to be active, I think it is our goal to verify which directive gets the error. One way to achieve that is to assert error's location. Note that the comment immediately above this line makes things very confusing, because there is no "#:b" in the source above. To strengthen the test and make it clearer, consider asserting diagnostics for defineX and !defineX scenarios separately, including location and quoting proper source line for the errors. #Closed
There was a problem hiding this comment.
As an alternative we could stop reporting ERR_PPIgnoredNeedsFileBasedProgram for directives that get ERR_PPIgnoredFollowsIf. One might say that reporting ERR_PPIgnoredNeedsFileBasedProgram doesn't really help a user in such scenarios because, whether the feature is enabled or not, the directive is going to be in a wrong position. If a user is fixing errors one by one, it is a waste of time to fix this error for the scenario. If we do that, the set of errors in this test will be the same for all combinations.
| .. commonDiagnostics, | ||
| // error CS9298: '#:' directives can be only used in file-based programs ('-features:FileBasedProgram') | ||
| // #:a | ||
| Diagnostic(ErrorCode.ERR_PPIgnoredNeedsFileBasedProgram, ":"), |
|
I find the current logic very confusing and, perhaps, not making sense. But there is an error for this: Not reporting CS9299 in disabled regions across the board would make sense to me. Always reporting it would make sense to me. But not the current behavior. Perhaps clearly specified rules in language spec terms could help. |
|
Done with review pass (commit 6) |
Opened dotnet/csharplang#10329. I agree it's unfortunate the rules a bit more complicated, but I feel like the benefit is worth that. |
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🟡 Changes recommended
The shipped-language breaking change needs documentation and completed language/compatibility review.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Balanced
| tree.GetDiagnostics().Verify( | ||
| // (3,6): error CS9299: '#:' directives cannot be after '#if' directive | ||
| // #:sdk test | ||
| Diagnostic(ErrorCode.ERR_PPIgnoredFollowsIf, ":").WithLocation(3, 6)); |
There was a problem hiding this comment.
Diagnostic(ErrorCode.ERR_PPIgnoredFollowsIf, ":").WithLocation(3, 6));
I do not think this behavior makes sense. There is no error for:
System.Console.WriteLine();
#if false
System.Console.WriteLine("""
#:sdk test
""");
#endif
or for:
System.Console.WriteLine("""
#:sdk test
""");
But there is an error for:
#if false
System.Console.WriteLine("""
#:sdk test
""");
#endif
System.Console.WriteLine();
Effectively, we are reporting an error for syntactically valid code in disabled region.
There was a problem hiding this comment.
Effectively, we are reporting an error for syntactically valid code in disabled region.
It's not a syntactically valid code. Disabled regions are not parsed as normal code. The lexer doesn't see a method call, a raw string, etc. It only checks for directives in disabled regions.
There was a problem hiding this comment.
BTW, the combinatorial nature of the test makes it very hard to comprehend what is the actual user experience. Please split the unit test so that a reader wouldn't have to compile code in his/her head in order to understand what scenario is being tested.
There was a problem hiding this comment.
It's not a syntactically valid code.
It is. Remove conditional compilation and see.
Disabled regions are not parsed as normal code. The lexer doesn't see a method call, a raw string, etc. It only checks for directives in disabled regions.
That is not a reason to treat code as not syntactically valid.
There was a problem hiding this comment.
I mean this is a preexisting issue. Consider:
#if false
string code = """
#define
""";
#endifThere is an error in the disabled region. Change the condition to #if true and it compiles fine.
There was a problem hiding this comment.
I mean this is a preexisting issue.
It is pre-existing for #define, and I do not consider this as a correct or useful behavior.
There was a problem hiding this comment.
I generally agree with that; that's why I fixed and want to preserve the fix for #78157 in the first place. I guess this is the same discussion as below: #85206 (comment)
| tree.GetDiagnostics().Verify( | ||
| // (4,2): error CS9297: '#:' directives cannot be after first token in file | ||
| // #:package System.Threading.RateLimiting@10.0.11 | ||
| Diagnostic(ErrorCode.ERR_PPIgnoredFollowsToken, ":").WithLocation(4, 2)); |
There was a problem hiding this comment.
| options = options.WithFeature(FeatureName); | ||
| } | ||
|
|
||
| SyntaxFactory.ParseSyntaxTree(source, options).GetDiagnostics().Verify(); |
There was a problem hiding this comment.
There was a problem hiding this comment.
Does my other reply #85206 (comment) answer this or is this a different question?
I guess I am having trouble to understand who or what benefits from the error in a disabled region. I assume the benefit from not reporting an error is the fact that an existing valid code isn't broken. |
That is one benefit, yes. Another is this situation. Imagine you have tests like void Test()
{
string code = """
#:directive
""";
}Everything compiles fine. Then you wrap them in for example #if NET
void Test()
{
string code = """
#:directive
""";
}
#endifSuddenly, when |
|
Hm, I see. Well, it feels useful to tell the user that the directive in an #if NEVER_DEFINED
#:directive
#endifDoes not have any effect, especially since we are lexing directives in disabled regions anyway. But I think I understand your point now and I'm fine with just not reporting errors in disabled regions for these directives. |
I do not think it is that useful, especially given that it becomes a source of all kind of troubles.
Are we? We are doing something, but that isn't proper lexing. Otherwise, we wouldn't have #78157. If we were doing proper lexing for that scenario, we wouldn't see any directive inside the disabled region for the scenario. |
|
@jjonescz I added a comment to the issue you are trying to address in this PR |
|
Done with review pass (commit 8) |
Fixes #85203.
Speclet change: dotnet/csharplang#10329
Microsoft Reviewers: Open in CodeFlow