Skip to content

Incorrect code generation for string-keyed object with parameterized constructor #1298

Closed

Description

@mayuki

Bug description

Generator v2.3 generates incorrect code for string-keyed objects with a parameterized constructor.
The generated Deserialize method contains a syntax error.

Repro steps

Generate code from a MessagePackObject definition such as the following.

using System;
using System.Collections.Generic;
using MessagePack;

namespace TempProject
{
    [MessagePackObject(true)]
    public class MyMessagePackObject
    {
        public int A { get; }
        public string B { get; }

        public MyMessagePackObject(int a, string b)
        {
            A = a;
            B = b;
        }
    }
}

Expected behavior

No compile errors.

Actual behavior

No temporary variables are emitted and all assignments will be reader.Skip(). The constructor call causes a syntax error.

public global::TempProject.MyMessagePackObject Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
{
    if (reader.TryReadNil())
    {
        return null;
    }

    options.Security.DepthStep(ref reader);
    var formatterResolver = options.Resolver;
    var length = reader.ReadMapHeader();

    for (int i = 0; i < length; i++)
    {
        var stringKey = global::MessagePack.Internal.CodeGenHelpers.ReadStringSpan(ref reader);
        switch (stringKey.Length)
        {
            default:
            FAIL:
                reader.Skip();
                continue;
            case 1:
                switch (global::MessagePack.Internal.AutomataKeyGen.GetKey(ref stringKey))
                {
                    default: goto FAIL;
                    case 65UL:
                        reader.Skip();
                        continue;
                    case 66UL:
                        reader.Skip();
                        continue;
                }

        }
    }

    var ____result = new global::TempProject.MyMessagePackObject(__A__, __B__);
    reader.Depth--;
    return ____result;
}
  • MessagePack 2.3.75
  • MessagePack.Generator 2.3.75
  • .NET 5

Additional context

Activity

  1. mayuki commented on Aug 3, 2021

    @mayuki
    ContributorAuthor

    I am currently working to fix it. I will submit a PR when the patch is ready.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions