Fix fixAbstractType for user defined typeclasses, fixes #19730 & #18409 (#19732)

This commit is contained in:
nc-x
2022-04-30 19:28:58 +05:30
committed by GitHub
parent e4a2c2d474
commit 4680ab61c0
3 changed files with 58 additions and 1 deletions

View File

@@ -645,7 +645,7 @@ proc fixAbstractType(c: PContext, n: PNode) =
skipTypes(it.typ, abstractVar).kind notin {tyOpenArray, tyVarargs}:
if skipTypes(it[1].typ, abstractVar).kind in
{tyNil, tyTuple, tySet} or it[1].isArrayConstr:
var s = skipTypes(it.typ, abstractVar)
var s = skipTypes(it.typ, abstractVar + tyUserTypeClasses)
if s.kind != tyUntyped:
changeType(c, it[1], s, check=true)
n[i] = it[1]

37
tests/concepts/t18409.nim Normal file
View File

@@ -0,0 +1,37 @@
discard """
action: "compile"
"""
# A vector space over a field F concept.
type VectorSpace*[F] = concept x, y, type V
vector_add(x, y) is V
scalar_mul(x, F) is V
dimension(V) is Natural
# Real numbers (here floats) form a vector space.
func vector_add*(v: float, w: float): float = v + w
func scalar_mul*(v: float, s: float): float = v * s
func dimension*(x: typedesc[float]): Natural = 1
# 2-tuples of real numbers form a vector space.
func vector_add*(v, w: (float, float)): (float, float) =
(vector_add(v[0], w[0]), vector_add(v[1], w[1]))
func scalar_mul*(v: (float, float), s: float): (float, float) =
(scalar_mul(v[0], s), scalar_mul(v[1], s))
func dimension*(x: typedesc[(float, float)]): Natural = 2
# Check concept requirements.
assert float is VectorSpace
assert (float, float) is VectorSpace
# Commutivity axiom for vector spaces over the same field.
func axiom_commutivity*[F](u, v: VectorSpace[F]): bool =
vector_add(u, v) == vector_add(v, u)
# This is okay.
assert axiom_commutivity(2.2, 3.3)
# This is not.
assert axiom_commutivity((2.2, 3.3), (4.4, 5.5))

20
tests/concepts/t19730.nim Normal file
View File

@@ -0,0 +1,20 @@
discard """
output: '''1.01.01.01.0
1.01.01.01.0
'''
"""
type
Color = concept c
c.r is SomeFloat
c.g is SomeFloat
c.b is SomeFloat
c.a is SomeFloat
proc useColor(color: Color) =
echo(color.r, color.g, color.b, color.a)
let color = (r: 1.0, g: 1.0, b: 1.0, a: 1.0)
useColor(color)
useColor((r: 1.0, g: 1.0, b: 1.0, a: 1.0))