Skip to content

Unbacked property has an initializer #3524

@ds5678

Description

@ds5678

Input code

This issue is in reference to the GlobalVariables.String class in this assembly:

ConvertedCpp.zip

I wrote a unit test:

using System;
using System.Runtime.CompilerServices;

namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
	internal interface IInlineArray<T>
	{
		static abstract int Length { get; }
	}
	[InlineArray(1)]
	public struct InlineArray_13_w3otm9c : IInlineArray<sbyte>, IInlineArray<byte>
	{
		private sbyte __element0;

		public static int Length => 13;
	}
	internal static class InlineArrayHelper
	{
		public static TBuffer Create<TBuffer, TElement>(ReadOnlySpan<TElement> contents) where TBuffer : struct, IInlineArray<TElement>
		{
			return default(TBuffer);
		}
	}
	internal static class String
	{
		static String()
		{
			Value = InlineArrayHelper.Create<InlineArray_13_w3otm9c, byte>("Hello world!\0"u8);
		}
		public unsafe static InlineArray_13_w3otm9c* __pointer;

		public unsafe static InlineArray_13_w3otm9c* Pointer => __pointer;

		public static InlineArray_13_w3otm9c Value {
			get {
				return default(InlineArray_13_w3otm9c);
			}
			set {
			}
		}
	}
}

The String class compiles to the same IL, but (for whatever reason) does not exhibit the problem.

UnbackedPropertyInitialization.opt.roslyn.zip

Erroneous output

// ConvertedCpp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// GlobalVariables.String
using Helpers;
using InlineArrays;

internal static class String
{
	public unsafe static InlineArray_13_w3otm9c* __pointer;

	public unsafe static InlineArray_13_w3otm9c* Pointer => __pointer;

	public static InlineArray_13_w3otm9c Value
	{
		get
		{
			return default(InlineArray_13_w3otm9c);
		}
		set
		{
		}
	} = InlineArrayHelper.Create<InlineArray_13_w3otm9c, byte>("Hello world!\0"u8);
}
error CS8050: Only auto-implemented properties, or properties that use the 'field' keyword, can have initializers.

Details

  • Product in use: ILSpy
  • Version in use: latest commit (b52544d)
  • As always, I'm willing to contribute fixes for the issues I post.
  • I'm struggling to see how those two assemblies are significantly different.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDecompilerThe decompiler engine itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions