From b5171f57ef00bffb12387d7daf3487c5e07645f9 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sat, 18 Aug 2018 15:27:47 +0200 Subject: Expand tyAlias in typeToString (#8634) Fixes #8339 --- compiler/types.nim | 2 ++ 1 file changed, 2 insertions(+) (limited to 'compiler') diff --git a/compiler/types.nim b/compiler/types.nim index 80624502c..d065ae29a 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -426,6 +426,8 @@ proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string = sfAnon notin t.sym.flags: if t.kind == tyInt and isIntLit(t): result = t.sym.name.s & " literal(" & $t.n.intVal & ")" + elif t.kind == tyAlias: + result = typeToString(t.sons[0]) elif prefer in {preferName, preferTypeName} or t.sym.owner.isNil: result = t.sym.name.s if t.kind == tyGenericParam and t.sonsLen > 0: -- cgit 1.4.1-2-gfad0 From 2def61606912625f3d20076e6686235996f25e62 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Mon, 20 Aug 2018 11:54:18 +0200 Subject: exploit nil seqs for performance (#8688) * changes $ for seqs to never produce 'nil' * exploit the fact that empty seqs don't have to allocate in the code generator --- compiler/ccgexprs.nim | 21 ++++++++++++++------- compiler/ccgtrav.nim | 2 +- lib/pure/xmltree.nim | 1 - lib/system.nim | 5 +---- tests/parallel/tsendtwice.nim | 12 ++++++------ tests/system/tostring.nim | 2 +- 6 files changed, 23 insertions(+), 20 deletions(-) (limited to 'compiler') diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 9ec034f67..b30d216f2 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -1176,7 +1176,7 @@ proc genNew(p: BProc, e: PNode) = rawGenNew(p, a, nil) gcUsage(p.config, e) -proc genNewSeqAux(p: BProc, dest: TLoc, length: Rope) = +proc genNewSeqAux(p: BProc, dest: TLoc, length: Rope; lenIsZero: bool) = let seqtype = skipTypes(dest.t, abstractVarRange) let args = [getTypeDesc(p.module, seqtype), genTypeInfo(p.module, seqtype, dest.lode.info), length] @@ -1187,17 +1187,23 @@ proc genNewSeqAux(p: BProc, dest: TLoc, length: Rope) = linefmt(p, cpsStmts, "if ($1) { #nimGCunrefRC1($1); $1 = NIM_NIL; }$n", dest.rdLoc) else: linefmt(p, cpsStmts, "if ($1) { #nimGCunrefNoCycle($1); $1 = NIM_NIL; }$n", dest.rdLoc) - call.r = ropecg(p.module, "($1) #newSeqRC1($2, $3)", args) - linefmt(p, cpsStmts, "$1 = $2;$n", dest.rdLoc, call.rdLoc) + if not lenIsZero: + call.r = ropecg(p.module, "($1) #newSeqRC1($2, $3)", args) + linefmt(p, cpsStmts, "$1 = $2;$n", dest.rdLoc, call.rdLoc) else: - call.r = ropecg(p.module, "($1) #newSeq($2, $3)", args) + if lenIsZero: + call.r = rope"NIM_NIL" + else: + call.r = ropecg(p.module, "($1) #newSeq($2, $3)", args) genAssignment(p, dest, call, {}) proc genNewSeq(p: BProc, e: PNode) = var a, b: TLoc initLocExpr(p, e.sons[1], a) initLocExpr(p, e.sons[2], b) - genNewSeqAux(p, a, b.rdLoc) + let lenIsZero = optNilSeqs notin p.options and + e[2].kind == nkIntLit and e[2].intVal == 0 + genNewSeqAux(p, a, b.rdLoc, lenIsZero) gcUsage(p.config, e) proc genNewSeqOfCap(p: BProc; e: PNode; d: var TLoc) = @@ -1297,7 +1303,8 @@ proc genSeqConstr(p: BProc, n: PNode, d: var TLoc) = elif d.k == locNone: getTemp(p, n.typ, d) # generate call to newSeq before adding the elements per hand: - genNewSeqAux(p, dest[], intLiteral(sonsLen(n))) + genNewSeqAux(p, dest[], intLiteral(sonsLen(n)), + optNilSeqs notin p.options and n.len == 0) for i in countup(0, sonsLen(n) - 1): initLoc(arr, locExpr, n[i], OnHeap) arr.r = ropecg(p.module, "$1$3[$2]", rdLoc(dest[]), intLiteral(i), dataField(p)) @@ -1320,7 +1327,7 @@ proc genArrToSeq(p: BProc, n: PNode, d: var TLoc) = getTemp(p, n.typ, d) # generate call to newSeq before adding the elements per hand: let L = int(lengthOrd(p.config, n.sons[1].typ)) - genNewSeqAux(p, d, intLiteral(L)) + genNewSeqAux(p, d, intLiteral(L), optNilSeqs notin p.options and L == 0) initLocExpr(p, n.sons[1], a) # bug #5007; do not produce excessive C source code: if L < 10: diff --git a/compiler/ccgtrav.nim b/compiler/ccgtrav.nim index e74d5a953..349cf2707 100644 --- a/compiler/ccgtrav.nim +++ b/compiler/ccgtrav.nim @@ -107,7 +107,7 @@ proc genTraverseProcSeq(c: TTraversalClosure, accessor: Rope, typ: PType) = var i: TLoc getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo(), tyInt), i) let oldCode = p.s(cpsStmts) - lineF(p, cpsStmts, "for ($1 = 0; $1 < $2->$3; $1++) {$n", + lineF(p, cpsStmts, "for ($1 = 0; $1 < ($2 ? $2->$3 : 0); $1++) {$n", [i.r, accessor, lenField(c.p)]) let oldLen = p.s(cpsStmts).len genTraverseProc(c, "$1$3[$2]" % [accessor, i.r, dataField(c.p)], typ.sons[0]) diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim index 47658b59b..d536cfed0 100644 --- a/lib/pure/xmltree.nim +++ b/lib/pure/xmltree.nim @@ -377,7 +377,6 @@ proc findAll*(n: XmlNode, tag: string, result: var seq[XmlNode]) = ## findAll(html, "img", tags) ## for imgTag in tags: ## process(imgTag) - assert isNil(result) == false assert n.k == xnElement for child in n.items(): if child.k != xnElement: diff --git a/lib/system.nim b/lib/system.nim index 001eb19de..d61924a5b 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2530,10 +2530,7 @@ proc `$`*[T](x: seq[T]): string = ## ## .. code-block:: nim ## $(@[23, 45]) == "@[23, 45]" - if x.isNil: - "nil" - else: - collectionToString(x, "@[", ", ", "]") + collectionToString(x, "@[", ", ", "]") # ----------------- GC interface --------------------------------------------- diff --git a/tests/parallel/tsendtwice.nim b/tests/parallel/tsendtwice.nim index 0700fc4da..0c923177a 100644 --- a/tests/parallel/tsendtwice.nim +++ b/tests/parallel/tsendtwice.nim @@ -1,11 +1,11 @@ discard """ - output: '''obj2 nil -obj nil -obj3 nil + output: '''obj2 @[] +obj @[] +obj3 @[] 3 -obj2 nil -obj nil -obj3 nil''' +obj2 @[] +obj @[] +obj3 @[]''' cmd: "nim c -r --threads:on $file" """ diff --git a/tests/system/tostring.nim b/tests/system/tostring.nim index 99299f203..42c07c0a4 100644 --- a/tests/system/tostring.nim +++ b/tests/system/tostring.nim @@ -24,7 +24,7 @@ doAssert "nan" == $(0.0/0.0) # nil tests # maybe a bit inconsistent in types var x: seq[string] -doAssert "nil" == $(x) +doAssert "@[]" == $(x) var y: string doAssert "" == $(y) -- cgit 1.4.1-2-gfad0 From b28c7d434b16ebd9cc33ef1d6b267b49660153ba Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 20 Aug 2018 16:54:13 +0200 Subject: Update all the default parameters after an instantiation (#8689) The old implementation relied on the (now?) wrong assumption that default-valued parameters can only be placed after the required ones. Fixes #8683 --- compiler/semcall.nim | 11 +++++------ tests/proc/t8683.nim | 11 +++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 tests/proc/t8683.nim (limited to 'compiler') diff --git a/compiler/semcall.nim b/compiler/semcall.nim index ef452fcdc..dc71f2567 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -399,12 +399,11 @@ proc updateDefaultParams(call: PNode) = # the default params with `nfDefaultParam` and `instantiateProcType` # computes correctly the default values for each instantiation. let calleeParams = call[0].sym.typ.n - for i in countdown(call.len - 1, 1): - if nfDefaultParam notin call[i].flags: - return - let def = calleeParams[i].sym.ast - if nfDefaultRefsParam in def.flags: call.flags.incl nfDefaultRefsParam - call[i] = def + for i in 1.. Date: Tue, 21 Aug 2018 14:51:23 +0200 Subject: sfForward is also sfReorder for skModule symbols (#8692) Take this into account while searching for undefined forward references. Fixes #8665 --- compiler/lookups.nim | 2 +- tests/modules/t8665.nim | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 tests/modules/t8665.nim (limited to 'compiler') diff --git a/compiler/lookups.nim b/compiler/lookups.nim index 1e9d963fa..1b5bee008 100644 --- a/compiler/lookups.nim +++ b/compiler/lookups.nim @@ -161,7 +161,7 @@ proc ensureNoMissingOrUnusedSymbols(c: PContext; scope: PScope) = var s = initTabIter(it, scope.symbols) var missingImpls = 0 while s != nil: - if sfForward in s.flags and s.kind != skType: + if sfForward in s.flags and s.kind notin {skType, skModule}: # too many 'implementation of X' errors are annoying # and slow 'suggest' down: if missingImpls == 0: diff --git a/tests/modules/t8665.nim b/tests/modules/t8665.nim new file mode 100644 index 000000000..51538df79 --- /dev/null +++ b/tests/modules/t8665.nim @@ -0,0 +1 @@ +import treorder -- cgit 1.4.1-2-gfad0 From cf20c4460c5b495a97b3f1f1d091f77832b89384 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 21 Aug 2018 15:07:44 +0200 Subject: More robust handling of deprecated pragmas (#8696) Prevent `deprecated` annotations to "slip" up to the parent module and warn about unsupported annotations. Accidentally fixes #7867 --- compiler/pragmas.nim | 10 ++++++++-- compiler/suggest.nim | 23 ++++++++++++++++------- tests/deprecated/importme.nim | 10 ++++++++++ tests/deprecated/tmodule1.nim | 23 +++++++++++++++++++++++ tests/deprecated/tnoannot.nim | 7 +++++++ 5 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 tests/deprecated/importme.nim create mode 100644 tests/deprecated/tmodule1.nim create mode 100644 tests/deprecated/tnoannot.nim (limited to 'compiler') diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index a067f2074..263068344 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -868,11 +868,17 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int, of wExplain: sym.flags.incl sfExplain of wDeprecated: - if sym != nil and sym.kind in routineKinds: + if sym != nil and sym.kind in routineKinds + {skType}: if it.kind in nkPragmaCallKinds: discard getStrLitNode(c, it) incl(sym.flags, sfDeprecated) + elif sym != nil and sym.kind != skModule: + # We don't support the extra annotation field + if it.kind in nkPragmaCallKinds: + localError(c.config, it.info, "annotation to deprecated not supported here") + incl(sym.flags, sfDeprecated) + # At this point we're quite sure this is a statement and applies to the + # whole module elif it.kind in nkPragmaCallKinds: deprecatedStmt(c, it) - elif sym != nil: incl(sym.flags, sfDeprecated) else: incl(c.module.flags, sfDeprecated) of wVarargs: noVal(c, it) diff --git a/compiler/suggest.nim b/compiler/suggest.nim index a21d64338..b52632c67 100644 --- a/compiler/suggest.nim +++ b/compiler/suggest.nim @@ -454,14 +454,23 @@ proc suggestSym*(conf: ConfigRef; info: TLineInfo; s: PSym; usageSym: var PSym; suggestResult(conf, symToSuggest(conf, s, isLocal=false, ideOutline, info, 100, PrefixMatch.None, false, 0)) proc warnAboutDeprecated(conf: ConfigRef; info: TLineInfo; s: PSym) = + var pragmaNode: PNode + if s.kind in routineKinds: - let n = s.ast[pragmasPos] - if n.kind != nkEmpty: - for it in n: - if whichPragma(it) == wDeprecated and it.safeLen == 2 and - it[1].kind in {nkStrLit..nkTripleStrLit}: - message(conf, info, warnDeprecated, it[1].strVal & "; " & s.name.s) - return + pragmaNode = s.ast[pragmasPos] + elif s.kind in {skType}: + # s.ast = nkTypedef / nkPragmaExpr / [nkSym, nkPragma] + pragmaNode = s.ast[0][1] + + doAssert pragmaNode == nil or pragmaNode.kind == nkPragma + + if pragmaNode != nil: + for it in pragmaNode: + if whichPragma(it) == wDeprecated and it.safeLen == 2 and + it[1].kind in {nkStrLit..nkTripleStrLit}: + message(conf, info, warnDeprecated, it[1].strVal & "; " & s.name.s) + return + message(conf, info, warnDeprecated, s.name.s) proc markUsed(conf: ConfigRef; info: TLineInfo; s: PSym; usageSym: var PSym) = diff --git a/tests/deprecated/importme.nim b/tests/deprecated/importme.nim new file mode 100644 index 000000000..0a9c6e37d --- /dev/null +++ b/tests/deprecated/importme.nim @@ -0,0 +1,10 @@ +type + Ty* {.deprecated.} = uint32 + Ty1* {.deprecated: "hello".} = uint32 + +var aVar* {.deprecated.}: char + +proc aProc*() {.deprecated.} = discard +proc aProc1*() {.deprecated: "hello".} = discard + +{.deprecated: "goodbye".} diff --git a/tests/deprecated/tmodule1.nim b/tests/deprecated/tmodule1.nim new file mode 100644 index 000000000..954836889 --- /dev/null +++ b/tests/deprecated/tmodule1.nim @@ -0,0 +1,23 @@ +discard """ + nimout: '''tmodule1.nim(11, 8) Warning: goodbye; importme is deprecated [Deprecated] +tmodule1.nim(14, 10) Warning: Ty is deprecated [Deprecated] +tmodule1.nim(17, 10) Warning: hello; Ty1 is deprecated [Deprecated] +tmodule1.nim(20, 8) Warning: aVar is deprecated [Deprecated] +tmodule1.nim(22, 3) Warning: aProc is deprecated [Deprecated] +tmodule1.nim(23, 3) Warning: hello; aProc1 is deprecated [Deprecated] +''' +""" + +import importme + +block: + var z: Ty + z = 0 +block: + var z: Ty1 + z = 0 +block: + echo aVar +block: + aProc() + aProc1() diff --git a/tests/deprecated/tnoannot.nim b/tests/deprecated/tnoannot.nim new file mode 100644 index 000000000..9cc5c7e06 --- /dev/null +++ b/tests/deprecated/tnoannot.nim @@ -0,0 +1,7 @@ +discard """ + line: 7 + errormsg: "annotation to deprecated not supported here" +""" + +var foo* {.deprecated.} = 42 +var foo1* {.deprecated: "no".} = 42 -- cgit 1.4.1-2-gfad0 From 96de224a63f44e7e5a40004b490519f895d1068d Mon Sep 17 00:00:00 2001 From: zah Date: Tue, 21 Aug 2018 23:14:12 +0300 Subject: Fix #8126 (#8712) This is a temporary fix that will be reworked in a follow up commit that aims to eliminate the tfExplicit flag from the compiler. The complete and proper fix was considered too risky for inclusion just before our 0.19 release. --- compiler/ast.nim | 1 + compiler/semtypes.nim | 2 +- compiler/sigmatch.nim | 3 ++- tests/concepts/tmonoid.nim | 12 ++++++++++++ tests/parser/ttypeclasses.nim | 4 ++++ 5 files changed, 20 insertions(+), 2 deletions(-) (limited to 'compiler') diff --git a/compiler/ast.nim b/compiler/ast.nim index 01e70ce75..21e129d06 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -506,6 +506,7 @@ type tfGenericTypeParam tfImplicitTypeParam tfInferrableStatic + tfConceptMatchedTypeSym tfExplicit # for typedescs, marks types explicitly prefixed with the # `type` operator (e.g. type int) tfWildcard # consider a proc like foo[T, I](x: Type[T, I]) diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 99f2cf20d..0a1e14236 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -1342,7 +1342,7 @@ proc semTypeClass(c: PContext, n: PNode, prev: PType): PType = if modifier != tyNone: dummyName = param[0] dummyType = c.makeTypeWithModifier(modifier, candidateTypeSlot) - if modifier == tyTypeDesc: dummyType.flags.incl tfExplicit + if modifier == tyTypeDesc: dummyType.flags.incl tfConceptMatchedTypeSym else: dummyName = param dummyType = candidateTypeSlot diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index f206119ec..932163055 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -993,7 +993,8 @@ proc typeRelImpl(c: var TCandidate, f, aOrig: PType, useTypeLoweringRuleInTypeClass = c.c.matchedConcept != nil and not c.isNoCall and f.kind != tyTypeDesc and - tfExplicit notin aOrig.flags + tfExplicit notin aOrig.flags and + tfConceptMatchedTypeSym notin aOrig.flags aOrig = if useTypeLoweringRuleInTypeClass: aOrig.skipTypes({tyTypeDesc}) diff --git a/tests/concepts/tmonoid.nim b/tests/concepts/tmonoid.nim index 49b3239bd..e0e19adbc 100644 --- a/tests/concepts/tmonoid.nim +++ b/tests/concepts/tmonoid.nim @@ -11,3 +11,15 @@ type Monoid = concept x, y proc z(x: typedesc[int]): int = 0 echo(int is Monoid) + +# https://github.com/nim-lang/Nim/issues/8126 +type AdditiveMonoid* = concept x, y, type T + x + y is T + + # some redundant checks to test an alternative approaches: + type TT = type(x) + x + y is type(x) + x + y is TT + +doAssert(1 is AdditiveMonoid) + diff --git a/tests/parser/ttypeclasses.nim b/tests/parser/ttypeclasses.nim index 46fd20686..06146dcb6 100644 --- a/tests/parser/ttypeclasses.nim +++ b/tests/parser/ttypeclasses.nim @@ -40,3 +40,7 @@ static: assert y isnot tuple assert z isnot seq + # XXX: These cases don't work properly at the moment: + # assert type[int] isnot int + # assert type(int) isnot int + -- cgit 1.4.1-2-gfad0 From e66d9c93812fcea6d94b7673a8f965c8f4e41398 Mon Sep 17 00:00:00 2001 From: Iván Montes Date: Tue, 21 Aug 2018 22:17:19 +0200 Subject: Cosmetic: fix typo on TRIPLESTR_LIT (#8663) --- compiler/lexer.nim | 2 +- compiler/parser.nim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'compiler') diff --git a/compiler/lexer.nim b/compiler/lexer.nim index 9727d16a7..6ad1d9fc6 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -1209,7 +1209,7 @@ proc rawGetTok*(L: var TLexer, tok: var TToken) = tok.tokType = tkInvalid lexMessage(L, errGenerated, "invalid token: " & c & " (\\" & $(ord(c)) & ')') of '\"': - # check for extended raw string literal: + # check for generalized raw string literal: var rawMode = L.bufpos > 0 and L.buf[L.bufpos-1] in SymChars getString(L, tok, rawMode) if rawMode: diff --git a/compiler/parser.nim b/compiler/parser.nim index 98ccb05b9..246dcb814 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -1638,7 +1638,7 @@ proc parseStaticOrDefer(p: var TParser; k: TNodeKind): PNode = addSon(result, parseStmt(p)) proc parseAsm(p: var TParser): PNode = - #| asmStmt = 'asm' pragma? (STR_LIT | RSTR_LIT | TRIPLE_STR_LIT) + #| asmStmt = 'asm' pragma? (STR_LIT | RSTR_LIT | TRIPLESTR_LIT) result = newNodeP(nkAsmStmt, p) getTokNoInd(p) if p.tok.tokType == tkCurlyDotLe: addSon(result, parsePragma(p)) -- cgit 1.4.1-2-gfad0 From a87341775aa424f252e9e17d58119b0758b58693 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 22 Aug 2018 09:40:31 +0200 Subject: Don't consider tyAnd/tyNot/tyOr/tyAnything as generic (#8700) * Don't consider tyAnd/tyNot/tyOr/tyAnything as generic `containsGenericType` was too shallow and didn't check all the branches. The resulting half-processed nodes are often simplified by the constant folding pass but when that's not possible we get a nasty error during codegen. Fixes #8693 * Move the blame onto the semFold pass Slightly better evaluation of `is` forms. --- compiler/semexprs.nim | 5 +++++ compiler/semfold.nim | 46 +++++++++++++++++++++++++++++----------------- tests/magics/t8693.nim | 29 +++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 tests/magics/t8693.nim (limited to 'compiler') diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index c9f9eb33f..91bee54ac 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -352,6 +352,11 @@ proc isOpImpl(c: PContext, n: PNode, flags: TExprFlags): PNode = res = t.kind == tyProc and t.callConv == ccClosure and tfIterator notin t.flags + of "iterator": + let t = skipTypes(t1, abstractRange) + res = t.kind == tyProc and + t.callConv == ccClosure and + tfIterator in t.flags else: res = false else: diff --git a/compiler/semfold.nim b/compiler/semfold.nim index a6c185fdc..0a33fea7a 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -173,32 +173,41 @@ proc makeRangeF(typ: PType, first, last: BiggestFloat; g: ModuleGraph): PType = result.n = n addSonSkipIntLit(result, skipTypes(typ, {tyRange})) -proc evalIs(n, a: PNode): PNode = +proc evalIs(n: PNode, lhs: PSym, g: ModuleGraph): PNode = # XXX: This should use the standard isOpImpl - #internalAssert a.kind == nkSym and a.sym.kind == skType - #internalAssert n.sonsLen == 3 and - # n[2].kind in {nkStrLit..nkTripleStrLit, nkType} + internalAssert g.config, + n.sonsLen == 3 and + lhs.typ != nil and + n[2].kind in {nkStrLit..nkTripleStrLit, nkType} - let t1 = a.sym.typ + var + res = false + t1 = lhs.typ + t2 = n[2].typ + + if t1.kind == tyTypeDesc and t2.kind != tyTypeDesc: + t1 = t1.base if n[2].kind in {nkStrLit..nkTripleStrLit}: case n[2].strVal.normalize of "closure": let t = skipTypes(t1, abstractRange) - result = newIntNode(nkIntLit, ord(t.kind == tyProc and - t.callConv == ccClosure and - tfIterator notin t.flags)) + res = t.kind == tyProc and + t.callConv == ccClosure and + tfIterator notin t.flags of "iterator": let t = skipTypes(t1, abstractRange) - result = newIntNode(nkIntLit, ord(t.kind == tyProc and - t.callConv == ccClosure and - tfIterator in t.flags)) - else: discard + res = t.kind == tyProc and + t.callConv == ccClosure and + tfIterator in t.flags + else: + res = false else: # XXX semexprs.isOpImpl is slightly different and requires a context. yay. let t2 = n[2].typ - var match = sameType(t1, t2) - result = newIntNode(nkIntLit, ord(match)) + res = sameType(t1, t2) + + result = newIntNode(nkIntLit, ord(res)) result.typ = n.typ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode = @@ -584,6 +593,9 @@ proc getConstExpr(m: PSym, n: PNode; g: ModuleGraph): PNode = result = copyTree(s.ast) of skProc, skFunc, skMethod: result = n + of skParam: + if s.typ != nil and s.typ.kind == tyTypeDesc: + result = newSymNodeTypeDesc(s, n.info) of skType: # XXX gensym'ed symbols can come here and cannot be resolved. This is # dirty, but correct. @@ -651,9 +663,9 @@ proc getConstExpr(m: PSym, n: PNode; g: ModuleGraph): PNode = of mConStrStr: result = foldConStrStr(m, n, g) of mIs: - let a = getConstExpr(m, n[1], g) - if a != nil and a.kind == nkSym and a.sym.kind == skType: - result = evalIs(n, a) + let lhs = getConstExpr(m, n[1], g) + if lhs != nil and lhs.kind == nkSym: + result = evalIs(n, lhs.sym, g) else: result = magicCall(m, n, g) except OverflowError: diff --git a/tests/magics/t8693.nim b/tests/magics/t8693.nim new file mode 100644 index 000000000..554244de4 --- /dev/null +++ b/tests/magics/t8693.nim @@ -0,0 +1,29 @@ +discard """ + output: '''true +false +true +false +false +true +true +false +true +true +''' +""" + +type Foo = int | float + +proc bar(t1, t2: typedesc): bool = + echo (t1 is t2) + (t2 is t1) + +proc bar[T](x: T, t2: typedesc): bool = + echo (T is t2) + (t2 is T) + +echo bar(int, Foo) +echo bar(4, Foo) +echo bar(any, int) +echo bar(int, any) +echo bar(Foo, Foo) -- cgit 1.4.1-2-gfad0 From d0f4a929e096a4924b6af8be702313cc5a5a9f5b Mon Sep 17 00:00:00 2001 From: cooldome Date: Wed, 22 Aug 2018 12:16:18 +0200 Subject: fixes 8594 (#8721) --- compiler/rodutils.nim | 4 ++-- lib/pure/math.nim | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'compiler') diff --git a/compiler/rodutils.nim b/compiler/rodutils.nim index a774cdba7..90431999a 100644 --- a/compiler/rodutils.nim +++ b/compiler/rodutils.nim @@ -10,8 +10,8 @@ ## Serialization utilities for the compiler. import strutils, math -# MSVC prior to 2013 doesn't have C99 functions -when defined(windows) and (defined(vcc) or defined(bcc)): +# bcc on windows doesn't have C99 functions +when defined(windows) and defined(bcc): {.emit: """#if defined(_MSC_VER) && _MSC_VER < 1900 #include static int c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) { diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 79f287651..bc804eb86 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -265,7 +265,7 @@ proc arcsech*[T: float32|float64](x: T): T = arccosh(1.0 / x) proc arccsch*[T: float32|float64](x: T): T = arcsinh(1.0 / x) ## Computes the inverse hyperbolic cosecant of `x` -const windowsCC89 = defined(windows) and (defined(vcc) or defined(bcc)) +const windowsCC89 = defined(windows) and defined(bcc) when not defined(JS): # C proc hypot*(x, y: float32): float32 {.importc: "hypotf", header: "".} -- cgit 1.4.1-2-gfad0 From d94ee75d1cc4ce7caad014944f943cdfbae49715 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 22 Aug 2018 15:49:43 +0200 Subject: Pick skParam symbols when resolving type idents (#8720) Fixes #8357 --- compiler/semtypes.nim | 2 +- tests/proc/t8357.nim | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/proc/t8357.nim (limited to 'compiler') diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 0a1e14236..dd1e96bde 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -369,7 +369,7 @@ proc semTypeIdent(c: PContext, n: PNode): PSym = if n.kind == nkSym: result = getGenSym(c, n.sym) else: - result = pickSym(c, n, {skType, skGenericParam}) + result = pickSym(c, n, {skType, skGenericParam, skParam}) if result.isNil: result = qualifiedLookUp(c, n, {checkAmbiguity, checkUndeclared}) if result != nil: diff --git a/tests/proc/t8357.nim b/tests/proc/t8357.nim new file mode 100644 index 000000000..350ebe356 --- /dev/null +++ b/tests/proc/t8357.nim @@ -0,0 +1,10 @@ +discard """ + output: "Hello" +""" + +type + T = ref int + +let r = new(string) +r[] = "Hello" +echo r[] -- cgit 1.4.1-2-gfad0 From 7dcf435b4bf5e0de37b04d8062dd74177eedd7e8 Mon Sep 17 00:00:00 2001 From: Oscar Nihlgård Date: Wed, 22 Aug 2018 15:59:33 +0200 Subject: Fix abs & unary sub in semfold (#8724) --- compiler/semfold.nim | 8 ++++++-- tests/misc/tsemfold.nim | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'compiler') diff --git a/compiler/semfold.nim b/compiler/semfold.nim index 0a33fea7a..444940144 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -69,9 +69,13 @@ proc foldSub*(a, b: BiggestInt, n: PNode; g: ModuleGraph): PNode = checkInRange(g.config, n, res): result = newIntNodeT(res, n, g) +proc foldUnarySub(a: BiggestInt, n: PNode, g: ModuleGraph): PNode = + if a != firstOrd(g.config, n.typ): + result = newIntNodeT(-a, n, g) + proc foldAbs*(a: BiggestInt, n: PNode; g: ModuleGraph): PNode = if a != firstOrd(g.config, n.typ): - result = newIntNodeT(a, n, g) + result = newIntNodeT(abs(a), n, g) proc foldMod*(a, b: BiggestInt, n: PNode; g: ModuleGraph): PNode = if b != 0'i64: @@ -216,7 +220,7 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode = case m of mOrd: result = newIntNodeT(getOrdValue(a), n, g) of mChr: result = newIntNodeT(getInt(a), n, g) - of mUnaryMinusI, mUnaryMinusI64: result = newIntNodeT(- getInt(a), n, g) + of mUnaryMinusI, mUnaryMinusI64: result = foldUnarySub(getInt(a), n, g) of mUnaryMinusF64: result = newFloatNodeT(- getFloat(a), n, g) of mNot: result = newIntNodeT(1 - getInt(a), n, g) of mCard: result = newIntNodeT(nimsets.cardSet(g.config, a), n, g) diff --git a/tests/misc/tsemfold.nim b/tests/misc/tsemfold.nim index 18c282d9e..2101a8b50 100644 --- a/tests/misc/tsemfold.nim +++ b/tests/misc/tsemfold.nim @@ -9,6 +9,7 @@ doAssertRaises(DivByZeroError): discard 1 mod 0 doAssertRaises(DivByZeroError): discard 1 div 0 doAssertRaises(OverflowError): discard low(int8) div -1'i8 +doAssertRaises(OverflowError): discard -low(int64) doAssertRaises(OverflowError): discard low(int64) - 1'i64 doAssertRaises(OverflowError): discard high(int64) + 1'i64 @@ -21,3 +22,6 @@ doAssertRaises(OverflowError): discard low(int64) * -1 doAssertRaises(OverflowError): discard high(int8) * 2 doAssertRaises(OverflowError): discard high(int64) * 2 +doAssert abs(-1) == 1 +doAssert 2 div 2 == 1 +doAssert 2 * 3 == 6 \ No newline at end of file -- cgit 1.4.1-2-gfad0 From b02bc661974caea6a6c34b78b2df9af13e917d7c Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Wed, 22 Aug 2018 23:19:03 -0700 Subject: better formatting for recursive module dependency (#8735) --- compiler/lookups.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'compiler') diff --git a/compiler/lookups.nim b/compiler/lookups.nim index 1b5bee008..7f0882754 100644 --- a/compiler/lookups.nim +++ b/compiler/lookups.nim @@ -259,7 +259,7 @@ proc errorUseQualifier*(c: PContext; info: TLineInfo; s: PSym) = proc errorUndeclaredIdentifier*(c: PContext; info: TLineInfo; name: string) = var err = "undeclared identifier: '" & name & "'" if c.recursiveDep.len > 0: - err.add "\nThis might be caused by a recursive module dependency: " + err.add "\nThis might be caused by a recursive module dependency:\n" err.add c.recursiveDep # prevent excessive errors for 'nim check' c.recursiveDep = "" -- cgit 1.4.1-2-gfad0 From 52bee6baba1b5aeb5abe763c06478cd3139a5a84 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Thu, 23 Aug 2018 01:18:10 -0700 Subject: partially fix #8218: nim doc --project (#8737) --- compiler/docgen.nim | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'compiler') diff --git a/compiler/docgen.nim b/compiler/docgen.nim index b35452365..3842f0ad4 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -787,14 +787,13 @@ proc getOutFile2(conf: ConfigRef; filename, ext, dir: string): string = proc writeOutput*(d: PDoc, filename, outExt: string, useWarning = false) = var content = genOutFile(d) - var success = true if optStdout in d.conf.globalOptions: writeRope(stdout, content) else: let outfile = getOutFile2(d.conf, filename, outExt, "htmldocs") - success = writeRope(content, outfile) - if not success: - rawMessage(d.conf, if useWarning: warnCannotOpenFile else: errCannotOpenFile, filename) + createDir(outfile.parentDir) + if not writeRope(content, outfile): + rawMessage(d.conf, if useWarning: warnCannotOpenFile else: errCannotOpenFile, outfile) proc writeOutputJson*(d: PDoc, filename, outExt: string, useWarning = false) = -- cgit 1.4.1-2-gfad0 From d6d3f092a369ae24ab4f51a332a8056e18c215b0 Mon Sep 17 00:00:00 2001 From: Oscar Nihlgård Date: Thu, 23 Aug 2018 10:23:02 +0200 Subject: Fix for module alias inside brackets (#8726) --- compiler/importer.nim | 26 +++++++++++++++++++------- compiler/modulepaths.nim | 7 ------- tests/modules/timportas.nim | 11 +++++++++++ 3 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 tests/modules/timportas.nim (limited to 'compiler') diff --git a/compiler/importer.nim b/compiler/importer.nim index c013b93ab..acc3d26d2 100644 --- a/compiler/importer.nim +++ b/compiler/importer.nim @@ -174,20 +174,32 @@ proc impMod(c: PContext; it: PNode; importStmtResult: PNode) = #importForwarded(c, m.ast, emptySet) proc evalImport(c: PContext, n: PNode): PNode = - #result = n result = newNodeI(nkImportStmt, n.info) for i in countup(0, sonsLen(n) - 1): let it = n.sons[i] if it.kind == nkInfix and it.len == 3 and it[2].kind == nkBracket: let sep = it[0] let dir = it[1] - let a = newNodeI(nkInfix, it.info) - a.add sep - a.add dir - a.add sep # dummy entry, replaced in the loop + var imp = newNodeI(nkInfix, it.info) + imp.add sep + imp.add dir + imp.add sep # dummy entry, replaced in the loop for x in it[2]: - a.sons[2] = x - impMod(c, a, result) + if x.kind == nkInfix and x.sons[0].ident.s == "as": + imp.sons[2] = x.sons[1] + let impAs = newNodeI(nkImportAs, it.info) + impAs.add imp + impAs.add x.sons[2] + imp = impAs + impMod(c, imp, result) + else: + imp.sons[2] = x + impMod(c, imp, result) + elif it.kind == nkInfix and it.sons[0].ident.s == "as": + let imp = newNodeI(nkImportAs, it.info) + imp.add it.sons[1] + imp.add it.sons[2] + impMod(c, imp, result) else: impMod(c, it, result) diff --git a/compiler/modulepaths.nim b/compiler/modulepaths.nim index e5cbf3a2c..118002fcf 100644 --- a/compiler/modulepaths.nim +++ b/compiler/modulepaths.nim @@ -126,13 +126,6 @@ proc getModuleName*(conf: ConfigRef; n: PNode): string = of nkInfix: let n0 = n[0] let n1 = n[1] - if n0.kind == nkIdent and n0.ident.s == "as": - # XXX hack ahead: - n.kind = nkImportAs - n.sons[0] = n.sons[1] - n.sons[1] = n.sons[2] - n.sons.setLen(2) - return getModuleName(conf, n.sons[0]) when false: if n1.kind == nkPrefix and n1[0].kind == nkIdent and n1[0].ident.s == "$": if n0.kind == nkIdent and n0.ident.s == "/": diff --git a/tests/modules/timportas.nim b/tests/modules/timportas.nim new file mode 100644 index 000000000..4681a1ee0 --- /dev/null +++ b/tests/modules/timportas.nim @@ -0,0 +1,11 @@ +discard """ + action: run +""" + +import .. / modules / [definitions as foo] +import std / times as bar +import definitions as baz + +discard foo.v +discard bar.now +discard baz.v \ No newline at end of file -- cgit 1.4.1-2-gfad0