CheckTupleElementNames throws error for missing tuple element.#29042
CheckTupleElementNames throws error for missing tuple element.#29042chborl merged 6 commits intodotnet:dev15.8.xfrom
Conversation
|
@CyrusNajmabadi , @jcouv , since tuple element names are optional, I don't think |
|
Also @heejaechang , do you think this fix is viable? |
|
I don't think we should relax this check. If the IDE needs to provide only some names, I think passing |
There was a problem hiding this comment.
this check feels real bit strange to me. so null or "+" is okay but "" not okay?
There was a problem hiding this comment.
I would check for string.IsNullOrWhitespace(...) ? null : xxx
|
@jcouv , passing |
There was a problem hiding this comment.
It feels like such a helper method is likely to already exist. Can you double-check?
jcouv
left a comment
There was a problem hiding this comment.
LGTM (iteration 4), but please confirm there isn't already a helper method to check for whitespace/newlines. Thanks
There was a problem hiding this comment.
fwiw @jcouv . i do find it odd that there is this restriction in the core API. i.e. you allow null. you allow whitespace. you allow normal text. but you basically don't allow the empty string. seems... string to me. But if you really want that, then this change seems ok :)
@chborl small nit. could you just make this:
else if (expr is IdentifierName name) { ...Then you don't need the multiple casts.
There was a problem hiding this comment.
@chborl since 'null' is allowed, this check should be:
elementNamesBuilder.Add(name.Identifier.ValueText == "" ? null : name.Identifier.ValueText)
There was a problem hiding this comment.
@CyrusNajmabadi I agree the restriction is a bit strange, but arguably this PR demonstrates why it is useful: it helps guide users to pass null rather than empty, when they want to represent missing names ;-)
There was a problem hiding this comment.
@jcouv so whitespace is also okay? just not empty string? is empty string used as some kind of special state marker? is that why empty string is explicitly blocked?
There was a problem hiding this comment.
@jcouv asking since I saw Chery changed back from IsNullOrWhitespace to checking explicitly just "" string.
will null or whitespaces input will change any outcome of the API?
There was a problem hiding this comment.
Correct, you can create a tuple symbol with illegal names, but we block empty strings.
This check was added in PR #14958
The most relevant comment I could find is that "fields can't have blank names".
This was tested explicitly:
[Fact]
public void CreateTupleTypeSymbol2_BadNames()
{
var comp = CSharpCompilation.Create("test", references: new[] { MscorlibRef });
ITypeSymbol intType = comp.GetSpecialType(SpecialType.System_Int32);
// illegal C# identifiers and blank
var tuple2 = comp.CreateTupleTypeSymbol(ImmutableArray.Create(intType, intType), ImmutableArray.Create("123", " "));
Assert.Equal(new[] { "123", " " }, GetTupleElementNames(tuple2));
// reserved identifiers
var tuple3 = comp.CreateTupleTypeSymbol(ImmutableArray.Create(intType, intType), ImmutableArray.Create("return", "class"));
Assert.Equal(new[] { "return", "class" }, GetTupleElementNames(tuple3));
}
CyrusNajmabadi
left a comment
There was a problem hiding this comment.
Close. but needs a little refinement. Thanks!
Customer scenario
Type the following in editor window
Put caret at the opening parenthesis on the right side of the assignment statement and invoke Signature Helper (Ctrl+Shift+space)
Actual:
CheckTupleElementNames throws error CodeAnalysisResource.TupleElementNameEmpty
Expected:
No error thrown
Bugs this fixes
Fixes: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/655607
Workarounds, if any
none
Risk
None
Performance impact
None
Is this a regression from a previous update?
No
Root cause analysis
Original code did not account for missing tuple element
How was the bug found?
Watson
Test documentation updated?
New unit test added