[nem-bug] [Nemerle 0000639]: A compile-time calculus and code generation

feedback at nemerle.org feedback at nemerle.org
Wed Mar 29 21:33:20 CEST 2006


The following issue has been RESOLVED.
======================================================================
<http://nemerle.org/bugs/view.php?id=639> 
======================================================================
Reported By:                VladD2
Assigned To:                nazgul
======================================================================
Project:                    Nemerle
Issue ID:                   639
Category:                   Compiler
Reproducibility:            always
Severity:                   crash
Priority:                   normal
Status:                     resolved
Resolution:                 fixed
Fixed in Version:           
======================================================================
Date Submitted:             03-28-2006 02:27 CEST
Last Modified:              03-29-2006 21:33 CEST
======================================================================
Summary:                    A compile-time calculus and code generation
Description: 
If write macro like this:
using System;

macro CompileTimeFactorial(x : uint)
{
  def factorial = Math.Factorial(x);
  <[ $(factorial : ulong) ]>
}

public module Math
{
  public static Factorial(x : uint) : ulong
  {
    def Loop(acc : ulong, x : uint)
    {
      if (x <= 1) acc else Loop(acc * x, x - 1)
    };
    
    Loop (1UL, x)
  }
}

and try to use it like this:
WriteLine(CompileTimeFactorial(0));

The compiler generate such IL:
.method private hidebysig static void Main() cil managed
{
      .entrypoint
      .maxstack 1
      L_0000: ldc.i8 1
      L_0009: conv.u8 
      L_000a: call void [mscorlib]System.Console::WriteLine(int32)
      L_000f: ret 
}

That is in a fanction which take int32 parameter pass uint64 argument.
If run this code AV is expected.
======================================================================

----------------------------------------------------------------------
 nazgul - 03-28-06 21:39 
----------------------------------------------------------------------
This is strange, the literal expression created inside macro is properly
initialized as UInt64 literal. I don't know why overloading engine treat
it as int.

----------------------------------------------------------------------
 VladD2 - 03-28-06 22:04 
----------------------------------------------------------------------
The bug going out if result value (constant) is big.

For example:
WriteLine(CompileTimeFactorial(20));
work correctly.

Apparently overloading engine work with result constant value.

I avoid this probleb in the following way:
macro CompileTimeFactorial(x : uint)
{
  //
&http://nemerle.org/bugs/view.php?id=1055;&http://nemerle.org/bugs/view.php?id=1088;&http://nemerle.org/bugs/view.php?id=1080;&http://nemerle.org/bugs/view.php?id=1074;&http://nemerle.org/bugs/view.php?id=1077;&http://nemerle.org/bugs/view.php?id=1076;&http://nemerle.org/bugs/view.php?id=1077;&http://nemerle.org/bugs/view.php?id=1085;&http://nemerle.org/bugs/view.php?id=1080;&http://nemerle.org/bugs/view.php?id=1077;
&http://nemerle.org/bugs/view.php?id=1090;&http://nemerle.org/bugs/view.php?id=1080;&http://nemerle.org/bugs/view.php?id=1087;&http://nemerle.org/bugs/view.php?id=1086;&http://nemerle.org/bugs/view.php?id=1074;
(&http://nemerle.org/bugs/view.php?id=1074;&http://nemerle.org/bugs/view.php?id=1090;&http://nemerle.org/bugs/view.php?id=1086;&http://nemerle.org/bugs/view.php?id=1088;&http://nemerle.org/bugs/view.php?id=1086;&http://nemerle.org/bugs/view.php?id=1081;
 : ulong)
&http://nemerle.org/bugs/view.php?id=1090;&http://nemerle.org/bugs/view.php?id=1088;&http://nemerle.org/bugs/view.php?id=1077;&http://nemerle.org/bugs/view.php?id=1073;&http://nemerle.org/bugs/view.php?id=1091;&http://nemerle.org/bugs/view.php?id=1077;&http://nemerle.org/bugs/view.php?id=1090;&http://nemerle.org/bugs/view.php?id=1089;&http://nemerle.org/bugs/view.php?id=1103;
&http://nemerle.org/bugs/view.php?id=1080;&http://nemerle.org/bugs/view.php?id=1079;-&http://nemerle.org/bugs/view.php?id=1079;&http://nemerle.org/bugs/view.php?id=1072;
&http://nemerle.org/bugs/view.php?id=1086;&http://nemerle.org/bugs/view.php?id=1096;&http://nemerle.org/bugs/view.php?id=1080;&http://nemerle.org/bugs/view.php?id=1073;&http://nemerle.org/bugs/view.php?id=1082;&http://nemerle.org/bugs/view.php?id=1080;
&http://nemerle.org/bugs/view.php?id=1074;
&http://nemerle.org/bugs/view.php?id=1082;&http://nemerle.org/bugs/view.php?id=1086;&http://nemerle.org/bugs/view.php?id=1084;&http://nemerle.org/bugs/view.php?id=1087;&http://nemerle.org/bugs/view.php?id=1080;&http://nemerle.org/bugs/view.php?id=1083;&http://nemerle.org/bugs/view.php?id=1103;&http://nemerle.org/bugs/view.php?id=1090;&http://nemerle.org/bugs/view.php?id=1086;&http://nemerle.org/bugs/view.php?id=1088;&http://nemerle.org/bugs/view.php?id=1077;
:(
  <[ ($(Math.Factorial(x) : ulong) : ulong) ]>
}

----------------------------------------------------------------------
 VladD2 - 03-28-06 22:06 
----------------------------------------------------------------------
Sorry, I forget to remove russion comment.
macro CompileTimeFactorial(x : uint)
{
  <[ ($(Math.Factorial(x) : ulong) : ulong) ]>
}

----------------------------------------------------------------------
 nazgul - 03-28-06 22:18 
----------------------------------------------------------------------
The reason is that macro created literal is just  (1 : ulong), and parser
created one is ((1 :> ulong) : ulong)... Later for macro created one the
function
 static internal LiteralConversionPossible (lit : Literal, target : MType)
: bool

is successfully marking (1 : ulong) as convertible to int, which is wrong,
but I'm a little afraid that some place of overloading engine relies on
this 'feature'.

I will try fixing LiteralConversionPossible function to do not allow
conversion if we know the exact type of literal.

----------------------------------------------------------------------
 nazgul - 03-29-06 21:33 
----------------------------------------------------------------------
This hack was used in many places of compiler, so I just changed the way
quotations are translated...

Fixed on trunk (r6167)

Issue History
Date Modified  Username       Field                    Change              
======================================================================
03-28-06 02:27 VladD2         New Issue                                    
03-28-06 21:39 nazgul         Note Added: 0001127                          
03-28-06 22:04 VladD2         Note Added: 0001128                          
03-28-06 22:06 VladD2         Note Added: 0001129                          
03-28-06 22:18 nazgul         Note Added: 0001130                          
03-29-06 21:33 nazgul         Status                   new => resolved     
03-29-06 21:33 nazgul         Resolution               open => fixed       
03-29-06 21:33 nazgul         Assigned To               => nazgul          
03-29-06 21:33 nazgul         Note Added: 0001131                          
======================================================================




More information about the bugs mailing list