Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/class_decl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */
Expand Down
17 changes: 17 additions & 0 deletions src/pending_changes/gh-583-dllimport-constexpr-defaulted-ctor.txt
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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 {
^

Original file line number Diff line number Diff line change
@@ -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 {
^

Original file line number Diff line number Diff line change
@@ -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 {
^

Original file line number Diff line number Diff line change
@@ -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{};
^

Original file line number Diff line number Diff line change
@@ -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{};
^

40 changes: 40 additions & 0 deletions tests/tests/regressions/dllimport-constexpr-defaulted-ctor.sft.cpp
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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{};
Loading