summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2024-08-25 23:24:20 +0300
committerGitHub <noreply@github.com>2024-08-25 22:24:20 +0200
commit09dcff71c82147b28059c6470bd921afab9c825e (patch)
treeb513a7a55fa5eea14b139685da7c547ba969b770 /tests
parent0d53b6e027a66bc60ca853d57c019338bb73ba0b (diff)
downloadNim-09dcff71c82147b28059c6470bd921afab9c825e.tar.gz
generate symchoice for ambiguous types in templates & generics + handle types in symchoices (#23997)
fixes #23898, supersedes #23966 and #23990

Since #20631 ambiguous type symbols in templates are rejected outright,
now we generate a symchoice for type nodes if they're ambiguous, a
generalization of what was done in #22375. This is done for generics as
well. Symchoices also handle type symbols better now, ensuring their
type is a `typedesc` type; this probably isn't necessary for everything
to work but it makes the logic more robust.

Similar to #23989, we have to prepare for the fact that ambiguous type
symbols behave differently than normal type symbols and either error
normally or relegate to other routine symbols if the symbol is being
called. Generating a symchoice emulates this behavior, `semExpr` will
find the type symbol first, but since the symchoice has other symbols,
it will count as an ambiguous type symbol.

I know it seems spammy to carry around an ambiguity flag everywhere, but
in the future when we have something like #23104 we could just always
generate a symchoice, and the symchoice itself would carry the info of
whether the first symbol was ambiguous. But this could harm compiler
performance/memory use, it might be better to generate it only when we
have to, which in the case of type symbols is only when they're
ambiguous.
Diffstat (limited to 'tests')
-rw-r--r--tests/lookups/mambtype1.nim (renamed from tests/lookups/mqualifiedamb1.nim)0
-rw-r--r--tests/lookups/mambtype2.nim4
-rw-r--r--tests/lookups/mqualifiedamb2.nim4
-rw-r--r--tests/lookups/tambtype.nim20
-rw-r--r--tests/lookups/tqualifiedamb.nim4
5 files changed, 24 insertions, 8 deletions
diff --git a/tests/lookups/mqualifiedamb1.nim b/tests/lookups/mambtype1.nim
index 47046142e..47046142e 100644
--- a/tests/lookups/mqualifiedamb1.nim
+++ b/tests/lookups/mambtype1.nim
diff --git a/tests/lookups/mambtype2.nim b/tests/lookups/mambtype2.nim
new file mode 100644
index 000000000..cf622466b
--- /dev/null
+++ b/tests/lookups/mambtype2.nim
@@ -0,0 +1,4 @@
+import ./mambtype1
+export mambtype1
+template K*(kind: static int): auto = typedesc[mambtype1.K]
+template B*(kind: static int): auto = typedesc[mambtype1.K]
diff --git a/tests/lookups/mqualifiedamb2.nim b/tests/lookups/mqualifiedamb2.nim
deleted file mode 100644
index 3ea5bd04f..000000000
--- a/tests/lookups/mqualifiedamb2.nim
+++ /dev/null
@@ -1,4 +0,0 @@
-import ./mqualifiedamb1
-export mqualifiedamb1
-template K*(kind: static int): auto = typedesc[mqualifiedamb1.K]
-template B*(kind: static int): auto = typedesc[mqualifiedamb1.K]
diff --git a/tests/lookups/tambtype.nim b/tests/lookups/tambtype.nim
new file mode 100644
index 000000000..a292db83a
--- /dev/null
+++ b/tests/lookups/tambtype.nim
@@ -0,0 +1,20 @@
+import ./mambtype2
+
+block: # issue #23893
+  discard default(K(0))       # works
+  discard default(mambtype2.B(0))     # works
+  discard default(mambtype2.K(0))     # doesn't work
+
+block: # issue #23898, in template
+  template r() =
+    discard default(B(0))     # compiles
+    discard default(mambtype2.B(0))   # compiles
+    discard default(K(0))     # does not compile
+  r()
+
+block: # in generics
+  proc foo[T]() =
+    discard default(B(0))     # compiles
+    discard default(mambtype2.B(0))   # compiles
+    discard default(K(0))     # does not compile
+  foo[int]()
diff --git a/tests/lookups/tqualifiedamb.nim b/tests/lookups/tqualifiedamb.nim
deleted file mode 100644
index a5e1955f3..000000000
--- a/tests/lookups/tqualifiedamb.nim
+++ /dev/null
@@ -1,4 +0,0 @@
-import ./mqualifiedamb2
-discard default(K(0))       # works
-discard default(mqualifiedamb2.B(0))     # works
-discard default(mqualifiedamb2.K(0))     # doesn't work