aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2025-08-15 22:37:42 +0200
committerJakub Jelinek <jakub@gcc.gnu.org>2025-08-15 22:37:42 +0200
commit91b5fc4ae2e5c8693cc0b619562f15f90c0fdfaa (patch)
tree27ac1512ae18f42b976a94cb0417dc53bcb78f8f
parent828c48f0680e209e96bcb76a232665ed047e92cc (diff)
downloadgcc-91b5fc4ae2e5c8693cc0b619562f15f90c0fdfaa.zip
gcc-91b5fc4ae2e5c8693cc0b619562f15f90c0fdfaa.tar.gz
gcc-91b5fc4ae2e5c8693cc0b619562f15f90c0fdfaa.tar.bz2
c++: Add testcases for the defarg part of P1766R1 [PR121552]
The following patch adds some testcases for the default argument (function and template) part of the paper, making sure we diagnose multiple defargs in the same TU and when visible in modules and DTRT when some aren't visible and some are visible and they are equal. Not testing when they are different since that is IFNDR. 2025-08-15 Jakub Jelinek <jakub@redhat.com> PR c++/121552 * g++.dg/parse/defarg21.C: New test. * g++.dg/template/defarg24.C: New test. * g++.dg/modules/default-arg-4_a.C: New test. * g++.dg/modules/default-arg-4_b.C: New test. * g++.dg/modules/default-arg-5_a.C: New test. * g++.dg/modules/default-arg-5_b.C: New test.
-rw-r--r--gcc/testsuite/g++.dg/modules/default-arg-4_a.C19
-rw-r--r--gcc/testsuite/g++.dg/modules/default-arg-4_b.C36
-rw-r--r--gcc/testsuite/g++.dg/modules/default-arg-5_a.C23
-rw-r--r--gcc/testsuite/g++.dg/modules/default-arg-5_b.C35
-rw-r--r--gcc/testsuite/g++.dg/parse/defarg21.C38
-rw-r--r--gcc/testsuite/g++.dg/template/defarg24.C37
6 files changed, 188 insertions, 0 deletions
diff --git a/gcc/testsuite/g++.dg/modules/default-arg-4_a.C b/gcc/testsuite/g++.dg/modules/default-arg-4_a.C
new file mode 100644
index 0000000..fea1622
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/default-arg-4_a.C
@@ -0,0 +1,19 @@
+// C++20 P1766R1 - Mitigating minor modules maladies
+// { dg-additional-options "-fmodules-ts -Wno-global-module" }
+// { dg-module-cmi M }
+
+module;
+
+int foo (int i = 42);
+template <typename T, typename U = int>
+int bar ();
+template <typename T, int N = 42>
+int baz ();
+
+export module M;
+
+export inline int
+qux ()
+{
+ return foo () + bar <int> () + baz <int> ();
+}
diff --git a/gcc/testsuite/g++.dg/modules/default-arg-4_b.C b/gcc/testsuite/g++.dg/modules/default-arg-4_b.C
new file mode 100644
index 0000000..98b3a5f4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/default-arg-4_b.C
@@ -0,0 +1,36 @@
+// C++20 P1766R1 - Mitigating minor modules maladies
+// { dg-do run }
+// { dg-additional-options "-fmodules-ts" }
+
+import M;
+
+int
+foo (int i = 42)
+{
+ return i;
+}
+
+template <typename T, typename U = int>
+int
+bar ()
+{
+ return sizeof (U);
+}
+
+template <typename T, int N = 42>
+int
+baz ()
+{
+ return N;
+}
+
+int
+main ()
+{
+ if (foo () + bar <int> () + baz <int> () != qux ())
+ __builtin_abort ();
+ if (foo () != foo (42)
+ || bar <int> () != bar <int, int> ()
+ || baz <int> () != baz <int, 42> ())
+ __builtin_abort ();
+}
diff --git a/gcc/testsuite/g++.dg/modules/default-arg-5_a.C b/gcc/testsuite/g++.dg/modules/default-arg-5_a.C
new file mode 100644
index 0000000..38e2aee
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/default-arg-5_a.C
@@ -0,0 +1,23 @@
+// C++20 P1766R1 - Mitigating minor modules maladies
+// { dg-additional-options "-fmodules-ts -Wno-global-module" }
+// { dg-module-cmi M }
+
+module;
+
+int foo (int i = 42);
+template <typename T, typename U = int>
+int bar ();
+template <typename T, int N = 42>
+int baz ();
+
+export module M;
+
+export inline int
+qux ()
+{
+ return foo () + bar <int> () + baz <int> ();
+}
+
+export using ::foo;
+export using ::bar;
+export using ::baz;
diff --git a/gcc/testsuite/g++.dg/modules/default-arg-5_b.C b/gcc/testsuite/g++.dg/modules/default-arg-5_b.C
new file mode 100644
index 0000000..be2c22e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/default-arg-5_b.C
@@ -0,0 +1,35 @@
+// C++20 P1766R1 - Mitigating minor modules maladies
+// { dg-additional-options "-fmodules-ts" }
+
+import M;
+
+int
+foo (int i = 42) // { dg-error "default argument given for parameter 1 of 'int foo\\\(int\\\)'" }
+{
+ return i;
+}
+
+template <typename T, typename U = int> // { dg-error "redefinition of default argument for 'class U'" }
+int
+bar ()
+{
+ return sizeof (U);
+}
+
+template <typename T, int N = 42> // { dg-error "redefinition of default argument for 'int N'" }
+int
+baz ()
+{
+ return N;
+}
+
+int
+main ()
+{
+ if (foo () + bar <int> () + baz <int> () != qux ())
+ __builtin_abort ();
+ if (foo () != foo (42)
+ || bar <int> () != bar <int, int> ()
+ || baz <int> () != baz <int, 42> ())
+ __builtin_abort ();
+}
diff --git a/gcc/testsuite/g++.dg/parse/defarg21.C b/gcc/testsuite/g++.dg/parse/defarg21.C
new file mode 100644
index 0000000..0cd1ce9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/defarg21.C
@@ -0,0 +1,38 @@
+// C++20 P1766R1 - Mitigating minor modules maladies
+// { dg-do compile }
+
+int f1 (int);
+int f1 (int = 42);
+int f2 (int);
+int f2 (int = 42); // { dg-message "previous specification in 'int f2\\\(int\\\)' here" }
+int f2 (int = 42); // { dg-error "default argument given for parameter 1 of 'int f2\\\(int\\\)'" }
+int f3 (int = 42); // { dg-message "previous specification in 'int f3\\\(int\\\)' here" }
+int f3 (int = 43); // { dg-error "default argument given for parameter 1 of 'int f3\\\(int\\\)'" }
+namespace A
+{
+ int f4 (int = 1); // { dg-message "previous specification in 'int A::f4\\\(int\\\)' here" }
+ int f5 (int = 1); // { dg-message "previous specification in 'int A::f5\\\(int\\\)' here" }
+}
+namespace A
+{
+ int f4 (int = 1); // { dg-error "default argument given for parameter 1 of 'int A::f4\\\(int\\\)'" }
+ int f5 (int = 2); // { dg-error "default argument given for parameter 1 of 'int A::f5\\\(int\\\)'" }
+}
+template <int N>
+int f6 (long = 42L);
+template <int N>
+int f6 (long = 42L); // { dg-error "redeclaration of 'template<int N> int f6\\\(long int\\\)' may not have default arguments" }
+
+void
+foo ()
+{
+ int f7 (int = 42); // { dg-message "previous specification in 'int f7\\\(int\\\)' here" }
+ int f7 (int = 42); // { dg-error "default argument given for parameter 1 of 'int f7\\\(int\\\)'" }
+ int f8 (int = 42);
+ {
+ int f8 (int = 42);
+ {
+ int f8 (int = 43);
+ }
+ }
+}
diff --git a/gcc/testsuite/g++.dg/template/defarg24.C b/gcc/testsuite/g++.dg/template/defarg24.C
new file mode 100644
index 0000000..9f585af
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/defarg24.C
@@ -0,0 +1,37 @@
+// C++20 P1766R1 - Mitigating minor modules maladies
+// { dg-do compile { target c++11 } }
+
+template <int N>
+int f1 (int);
+template <int N = 42>
+int f1 (int);
+template <int N> // { dg-message "original definition appeared here" }
+int f2 (int);
+template <int N = 42>
+int f2 (int);
+template <int N = 42> // { dg-error "redefinition of default argument for 'int N'" }
+int f2 (int);
+template <int N = 42> // { dg-message "original definition appeared here" }
+int f3 (int);
+template <int N = 43> // { dg-error "redefinition of default argument for 'int N'" }
+int f3 (int);
+template <typename T>
+int f4 (int);
+template <typename T = int>
+int f4 (int);
+namespace A
+{
+ template <typename T> // { dg-message "original definition appeared here" }
+ int f5 (int);
+ template <typename T = int>
+ int f5 (int);
+ template <typename T = int> // { dg-message "original definition appeared here" }
+ int f6 (int);
+}
+namespace A
+{
+ template <typename T = int> // { dg-error "redefinition of default argument for 'class T'" }
+ int f5 (int);
+ template <typename T = long> // { dg-error "redefinition of default argument for 'class T'" }
+ int f6 (int);
+}