Bug 3628 - Compile works using csc but fails with mcs
Summary: Compile works using csc but fails with mcs
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2012-02-23 22:38 UTC by Neale Ferguson
Modified: 2012-02-24 06:36 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Neale Ferguson 2012-02-23 22:38:26 UTC
The following compiles under csc but fails with mcs with:
test.cs(45,9): error CS0171: Field `Testing.Network.Messages.SecurityAliasMessage.securityId' must be fully assigned before control leaves the constructor

//  
// Author:
//       James P Michels III <james.p.michels@gmail.com>
//
// Copyright (c) 2011 James P Michels III
//
// All rights reserved.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace Testing.Network.Messages
{
	public static class MainClass
	{
		public static void Main (string[] args)
		{
			Console.WriteLine("hello");
		}
	}


	public enum Mic : uint { a,b,c };

    [StructLayout(LayoutKind.Sequential, Pack = 1)]    
    public unsafe struct SecurityAliasMessage
    {
        public readonly uint alias;
        private fixed byte securityId[maxLength];
        public readonly Mic mic;
        
        private const ushort maxLength = 30;
        
        public SecurityAliasMessage(uint alias, string securityId, Mic mic)
        {
            this.alias = alias;
            this.mic = mic;
            
            if (securityId.Length > maxLength) throw new ArgumentException("security id to long");
                        
            unsafe
            {                
                fixed (byte* pBytes = this.securityId)
                {
                    for (int i = 0; i < securityId.Length; i++)
                    {
                        pBytes[i] = (byte)securityId[i];
                    }
                }                    
            }
        }
        
        private unsafe static readonly int size = sizeof(SecurityAliasMessage);
        public static int Size
        {
            get
            {
                return size;
            }
        }
        
        public SecurityAliasMessage(byte[] buffer, int offset)
        {
            if (buffer == null) throw new ArgumentNullException("buffer");
            
            unsafe
            {
                if (offset < 0 || buffer.Length < offset + size) throw new ArgumentException("offset");
                
                fixed(byte* pBuffer = &buffer[offset])
                {
                    this = *(SecurityAliasMessage*)pBuffer;                    
                }
            }
        }
        
        public int Write(byte[] buffer, int offset)
        {
            if (buffer == null) throw new ArgumentNullException("buffer");
            
            unsafe
            {
                if (offset < 0 || buffer.Length < offset + size) throw new ArgumentException("offset");

                fixed(byte* pBuffer = &buffer[offset])
                {
                    *(SecurityAliasMessage*)pBuffer = this;                    
                }
                
                return size;
            }
        }
        
        public string SecurityId
        {            
            get
            {
                StringBuilder buf = new StringBuilder(maxLength);
                
                unsafe
                {
                    fixed (byte* pBytes = this.securityId)
                    {
                        for (int i = 0; i < maxLength; i++)
                        {
                            if (pBytes[i] == 0) break;
                            buf.Append((char)pBytes[i]);
                        }
                    }                    
                }
                
                return buf.ToString();
            }
        }
    }
}

Changing:
        public SecurityAliasMessage(uint alias, string securityId, Mic mic)
to
        public SecurityAliasMessage(uint alias, string securityId, Mic mic) : this()

Allows it to compile with mcs without error.
Comment 1 Marek Safar 2012-02-24 06:36:50 UTC
Already fixed in master