From 2d907ac334659a06de8c956c788cfe64ee0d240d Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 15 Jan 2018 00:09:11 +0100 Subject: make tests green again --- tests/concepts/t3330.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/concepts') diff --git a/tests/concepts/t3330.nim b/tests/concepts/t3330.nim index 722c0a0e0..bf8bffc7d 100644 --- a/tests/concepts/t3330.nim +++ b/tests/concepts/t3330.nim @@ -6,9 +6,9 @@ but expected one of: proc test(foo: Foo[int]) t3330.nim(25, 8) Hint: Non-matching candidates for add(k, string, T) proc add(x: var string; y: string) -proc add(result: var string; x: float) proc add(x: var string; y: char) proc add(result: var string; x: int64) +proc add(result: var string; x: float) proc add(x: var string; y: cstring) proc add[T](x: var seq[T]; y: openArray[T]) proc add[T](x: var seq[T]; y: T) -- cgit 1.4.1-2-gfad0 From 326b7dc5562d3f3d2b5848723ceaeba5b8182be1 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Sun, 4 Feb 2018 07:04:50 +0100 Subject: improve the error messages regarding type mismatches in overloading resolution --- compiler/semcall.nim | 18 ++++++++++++++++-- compiler/sigmatch.nim | 8 ++++++-- tests/concepts/t3330.nim | 37 +++++++++++++++++++++++++++++------- tests/errmsgs/tdetailed_position.nim | 22 +++++++++++++++++++++ 4 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 tests/errmsgs/tdetailed_position.nim (limited to 'tests/concepts') diff --git a/compiler/semcall.nim b/compiler/semcall.nim index c580f8fd5..a8ab2f742 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -106,6 +106,7 @@ proc pickBestCandidate(c: PContext, headSymbol: PNode, errors.safeAdd(CandidateError( sym: sym, unmatchedVarParam: int z.mutabilityProblem, + firstMismatch: z.firstMismatch, diagnostics: z.diagnostics)) else: # Symbol table has been modified. Restart and pre-calculate all syms @@ -154,7 +155,20 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors): else: add(candidates, err.sym.getProcHeader(prefer)) add(candidates, "\n") - if err.unmatchedVarParam != 0 and err.unmatchedVarParam < n.len: + if err.firstMismatch != 0 and n.len > 2: + add(candidates, "first type mismatch at position: " & $err.firstMismatch & + "\nrequired type: ") + if err.firstMismatch < err.sym.typ.len: + candidates.add typeToString(err.sym.typ.sons[err.firstMismatch]) + else: + candidates.add "none" + if err.firstMismatch < n.len: + candidates.add "\nbut expression '" + candidates.add renderTree(n[err.firstMismatch]) + candidates.add "' is of type: " + candidates.add typeToString(n[err.firstMismatch].typ) + candidates.add "\n" + elif err.unmatchedVarParam != 0 and err.unmatchedVarParam < n.len: add(candidates, "for a 'var' type a variable needs to be passed, but '" & renderTree(n[err.unmatchedVarParam]) & "' is immutable\n") for diag in err.diagnostics: @@ -189,7 +203,7 @@ proc bracketNotFoundError(c: PContext; n: PNode) = while symx != nil: if symx.kind in routineKinds: errors.add(CandidateError(sym: symx, - unmatchedVarParam: 0, + unmatchedVarParam: 0, firstMismatch: 0, diagnostics: nil)) symx = nextOverloadIter(o, c, headSymbol) if errors.len == 0: diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 123e11c68..090f30f16 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -24,7 +24,7 @@ type CandidateError* = object sym*: PSym - unmatchedVarParam*: int + unmatchedVarParam*, firstMismatch*: int diagnostics*: seq[string] CandidateErrors* = seq[CandidateError] @@ -67,7 +67,9 @@ type # or when the explain pragma is used. may be # triggered with an idetools command in the # future. - inheritancePenalty: int # to prefer closest father object type + inheritancePenalty: int # to prefer closest father object type + firstMismatch*: int # position of the first type mismatch for + # better error messages TTypeRelFlag* = enum trDontBind @@ -2249,6 +2251,7 @@ proc matchesAux(c: PContext, n, nOrig: PNode, n.sons[a], nOrig.sons[a]) if arg == nil: m.state = csNoMatch + m.firstMismatch = f return if m.baseTypeMatch: #assert(container == nil) @@ -2303,6 +2306,7 @@ proc matches*(c: PContext, n, nOrig: PNode, m: var TCandidate) = else: # no default value m.state = csNoMatch + m.firstMismatch = f break else: # use default value: diff --git a/tests/concepts/t3330.nim b/tests/concepts/t3330.nim index bf8bffc7d..6c9883618 100644 --- a/tests/concepts/t3330.nim +++ b/tests/concepts/t3330.nim @@ -1,25 +1,48 @@ discard """ errormsg: "type mismatch: got (Bar[system.int])" nimout: ''' -t3330.nim(40, 4) Error: type mismatch: got (Bar[system.int]) +t3330.nim(63, 4) Error: type mismatch: got (Bar[system.int]) but expected one of: proc test(foo: Foo[int]) -t3330.nim(25, 8) Hint: Non-matching candidates for add(k, string, T) +t3330.nim(48, 8) Hint: Non-matching candidates for add(k, string, T) proc add(x: var string; y: string) +first type mismatch at position: 1 +required type: var string +but expression 'k' is of type: Alias proc add(x: var string; y: char) +first type mismatch at position: 1 +required type: var string +but expression 'k' is of type: Alias proc add(result: var string; x: int64) +first type mismatch at position: 1 +required type: var string +but expression 'k' is of type: Alias proc add(result: var string; x: float) +first type mismatch at position: 1 +required type: var string +but expression 'k' is of type: Alias proc add(x: var string; y: cstring) +first type mismatch at position: 1 +required type: var string +but expression 'k' is of type: Alias proc add[T](x: var seq[T]; y: openArray[T]) +first type mismatch at position: 1 +required type: var seq[T] +but expression 'k' is of type: Alias proc add[T](x: var seq[T]; y: T) +first type mismatch at position: 1 +required type: var seq[T] +but expression 'k' is of type: Alias -t3330.nim(25, 8) template/generic instantiation from here -t3330.nim(32, 6) Foo: 'bar.value' cannot be assigned to -t3330.nim(25, 8) template/generic instantiation from here -t3330.nim(33, 6) Foo: 'bar.x' cannot be assigned to -''' +t3330.nim(48, 8) template/generic instantiation from here +t3330.nim(55, 6) Foo: 'bar.value' cannot be assigned to +t3330.nim(48, 8) template/generic instantiation from here +t3330.nim(56, 6) Foo: 'bar.x' cannot be assigned to + +expression: test(bar)''' """ + type Foo[T] = concept k add(k, string, T) diff --git a/tests/errmsgs/tdetailed_position.nim b/tests/errmsgs/tdetailed_position.nim new file mode 100644 index 000000000..d6db94c3e --- /dev/null +++ b/tests/errmsgs/tdetailed_position.nim @@ -0,0 +1,22 @@ + +discard """ +cmd: "nim check $file" +errormsg: "type mismatch: got (int literal(1), int literal(2), int literal(3))" +nimout: ''' +but expected one of: +proc main(a, b, c: string) +first type mismatch at position: 1 +required type: string +but expression '1' is of type: int literal(1) + +expression: main(1, 2, 3) +''' +""" + +const + myconst = "abcdefghijklmnopqrstuvwxyz" + +proc main(a, b, c: string) {.deprecated: "use foo " & "instead " & myconst.} = + return + +main(1, 2, 3) -- cgit 1.4.1-2-gfad0 From ef6eda4cb4b28a6c4a2706ea56240bc708e36349 Mon Sep 17 00:00:00 2001 From: Araq Date: Sat, 10 Feb 2018 20:39:05 +0100 Subject: better error messages: use instead of (T1, T2) in order to prevent confusions with tuple types --- compiler/msgs.nim | 2 +- compiler/semcall.nim | 8 ++--- compiler/semexprs.nim | 2 +- compiler/semtypes.nim | 4 +-- compiler/types.nim | 2 +- tests/array/tarrayplus.nim | 2 +- tests/concepts/t3330.nim | 46 +++++++++++++-------------- tests/concepts/texplain.nim | 8 ++--- tests/concepts/twrapconcept.nim | 4 +-- tests/errmsgs/t4756.nim | 2 +- tests/errmsgs/t5167_4.nim | 2 +- tests/errmsgs/tconceptconstraint.nim | 6 ++-- tests/errmsgs/tdetailed_position.nim | 8 ++--- tests/errmsgs/tgenericconstraint.nim | 4 +-- tests/errmsgs/tmake_tuple_visible.nim | 4 +-- tests/errmsgs/tshow_asgn.nim | 2 +- tests/metatype/tvoid_must_not_match.nim | 2 +- tests/overload/tissue966.nim | 2 +- tests/typerel/temptynode.nim | 2 +- tests/typerel/tgeneric_subtype_regression.nim | 2 +- tests/typerel/tno_int_in_bool_context.nim | 2 +- tests/typerel/tnocontains.nim | 2 +- tests/typerel/tregionptrs.nim | 2 +- tests/typerel/ttypedesc_as_genericparam1.nim | 2 +- tests/typerel/ttypenoval.nim | 2 +- 25 files changed, 62 insertions(+), 62 deletions(-) (limited to 'tests/concepts') diff --git a/compiler/msgs.nim b/compiler/msgs.nim index 954883b01..6439d47ee 100644 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -272,7 +272,7 @@ const errXStackEscape: "address of '$1' may not escape its stack frame", errVarForOutParamNeededX: "for a \'var\' type a variable needs to be passed; but '$1' is immutable", errPureTypeMismatch: "type mismatch", - errTypeMismatch: "type mismatch: got (", + errTypeMismatch: "type mismatch: got <", errButExpected: "but expected one of: ", errButExpectedX: "but expected \'$1\'", errAmbiguousCallXYZ: "ambiguous call; both $1 and $2 match for: $3", diff --git a/compiler/semcall.nim b/compiler/semcall.nim index 01f291017..d8ee6e7a1 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -156,14 +156,14 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors): add(candidates, err.sym.getProcHeader(prefer)) add(candidates, "\n") if err.firstMismatch != 0 and n.len > 2: - add(candidates, "first type mismatch at position: " & $err.firstMismatch & - "\nrequired type: ") + add(candidates, " first type mismatch at position: " & $err.firstMismatch & + "\n required type: ") if err.firstMismatch < err.sym.typ.len: candidates.add typeToString(err.sym.typ.sons[err.firstMismatch]) else: candidates.add "none" if err.firstMismatch < n.len: - candidates.add "\nbut expression '" + candidates.add "\n but expression '" candidates.add renderTree(n[err.firstMismatch]) candidates.add "' is of type: " candidates.add typeToString(n[err.firstMismatch].typ) @@ -190,7 +190,7 @@ proc notFoundError*(c: PContext, n: PNode, errors: CandidateErrors) = let (prefer, candidates) = presentFailedCandidates(c, n, errors) var result = msgKindToString(errTypeMismatch) add(result, describeArgs(c, n, 1, prefer)) - add(result, ')') + add(result, '>') if candidates != "": add(result, "\n" & msgKindToString(errButExpected) & "\n" & candidates) localError(n.info, errGenerated, result & "\nexpression: " & $n) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index d4b1eec88..7f58e266e 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -735,7 +735,7 @@ proc semIndirectOp(c: PContext, n: PNode, flags: TExprFlags): PNode = hasErrorType = true break if not hasErrorType: - add(msg, ")\n" & msgKindToString(errButExpected) & "\n" & + add(msg, ">\n" & msgKindToString(errButExpected) & "\n" & typeToString(n.sons[0].typ)) localError(n.info, errGenerated, msg) return errorNode(c, n) diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index f14b40a9b..df274c7db 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -1149,8 +1149,8 @@ proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType = if m.state != csMatch: let err = "cannot instantiate " & typeToString(t) & "\n" & - "got: (" & describeArgs(c, n) & ")\n" & - "but expected: (" & describeArgs(c, t.n, 0) & ")" + "got: <" & describeArgs(c, n) & ">\n" & + "but expected: <" & describeArgs(c, t.n, 0) & ">" localError(n.info, errGenerated, err) return newOrPrevType(tyError, prev, c) diff --git a/compiler/types.nim b/compiler/types.nim index 452e95dfd..10827f830 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -1620,7 +1620,7 @@ proc typeMismatch*(info: TLineInfo, formal, actual: PType) = let desc = typeToString(formal, preferDesc) let x = if named == desc: named else: named & " = " & desc var msg = msgKindToString(errTypeMismatch) & - typeToString(actual) & ") " & + typeToString(actual) & "> " & msgKindToString(errButExpectedX) % [x] if formal.kind == tyProc and actual.kind == tyProc: diff --git a/tests/array/tarrayplus.nim b/tests/array/tarrayplus.nim index 33921c0e1..09bae77fd 100644 --- a/tests/array/tarrayplus.nim +++ b/tests/array/tarrayplus.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "type mismatch: got (array[0..2, float], array[0..1, float])" + errormsg: "type mismatch: got " """ proc `+`*[R, T] (v1, v2: array[R, T]): array[R, T] = diff --git a/tests/concepts/t3330.nim b/tests/concepts/t3330.nim index 6c9883618..a4fff7fb3 100644 --- a/tests/concepts/t3330.nim +++ b/tests/concepts/t3330.nim @@ -1,38 +1,38 @@ discard """ -errormsg: "type mismatch: got (Bar[system.int])" +errormsg: "type mismatch: got " nimout: ''' -t3330.nim(63, 4) Error: type mismatch: got (Bar[system.int]) +t3330.nim(63, 4) Error: type mismatch: got but expected one of: proc test(foo: Foo[int]) t3330.nim(48, 8) Hint: Non-matching candidates for add(k, string, T) proc add(x: var string; y: string) -first type mismatch at position: 1 -required type: var string -but expression 'k' is of type: Alias + first type mismatch at position: 1 + required type: var string + but expression 'k' is of type: Alias proc add(x: var string; y: char) -first type mismatch at position: 1 -required type: var string -but expression 'k' is of type: Alias + first type mismatch at position: 1 + required type: var string + but expression 'k' is of type: Alias proc add(result: var string; x: int64) -first type mismatch at position: 1 -required type: var string -but expression 'k' is of type: Alias + first type mismatch at position: 1 + required type: var string + but expression 'k' is of type: Alias proc add(result: var string; x: float) -first type mismatch at position: 1 -required type: var string -but expression 'k' is of type: Alias + first type mismatch at position: 1 + required type: var string + but expression 'k' is of type: Alias proc add(x: var string; y: cstring) -first type mismatch at position: 1 -required type: var string -but expression 'k' is of type: Alias + first type mismatch at position: 1 + required type: var string + but expression 'k' is of type: Alias proc add[T](x: var seq[T]; y: openArray[T]) -first type mismatch at position: 1 -required type: var seq[T] -but expression 'k' is of type: Alias + first type mismatch at position: 1 + required type: var seq[T] + but expression 'k' is of type: Alias proc add[T](x: var seq[T]; y: T) -first type mismatch at position: 1 -required type: var seq[T] -but expression 'k' is of type: Alias + first type mismatch at position: 1 + required type: var seq[T] + but expression 'k' is of type: Alias t3330.nim(48, 8) template/generic instantiation from here t3330.nim(55, 6) Foo: 'bar.value' cannot be assigned to diff --git a/tests/concepts/texplain.nim b/tests/concepts/texplain.nim index de8ddf890..4925a25b3 100644 --- a/tests/concepts/texplain.nim +++ b/tests/concepts/texplain.nim @@ -26,14 +26,14 @@ texplain.nim(70, 6) ExplainedConcept: undeclared field: '.' texplain.nim(70, 6) ExplainedConcept: expression '.' cannot be called texplain.nim(69, 5) ExplainedConcept: concept predicate failed -texplain.nim(113, 20) Error: type mismatch: got (NonMatchingType) +texplain.nim(113, 20) Error: type mismatch: got but expected one of: proc e(o: ExplainedConcept): int texplain.nim(69, 5) ExplainedConcept: concept predicate failed proc e(i: int): int expression: e(n) -texplain.nim(114, 20) Error: type mismatch: got (NonMatchingType) +texplain.nim(114, 20) Error: type mismatch: got but expected one of: proc r(o: RegularConcept): int texplain.nim(73, 5) RegularConcept: concept predicate failed @@ -45,7 +45,7 @@ texplain.nim(115, 20) Hint: Non-matching candidates for r(y) proc r[T](a: SomeNumber; b: T; c: auto) proc r(i: string): int -texplain.nim(123, 2) Error: type mismatch: got (MatchingType) +texplain.nim(123, 2) Error: type mismatch: got but expected one of: proc f(o: NestedConcept) texplain.nim(73, 6) RegularConcept: undeclared field: 'foo' @@ -61,7 +61,7 @@ texplain.nim(77, 5) NestedConcept: concept predicate failed expression: f(y) ''' line: 123 - errormsg: "type mismatch: got (MatchingType)" + errormsg: "type mismatch: got " """ type diff --git a/tests/concepts/twrapconcept.nim b/tests/concepts/twrapconcept.nim index 25a855e34..377b63afe 100644 --- a/tests/concepts/twrapconcept.nim +++ b/tests/concepts/twrapconcept.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "type mismatch: got (string)" + errormsg: "type mismatch: got " line: 21 nimout: "twrapconcept.nim(11, 5) Foo: concept predicate failed" """ @@ -9,7 +9,7 @@ discard """ type Foo = concept foo foo.get is int - + FooWrap[F: Foo] = object foo: F diff --git a/tests/errmsgs/t4756.nim b/tests/errmsgs/t4756.nim index 91fc90f4b..262614ba0 100644 --- a/tests/errmsgs/t4756.nim +++ b/tests/errmsgs/t4756.nim @@ -1,5 +1,5 @@ discard """ -errormsg: "type mismatch: got (string, arr: seq[empty])" +errormsg: "type mismatch: got " line: 15 """ diff --git a/tests/errmsgs/t5167_4.nim b/tests/errmsgs/t5167_4.nim index 3d77fae02..7a263622b 100644 --- a/tests/errmsgs/t5167_4.nim +++ b/tests/errmsgs/t5167_4.nim @@ -1,5 +1,5 @@ discard """ -errormsg: "type mismatch: got (proc [*missing parameters*](x: int) | proc (x: string){.gcsafe, locks: 0.})" +errormsg: "type mismatch: got " line: 19 """ diff --git a/tests/errmsgs/tconceptconstraint.nim b/tests/errmsgs/tconceptconstraint.nim index c1f0b94eb..9ab1708c7 100644 --- a/tests/errmsgs/tconceptconstraint.nim +++ b/tests/errmsgs/tconceptconstraint.nim @@ -2,15 +2,15 @@ discard """ errormsg: "cannot instantiate B" line: 20 nimout: ''' -got: (type string) -but expected: (T: A) +got: +but expected: ''' """ type A = concept c advance(c) - + B[T: A] = object child: ref B[T] diff --git a/tests/errmsgs/tdetailed_position.nim b/tests/errmsgs/tdetailed_position.nim index d6db94c3e..ce5b18bbd 100644 --- a/tests/errmsgs/tdetailed_position.nim +++ b/tests/errmsgs/tdetailed_position.nim @@ -1,13 +1,13 @@ discard """ cmd: "nim check $file" -errormsg: "type mismatch: got (int literal(1), int literal(2), int literal(3))" +errormsg: "type mismatch: got " nimout: ''' but expected one of: proc main(a, b, c: string) -first type mismatch at position: 1 -required type: string -but expression '1' is of type: int literal(1) + first type mismatch at position: 1 + required type: string + but expression '1' is of type: int literal(1) expression: main(1, 2, 3) ''' diff --git a/tests/errmsgs/tgenericconstraint.nim b/tests/errmsgs/tgenericconstraint.nim index 9129d257b..e3093fead 100644 --- a/tests/errmsgs/tgenericconstraint.nim +++ b/tests/errmsgs/tgenericconstraint.nim @@ -2,8 +2,8 @@ discard """ errormsg: "cannot instantiate B" line: 14 nimout: ''' -got: (type int) -but expected: (T: string or float) +got: +but expected: ''' """ diff --git a/tests/errmsgs/tmake_tuple_visible.nim b/tests/errmsgs/tmake_tuple_visible.nim index 43337c2a9..e059368ad 100644 --- a/tests/errmsgs/tmake_tuple_visible.nim +++ b/tests/errmsgs/tmake_tuple_visible.nim @@ -1,7 +1,7 @@ discard """ - errormsg: '''got (tuple of (type NimEdAppWindow, int))''' + errormsg: '''got ''' line: 22 - nimout: '''got (tuple of (type NimEdAppWindow, int)) + nimout: '''got but expected one of: template xxx(tn: typedesc; i: int)''' """ diff --git a/tests/errmsgs/tshow_asgn.nim b/tests/errmsgs/tshow_asgn.nim index 250f786e2..1627c9b71 100644 --- a/tests/errmsgs/tshow_asgn.nim +++ b/tests/errmsgs/tshow_asgn.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "type mismatch: got (int) but expected 'cshort = int16'" + errormsg: "type mismatch: got but expected 'cshort = int16'" line: 12 column: 10 file: "tshow_asgn.nim" diff --git a/tests/metatype/tvoid_must_not_match.nim b/tests/metatype/tvoid_must_not_match.nim index 240c3f751..c786c2f16 100644 --- a/tests/metatype/tvoid_must_not_match.nim +++ b/tests/metatype/tvoid_must_not_match.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "type mismatch: got (Future[system.int], void)" + errormsg: "type mismatch: got " line: 20 """ diff --git a/tests/overload/tissue966.nim b/tests/overload/tissue966.nim index 2911348cf..c5b28e217 100644 --- a/tests/overload/tissue966.nim +++ b/tests/overload/tissue966.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "type mismatch: got (PTest)" + errormsg: "type mismatch: got " """ type diff --git a/tests/typerel/temptynode.nim b/tests/typerel/temptynode.nim index 32148ce13..df308fbc2 100644 --- a/tests/typerel/temptynode.nim +++ b/tests/typerel/temptynode.nim @@ -1,6 +1,6 @@ discard """ line: 16 - errormsg: "type mismatch: got (void)" + errormsg: "type mismatch: got " """ # bug #950 diff --git a/tests/typerel/tgeneric_subtype_regression.nim b/tests/typerel/tgeneric_subtype_regression.nim index e279c0ad4..def5d721e 100644 --- a/tests/typerel/tgeneric_subtype_regression.nim +++ b/tests/typerel/tgeneric_subtype_regression.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "type mismatch: got (FooRef[system.string])" + errormsg: "type mismatch: got " line: 15 """ diff --git a/tests/typerel/tno_int_in_bool_context.nim b/tests/typerel/tno_int_in_bool_context.nim index 557759169..a4b4237d2 100644 --- a/tests/typerel/tno_int_in_bool_context.nim +++ b/tests/typerel/tno_int_in_bool_context.nim @@ -1,6 +1,6 @@ discard """ line: 6 - errormsg: "type mismatch: got (int literal(1)) but expected 'bool'" + errormsg: "type mismatch: got but expected 'bool'" """ if 1: diff --git a/tests/typerel/tnocontains.nim b/tests/typerel/tnocontains.nim index 4f4951478..a93db2fc3 100644 --- a/tests/typerel/tnocontains.nim +++ b/tests/typerel/tnocontains.nim @@ -1,7 +1,7 @@ discard """ file: "tnocontains.nim" line: 10 - errormsg: "type mismatch: got (string, string)" + errormsg: "type mismatch: got " """ # shouldn't compile since it doesn't do what you think it does without diff --git a/tests/typerel/tregionptrs.nim b/tests/typerel/tregionptrs.nim index a8d2e7a6d..9eeded18b 100644 --- a/tests/typerel/tregionptrs.nim +++ b/tests/typerel/tregionptrs.nim @@ -1,6 +1,6 @@ discard """ line: 16 - errormsg: "type mismatch: got (BPtr) but expected 'APtr = ptr[RegionA, int]'" + errormsg: "type mismatch: got but expected 'APtr = ptr[RegionA, int]'" """ type diff --git a/tests/typerel/ttypedesc_as_genericparam1.nim b/tests/typerel/ttypedesc_as_genericparam1.nim index 88c0509b2..9ae464842 100644 --- a/tests/typerel/ttypedesc_as_genericparam1.nim +++ b/tests/typerel/ttypedesc_as_genericparam1.nim @@ -1,6 +1,6 @@ discard """ line: 6 - errormsg: "type mismatch: got (type int)" + errormsg: "type mismatch: got " """ # bug #3079, #1146 echo repr(int) diff --git a/tests/typerel/ttypenoval.nim b/tests/typerel/ttypenoval.nim index eabca48f6..720e5d662 100644 --- a/tests/typerel/ttypenoval.nim +++ b/tests/typerel/ttypenoval.nim @@ -1,7 +1,7 @@ discard """ file: "ttypenoval.nim" line: 38 - errormsg: "type mismatch: got (type int) but expected 'int'" + errormsg: "type mismatch: got but expected 'int'" """ # A min-heap. -- cgit 1.4.1-2-gfad0 From 121b9e26fb9d1ae6037c806dbb12a3ae0e26ded6 Mon Sep 17 00:00:00 2001 From: zah Date: Sat, 24 Mar 2018 16:28:09 +0200 Subject: Static[T] fixes (#7333) * fix the usage of unresolved static[T] parameters in proc signatures * fix tsametype and tmacrogenerics * Allow creating composite type classes with concepts and using them in type signatures * Allow integers to be used in ident concatenations * Support using imported C++ generic types in proc signatures * fixes #7230 * closes #7379 * re-enable some metatype tests --- compiler/ast.nim | 9 ++--- compiler/ccgtypes.nim | 7 ++++ compiler/lookups.nim | 3 +- compiler/semexprs.nim | 40 +++++++++++++++++---- compiler/seminst.nim | 30 ++++++++++++++++ compiler/semtypes.nim | 5 ++- compiler/semtypinst.nim | 4 +++ compiler/sigmatch.nim | 2 +- compiler/types.nim | 2 ++ tests/concepts/tseqofconcept.nim | 19 ++++++++++ tests/cpp/tcovariancerules.nim | 2 +- tests/cpp/tvector_iterator.nim | 2 +- tests/metatype/tstaticparammacro.nim | 13 +++---- tests/metatype/ttypeselectors.nim | 64 +++++++++++++++++++++++++++++++++- tests/metatype/typeclassinference.nim | 3 +- tests/misc/tidentconcatenations.nim | 32 +++++++++++++++++ tests/statictypes/tcryptodigest.nim | 44 +++++++++++++++++++++++ tests/statictypes/tstaticimportcpp.nim | 10 ++++-- 18 files changed, 265 insertions(+), 26 deletions(-) create mode 100644 tests/concepts/tseqofconcept.nim create mode 100644 tests/misc/tidentconcatenations.nim create mode 100644 tests/statictypes/tcryptodigest.nim (limited to 'tests/concepts') diff --git a/compiler/ast.nim b/compiler/ast.nim index 8286e3bb7..a28a7e7e3 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -981,6 +981,7 @@ const nkPragmaCallKinds* = {nkExprColonExpr, nkCall, nkCallStrLit} nkLiterals* = {nkCharLit..nkTripleStrLit} + nkFloatLiterals* = {nkFloatLit..nkFloat128Lit} nkLambdaKinds* = {nkLambda, nkDo} declarativeDefs* = {nkProcDef, nkFuncDef, nkMethodDef, nkIteratorDef, nkConverterDef} procDefs* = nkLambdaKinds + declarativeDefs @@ -1476,7 +1477,7 @@ proc copyNode*(src: PNode): PNode = echo "COMES FROM ", src.id case src.kind of nkCharLit..nkUInt64Lit: result.intVal = src.intVal - of nkFloatLit..nkFloat128Lit: result.floatVal = src.floatVal + of nkFloatLiterals: result.floatVal = src.floatVal of nkSym: result.sym = src.sym of nkIdent: result.ident = src.ident of nkStrLit..nkTripleStrLit: result.strVal = src.strVal @@ -1495,7 +1496,7 @@ proc shallowCopy*(src: PNode): PNode = echo "COMES FROM ", src.id case src.kind of nkCharLit..nkUInt64Lit: result.intVal = src.intVal - of nkFloatLit..nkFloat128Lit: result.floatVal = src.floatVal + of nkFloatLiterals: result.floatVal = src.floatVal of nkSym: result.sym = src.sym of nkIdent: result.ident = src.ident of nkStrLit..nkTripleStrLit: result.strVal = src.strVal @@ -1515,7 +1516,7 @@ proc copyTree*(src: PNode): PNode = echo "COMES FROM ", src.id case src.kind of nkCharLit..nkUInt64Lit: result.intVal = src.intVal - of nkFloatLit..nkFloat128Lit: result.floatVal = src.floatVal + of nkFloatLiterals: result.floatVal = src.floatVal of nkSym: result.sym = src.sym of nkIdent: result.ident = src.ident of nkStrLit..nkTripleStrLit: result.strVal = src.strVal @@ -1564,7 +1565,7 @@ proc getInt*(a: PNode): BiggestInt = proc getFloat*(a: PNode): BiggestFloat = case a.kind - of nkFloatLit..nkFloat128Lit: result = a.floatVal + of nkFloatLiterals: result = a.floatVal else: internalError(a.info, "getFloat") result = 0.0 diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim index 4fc029116..ed44c577d 100644 --- a/compiler/ccgtypes.nim +++ b/compiler/ccgtypes.nim @@ -842,6 +842,13 @@ proc getTypeDescAux(m: BModule, origTyp: PType, check: var IntSet): Rope = # always call for sideeffects: assert t.kind != tyTuple discard getRecordDesc(m, t, result, check) + # The resulting type will include commas and these won't play well + # with the C macros for defining procs such as N_NIMCALL. We must + # create a typedef for the type and use it in the proc signature: + let typedefName = ~"TY" & $sig + addf(m.s[cfsTypes], "typedef $1 $2;$n", [result, typedefName]) + m.typeCache[sig] = typedefName + result = typedefName else: when false: if t.sym != nil and t.sym.name.s == "KeyValuePair": diff --git a/compiler/lookups.nim b/compiler/lookups.nim index c409acc59..0675c5ca0 100644 --- a/compiler/lookups.nim +++ b/compiler/lookups.nim @@ -44,6 +44,7 @@ proc considerQuotedIdent*(n: PNode, origin: PNode = nil): PIdent = case x.kind of nkIdent: id.add(x.ident.s) of nkSym: id.add(x.sym.name.s) + of nkLiterals - nkFloatLiterals: id.add(x.renderTree) else: handleError(n, origin) result = getIdent(id) of nkOpenSymChoice, nkClosedSymChoice: @@ -456,4 +457,4 @@ proc pickSym*(c: PContext, n: PNode; kinds: set[TSymKind]; a = nextOverloadIter(o, c, n) proc isInfixAs*(n: PNode): bool = - return n.kind == nkInfix and considerQuotedIdent(n[0]).s == "as" \ No newline at end of file + return n.kind == nkInfix and considerQuotedIdent(n[0]).s == "as" diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 7e7c496e3..a523bfc9e 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -884,6 +884,9 @@ const proc readTypeParameter(c: PContext, typ: PType, paramName: PIdent, info: TLineInfo): PNode = + # Note: This function will return emptyNode when attempting to read + # a static type parameter that is not yet resolved (e.g. this may + # happen in proc signatures such as `proc(x: T): array[T.sizeParam, U]` if typ.kind in {tyUserTypeClass, tyUserTypeClassInst}: for statement in typ.n: case statement.kind @@ -914,7 +917,10 @@ proc readTypeParameter(c: PContext, typ: PType, if tParam.sym.name.id == paramName.id: let rawTyp = ty.sons[s + 1] if rawTyp.kind == tyStatic: - return rawTyp.n + if rawTyp.n != nil: + return rawTyp.n + else: + return emptyNode else: let foundTyp = makeTypeDesc(c, rawTyp) return newSymNode(copySym(tParam.sym).linkTo(foundTyp), info) @@ -1079,21 +1085,43 @@ proc builtinFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode = template tryReadingGenericParam(t: PType) = case t.kind of tyTypeParamsHolders: - return readTypeParameter(c, t, i, n.info) + result = readTypeParameter(c, t, i, n.info) + if result == emptyNode: + result = n + n.typ = makeTypeFromExpr(c, n.copyTree) + return of tyUserTypeClasses: if t.isResolvedUserTypeClass: return readTypeParameter(c, t, i, n.info) else: n.typ = makeTypeFromExpr(c, copyTree(n)) return n - of tyGenericParam: + of tyGenericParam, tyAnything: n.typ = makeTypeFromExpr(c, copyTree(n)) return n else: discard - if isTypeExpr(n.sons[0]) or (ty.kind == tyTypeDesc and ty.base.kind != tyNone): - if ty.kind == tyTypeDesc: ty = ty.base + var argIsType = false + + if ty.kind == tyTypeDesc: + if ty.base.kind == tyNone: + # This is a still unresolved typedesc parameter. + # If this is a regular proc, then all bets are off and we must return + # tyFromExpr, but when this happen in a macro this is not a built-in + # field access and we leave the compiler to compile a normal call: + if getCurrOwner(c).kind != skMacro: + n.typ = makeTypeFromExpr(c, n.copyTree) + return n + else: + return nil + else: + ty = ty.base + argIsType = true + else: + argIsType = isTypeExpr(n.sons[0]) + + if argIsType: ty = ty.skipTypes(tyDotOpTransparent) case ty.kind of tyEnum: @@ -2186,7 +2214,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode = # because of the changed symbol binding, this does not mean that we # don't have to check the symbol for semantics here again! result = semSym(c, n, n.sym, flags) - of nkEmpty, nkNone, nkCommentStmt: + of nkEmpty, nkNone, nkCommentStmt, nkType: discard of nkNilLit: if result.typ == nil: result.typ = getSysType(tyNil) diff --git a/compiler/seminst.nim b/compiler/seminst.nim index acea9330b..7fedaca5b 100644 --- a/compiler/seminst.nim +++ b/compiler/seminst.nim @@ -174,6 +174,8 @@ proc sideEffectsCheck(c: PContext, s: PSym) = proc instGenericContainer(c: PContext, info: TLineInfo, header: PType, allowMetaTypes = false): PType = + internalAssert header.kind == tyGenericInvocation + var typeMap: LayeredIdTable cl: TReplTypeVars @@ -185,7 +187,35 @@ proc instGenericContainer(c: PContext, info: TLineInfo, header: PType, cl.info = info cl.c = c cl.allowMetaTypes = allowMetaTypes + + # We must add all generic params in scope, because the generic body + # may include tyFromExpr nodes depending on these generic params. + # XXX: This looks quite similar to the code in matchUserTypeClass, + # perhaps the code can be extracted in a shared function. + openScope(c) + let genericTyp = header.base + for i in 0 .. (genericTyp.len - 2): + let genParam = genericTyp[i] + var param: PSym + + template paramSym(kind): untyped = + newSym(kind, genParam.sym.name, genericTyp.sym, genParam.sym.info) + + if genParam.kind == tyStatic: + param = paramSym skConst + param.ast = header[i+1].n + param.typ = header[i+1] + else: + param = paramSym skType + param.typ = makeTypeDesc(c, header[i+1]) + + # this scope was not created by the user, + # unused params shoudn't be reported. + param.flags.incl sfUsed + addDecl(c, param) + result = replaceTypeVarsT(cl, header) + closeScope(c) proc instantiateProcType(c: PContext, pt: TIdTable, prc: PSym, info: TLineInfo) = diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index be6eac052..b43a96a87 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -1397,7 +1397,10 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType = fixupTypeOf(c, prev, typExpr) result = typExpr.typ else: - result = semTypeExpr(c, n, prev) + if c.inGenericContext > 0 and n.kind == nkCall: + result = makeTypeFromExpr(c, n.copyTree) + else: + result = semTypeExpr(c, n, prev) of nkWhenStmt: var whenResult = semWhen(c, n, false) if whenResult.kind == nkStmtList: whenResult.kind = nkStmtListType diff --git a/compiler/semtypinst.nim b/compiler/semtypinst.nim index d9b368b0e..09434c925 100644 --- a/compiler/semtypinst.nim +++ b/compiler/semtypinst.nim @@ -460,6 +460,10 @@ proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType = of tyFromExpr: if cl.allowMetaTypes: return + # This assert is triggered when a tyFromExpr was created in a cyclic + # way. You should break the cycle at the point of creation by introducing + # a call such as: `n.typ = makeTypeFromExpr(c, n.copyTree)` + # Otherwise, the cycle will be fatal for the prepareNode call below assert t.n.typ != t var n = prepareNode(cl, t.n) if n.kind != nkEmpty: diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 2b45a2452..96d815df7 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -1884,7 +1884,7 @@ proc paramTypesMatchAux(m: var TCandidate, f, a: PType, return arg elif f.kind == tyTypeDesc: return arg - elif f.kind == tyStatic: + elif f.kind == tyStatic and arg.typ.n != nil: return arg.typ.n else: return argSemantized # argOrig diff --git a/compiler/types.nim b/compiler/types.nim index 7ae02486e..ff3572517 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -1136,6 +1136,8 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind, of tyTypeClasses: if tfGenericTypeParam in t.flags or taConcept in flags: #or taField notin flags: discard + elif t.isResolvedUserTypeClass: + result = typeAllowedAux(marker, t.lastSon, kind, flags) elif kind notin {skParam, skResult}: result = t of tyGenericBody, tyGenericParam, tyGenericInvocation, diff --git a/tests/concepts/tseqofconcept.nim b/tests/concepts/tseqofconcept.nim new file mode 100644 index 000000000..5e44117ea --- /dev/null +++ b/tests/concepts/tseqofconcept.nim @@ -0,0 +1,19 @@ +discard """ +output: "1\n2\n3" +""" + +type + MyConcept = concept x + someProc(x) + + SomeSeq = seq[MyConcept] + +proc someProc(x:int) = echo x + +proc work (s: SomeSeq) = + for item in s: + someProc item + +var s = @[1, 2, 3] +work s + diff --git a/tests/cpp/tcovariancerules.nim b/tests/cpp/tcovariancerules.nim index 9365a3a18..f81d67a50 100644 --- a/tests/cpp/tcovariancerules.nim +++ b/tests/cpp/tcovariancerules.nim @@ -300,7 +300,7 @@ reject wantsVarPointer2(pcat) # covariance may be allowed for certain extern types -{.emit: """ +{.emit: """/*TYPESECTION*/ template struct FN { typedef void (*type)(T); }; template struct ARR { typedef T DataType[2]; DataType data; }; """.} diff --git a/tests/cpp/tvector_iterator.nim b/tests/cpp/tvector_iterator.nim index 9df3754ba..4d686955f 100644 --- a/tests/cpp/tvector_iterator.nim +++ b/tests/cpp/tvector_iterator.nim @@ -2,7 +2,7 @@ discard """ targets: "cpp" """ -{.emit: """ +{.emit: """/*TYPESECTION*/ template struct Vector { diff --git a/tests/metatype/tstaticparammacro.nim b/tests/metatype/tstaticparammacro.nim index bd3295874..02021185f 100644 --- a/tests/metatype/tstaticparammacro.nim +++ b/tests/metatype/tstaticparammacro.nim @@ -14,7 +14,6 @@ AST b 20Test 20 ''' - disabled: true """ import macros @@ -26,7 +25,7 @@ type const data: Tconfig = (@["aa", "bb"], @[11, 22]) -macro mymacro(data: static[TConfig]) = +macro mymacro(data: static[TConfig]): untyped = echo "letters" for s in items(data.letters): echo s @@ -44,10 +43,10 @@ const a : Ta = @[(11, 22), (33, 44)] b : Tb = (@[55,66], @[77, 88]) -macro mA(data: static[Ta]) = +macro mA(data: static[Ta]): untyped = echo "AST a \n", repr(data) -macro mB(data: static[Tb]) = +macro mB(data: static[Tb]): untyped = echo "AST b \n", repr(data) echo data.e[0] @@ -57,13 +56,15 @@ mB(b) type Foo[N: static[int], Z: static[string]] = object -macro staticIntMacro(f: static[int]) = echo f +macro staticIntMacro(f: static[int]): untyped = + echo f + staticIntMacro 10 var x: Foo[20, "Test"] -macro genericMacro[N; Z: static[string]](f: Foo[N, Z], ll = 3, zz = 12) = +macro genericMacro[N; Z: static[string]](f: Foo[N, Z], ll = 3, zz = 12): untyped = echo N, Z genericMacro x diff --git a/tests/metatype/ttypeselectors.nim b/tests/metatype/ttypeselectors.nim index c29fd15ce..1209fe78f 100644 --- a/tests/metatype/ttypeselectors.nim +++ b/tests/metatype/ttypeselectors.nim @@ -1,4 +1,9 @@ -import macros +discard """ +output: "8\n8\n4" +""" + +import + macros, typetraits template selectType(x: int): typeDesc = when x < 10: @@ -11,6 +16,9 @@ template simpleTypeTempl: typeDesc = macro typeFromMacro: typedesc = string +# The tests below check that the result variable of the +# selected type matches the literal types in the code: + proc t1*(x: int): simpleTypeTempl() = result = "test" @@ -37,3 +45,57 @@ proc t6*(x: type(t3(0))): type(t1(0)) = proc t7*(x: int): type($x) = result = "test" +# This is a more compicated example involving a type +# selection through a macro: +# https://github.com/nim-lang/Nim/issues/7230 + +macro getBase*(bits: static[int]): untyped = + if bits >= 128: + result = newTree(nnkBracketExpr, ident("MpUintBase"), ident("uint64")) + else: + result = newTree(nnkBracketExpr, ident("MpUintBase"), ident("uint32")) + +type + BaseUint* = SomeUnsignedInt or MpUintBase + + MpUintBase*[T] = object + lo*, hi*: T + + ## This gets type mismatch + MpUint*[bits: static[int]] = getBase(bits) + +var m1: MpUint[128] +var m2: MpUint[64] +var m3: getBase(32) + +static: + # assert m1.type.name == "MpUintBase[uint64]" + assert m1.lo.type.name == "uint64" + assert m2.lo.type.name == "uint32" + assert m3.type.name == "MpUintBase[system.uint32]" + +# https://github.com/nim-lang/Nim/issues/7379 + +import macros, typetraits + +macro works(): untyped = + result = getType(int64) + +macro fails(bits: static[int]): untyped = + if bits > 64: + result = getType(int64) + else: + result = getType(int32) + +type + Foo*[bits: static[int]] = works() + Bar*[bits: static[int]] = fails(bits) + +var a: Foo[16] +var b: Bar[256] +var c: Bar[32] + +echo sizeof(a) +echo sizeof(b) +echo sizeof(c) + diff --git a/tests/metatype/typeclassinference.nim b/tests/metatype/typeclassinference.nim index 8b99eb501..c845e04f7 100644 --- a/tests/metatype/typeclassinference.nim +++ b/tests/metatype/typeclassinference.nim @@ -1,7 +1,6 @@ discard """ errormsg: "type mismatch: got but expected 'ptr'" line: 20 - disabled: true """ import typetraits @@ -12,7 +11,7 @@ type var x = Vec([1, 2, 3]) static: - assert x.type.name == "Vec[static[int](3), int]" + assert x.type.name == "Vec[3, system.int]" var str1: string = "hello, world!" var ptr1: ptr = addr(str1) diff --git a/tests/misc/tidentconcatenations.nim b/tests/misc/tidentconcatenations.nim new file mode 100644 index 000000000..302c51d87 --- /dev/null +++ b/tests/misc/tidentconcatenations.nim @@ -0,0 +1,32 @@ +type + Hash*[bits: static[int]] = object + data*: array[bits div 8, uint8] + +{.emit: """ + +void sha_256(void* input, int input_len, void* output, int output_len) {} +void sha_512(void* input, int input_len, void* output, int output_len) {} + +void keccak_256(void* input, int input_len, void* output, int output_len) {} +void keccak_512(void* input, int input_len, void* output, int output_len) {} + +""".} + +template defineKeccak(bits: untyped) = + proc `extKeccak bits`(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.nodecl, importc: "keccak_" & astToStr(bits).} + +template defineSha(bits: static[int]) = + proc `extSha bits`(output: pointer, outSize: csize, input: pointer, inputSize: csize) {.nodecl, importc: "sha_" & astToStr(bits).} + +template defineHashProcs(bits) = + defineSha(bits) + defineKeccak(bits) + +defineHashProcs(256) +defineHashProcs(512) + +extSha256(nil, 0, nil, 0) +extSha512(nil, 0, nil, 0) +extKeccak256(nil, 0, nil, 0) +extKeccak512(nil, 0, nil, 0) + diff --git a/tests/statictypes/tcryptodigest.nim b/tests/statictypes/tcryptodigest.nim new file mode 100644 index 000000000..c78a4188a --- /dev/null +++ b/tests/statictypes/tcryptodigest.nim @@ -0,0 +1,44 @@ +discard """ + output: "Digest[128]\nDigest[256]" +""" + +import typetraits + +type + Digest[bits: static[int]] = object + data: array[bits div 8, byte] + + ContextKind = enum + A, B, C + + HashingContext[bits: static[int], kind: static[ContextKind]] = object + ctx: array[bits div 8, byte] + + Hash128 = HashingContext[128, A] + Hash256 = HashingContext[256, B] + + HMAC[HashType] = object + h: HashType + +proc init(c: var HashingContext) = discard +proc update(c: var HashingContext, data: ptr byte, dataLen: uint) = discard +proc finish(c: var HashingContext): Digest[c.bits] = discard + +proc digest(T: typedesc, data: ptr byte, dataLen: uint): Digest[T.bits] = + mixin init, update, finish + + var ctx: T + ctx.init() + ctx.update(data, dataLen) + result = ctx.finish() + +var h = Hash128.digest(nil, 0) +echo h.type.name + +proc finish(hmac: var HMAC): Digest[HMAC.HashType.bits] = + discard + +var hm: HMAC[Hash256] +var d = hm.finish +echo d.type.name + diff --git a/tests/statictypes/tstaticimportcpp.nim b/tests/statictypes/tstaticimportcpp.nim index e13cc36b4..0cbbc1df6 100644 --- a/tests/statictypes/tstaticimportcpp.nim +++ b/tests/statictypes/tstaticimportcpp.nim @@ -1,9 +1,9 @@ discard """ targets: "cpp" -output: "[0, 0, 10, 0]\n5\n1.2\n15\ntest" +output: "[0, 0, 10, 0]\n5\n1.2\n15\ntest\n[0, 0, 20, 0]" """ -{.emit: """ +{.emit: """/*TYPESECTION*/ template struct GenericIntType { @@ -51,3 +51,9 @@ echo c.field echo d.field echo e.field +proc plus(a, b: GenInt4): GenInt4 = + for i in 0 ..< result.data.len: + result.data[i] = a.data[i] + b.data[i] + +echo plus(a, a).data + -- cgit 1.4.1-2-gfad0 From ed79201d0b9710efb9ad89ab6ecbe7eff1742516 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 30 Apr 2018 11:16:56 +0200 Subject: make more tests green --- lib/pure/encodings.nim | 2 +- lib/pure/ospaths.nim | 2 +- tests/concepts/tstackconcept.nim | 2 +- tests/stdlib/twchartoutf8.nim | 1 - tests/vm/tcompiletimetable.nim | 1 - 5 files changed, 3 insertions(+), 5 deletions(-) (limited to 'tests/concepts') diff --git a/lib/pure/encodings.nim b/lib/pure/encodings.nim index 5840d443d..731fbbc29 100644 --- a/lib/pure/encodings.nim +++ b/lib/pure/encodings.nim @@ -36,7 +36,7 @@ when defined(windows): while i < a.len and j < b.len: if a[i] in {'-', '_'}: inc i if b[j] in {'-', '_'}: inc j - if a[i].toLower != b[j].toLower: return false + if i < a.len and j < b.len and a[i].toLowerAscii != b[j].toLowerAscii: return false inc i inc j result = i == a.len and j == b.len diff --git a/lib/pure/ospaths.nim b/lib/pure/ospaths.nim index aeb4a149e..ee2b715d3 100644 --- a/lib/pure/ospaths.nim +++ b/lib/pure/ospaths.nim @@ -477,7 +477,7 @@ proc unixToNativePath*(path: string, drive=""): string {. var i = start while i < len(path): # ../../../ --> :::: - if path[i] == '.' and path[i+1] == '.' and path[i+2] == '/': + if i+2 < path.len and path[i] == '.' and path[i+1] == '.' and path[i+2] == '/': # parent directory when defined(macos): if result[high(result)] == ':': diff --git a/tests/concepts/tstackconcept.nim b/tests/concepts/tstackconcept.nim index 2238dacb6..cb8db566d 100644 --- a/tests/concepts/tstackconcept.nim +++ b/tests/concepts/tstackconcept.nim @@ -31,7 +31,7 @@ type s.pop() is T type ValueType = T - const ValueTypeName = T.name.toUpper + const ValueTypeName = T.name.toUpperAscii proc genericAlgorithm[T](s: var Stack[T], y: T) = static: diff --git a/tests/stdlib/twchartoutf8.nim b/tests/stdlib/twchartoutf8.nim index b2f68ee32..a6602e3e3 100644 --- a/tests/stdlib/twchartoutf8.nim +++ b/tests/stdlib/twchartoutf8.nim @@ -30,7 +30,6 @@ else: result = newString(size) let res = WideCharToMultiByte(CP_UTF8, 0'i32, cast[LPWCSTR](addr(wc[0])), wclen, cstring(result), size, cstring(nil), LPBOOL(nil)) - result[size] = chr(0) doAssert size == res proc testCP(wc: WideCString, lo, hi: int) = diff --git a/tests/vm/tcompiletimetable.nim b/tests/vm/tcompiletimetable.nim index b5b84a790..e78c06536 100644 --- a/tests/vm/tcompiletimetable.nim +++ b/tests/vm/tcompiletimetable.nim @@ -4,7 +4,6 @@ discard """ 4:2 Got Hi Got Hey''' - disabled: "true" """ # bug #404 -- cgit 1.4.1-2-gfad0