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

Switch over type throws null ref exception #16293

Closed
rogeralsing opened this issue Jan 6, 2017 · 1 comment
Closed

Switch over type throws null ref exception #16293

rogeralsing opened this issue Jan 6, 2017 · 1 comment
Assignees
Labels
Area-Compilers Bug New Language Feature - Pattern Matching Pattern Matching Resolution-Duplicate The described behavior is tracked in another issue
Milestone

Comments

@rogeralsing
Copy link

rogeralsing commented Jan 6, 2017

Reporting the unexpected behavior seen in this question: http://stackoverflow.com/questions/41504414/c-sharp-7-pattern-matching-semantics

Version Used:
Microsoft Visual Studio Professional 2017 RC
Version 15.0.26014.0 D15REL
Microsoft .NET Framework
Version 4.6.01038

Installed Version: Professional
Visual C# 2017 RC 00369-50000-00000-AA365
Microsoft Visual C# 2017 RC

Steps to Reproduce:

  1. I have not been able to reproduce this outside of my library code:
    I have this block of code:
    https://github.com/AsynkronIT/protoactor-dotnet/blob/master/src/Proto.Remoting/EndpointManager.cs#L21
var msg = context.Message;
//TODO: convert to switch later, currently doesnt work, switching on type throws null ref error
if (msg is Started)
{
    Console.WriteLine("[REMOTING] Started EndpointManager");
    return Actor.Done;
}
if (msg is MessageEnvelope)
{
    var env = (MessageEnvelope)msg;
    PID pid;
    if (!_connections.TryGetValue(env.Target.Host, out pid))
    {
        Console.WriteLine("Resolving EndpointWriter for {0}", env.Target.Host);
        var props =
            Actor.FromProducer(() => new EndpointWriter(env.Target.Host))
                .WithMailbox(() => new EndpointWriterMailbox());
        pid = context.Spawn(props);
        _connections.Add(env.Target.Host, pid);
    }
    pid.Tell(msg);
    return Actor.Done;
}
return Actor.Done;

Which works as expected.

This block however, https://github.com/AsynkronIT/protoactor-dotnet/blob/master/src/Proto.Remoting/EndpointManager.cs#L45

switch (context.Message)
{
    case Started _:
        Console.WriteLine("[REMOTING] Started EndpointManager");
        return Actor.Done;
    case MessageEnvelope env:
        PID pid;
        if (!_connections.TryGetValue(env.Target.Host, out pid))
        {
            Console.WriteLine("Resolving EndpointWriter for {0}", env.Target.Host);
            var props =
                Actor.FromProducer(() => new EndpointWriter(env.Target.Host))
                    .WithMailbox(() => new EndpointWriterMailbox());
            pid = context.Spawn(props);
            _connections.Add(env.Target.Host, pid);
        }
        pid.Tell(env);
        return Actor.Done;
    default:
        return Actor.Done;
}

Throws Null reference exception.

I have narrowed it down to the case MessageEnvelope env: line.
Replacing that with:

case MessageEnvelope _:
     var env = context.Message as MessageEnvelope;

Fixes the issue.

Expected Behavior:
I would expect the above to work just like the original if based code.

Actual Behavior:
Null reference exception on the case MessageEnvelope env: line.
Or rather it throws null reference exception directly on the switch(context.Message) before I am able to step into any of the case blocks. but the above mentioned fix removes the issue.

@gafter
Copy link
Member

gafter commented Jan 6, 2017

This is a dup of #16066

@gafter gafter closed this as completed Jan 6, 2017
@gafter gafter added the Resolution-Duplicate The described behavior is tracked in another issue label Jan 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug New Language Feature - Pattern Matching Pattern Matching Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

4 participants