Skip to content

Commit

Permalink
Fix #5127
Browse files Browse the repository at this point in the history
  • Loading branch information
zah committed Jun 19, 2017
1 parent 816fe8d commit 17b43f8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 32 deletions.
2 changes: 1 addition & 1 deletion compiler/semstmts.nim
Expand Up @@ -1740,7 +1740,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]):
Expand Down
11 changes: 6 additions & 5 deletions compiler/sigmatch.nim
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions tests/concepts/texplain.nim
Expand Up @@ -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
Expand All @@ -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)"
Expand Down
39 changes: 22 additions & 17 deletions 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

22 changes: 22 additions & 0 deletions 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"

0 comments on commit 17b43f8

Please sign in to comment.