From d2df4b85f3472768d38503d1ace5194aa839e5ef Mon Sep 17 00:00:00 2001 From: Jakub Labenski Date: Mon, 16 Nov 2026 09:54:01 +0100 Subject: [PATCH] Fix constexpr constructors in dllimport classes [GH #583] --- src/class_decl.c | 6 ++- ...583-dllimport-constexpr-defaulted-ctor.txt | 17 ++++++++ .../default.1.1.txt | 5 +++ .../default.2.1.txt | 5 +++ .../default.3.1.txt | 5 +++ .../default.1.1.txt | 13 ++++++ .../default.2.1.txt | 13 ++++++ ...dllimport-constexpr-defaulted-ctor.sft.cpp | 40 +++++++++++++++++++ ...rt-implicit-constexpr-default-ctor.sft.cpp | 20 ++++++++++ 9 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 src/pending_changes/gh-583-dllimport-constexpr-defaulted-ctor.txt create mode 100644 tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.3.1.txt create mode 100644 tests/tests/regressions/.dllimport-implicit-constexpr-default-ctor.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.dllimport-implicit-constexpr-default-ctor.rto/default.2.1.txt create mode 100644 tests/tests/regressions/dllimport-constexpr-defaulted-ctor.sft.cpp create mode 100644 tests/tests/regressions/dllimport-implicit-constexpr-default-ctor.sft.cpp diff --git a/src/class_decl.c b/src/class_decl.c index 30cfd6a7ba..c67777e2f4 100644 --- a/src/class_decl.c +++ b/src/class_decl.c @@ -23445,7 +23445,8 @@ skip the base class portion of the check. field initializer or the type has a constexpr default constructor, and (c) every direct base class has an unambiguous constexpr default constructor. In Microsoft mode, the generated default constructor of a - dllimport class with virtual functions is not "constexpr" either. Don't + dllimport class with virtual functions is not implicitly "constexpr". + An explicitly declared constexpr constructor remains constexpr. Don't attempt to check this for nonreal classes because it is not always meaningful and the downstream code cannot always handle such classes. */ push_class_reactivation_scope(class_type, /*extend_namespace=*/FALSE); @@ -23455,7 +23456,8 @@ skip the base class portion of the check. (!check_bases || bases_initialized_for_constexpr_constructor(class_type)) #if MICROSOFT_EXTENSIONS_ALLOWED - && !((class_type_supp(class_type)->decl_modifiers & DM_DLLIMPORT) && + && !(microsoft_mode && !ctor_rp->is_declared_constexpr && + (class_type_supp(class_type)->decl_modifiers & DM_DLLIMPORT) && class_type->variant.class_struct_union .any_virtual_functions_including_in_base_classes) #endif /* MICROSOFT_EXTENSIONS_ALLOWED */ diff --git a/src/pending_changes/gh-583-dllimport-constexpr-defaulted-ctor.txt b/src/pending_changes/gh-583-dllimport-constexpr-defaulted-ctor.txt new file mode 100644 index 0000000000..7f5029e486 --- /dev/null +++ b/src/pending_changes/gh-583-dllimport-constexpr-defaulted-ctor.txt @@ -0,0 +1,17 @@ +[GH #583] +Microsoft C++ compatibility: constexpr constructors in dllimport classes + +In Microsoft mode, explicitly constexpr defaulted default constructors of +dllimport classes with virtual functions were incorrectly rejected. They are +now accepted if they satisfy the usual constexpr requirements. In Microsoft +mode, implicitly generated constructors keep their previous behavior. For +example: + + struct __declspec(dllimport) X { + constexpr X() = default; // Previously an error. Now okay. + virtual ~X(); + }; + +In GNU and Clang emulation modes, dllimport no longer prevents an implicitly +declared default constructor of a class with virtual functions from being +constexpr. diff --git a/tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.1.1.txt b/tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.1.1.txt new file mode 100644 index 0000000000..78323b2f93 --- /dev/null +++ b/tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.1.1.txt @@ -0,0 +1,5 @@ +fe_only -DTEST_NUMBER=1 --microsoft Test_name.c +"Test_name.c", line 25: warning: base class dllexport/dllimport specification differs from that of the derived class + struct __declspec(dllimport) ImportedInheritedVirtualFunction : Base { + ^ + diff --git a/tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.2.1.txt b/tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.2.1.txt new file mode 100644 index 0000000000..4b49f78412 --- /dev/null +++ b/tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.2.1.txt @@ -0,0 +1,5 @@ +fe_only -DTEST_NUMBER=2 --c++17 --g++ --ms_extensions Test_name.c +"Test_name.c", line 25: warning: base class dllexport/dllimport specification differs from that of the derived class + struct __declspec(dllimport) ImportedInheritedVirtualFunction : Base { + ^ + diff --git a/tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.3.1.txt b/tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.3.1.txt new file mode 100644 index 0000000000..5e60b8b9c5 --- /dev/null +++ b/tests/tests/regressions/.dllimport-constexpr-defaulted-ctor.rto/default.3.1.txt @@ -0,0 +1,5 @@ +fe_only -DTEST_NUMBER=3 --c++17 --clang --ms_extensions Test_name.c +"Test_name.c", line 25: warning: base class dllexport/dllimport specification differs from that of the derived class + struct __declspec(dllimport) ImportedInheritedVirtualFunction : Base { + ^ + diff --git a/tests/tests/regressions/.dllimport-implicit-constexpr-default-ctor.rto/default.1.1.txt b/tests/tests/regressions/.dllimport-implicit-constexpr-default-ctor.rto/default.1.1.txt new file mode 100644 index 0000000000..d7c6f8bbf6 --- /dev/null +++ b/tests/tests/regressions/.dllimport-implicit-constexpr-default-ctor.rto/default.1.1.txt @@ -0,0 +1,13 @@ +fe_only -DTEST_NUMBER=1 --c++17 --g++ --ms_extensions Test_name.c +"Test_name.c", line 18: warning: base class dllexport/dllimport specification differs from that of the derived class + struct __declspec(dllimport) ImportedWithInheritedVirtualFunction : Base {}; + ^ + +"Test_name.c", line 12: warning: variable "imported" was declared but never referenced + constexpr ImportedWithImplicitDefaultConstructor imported{}; + ^ + +"Test_name.c", line 20: warning: variable "inherited" was declared but never referenced + constexpr ImportedWithInheritedVirtualFunction inherited{}; + ^ + diff --git a/tests/tests/regressions/.dllimport-implicit-constexpr-default-ctor.rto/default.2.1.txt b/tests/tests/regressions/.dllimport-implicit-constexpr-default-ctor.rto/default.2.1.txt new file mode 100644 index 0000000000..d5e9f85100 --- /dev/null +++ b/tests/tests/regressions/.dllimport-implicit-constexpr-default-ctor.rto/default.2.1.txt @@ -0,0 +1,13 @@ +fe_only -DTEST_NUMBER=2 --c++17 --clang --ms_extensions Test_name.c +"Test_name.c", line 18: warning: base class dllexport/dllimport specification differs from that of the derived class + struct __declspec(dllimport) ImportedWithInheritedVirtualFunction : Base {}; + ^ + +"Test_name.c", line 12: warning: variable "imported" was declared but never referenced + constexpr ImportedWithImplicitDefaultConstructor imported{}; + ^ + +"Test_name.c", line 20: warning: variable "inherited" was declared but never referenced + constexpr ImportedWithInheritedVirtualFunction inherited{}; + ^ + diff --git a/tests/tests/regressions/dllimport-constexpr-defaulted-ctor.sft.cpp b/tests/tests/regressions/dllimport-constexpr-defaulted-ctor.sft.cpp new file mode 100644 index 0000000000..38411d76c2 --- /dev/null +++ b/tests/tests/regressions/dllimport-constexpr-defaulted-ctor.sft.cpp @@ -0,0 +1,40 @@ +//type:fp +//options:--microsoft:--c++17 --g++ --ms_extensions:--c++17 --clang --ms_extensions + +// An explicitly constexpr defaulted constructor in a dllimport class must +// not be rejected just because the class has virtual functions. Check +// Microsoft, GNU, and Clang modes with direct and inherited virtual functions. + +// The original reproducer has a virtual destructor. +class __declspec(dllimport) QRunnable { +public: + constexpr QRunnable() noexcept = default; + virtual ~QRunnable(); +}; + +struct __declspec(dllimport) ImportedVirtualFunction { + constexpr ImportedVirtualFunction() noexcept = default; + virtual void run(); +}; + +struct Base { + virtual void run(); +}; + +// A virtual function inherited from a base also exercises the restriction. +struct __declspec(dllimport) ImportedInheritedVirtualFunction : Base { + constexpr ImportedInheritedVirtualFunction() noexcept = default; +}; + +// A default member initializer must still pass the usual constexpr checks. +struct __declspec(dllimport) ImportedWithMemberInitializer { + constexpr ImportedWithMemberInitializer() noexcept = default; + virtual void run(); + int value = 42; +}; + +// Control case: without virtual functions, the constructor was already valid. +struct __declspec(dllimport) ImportedWithoutVirtualFunction { + constexpr ImportedWithoutVirtualFunction() noexcept = default; + int value = 42; +}; diff --git a/tests/tests/regressions/dllimport-implicit-constexpr-default-ctor.sft.cpp b/tests/tests/regressions/dllimport-implicit-constexpr-default-ctor.sft.cpp new file mode 100644 index 0000000000..60fc9b77fd --- /dev/null +++ b/tests/tests/regressions/dllimport-implicit-constexpr-default-ctor.sft.cpp @@ -0,0 +1,20 @@ +//type:fp +//options:--c++17 --g++ --ms_extensions:--c++17 --clang --ms_extensions + +// The Microsoft-only dllimport restriction on implicit constexpr default +// constructors must not apply in GNU or Clang mode. Construct constexpr +// objects to check both directly declared and inherited virtual functions. + +struct __declspec(dllimport) ImportedWithImplicitDefaultConstructor { + virtual void run(); +}; + +constexpr ImportedWithImplicitDefaultConstructor imported{}; + +struct Base { + virtual void run(); +}; + +struct __declspec(dllimport) ImportedWithInheritedVirtualFunction : Base {}; + +constexpr ImportedWithInheritedVirtualFunction inherited{};