473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ms csharp compiler bug

I am working on a c# compiler for the DynamicMethod so I try a lot of
strange things out. I came across this one:

public void CompileBug()
{
object obj = null + new object();
Console.WriteLine(obj.GetType().ToString() );
}

// IL disasm
{
// Method begins at RVA 0x111bc
// Code size 31 (0x1f)
.maxstack 2
.locals init ([0] object obj)
IL_0000: /* 00 | */ nop
IL_0001: /* 14 | */ ldnull
IL_0002: /* 73 | (0A)000024 */ newobj instance void
[mscorlib]System.Object::.ctor()
IL_0007: /* 28 | (0A)0001D0 */ call string
[mscorlib]System.String::Concat(object,

object)
IL_000c: /* 0A | */ stloc.0
IL_000d: /* 06 | */ ldloc.0
IL_000e: /* 6F | (0A)0000B4 */ callvirt instance class
[mscorlib]System.Type [mscorlib]System.Object::GetType()
IL_0013: /* 6F | (0A)00006E */ callvirt instance string
[mscorlib]System.Object::ToString()
IL_0018: /* 28 | (0A)000028 */ call void
[mscorlib]System.Console::WriteLine(string)
IL_001d: /* 00 | */ nop
IL_001e: /* 2A | */ ret
} // end of method XXX::CompileBug

I assume this should not compile at all. If I am right in my assumption
what should the error message be ? Something like "No implicit conversion
for ..." .
Is a literal null with no context considered to be of type Object (I thought
I saw that in ECMA 334).

Jul 10 '06 #1
8 1403
It just occured to me that the error message should be more like "Operator
'+' cannot be applied to operands of type 'object' and 'object'" , since the
null literal would be implicitly converted to Object. Is this so.

"Toby Altman" wrote:
I am working on a c# compiler for the DynamicMethod so I try a lot of
strange things out. I came across this one:

public void CompileBug()
{
object obj = null + new object();
Console.WriteLine(obj.GetType().ToString() );
}

// IL disasm
{
// Method begins at RVA 0x111bc
// Code size 31 (0x1f)
.maxstack 2
.locals init ([0] object obj)
IL_0000: /* 00 | */ nop
IL_0001: /* 14 | */ ldnull
IL_0002: /* 73 | (0A)000024 */ newobj instance void
[mscorlib]System.Object::.ctor()
IL_0007: /* 28 | (0A)0001D0 */ call string
[mscorlib]System.String::Concat(object,

object)
IL_000c: /* 0A | */ stloc.0
IL_000d: /* 06 | */ ldloc.0
IL_000e: /* 6F | (0A)0000B4 */ callvirt instance class
[mscorlib]System.Type [mscorlib]System.Object::GetType()
IL_0013: /* 6F | (0A)00006E */ callvirt instance string
[mscorlib]System.Object::ToString()
IL_0018: /* 28 | (0A)000028 */ call void
[mscorlib]System.Console::WriteLine(string)
IL_001d: /* 00 | */ nop
IL_001e: /* 2A | */ ret
} // end of method XXX::CompileBug

I assume this should not compile at all. If I am right in my assumption
what should the error message be ? Something like "No implicit conversion
for ..." .
Is a literal null with no context considered to be of type Object (I thought
I saw that in ECMA 334).
Jul 10 '06 #2
it looks strange but i'd consider it a valid expression and so it should
compile and even produce a result. Why do you think it is a bug?
Mathematically, an empty set plus an object must evaluate to the object
right?

However, when I run the example, I get a system.string being printed out
with the code. Removing the null, I get a system.object. That to me implies
an inconsistency.

thoughts/comments?
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------
"Toby Altman" <To********@discussions.microsoft.comwrote in message
news:AE**********************************@microsof t.com...
>I am working on a c# compiler for the DynamicMethod so I try a lot of
strange things out. I came across this one:

public void CompileBug()
{
object obj = null + new object();
Console.WriteLine(obj.GetType().ToString() );
}

// IL disasm
{
// Method begins at RVA 0x111bc
// Code size 31 (0x1f)
.maxstack 2
.locals init ([0] object obj)
IL_0000: /* 00 | */ nop
IL_0001: /* 14 | */ ldnull
IL_0002: /* 73 | (0A)000024 */ newobj instance void
[mscorlib]System.Object::.ctor()
IL_0007: /* 28 | (0A)0001D0 */ call string
[mscorlib]System.String::Concat(object,

object)
IL_000c: /* 0A | */ stloc.0
IL_000d: /* 06 | */ ldloc.0
IL_000e: /* 6F | (0A)0000B4 */ callvirt instance class
[mscorlib]System.Type [mscorlib]System.Object::GetType()
IL_0013: /* 6F | (0A)00006E */ callvirt instance string
[mscorlib]System.Object::ToString()
IL_0018: /* 28 | (0A)000028 */ call void
[mscorlib]System.Console::WriteLine(string)
IL_001d: /* 00 | */ nop
IL_001e: /* 2A | */ ret
} // end of method XXX::CompileBug

I assume this should not compile at all. If I am right in my assumption
what should the error message be ? Something like "No implicit conversion
for ..." .
Is a literal null with no context considered to be of type Object (I
thought
I saw that in ECMA 334).

Jul 10 '06 #3
The string part is the bug. There is no implicit conversion from object to
string and besides neither operand is of type string. In a way I am not too
surprised about this bug as string does not overlode the + operator ( there
is no op_Addition method to call) the compiler (Mine anyway) has to add the
call to String::Concat .
"Alvin Bruney [MVP]" wrote:
it looks strange but i'd consider it a valid expression and so it should
compile and even produce a result. Why do you think it is a bug?
Mathematically, an empty set plus an object must evaluate to the object
right?

However, when I run the example, I get a system.string being printed out
with the code. Removing the null, I get a system.object. That to me implies
an inconsistency.

thoughts/comments?
--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------
"Toby Altman" <To********@discussions.microsoft.comwrote in message
news:AE**********************************@microsof t.com...
I am working on a c# compiler for the DynamicMethod so I try a lot of
strange things out. I came across this one:

public void CompileBug()
{
object obj = null + new object();
Console.WriteLine(obj.GetType().ToString() );
}

// IL disasm
{
// Method begins at RVA 0x111bc
// Code size 31 (0x1f)
.maxstack 2
.locals init ([0] object obj)
IL_0000: /* 00 | */ nop
IL_0001: /* 14 | */ ldnull
IL_0002: /* 73 | (0A)000024 */ newobj instance void
[mscorlib]System.Object::.ctor()
IL_0007: /* 28 | (0A)0001D0 */ call string
[mscorlib]System.String::Concat(object,

object)
IL_000c: /* 0A | */ stloc.0
IL_000d: /* 06 | */ ldloc.0
IL_000e: /* 6F | (0A)0000B4 */ callvirt instance class
[mscorlib]System.Type [mscorlib]System.Object::GetType()
IL_0013: /* 6F | (0A)00006E */ callvirt instance string
[mscorlib]System.Object::ToString()
IL_0018: /* 28 | (0A)000028 */ call void
[mscorlib]System.Console::WriteLine(string)
IL_001d: /* 00 | */ nop
IL_001e: /* 2A | */ ret
} // end of method XXX::CompileBug

I assume this should not compile at all. If I am right in my assumption
what should the error message be ? Something like "No implicit conversion
for ..." .
Is a literal null with no context considered to be of type Object (I
thought
I saw that in ECMA 334).


Jul 10 '06 #4
By the way, object foo() { return new Object() + new Object(); } does not
compile (since Object does not overload the '+' operator ( or any operator).
Jul 10 '06 #5
Toby Altman <To********@discussions.microsoft.comwrote:
I am working on a c# compiler for the DynamicMethod so I try a lot of
strange things out. I came across this one:

public void CompileBug()
{
object obj = null + new object();
Console.WriteLine(obj.GetType().ToString() );
}
I assume this should not compile at all.
As you know, "+" with strings is treated specially to make string
concatenation work with objects. The relevant section is 14.7.4, the
Addition operator:

"The binary + operator performs string concatenation when one or both
operands are of type string. If an operand of string concatenation is
null, an empty string is substituted. Otherwise, any non-string operand
is converted to its string representation by invoking the virtual
ToString method inherited from type object. If ToString returns null, an
empty string is substituted."
If I am right in my assumption
what should the error message be ? Something like "No implicit conversion
for ..." .
Is a literal null with no context considered to be of type Object (I thought
I saw that in ECMA 334).
It is not. The type of the null literal is the null type (11.2.7), a
concept which only exists in the compiler - it's not in the CLR. A value
of the null type (there's only one, the "null" literal) is implicitly
convertible to any other reference type (13.1.4).

(All references to sections are from ECMA-334 3rd ed.)

-- Barry

--
http://barrkel.blogspot.com/
Jul 11 '06 #6
Barry Kelly <ba***********@gmail.comwrote:
As you know, "+" with strings is treated specially to make string
concatenation work with objects. The relevant section is 14.7.4, the
Addition operator:

"The binary + operator performs string concatenation when one or both
operands are of type string. If an operand of string concatenation is
null, an empty string is substituted. Otherwise, any non-string operand
is converted to its string representation by invoking the virtual
ToString method inherited from type object. If ToString returns null, an
empty string is substituted."
I omitted this: as this says, one or both operations should be of type
string. Since in your case one operand is of the null type and the other
is a value of type Object, the "+" operator shouldn't be matched.

-- Barry

--
http://barrkel.blogspot.com/
Jul 11 '06 #7

"Barry Kelly" wrote:
I omitted this: as this says, one or both operations should be of type
string. Since in your case one operand is of the null type and the other
is a value of type Object, the "+" operator shouldn't be matched.
Which is pretty much what I said (... the error message should be more like
"Operator '+' cannot be applied to operands of type 'object' and 'object'
...." and thus 'object foo(){return null + new object();}' should not compile.

My compiler would try to implicitly convert null to Object and since object
is a referenc type it would ( EMCA 334 13.1.4 Implicit reference conversions
.... From the null type (§11.2.7) to any reference-type." , but would not
find a overloaded '+' operator so the error message would be something like
the above.


>
-- Barry

--
http://barrkel.blogspot.com/
Jul 11 '06 #8
Toby Altman <To********@discussions.microsoft.comwrote:
"Barry Kelly" wrote:
I omitted this: as this says, one or both operations should be of type
string. Since in your case one operand is of the null type and the other
is a value of type Object, the "+" operator shouldn't be matched.

Which is pretty much what I said
Sure, but now that you've got hard references to the standard, you can
log a bug.

http://connect.microsoft.com/VisualStudio/Feedback
(... the error message should be more like
"Operator '+' cannot be applied to operands of type 'object' and 'object'
..."
It should be more like "Operator '+' cannot be applied to operands of
type 'null' and 'object' ..."
My compiler would try to implicitly convert null to Object
Null is convertible to every reference type, so you must produce an
ambiguous overload error where there is a conflict between different
overload resolutions involving reference types. You can't treat null as
Object. The null type is a distinct type.

-- Barry

--
http://barrkel.blogspot.com/
Jul 11 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sam74 | last post by:
Hi, I've downloaded framework 1.1 .net SDK from the microsoft webpage and i've seen that in the pakage there is the csharp compiler (csc.exe) If I develop a program with notebook and I compile it...
1
by: Azeem M. Suleman | last post by:
Hello, I need to use Csharp compiler and an interpretor. In my application user will use the application and application will write CSharp code on backend. But now i don't need to reinvent the...
0
by: Lee Alexander | last post by:
This isn't a CSharp language issue as such but this seemed the best place to put this :-) When compiling a CSharp application via DevStudio I want the IDE to still be responsive like when...
3
by: Alex | last post by:
Hi, The following article describes how to compile CSharp code programmatically http://support.microsoft.com/default.aspx? scid=http://support.microsoft.com:80/support/kb/articles/Q3...
9
by: Azeem M. Suleman | last post by:
Hello, I need to use Csharp compiler and an interpretor. In my application user will use the application and application will write CSharp code on backend. But now i don't need to reinvent the...
7
by: Peter Smirnov | last post by:
Sorry for this newbie question but as far as I heard one need at least VisualStudio to develop CSharp applications. Is this correct? Are there otherwise some command line tools like javac.exe and...
5
by: Andy Sutorius | last post by:
Hi, I am attempting to convert this vb function to csharp but I am getting stuck on the if statement dt.Rows(iLoop)("FAQCategoryID")). The compiler says "method name expected" and underlines...
5
by: Joel | last post by:
In the course of my project, I must include some custom logic and would like to integrate a small script language in my application for that purpose. In C++, I used the LUA script language and I...
19
by: auratius | last post by:
http://www.auratius.co.za/CSharpCodingStandards.html Complete CSharp Coding Standards 1. Naming Conventions and Styles 2. Coding Practices 3. Project Settings and Project Structure 4....
4
by: Ed | last post by:
Hi, dear all, Here are some questions when I was writing CLI code, which would be used by C# Project. 1. Reference Parameter If CLI method have % reference type parameter, which is ref class,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.