From 58813a3b2e375d91a307645168765372fb4584c8 Mon Sep 17 00:00:00 2001 From: metagn Date: Tue, 20 Aug 2024 12:41:50 +0300 Subject: [PATCH] make all generic aliases tyAlias (#23978) fixes #23977 The problem is that for *any* body of a generic declaration, [semstmts](https://github.com/nim-lang/Nim/blob/2e4d344b43b040a4dce2c478ca13e49979e491fc/compiler/semstmts.nim#L1610-L1611) sets the sym of its value to the generic type name, and [semtypes](https://github.com/nim-lang/Nim/blob/2e4d344b43b040a4dce2c478ca13e49979e491fc/compiler/semtypes.nim#L2143) just directly gives the referenced type *specifically* when the expression is a generic body. I'm blaming `semtypes` here because it's responsible for the type given but the exact opposite behavior specifically written in makes me think generating an alias type here maybe breaks something. --- compiler/semtypes.nim | 3 ++- tests/generics/taliashijack.nim | 8 ++++++++ tests/vm/tvmmisc.nim | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 tests/generics/taliashijack.nim diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 1219d6ed83..a5cd5b5b02 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -1646,7 +1646,8 @@ proc semGeneric(c: PContext, n: PNode, s: PSym, prev: PType): PType = recomputeFieldPositions(tx, tx.n, position) proc maybeAliasType(c: PContext; typeExpr, prev: PType): PType = - if typeExpr.kind in {tyObject, tyEnum, tyDistinct, tyForward, tyGenericBody} and prev != nil: + if prev != nil and (prev.kind == tyGenericBody or + typeExpr.kind in {tyObject, tyEnum, tyDistinct, tyForward, tyGenericBody}): result = newTypeS(tyAlias, c) result.rawAddSon typeExpr result.sym = prev.sym diff --git a/tests/generics/taliashijack.nim b/tests/generics/taliashijack.nim new file mode 100644 index 0000000000..fdebadded2 --- /dev/null +++ b/tests/generics/taliashijack.nim @@ -0,0 +1,8 @@ +# issue #23977 + +type Foo[T] = int + +proc foo(T: typedesc) = + var a: T + +foo(int) diff --git a/tests/vm/tvmmisc.nim b/tests/vm/tvmmisc.nim index d28f765740..3c4b47c48d 100644 --- a/tests/vm/tvmmisc.nim +++ b/tests/vm/tvmmisc.nim @@ -566,7 +566,7 @@ block: block: static: let x = int 1 - doAssert $(x.type) == "Foo" # Foo + doAssert $(x.type) == "int" # Foo block: static: