fixes #1809; implements overloading based on 'var T'

This commit is contained in:
Araq 2015-03-14 22:21:43 +01:00
commit d4bca58b7d
4 changed files with 149 additions and 154 deletions

View file

@ -211,7 +211,7 @@ proc isAssignable*(owner: PSym, n: PNode): TAssignableResult =
elif compareTypes(n.typ, n.sons[1].typ, dcEqIgnoreDistinct):
# types that are equal modulo distinction preserve l-value:
result = isAssignable(owner, n.sons[1])
of nkHiddenDeref, nkDerefExpr:
of nkHiddenDeref, nkDerefExpr, nkHiddenAddr:
result = arLValue
of nkObjUpConv, nkObjDownConv, nkCheckedFieldExpr:
result = isAssignable(owner, n.sons[0])
@ -221,6 +221,9 @@ proc isAssignable*(owner: PSym, n: PNode): TAssignableResult =
else:
discard
proc isLValue*(n: PNode): bool =
isAssignable(nil, n) in {arLValue, arLocalLValue}
proc matchNodeKinds*(p, n: PNode): bool =
# matches the parameter constraint 'p' against the concrete AST 'n'.
# Efficiency matters here.

View file

@ -191,21 +191,6 @@ proc complexDisambiguation(a, b: PType): int =
for i in 1 .. <a.len: x += a.sons[i].sumGeneric
for i in 1 .. <b.len: y += b.sons[i].sumGeneric
result = x - y
when false:
proc betterThan(a, b: PType): bool {.inline.} = a.sumGeneric > b.sumGeneric
if a.len > 1 and b.len > 1:
let aa = a.sons[1].sumGeneric
let bb = b.sons[1].sumGeneric
var a = a
var b = b
if aa < bb: swap(a, b)
# all must be better
for i in 2 .. <min(a.len, b.len):
if not a.sons[i].betterThan(b.sons[i]): return 0
# a must be longer or of the same length as b:
result = a.len - b.len
proc cmpCandidates*(a, b: TCandidate): int =
result = a.exactMatches - b.exactMatches
@ -1463,6 +1448,12 @@ proc matchesAux(c: PContext, n, nOrig: PNode,
else:
m.state = csNoMatch
return
if formal.typ.kind == tyVar:
if n.isLValue:
inc(m.genericMatches, 100)
else:
m.state = csNoMatch
return
var
# iterates over formal parameters

View file

@ -3,7 +3,6 @@ version 0.10.4
- improve GC-unsafety warnings
- make 'nil' work for 'add' and 'len'
- get rid of 'mget'; aka priority of 'var' needs to be 'var{lvalue}'
- disallow negative indexing
- improve the parser; deal with echo $foo gotcha
@ -59,9 +58,9 @@ Bugs
version 0.9.x
=============
- pragmas need 'bindSym' support
- allow simple read accesses to global variables --> difficult to ensure that
no data races happen
- pragmas need 'bindSym' support
- pragmas need re-work: 'push' is dangerous, 'hasPragma' does not work
reliably with user-defined pragmas
- memory manager: add a measure of fragmentation
@ -76,7 +75,6 @@ version 0.9.X
=============
- macros as type pragmas
- document how lazy overloading resolution works
- document NimMain and check whether it works for threading
GC

View file

@ -4,7 +4,7 @@ News
..
2015-03-01 Version 0.10.4 released
=================================
==================================
Changes affecting backwards compatibility
@ -54,6 +54,17 @@ News
takesBlock 1, 2, "some", 0.90, "random stuff":
echo "yay"
- Overloading by 'var T' is now finally possible:
.. code-block:: nim
proc varOrConst(x: var int) = echo "var"
proc varOrConst(x: int) = echo "const"
var x: int
varOrConst(x) # "var"
varOrConst(45) # "const"
Library additions
-----------------
@ -61,8 +72,8 @@ News
- Added multipart param to httpclient's ``post`` and ``postContent`` together
with a ``newMultipartData`` proc.
- Added `%*` operator for JSON.
- The compiler is now a Nimble package.
-
- The compiler is now available as Nimble package for c2nim.
Bugfixes
--------
@ -103,8 +114,7 @@ News
(`#1866 <https://github.com/Araq/Nim/issues/1866>`_).
- Fixed "Incorrect assembler generated"
(`#1907 <https://github.com/Araq/Nim/issues/1907>`_)
- Fixed "Expression templates that define macros are unusable in some contexts
"
- Fixed "Expression templates that define macros are unusable in some contexts"
(`#1903 <https://github.com/Araq/Nim/issues/1903>`_)
- Fixed "a second level generic subclass causes the compiler to crash"
(`#1919 <https://github.com/Araq/Nim/issues/1919>`_)
@ -116,8 +126,7 @@ News
(`#1838 <https://github.com/Araq/Nim/issues/1838>`_)
- Fixed "ICE with template"
(`#1915 <https://github.com/Araq/Nim/issues/1915>`_)
- Fixed "include the tool directory in the installer as it is required by koch
"
- Fixed "include the tool directory in the installer as it is required by koch"
(`#1947 <https://github.com/Araq/Nim/issues/1947>`_)
- Fixed "Can't compile if file location contains spaces on Windows"
(`#1955 <https://github.com/Araq/Nim/issues/1955>`_)
@ -151,14 +160,11 @@ News
(`#2039 <https://github.com/Araq/Nim/issues/2039>`_)
- Fixed "Macros fail to compile when compiled with --os:standalone"
(`#2041 <https://github.com/Araq/Nim/issues/2041>`_)
- Fixed "Reading from {.compileTime.} variables can cause code generation to f
ail"
- Fixed "Reading from {.compileTime.} variables can cause code generation to fail"
(`#2022 <https://github.com/Araq/Nim/issues/2022>`_)
- Fixed "Passing overloaded symbols to templates fails inside generic procedur
es"
- Fixed "Passing overloaded symbols to templates fails inside generic procedures"
(`#1988 <https://github.com/Araq/Nim/issues/1988>`_)
- Fixed "Compiling iterator with object assignment in release mode causes "var
not init""
- Fixed "Compiling iterator with object assignment in release mode causes "var not init""
(`#2023 <https://github.com/Araq/Nim/issues/2023>`_)
- Fixed "calling a large number of macros doing some computation fails"
(`#1989 <https://github.com/Araq/Nim/issues/1989>`_)
@ -196,8 +202,7 @@ es"
(`#2102 <https://github.com/Araq/Nim/issues/2102>`_)
- Fixed "hint[path] = off not working in nim.cfg"
(`#2103 <https://github.com/Araq/Nim/issues/2103>`_)
- Fixed "compiler crashes when getting a tuple from a sequence of generic tupl
es"
- Fixed "compiler crashes when getting a tuple from a sequence of generic tuples"
(`#2121 <https://github.com/Araq/Nim/issues/2121>`_)
- Fixed "nim check hangs with when"
(`#2123 <https://github.com/Araq/Nim/issues/2123>`_)
@ -225,13 +230,11 @@ es"
(`#2216 <https://github.com/Araq/Nim/issues/2216>`_)
- Fixed "--threads:on breaks async"
(`#2074 <https://github.com/Araq/Nim/issues/2074>`_)
- Fixed "Type mismatch not always caught, can generate bad code for C backend.
"
- Fixed "Type mismatch not always caught, can generate bad code for C backend."
(`#2169 <https://github.com/Araq/Nim/issues/2169>`_)
- Fixed "Failed C compilation when storing proc to own type in object"
(`#2233 <https://github.com/Araq/Nim/issues/2233>`_)
- Fixed "Unknown line/column number in constant declaration type conversion er
ror"
- Fixed "Unknown line/column number in constant declaration type conversion error"
(`#2252 <https://github.com/Araq/Nim/issues/2252>`_)
- Fixed "Adding {.compile.} fails if nimcache already exists."
(`#2247 <https://github.com/Araq/Nim/issues/2247>`_)