diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 8e019e4f6d..56435bbf1d 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -1777,7 +1777,7 @@ proc semStmtList(c: PContext, n: PNode, flags: TExprFlags): PNode = let verdict = semConstExpr(c, n[i]) if verdict.intVal == 0: - localError(result.info, "type class predicate failed") + localError(result.info, "concept predicate failed") of tyUnknown: continue else: discard if n.sons[i].typ == enforceVoidContext: #or usesResult(n.sons[i]): diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 725f8e287f..7a603a2d44 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -1040,11 +1040,12 @@ proc typeRelImpl(c: var TCandidate, f, aOrig: PType, else: isNone of tyUserTypeClass, tyUserTypeClassInst: - # consider this: 'var g: Node' *within* a concept where 'Node' - # is a concept too (tgraph) - let x = typeRel(c, a, f, flags + {trDontBind}) - if x >= isGeneric: - return isGeneric + if c.c.matchedConcept != nil: + # consider this: 'var g: Node' *within* a concept where 'Node' + # is a concept too (tgraph) + let x = typeRel(c, a, f, flags + {trDontBind}) + if x >= isGeneric: + return isGeneric else: discard case f.kind diff --git a/tests/concepts/texplain.nim b/tests/concepts/texplain.nim index 25a075fd17..417d1e5022 100644 --- a/tests/concepts/texplain.nim +++ b/tests/concepts/texplain.nim @@ -9,33 +9,33 @@ proc e(o: ExplainedConcept): int texplain.nim(65, 6) ExplainedConcept: undeclared field: 'foo' texplain.nim(65, 6) ExplainedConcept: undeclared field: '.' texplain.nim(65, 6) ExplainedConcept: expression '.' cannot be called -texplain.nim(65, 5) ExplainedConcept: type class predicate failed +texplain.nim(65, 5) ExplainedConcept: concept predicate failed texplain.nim(66, 6) ExplainedConcept: undeclared field: 'bar' texplain.nim(66, 6) ExplainedConcept: undeclared field: '.' texplain.nim(66, 6) ExplainedConcept: expression '.' cannot be called -texplain.nim(65, 5) ExplainedConcept: type class predicate failed +texplain.nim(65, 5) ExplainedConcept: concept predicate failed texplain.nim(105, 10) Hint: Non-matching candidates for e(10) proc e(o: ExplainedConcept): int texplain.nim(65, 6) ExplainedConcept: undeclared field: 'foo' texplain.nim(65, 6) ExplainedConcept: undeclared field: '.' texplain.nim(65, 6) ExplainedConcept: expression '.' cannot be called -texplain.nim(65, 5) ExplainedConcept: type class predicate failed +texplain.nim(65, 5) ExplainedConcept: concept predicate failed texplain.nim(66, 6) ExplainedConcept: undeclared field: 'bar' texplain.nim(66, 6) ExplainedConcept: undeclared field: '.' texplain.nim(66, 6) ExplainedConcept: expression '.' cannot be called -texplain.nim(65, 5) ExplainedConcept: type class predicate failed +texplain.nim(65, 5) ExplainedConcept: concept predicate failed texplain.nim(109, 20) Error: type mismatch: got (NonMatchingType) but expected one of: proc e(o: ExplainedConcept): int -texplain.nim(65, 5) ExplainedConcept: type class predicate failed +texplain.nim(65, 5) ExplainedConcept: concept predicate failed proc e(i: int): int texplain.nim(110, 20) Error: type mismatch: got (NonMatchingType) but expected one of: proc r(o: RegularConcept): int -texplain.nim(69, 5) RegularConcept: type class predicate failed +texplain.nim(69, 5) RegularConcept: concept predicate failed proc r[T](a: SomeNumber; b: T; c: auto) proc r(i: string): int @@ -49,12 +49,12 @@ proc f(o: NestedConcept) texplain.nim(69, 6) RegularConcept: undeclared field: 'foo' texplain.nim(69, 6) RegularConcept: undeclared field: '.' texplain.nim(69, 6) RegularConcept: expression '.' cannot be called -texplain.nim(69, 5) RegularConcept: type class predicate failed +texplain.nim(69, 5) RegularConcept: concept predicate failed texplain.nim(70, 6) RegularConcept: undeclared field: 'bar' texplain.nim(70, 6) RegularConcept: undeclared field: '.' texplain.nim(70, 6) RegularConcept: expression '.' cannot be called -texplain.nim(69, 5) RegularConcept: type class predicate failed -texplain.nim(73, 5) NestedConcept: type class predicate failed +texplain.nim(69, 5) RegularConcept: concept predicate failed +texplain.nim(73, 5) NestedConcept: concept predicate failed ''' line: 119 errormsg: "type mismatch: got (MatchingType)" diff --git a/tests/concepts/tgraph.nim b/tests/concepts/tgraph.nim index a0177a0430..985f04a615 100644 --- a/tests/concepts/tgraph.nim +++ b/tests/concepts/tgraph.nim @@ -1,29 +1,34 @@ -discard """ - output: '''XY is Node -MyGraph is Graph''' -""" # bug #3452 import math type - Node* = concept n - `==`(n, n) is bool + Node* = concept n + `==`(n, n) is bool - Graph* = concept g - var x: Node - distance(g, x, x) is float + Graph1* = concept g + type N = Node + distance(g, N, N) is float - XY* = tuple[x, y: int] + Graph2 = concept g + distance(g, Node, Node) is float - MyGraph* = object - points: seq[XY] + Graph3 = concept g + var x: Node + distance(g, x, x) is float -if XY is Node: - echo "XY is Node" + XY* = tuple[x, y: int] + + MyGraph* = object + points: seq[XY] + +static: + assert XY is Node proc distance*( g: MyGraph, a, b: XY): float = - sqrt( pow(float(a.x - b.x), 2) + pow(float(a.y - b.y), 2) ) + sqrt( pow(float(a.x - b.x), 2) + pow(float(a.y - b.y), 2) ) -if MyGraph is Graph: - echo "MyGraph is Graph" +static: + assert MyGraph is Graph1 + assert MyGraph is Graph2 + assert MyGraph is Graph3 diff --git a/tests/concepts/twrapconcept.nim b/tests/concepts/twrapconcept.nim new file mode 100644 index 0000000000..25a855e341 --- /dev/null +++ b/tests/concepts/twrapconcept.nim @@ -0,0 +1,22 @@ +discard """ + errormsg: "type mismatch: got (string)" + line: 21 + nimout: "twrapconcept.nim(11, 5) Foo: concept predicate failed" +""" + +# https://github.com/nim-lang/Nim/issues/5127 + +type + Foo = concept foo + foo.get is int + + FooWrap[F: Foo] = object + foo: F + +proc get(x: int): int = x + +proc wrap[F: Foo](foo: F): FooWrap[F] = FooWrap[F](foo: foo) + +let x = wrap(12) +let y = wrap "string" +