Deprecated the emulation of '__le' using '__lt'

As hinted in the manual for Lua 5.3, the emulation of the metamethod
for '__le' using '__le' has been deprecated. It is slow, complicates
the logic, and it is easy to avoid this emulation by defining a proper
'__le' function.

Moreover, often this emulation was used wrongly, with a programmer
assuming that an order is total when it is not (e.g., NaN in
floating-point numbers).
diff --git a/lstate.h b/lstate.h
index 2a36bd9..5461b29 100644
--- a/lstate.h
+++ b/lstate.h
@@ -138,9 +138,11 @@
 #define CIST_YPCALL	(1<<3)	/* call is a yieldable protected call */
 #define CIST_TAIL	(1<<4)	/* call was tail called */
 #define CIST_HOOKYIELD	(1<<5)	/* last hook called yielded */
-#define CIST_LEQ	(1<<6)  /* using __lt for __le */
-#define CIST_FIN	(1<<7)  /* call is running a finalizer */
-#define CIST_TRAN	(1<<8)	/* 'ci' has transfer information */
+#define CIST_FIN	(1<<6)  /* call is running a finalizer */
+#define CIST_TRAN	(1<<7)	/* 'ci' has transfer information */
+#if defined(LUA_COMPAT_LT_LE)
+#define CIST_LEQ	(1<<8)  /* using __lt for __le */
+#endif
 
 /* active function is a Lua function */
 #define isLua(ci)	(!((ci)->callstatus & CIST_C))
diff --git a/ltests.h b/ltests.h
index 54bc4f5..d44974a 100644
--- a/ltests.h
+++ b/ltests.h
@@ -13,6 +13,7 @@
 
 /* test Lua with compatibility code */
 #define LUA_COMPAT_MATHLIB
+#define LUA_COMPAT_LT_LE
 
 
 #define LUA_DEBUG
diff --git a/ltm.c b/ltm.c
index 1c1a18b..5c14818 100644
--- a/ltm.c
+++ b/ltm.c
@@ -188,6 +188,7 @@
                       TMS event) {
   if (callbinTM(L, p1, p2, L->top, event))  /* try original event */
     return !l_isfalse(s2v(L->top));
+#if defined(LUA_COMPAT_LT_LE)
   else if (event == TM_LE) {
       /* try '!(p2 < p1)' for '(p1 <= p2)' */
       L->ci->callstatus |= CIST_LEQ;  /* mark it is doing 'lt' for 'le' */
@@ -197,6 +198,7 @@
       }
       /* else error will remove this 'ci'; no need to clear mark */
   }
+#endif
   luaG_ordererror(L, p1, p2);  /* no metamethod found */
   return 0;  /* to avoid warnings */
 }
diff --git a/luaconf.h b/luaconf.h
index cc8e1bd..126257d 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -295,7 +295,7 @@
 */
 
 /*
-@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.2.
+@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3.
 ** You can define it to get all options, or change specific options
 ** to fit your specific needs.
 */
@@ -316,6 +316,12 @@
 */
 #define LUA_COMPAT_APIINTCASTS
 
+/*
+@@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod
+** using '__lt'.
+*/
+#define LUA_COMPAT_LT_LE
+
 #endif				/* } */
 
 
diff --git a/lvm.c b/lvm.c
index 7550dcc..add2694 100644
--- a/lvm.c
+++ b/lvm.c
@@ -754,10 +754,12 @@
     case OP_EQ: {  /* note that 'OP_EQI'/'OP_EQK' cannot yield */
       int res = !l_isfalse(s2v(L->top - 1));
       L->top--;
+#if defined(LUA_COMPAT_LT_LE)
       if (ci->callstatus & CIST_LEQ) {  /* "<=" using "<" instead? */
         ci->callstatus ^= CIST_LEQ;  /* clear mark */
         res = !res;  /* negate result */
       }
+#endif
       lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_JMP);
       if (res != GETARG_k(inst))  /* condition failed? */
         ci->u.l.savedpc++;  /* skip jump instruction */
diff --git a/manual/manual.of b/manual/manual.of
index 5a8b1b2..47a551b 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -474,17 +474,7 @@
 
 @item{@idx{__le}|
 the less equal (@T{<=}) operation.
-Unlike other operations,
-the less-equal operation can use two different events.
-First, Lua looks for the @idx{__le} metamethod in both operands,
-like in the less than operation.
-If it cannot find such a metamethod,
-then it will try the @idx{__lt} metamethod,
-assuming that @T{a <= b} is equivalent to @T{not (b < a)}.
-As with the other comparison operators,
-the result is always a boolean.
-(This use of the @idx{__lt} event can be removed in future versions;
-it is also slower than a real @idx{__le} metamethod.)
+Behavior similar to the less than operation.
 }
 
 @item{@idx{__index}|
@@ -1643,7 +1633,8 @@
 according to the usual rules of two-complement arithmetic.
 (In other words,
 they return the unique representable integer
-that is equal modulo @M{2@sp{64}} to the mathematical result.)
+that is equal modulo @M{2@sp{n}} to the mathematical result,
+where @M{n} is the number of bits of the integer type.)
 }
 
 @sect3{bitwise| @title{Bitwise Operators}
@@ -8537,6 +8528,12 @@
 not a float.
 }
 
+@item{
+The use of the @idx{__lt} metamethod to emulate @id{__le}
+has been removed.
+When needed, this metamethod must be explicitly defined.
+}
+
 }
 
 }
diff --git a/testes/coroutine.lua b/testes/coroutine.lua
index 182c1e1..36eae44 100644
--- a/testes/coroutine.lua
+++ b/testes/coroutine.lua
@@ -1,4 +1,4 @@
--- $Id: testes/coroutine.lua $
+-- $Id: testes/coroutine.lua 2018-07-25 15:31:04 -0300 $
 -- See Copyright Notice in file all.lua
 
 print "testing coroutines"
@@ -619,10 +619,8 @@
 
 assert(run(function () if (a>=b) then return '>=' else return '<' end end,
        {"le", "sub"}) == "<")
--- '<=' using '<'
-mt.__le = nil
 assert(run(function () if (a<=b) then return '<=' else return '>' end end,
-       {"lt"}) == "<=")
+       {"le", "sub"}) == "<=")
 assert(run(function () if (a==b) then return '==' else return '~=' end end,
        {"eq"}) == "~=")
 
@@ -677,7 +675,7 @@
       return val(a) < val(b)
     end,
   }
-  local mt2 = { __lt = mt1.__lt }   -- no __le
+  local mt2 = { __lt = mt1.__lt, __le = mt1.__le }
 
   local function run (f)
     local co = coroutine.wrap(f)
diff --git a/testes/events.lua b/testes/events.lua
index 21a822a..c4d43d5 100644
--- a/testes/events.lua
+++ b/testes/events.lua
@@ -1,4 +1,4 @@
--- $Id: testes/events.lua $
+-- $Id: testes/events.lua 2018-07-25 15:31:04 -0300 $
 -- See Copyright Notice in file all.lua
 
 print('testing metatables')
@@ -217,6 +217,13 @@
  return a<b, "dummy"
 end
 
+t.__le = function (a,b,c)
+  assert(c == nil)
+  if type(a) == 'table' then a = a.x end
+  if type(b) == 'table' then b = b.x end
+ return a<=b, "dummy"
+end
+
 function Op(x) return setmetatable({x=x}, t) end
 
 local function test ()
@@ -236,15 +243,6 @@
 
 test()
 
-t.__le = function (a,b,c)
-  assert(c == nil)
-  if type(a) == 'table' then a = a.x end
-  if type(b) == 'table' then b = b.x end
- return a<=b, "dummy"
-end
-
-test()  -- retest comparisons, now using both `lt' and `le'
-
 
 -- test `partial order'
 
@@ -266,14 +264,6 @@
   return next(b) ~= nil
 end
 
-t.__le = nil
-
-assert(Set{1,2,3} < Set{1,2,3,4})
-assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
-assert((Set{1,2,3,4} <= Set{1,2,3,4}))
-assert((Set{1,2,3,4} >= Set{1,2,3,4}))
-assert((Set{1,3} <= Set{3,5}))   -- wrong!! model needs a `le' method ;-)
-
 t.__le = function (a,b)
   for k in pairs(a) do
     if not b[k] then return false end
@@ -281,10 +271,15 @@
   return true
 end
 
-assert(not (Set{1,3} <= Set{3,5}))   -- now its OK!
+assert(Set{1,2,3} < Set{1,2,3,4})
+assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
+assert((Set{1,2,3,4} <= Set{1,2,3,4}))
+assert((Set{1,2,3,4} >= Set{1,2,3,4}))
+assert(not (Set{1,3} <= Set{3,5}))
 assert(not(Set{1,3} <= Set{3,5}))
 assert(not(Set{1,3} >= Set{3,5}))
 
+
 t.__eq = function (a,b)
   for k in pairs(a) do
     if not b[k] then return false end
@@ -376,6 +371,7 @@
 d = {}
 t1.__eq = function () return true end
 t1.__lt = function () return true end
+t1.__le = function () return false end
 setmetatable(d, t1)
 assert(c == d and c < d and not(d <= c))
 t2 = {}