[nem-bug] [Nemerle 0000719]: NotNull doesn't work with generic function parameters

feedback at nemerle.org feedback at nemerle.org
Sun Jul 16 15:29:57 CEST 2006


The following issue has been RESOLVED.
======================================================================
<http://nemerle.org/bugs/view.php?id=719> 
======================================================================
Reported By:                aleksey
Assigned To:                nazgul
======================================================================
Project:                    Nemerle
Issue ID:                   719
Category:                   The Macro Library
Reproducibility:            always
Severity:                   minor
Priority:                   normal
Status:                     resolved
Resolution:                 fixed
Fixed in Version:           
======================================================================
Date Submitted:             07-14-2006 06:36 CEST
Last Modified:              07-16-2006 15:29 CEST
======================================================================
Summary:                    NotNull doesn't work with generic function
parameters
Description: 
Example:
def foo[T]([NotNull] bar : T) : void
{
    // some code
}

Error message:
error: comparing a value type T.722 to System.Object (a `null' literal?)
with reference equality

Minimal example without NotNull macro:
def foo[T](bar : T) : bool
{
	bar != null
	// the only workaround which seems to work
	// (bar : object) != null
}

C# example which works:
static class Foobar
{
	static bool Foo<T>(T bar)
	{
		return bar != null;
	}

	static void Main()
	{
		System.Console.WriteLine(Foo(20));
	}
}
======================================================================

----------------------------------------------------------------------
 nazgul - 07-14-06 12:33 
----------------------------------------------------------------------
I don't think we should allow comparison to null for values, which may be
value types. If you added ': class' contraint, it should then work.

----------------------------------------------------------------------
 aleksey - 07-14-06 14:19 
----------------------------------------------------------------------
I see, though not exactly understand why(e.g. why C# designers allowed it
then). But in fact my report relates more to NotNull macro, rather than
comparison to null.

Imagine that we have some sort of method which *must* accept both
reference and value type parameters. For reference types it would be handy
to check for null. Adding ': class' constraint is not a solution at all,
since there is need to support both reference and value types. I wouldn't
care for comparison to null if NotNull worked in such cases, since NotNull
is much more concise and readable.

----------------------------------------------------------------------
 Snaury - 07-14-06 14:24 
----------------------------------------------------------------------
> Imagine that we have some sort of method which *must* accept both
reference and value type parameters.

I believe that's exactly why C# designers allowed it. For value types it
just always returns true, but for reference types it might be handy.
Looking at il made by C# reveals that it boxes argument to T compares to
null and it just works...

----------------------------------------------------------------------
 VladD2 - 07-14-06 15:19 
----------------------------------------------------------------------
If decompile this method:
static bool Test<T>(T value)
{
  return value != null;
}

you see:
.method private hidebysig static bool Test<T>(!!T value) cil managed
{
      .maxstack 2
      .locals init (
            [0] bool CS$1$0000)
      L_0000: nop 
      L_0001: ldarg.0 
      L_0002: box !!T
      L_0007: ldnull 
      L_0008: ceq 
      L_000a: ldc.i4.0 
      L_000b: ceq 
      L_000d: stloc.0 
      L_000e: br.s L_0010
      L_0010: ldloc.0 
      L_0011: ret 
}

----------------------------------------------------------------------
 nazgul - 07-14-06 15:21 
----------------------------------------------------------------------
I guess this is the same thing we do for
(value : object) != null

In my opinion only the NotNull macro should be changed to use this upcast.

----------------------------------------------------------------------
 nazgul - 07-16-06 15:29 
----------------------------------------------------------------------
Fixed on trunk (r6450).

Issue History
Date Modified  Username       Field                    Change              
======================================================================
07-14-06 06:36 aleksey        New Issue                                    
07-14-06 12:33 nazgul         Note Added: 0001367                          
07-14-06 14:19 aleksey        Note Added: 0001368                          
07-14-06 14:24 Snaury         Note Added: 0001369                          
07-14-06 15:19 VladD2         Note Added: 0001370                          
07-14-06 15:21 nazgul         Note Added: 0001371                          
07-16-06 15:29 nazgul         Status                   new => resolved     
07-16-06 15:29 nazgul         Resolution               open => fixed       
07-16-06 15:29 nazgul         Assigned To               => nazgul          
07-16-06 15:29 nazgul         Note Added: 0001373                          
======================================================================




More information about the bugs mailing list