Skip to content

Commit

Permalink
attempt to fix #7632 for generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Aug 6, 2018
1 parent baa7738 commit 2291bb1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion compiler/astalgo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ proc strTableContains*(t: TStrTable, n: PSym): bool =

proc strTableRawInsert(data: var TSymSeq, n: PSym) =
var h: Hash = n.name.h and high(data)
if sfImmediate notin n.flags:
if {sfImmediate, sfAllUntyped} * n.flags == {}:
# fast path:
while data[h] != nil:
if data[h] == n:
Expand Down
2 changes: 1 addition & 1 deletion compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ proc semOverloadedCall(c: PContext, n, nOrig: PNode,
var r = resolveOverloads(c, n, nOrig, filter, flags, errors, efExplain in flags)
if r.state == csMatch:
# this may be triggered, when the explain pragma is used
if errors.len > 0:
if errors.len > 0 and efExplain in flags:
let (_, candidates) = presentFailedCandidates(c, n, errors)
message(c.config, n.info, hintUserRaw,
"Non-matching candidates for " & renderTree(n) & "\n" &
Expand Down
6 changes: 4 additions & 2 deletions compiler/semgnrc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ proc semGenericStmt(c: PContext, n: PNode,
let sc = symChoice(c, fn, s, if s.isMixedIn: scForceOpen else: scOpen)
case s.kind
of skMacro:
if macroToExpand(s) and sc.safeLen <= 1:
# new feature: overloading by arity only in an 'immediate' expand
# context. XXX This needs to be documented.
if macroToExpand(s) and (sc.safeLen <= 1 or s.typ.len == n.len):
styleCheckUse(fn.info, s)
result = semMacroExpr(c, n, n, s, {efNoSemCheck})
result = semGenericStmt(c, result, flags, ctx)
Expand All @@ -238,7 +240,7 @@ proc semGenericStmt(c: PContext, n: PNode,
result = n
mixinContext = true
of skTemplate:
if macroToExpand(s) and sc.safeLen <= 1:
if macroToExpand(s) and (sc.safeLen <= 1 or s.typ.len == n.len):
styleCheckUse(fn.info, s)
result = semTemplateExpr(c, n, s, {efNoSemCheck})
result = semGenericStmt(c, result, flags, ctx)
Expand Down
4 changes: 2 additions & 2 deletions lib/pure/strformat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ template callFormatOption(res, arg, option) {.dirty.} =
else:
format($arg, option, res)

macro `&`*(pattern: string): untyped =
macro `&`*(pattern: untyped{lit}): untyped =
## For a specification of the ``&`` macro, see the module level documentation.
if pattern.kind notin {nnkStrLit..nnkTripleStrLit}:
error "string formatting (fmt(), &) only works with string literals", pattern
Expand Down Expand Up @@ -333,7 +333,7 @@ macro `&`*(pattern: string): untyped =
when defined(debugFmtDsl):
echo repr result

template fmt*(pattern: string): untyped =
template fmt*(pattern: untyped{lit}): untyped =
## An alias for ``&``.
bind `&`
&pattern
Expand Down
14 changes: 14 additions & 0 deletions tests/stdlib/mformat2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import strformat

proc fails*(a: static[int]) =
echo &"formatted {a:2}"

proc fails2*[N: static[int]](a: int) =
echo &"formatted {a:2}"

proc works*(a: int) =
echo &"formatted {a:2}"

proc fails0*(a: int or uint) =
echo &"formatted {a:2}"
17 changes: 17 additions & 0 deletions tests/stdlib/tformat2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
discard """
output: '''formatted 5
formatted 6
formatted 7
formatted 8'''
"""

# bug #7632
import mformat2

works(5)

fails0(6)

fails(7)

fails2[0](8)

0 comments on commit 2291bb1

Please sign in to comment.