Fixed exception message in GetSemanticModel#26659
Conversation
| } | ||
|
|
||
| throw new ArgumentException(string.Format(CSharpResources.SyntaxTreeNotFoundTo, tree), $"{nameof(trees)}[{i}]"); | ||
| throw new ArgumentException(string.Format(CSharpResources.SyntaxTreeNotFoundToRemove, tree.FilePath), $"{nameof(trees)}[{i}]"); |
There was a problem hiding this comment.
FilePath may be null, empty, or even duplicated across SyntaxTrees. It's a completely optional part of the whole API.
There was a problem hiding this comment.
Right, but do you have a better way of identifying the syntax tree for diagnostic purposes?
Considering that the whole syntax tree can be hundreds of lines long, I think that's a much worse option. No information or ambiguous information still seems better to me than overwhelmingly huge amount of information for an exception message.
There was a problem hiding this comment.
It could potentially be referred to by index.
There was a problem hiding this comment.
What index? If you're talking about index within the current compilation, then I think that could make sense in other situations, but not here, where the exception is that the SyntaxTree is not contained in this compilation.
There was a problem hiding this comment.
looking at the code, it should just be what it was before. i.e. $"{nameof(trees)}[{i}]". So it's telling you the index in the argument list where the problem tree occurs.
There was a problem hiding this comment.
Ah, I understand what index you mean now. You're right that that's potentially useful information, but that's still passed as paramName to the exception (which means it will show up in its Message property), I didn't change that.
Are you saying that the message parameter should be just "SyntaxTree not found to remove", which would make the Message property e.g.:
SyntaxTree not found to remove
Parameter name: trees[4]
And how would this apply to the other cases I changed in this PR, where there is no index (since there is just one SyntaxTree)?
There was a problem hiding this comment.
Are you saying that the message parameter should be just "SyntaxTree not found to remove",
That, or> SyntaxTree 'trees[4]' not found to remove"
There was a problem hiding this comment.
And how would this apply to the other cases I changed in this PR, where there is no index
If it's just a single tree, then no need to print this information as it will be obvious from teh call.
There was a problem hiding this comment.
@CyrusNajmabadi Done. I thought there's no need to repeat paramName in the message, so I went with the simpler version.
|
Does VB implementation have the same issue? #Resolved |
|
@AlekseyTs It does not. As far as I can see, VB does not check in var tree1 = SyntaxFactory.ParseSyntaxTree("");
var tree2 = SyntaxFactory.ParseSyntaxTree(@"
Class C
Sub M()
End Sub
End Class", path: "C.vb");
var compilation = VisualBasicCompilation.Create(null).AddSyntaxTrees(tree1);
compilation.GetSemanticModel(tree2);Should I open an issue about that? #Resolved |
Yes, please open an issue. #Resolved |
|
@AlekseyTs Done: #26689. #Resolved |
| <value>__arglist cannot have an argument passed by 'in' or 'out'</value> | ||
| </data> | ||
| <data name="SyntaxTreeNotFound" xml:space="preserve"> | ||
| <value>SyntaxTree '{0}' not found</value> |
There was a problem hiding this comment.
How about "SyntaxTree '{0}' is not part of the compilation"
There was a problem hiding this comment.
@gafter Yeah, that's better. I have also changed SyntaxTreeNotFoundToRemove to "SyntaxTree is not part of the compilation, so it cannot be removed".
|
@AlekseyTs Do you have any further comments for this PR? |
I didn't review changes in this PR and as of this moment it is not on my list to look at any time soon. |
|
@dotnet/roslyn-compiler Can we please have a second review of this bug fix? |
|
The build failure is because I removed the format item from the message, but localized versions still use the old format strings, e.g.: <trans-unit id="SyntaxTreeNotFoundToRemove">
<source>SyntaxTree is not part of the compilation, so it cannot be removed</source>
<target state="needs-review-translation">No se encontró SyntaxTree '{0}' para quitarlo</target>
<note />
</trans-unit>Should I change the xlf files like the one above to the untranslated state? E.g. <trans-unit id="SyntaxTreeNotFoundToRemove">
<source>SyntaxTree is not part of the compilation, so it cannot be removed</source>
<target state="new">SyntaxTree is not part of the compilation, so it cannot be removed</target>
<note />
</trans-unit> |
|
@svick yes I would change them to an untranslated state with |
|
@jmarolf Done. |
| if (SyntaxAndDeclarationManager.IsLoadedSyntaxTree(tree, loadedSyntaxTreeMap)) | ||
| { | ||
| throw new ArgumentException(string.Format(CSharpResources.SyntaxTreeFromLoadNoRemoveReplace, tree), $"{nameof(trees)}[{i}]"); | ||
| throw new ArgumentException(string.Format(CSharpResources.SyntaxTreeFromLoadNoRemoveReplace), $"{nameof(trees)}[{i}]"); |
There was a problem hiding this comment.
$"{nameof(trees)}[{i}]" [](start = 118, length = 23)
The extra arguments can be removed, for string where {0} was removed.
There was a problem hiding this comment.
The remaining arguments are for the ArgumentException constructor, not for string.Format. Though the call to string.Format can be removed, so I just did that.
| </data> | ||
| <data name="SyntaxTreeNotFoundTo" xml:space="preserve"> | ||
| <value>SyntaxTree '{0}' not found to remove</value> | ||
| <data name="SyntaxTreeNotFoundToRemove" xml:space="preserve"> |
There was a problem hiding this comment.
Please find at least one test affected by each of the resource string changes and update the expectations.
(To do that, you run the test with an empty VerifyDiagnostics() and the test error will give you the text to copy/paste to update the baseline)
There was a problem hiding this comment.
Those resource strings are exception messages, not diagnostics.
Should I test the messages by e.g. changing this test to the following?
AssertEx.Throws<ArgumentException>(
() => comp.ReplaceSyntaxTree(newTree: SyntaxFactory.ParseSyntaxTree("Using System;"), oldTree: t1),
e => Assert.Equal(CSharpResources.SyntaxTreeNotFoundToRemove, e.Message));Doing that doesn't really seem worth it to me.
Or am I missing something and there is a way to test exception messages using VerifyDiagnostics?
There was a problem hiding this comment.
I think it's fine to leave as-is, given that it's for exceptions not diagnostics.
jcouv
left a comment
There was a problem hiding this comment.
Done with review pass (iteration 6).
|
@svick Thanks for the clarifications. Makes sense |
|
@svick Thank you for the contribution. You are awesome! |
* dotnet/master: (732 commits) Disable procdump on Spanish runs use named args. Exclude System.Diagnostics.DiagnosticSource from PortableFacades Exclude System.Net.Http from PortableFacades Align the implementations of IAssemblySymbol.GivesAccessTo (#26727) Fixed exception message in GetSemanticModel (#26659) Implement IDiscardSymbol.Equals and GetHashCode (#26720) Produce correct diagnostics on implicit operator conversions for stackalloc expressions (#26248) Remove special case handling of CreateDiagnosticProviderAndFixer throwing an exception Add verbose logging to DevDivInsertionFiles if you want it Exclude Microsoft.Composition as something we build a dependency map for Fix the message for Make Field Readonly Update LangVersion to 7.3 (#26698) fix extension deployment and add explainatory comment Add TaskExtensions to compiler package Add System.Diagnostics.DiagnosticSource to SignToolData.json Include missing facade: System.Diagnostics.DiagnosticSource Use default expressions. Remove forwarding method. Revert project chnage. ...
Fixes #26658.
For example, for this code:
the exception changes from:
to:
Questions:
SyntaxTreein an exception message?SyntaxTreeNotFoundToresource and copied translations from the old resource to the new one (which also removed BOM from the translation files). Is that okay?