From 8de053d8708d3547edeee8c4846b355a557f1d18 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Tue, 20 Apr 2021 22:00:45 -0700 Subject: [PATCH] fix `hintMsgOrigin` regression + simplify msgs code (#17805) --- compiler/msgs.nim | 5 +---- compiler/semdata.nim | 18 +++++++----------- compiler/semexprs.nim | 2 +- compiler/semmagic.nim | 8 ++++---- compiler/semobjconstr.nim | 13 ++++++------- compiler/semstmts.nim | 8 +++----- 6 files changed, 22 insertions(+), 32 deletions(-) diff --git a/compiler/msgs.nim b/compiler/msgs.nim index 30421fe6ab..89949ef177 100644 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -14,7 +14,7 @@ import std/private/miscdollars import strutils2 type InstantiationInfo* = typeof(instantiationInfo()) -template instLoc(): InstantiationInfo = instantiationInfo(-2, fullPaths = true) +template instLoc*(): InstantiationInfo = instantiationInfo(-2, fullPaths = true) template toStdOrrKind(stdOrr): untyped = if stdOrr == stdout: stdOrrStdout else: stdOrrStderr @@ -600,9 +600,6 @@ template localError*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") template localError*(conf: ConfigRef; info: TLineInfo, arg: string) = liMessage(conf, info, errGenerated, arg, doNothing, instLoc()) -template localError*(conf: ConfigRef; info: TLineInfo, format: string, params: openArray[string]) = - liMessage(conf, info, errGenerated, format % params, doNothing, instLoc()) - template message*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg = "") = liMessage(conf, info, msg, arg, doNothing, instLoc()) diff --git a/compiler/semdata.nim b/compiler/semdata.nim index 354d4587dd..95278893ad 100644 --- a/compiler/semdata.nim +++ b/compiler/semdata.nim @@ -501,18 +501,14 @@ proc errorNode*(c: PContext, n: PNode): PNode = result = newNodeI(nkEmpty, n.info) result.typ = errorType(c) -proc localErrorNode*(c: PContext, n: PNode, info: TLineInfo, msg: TMsgKind, arg: string): PNode = - localError(c.config, info, msg, arg) - result = errorNode(c, n) +template localErrorNode*(c: PContext, n: PNode, arg: string, info: TLineInfo, kind: TMsgKind = errGenerated): PNode = + liMessage(c.config, info, kind, arg, doNothing, instLoc()) + errorNode(c, n) -proc localErrorNode*(c: PContext, n: PNode, info: TLineInfo, arg: string): PNode = - localErrorNode(c, n, info, errGenerated, arg) - -proc localErrorNode*(c: PContext, n: PNode, msg: TMsgKind, arg: string): PNode = - localErrorNode(c, n, n.info, msg, arg) - -proc localErrorNode*(c: PContext, n: PNode, arg: string): PNode = - localErrorNode(c, n, n.info, errGenerated, arg) +template localErrorNode*(c: PContext, n: PNode, arg: string, kind: TMsgKind = errGenerated): PNode = + let n2 = n + liMessage(c.config, n2.info, kind, arg, doNothing, instLoc()) + errorNode(c, n2) proc fillTypeS*(dest: PType, kind: TTypeKind, c: PContext) = dest.kind = kind diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 3538317363..defceb291b 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -2620,7 +2620,7 @@ proc semTupleConstr(c: PContext, n: PNode, flags: TExprFlags): PNode = # check if either everything or nothing is tyTypeDesc for i in 1.. high(TSymChoiceRule).int: - return localErrorNode(c, n, n[2].info, errConstExprExpected) + return localErrorNode(c, n, errConstExprExpected, n[2].info) let id = newIdentNode(getIdent(c.cache, sl.strVal), n.info) let s = qualifiedLookUp(c, id, {checkUndeclared}) @@ -251,10 +251,10 @@ proc semBindSym(c: PContext, n: PNode): PNode = proc opBindSym(c: PContext, scope: PScope, n: PNode, isMixin: int, info: PNode): PNode = if n.kind notin {nkStrLit, nkRStrLit, nkTripleStrLit, nkIdent}: - return localErrorNode(c, n, info.info, errStringOrIdentNodeExpected) + return localErrorNode(c, n, errStringOrIdentNodeExpected, info.info) if isMixin < 0 or isMixin > high(TSymChoiceRule).int: - return localErrorNode(c, n, info.info, errConstExprExpected) + return localErrorNode(c, n, errConstExprExpected, info.info) let id = if n.kind == nkIdent: n else: newIdentNode(getIdent(c.cache, n.strVal), info.info) diff --git a/compiler/semobjconstr.nim b/compiler/semobjconstr.nim index 63b4b9a754..01d5a8b28a 100644 --- a/compiler/semobjconstr.nim +++ b/compiler/semobjconstr.nim @@ -210,7 +210,7 @@ proc semConstructFields(c: PContext, n: PNode, localError(c.config, constrCtx.initExpr.info, "a case selecting discriminator '$1' with value '$2' " & "appears in the object construction, but the field(s) $3 " & - "are in conflict with this value.", + "are in conflict with this value." % [discriminator.sym.name.s, discriminatorVal.renderTree, fields]) template valuesInConflictError(valsDiff) = @@ -365,11 +365,10 @@ proc defaultConstructionError(c: PContext, t: PType, info: TLineInfo) = assert constrCtx.missingFields.len > 0 localError(c.config, info, "The $1 type doesn't have a default value. The following fields must " & - "be initialized: $2.", - [typeToString(t), listSymbolNames(constrCtx.missingFields)]) + "be initialized: $2." % [typeToString(t), listSymbolNames(constrCtx.missingFields)]) elif objType.kind == tyDistinct: localError(c.config, info, - "The $1 distinct type doesn't have a default value.", [typeToString(t)]) + "The $1 distinct type doesn't have a default value." % typeToString(t)) else: assert false, "Must not enter here." @@ -379,7 +378,7 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode = for child in n: result.add child if t == nil: - return localErrorNode(c, result, errGenerated, "object constructor needs an object type") + return localErrorNode(c, result, "object constructor needs an object type") t = skipTypes(t, {tyGenericInst, tyAlias, tySink, tyOwned}) if t.kind == tyRef: @@ -390,7 +389,7 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode = # multiple times as long as they don't have closures. result.typ.flags.incl tfHasOwned if t.kind != tyObject: - return localErrorNode(c, result, errGenerated, "object constructor needs an object type") + return localErrorNode(c, result, "object constructor needs an object type") # Check if the object is fully initialized by recursively testing each # field (if this is a case object, initialized fields in two different @@ -404,7 +403,7 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode = if constrCtx.missingFields.len > 0: hasError = true localError(c.config, result.info, - "The $1 type requires the following fields to be initialized: $2.", + "The $1 type requires the following fields to be initialized: $2." % [t.sym.name.s, listSymbolNames(constrCtx.missingFields)]) # Since we were traversing the object fields, it's possible that diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index a100471873..22e4295fa8 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -725,7 +725,7 @@ proc semForVars(c: PContext, n: PNode; flags: TExprFlags): PNode = if n.len == 3: if n[0].kind == nkVarTuple: if n[0].len-1 != iterAfterVarLent.len: - return localErrorNode(c, n, n[0].info, errWrongNumberOfVariables) + return localErrorNode(c, n, errWrongNumberOfVariables, n[0].info) for i in 0.. 0 and not isException(typ.lastSon): - localError(c.config, n.info, "raised object of type $1 does not inherit from Exception", - [typeToString(typ)]) + localError(c.config, n.info, "raised object of type $1 does not inherit from Exception" % typeToString(typ)) proc addGenericParamListToScope(c: PContext, n: PNode) = if n.kind != nkGenericParams: illFormedAst(n, c.config) @@ -2201,8 +2200,7 @@ proc inferConceptStaticParam(c: PContext, inferred, n: PNode) = if not sameType(res.typ, typ.base): localError(c.config, n.info, "cannot infer the concept parameter '%s', due to a type mismatch. " & - "attempt to equate '%s' and '%s'.", - [inferred.renderTree, $res.typ, $typ.base]) + "attempt to equate '%s' and '%s'." % [inferred.renderTree, $res.typ, $typ.base]) typ.n = res proc semStmtList(c: PContext, n: PNode, flags: TExprFlags): PNode =