Skip to content

Commit

Permalink
Colours are nice
Browse files Browse the repository at this point in the history
  • Loading branch information
beef331 committed Dec 15, 2020
1 parent 1082d62 commit 94afba9
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# included from sem.nim

from algorithm import sort
import sets
from terminal import ForegroundColor

proc sameMethodDispatcher(a, b: PSym): bool =
result = false
Expand Down Expand Up @@ -160,6 +162,8 @@ proc renderNotLValue(n: PNode): string =
elif n.kind in {nkHiddenStdConv, nkHiddenSubConv} and n.len == 2:
result = typeToString(n.typ.skipTypes(abstractVar)) & "(" & result & ")"

proc color(str: string, color: ForegroundColor): string = "\x1b[" & $(color.ord) & "m" & str & "\x1b[0m"

proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
(TPreferedDesc, string) =
var prefer = preferName
Expand Down Expand Up @@ -198,6 +202,7 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
var candidatesAll: seq[string]
var candidates = ""
var skipped = 0
var immutSyms: HashSet[string] #All failed immutable params
for err in errors:
candidates.setLen 0
if filterOnlyFirst and err.firstMismatch.arg == 1:
Expand All @@ -208,23 +213,24 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
{renderNoBody, renderNoComments, renderNoPragmas}))
else:
candidates.add(getProcHeader(c.config, err.sym, prefer))
let immutMsgIndex = candidates.len
candidates.addDeclaredLocMaybe(c.config, err.sym)
candidates.add("\n")
let nArg = if err.firstMismatch.arg < n.len: n[err.firstMismatch.arg] else: nil
let nameParam = if err.firstMismatch.formal != nil: err.firstMismatch.formal.name.s else: ""
if n.len > 1:
candidates.add(" first type mismatch at position: " & $err.firstMismatch.arg)
candidates.add((" first type mismatch at position: " & $err.firstMismatch.arg).color(fgRed))
# candidates.add "\n reason: " & $err.firstMismatch.kind # for debugging
case err.firstMismatch.kind
of kUnknownNamedParam:
if nArg == nil:
candidates.add("\n unknown named parameter")
candidates.add("\n unknown named parameter".color(fgRed))
else:
candidates.add("\n unknown named parameter: " & $nArg[0])
of kAlreadyGiven: candidates.add("\n named param already provided: " & $nArg[0])
of kPositionalAlreadyGiven: candidates.add("\n positional param was already given as named param")
of kExtraArg: candidates.add("\n extra argument given")
of kMissingParam: candidates.add("\n missing parameter: " & nameParam)
candidates.add(("\n unknown named parameter: " & $nArg[0]).color(fgRed))
of kAlreadyGiven: candidates.add(("\n named param already provided: " & $nArg[0]).color(fgRed))
of kPositionalAlreadyGiven: candidates.add("\n positional param was already given as named param".color(fgRed))
of kExtraArg: candidates.add("\n extra argument given".color(fgRed))
of kMissingParam: candidates.add(("\n missing parameter: " & nameParam).color(fgRed))
of kTypeMismatch, kVarNeeded:
doAssert nArg != nil
var wanted = err.firstMismatch.formal.typ
Expand All @@ -234,25 +240,34 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
candidates.addDeclaredLocMaybe(c.config, wanted)
candidates.add "\n but expression '"
if err.firstMismatch.kind == kVarNeeded:
candidates.add renderNotLValue(nArg)
candidates.add "' is immutable, not 'var'"
candidates.add (renderNotLValue(nArg)).color(fgYellow)
immutSyms.incl renderNotLValue(nArg)
candidates.add "'" & " is immutable, not 'var'".color(fgRed)
else:
candidates.add renderTree(nArg)
candidates.add "' is of type: "
candidates.add renderTree(nArg).color(fgRed)
candidates.add "'" & " is of type: ".color(fgRed)
var got = nArg.typ
candidates.add typeToString(got)
candidates.add typeToString(got).color(fgRed)
candidates.addDeclaredLocMaybe(c.config, got)
doAssert wanted != nil
if got != nil: effectProblem(wanted, got, candidates, c)
of kUnknown: discard "do not break 'nim check'"
candidates.add "\n"
candidates.add "\n\n"
if err.firstMismatch.arg == 1 and nArg.kind == nkTupleConstr and
n.kind == nkCommand:
maybeWrongSpace = true
for diag in err.diagnostics:
candidates.add(diag & "\n")
candidatesAll.add candidates
candidatesAll.sort # fix #13538
if immutSyms.len > 0:
let wrappedSyms = block:
var res = ""
for sym in immutSyms:
if res.len > 0: res.add(", ")
res.add("`" & sym & "`")
res
candidatesAll.insert(("\nAtleast one of the mismatches is due to " & wrappedSyms & " being immutable.\n\n").color(fgYellow), 0)
candidates = join(candidatesAll)
if skipped > 0:
candidates.add($skipped & " other mismatching symbols have been " &
Expand All @@ -265,10 +280,10 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
const
errTypeMismatch = "type mismatch: got <"
errButExpected = "but expected one of: "
errUndeclaredField = "undeclared field: '$1'"
errUndeclaredRoutine = "attempting to call undeclared routine: '$1'"
errBadRoutine = "attempting to call routine: '$1'$2"
errAmbiguousCallXYZ = "ambiguous call; both $1 and $2 match for: $3"
errUndeclaredField = "undeclared field: '$1'".color(fgRed)
errUndeclaredRoutine = "attempting to call undeclared routine: '$1'".color(fgRed)
errBadRoutine = "attempting to call routine: '$1'$2".color(fgRed)
errAmbiguousCallXYZ = "ambiguous call; both $1 and $2 match for: $3".color(fgRed)

proc notFoundError*(c: PContext, n: PNode, errors: CandidateErrors) =
# Gives a detailed error message; this is separated from semOverloadedCall,
Expand Down

0 comments on commit 94afba9

Please sign in to comment.