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

Inferred nullability should flow into anonymous type fields #24018

Closed
cston opened this issue Jan 3, 2018 · 2 comments
Closed

Inferred nullability should flow into anonymous type fields #24018

cston opened this issue Jan 3, 2018 · 2 comments

Comments

@cston
Copy link
Member

cston commented Jan 3, 2018

The anonymous type field in the following should be declared as string rather than string?. Instead, y.ToString() generates a warning that y may be null.

using System.Linq;
class C
{
    static void Main()
    {
        var x = new string[] { "" };
        var y = x.Select(s => new { s }).First().s;
        y.ToString();
    }
}

(jcouv update:)
I also noticed a reference to the feature flag in binding logic for anonymous types, which seems wrong (the feature flag should only affect diagnostics, but not symbols from binding).

    private BoundExpression BindAnonymousObjectCreation(AnonymousObjectCreationExpressionSyntax node, DiagnosticBag diagnostics)
        {
...
                //  calculate the expression's type and report errors if needed
                TypeSymbol fieldType = GetAnonymousTypeFieldType(boundExpressions[i], fieldInitializer, diagnostics, ref hasError);

                // build anonymous type field descriptor
                fieldSyntaxNodes[i] = (nameToken.Kind() == SyntaxKind.IdentifierToken) ? (CSharpSyntaxNode)nameToken.Parent : fieldInitializer;
                fields[i] = new AnonymousTypeField(fieldName == null ? "$" + i.ToString() : fieldName, fieldSyntaxNodes[i].Location, 
                                                   TypeSymbolWithAnnotations.Create(fieldType, isNullableIfReferenceType: node.IsFeatureStaticNullCheckingEnabled()));  // <===== THIS SEEMS FISHY

...
            }
@HaloFour
Copy link

HaloFour commented Jan 4, 2018

This is a much simpler example that demonstrates the issue without flow analysis having to know anything about LINQ methods:

using System;
public class C {
    public void M() {
        var s = "";
        var a = new { s };
        var r = a.s.ToString(); // CS8602
    }
}

@jaredpar
Copy link
Member

Related to #31394

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants