Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clang either crashes or choose wrong destructor when using multiple destructors using C++20 constraints #49914

Closed
geekyMrK mannequin opened this issue Jun 3, 2021 · 2 comments
Labels
bugzilla Issues migrated from bugzilla c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts duplicate Resolved as duplicate

Comments

@geekyMrK
Copy link
Mannequin

geekyMrK mannequin commented Jun 3, 2021

Bugzilla Link 50570
Version trunk
OS All
Attachments 1 of the program which i described above (also have links to godbolt)
CC @zygoloid

Extended Description

Overview: When we use multiple destructor (1 default and 1 constraint 1) , using just a simple constraint, then 1 of 2 things happen:

A.) If we define the constraint destructor first, and then define default destructor:
https://godbolt.org/z/PWbfzscs4

#include <iostream>
#include <concepts>

template <typename T>
concept hasReleaseMethod = requires(T t)
{
	t.release();
};

template <typename T>
class ComWrapper
{
	T t;

  public:
	~ComWrapper() requires requires(T t) { t.release(); }
	{
		std::cout << "destructor";
		t.release();
	}

	~ComWrapper() = default;
};

struct hasRelease
{
	int a = 23;
	void release()
	{
		std::cout << "in release method:\n"
				  << a;
	}
	void method()
	{
	}
};

struct notHadRelease
{
};

int main()
{
	ComWrapper<hasRelease> Ob;
	ComWrapper<notHadRelease> ob2;
};

Actual result: the compiler crashed (you can see the error through the cooler l compiler explorer (godbolt)).

Expected result: the clang should choose the constraint destructor on ob1 (hasRelease) and default destructor on ob2. The GCC produce the expected output

The program works on GCC and didn't work on clang, where we declared the constraint destructor first and then default destructor (GCC compiler & executor on the upper window and Clang on the lower)
https://godbolt.org/z/PWbfzscs4
You can check on godbolt through the above link

B.) When we declared the default destructor first, & then the constraint destructor

//The program is same as above except the default destructor is declared first

#include <iostream>
#include <concepts>

template <typename T>
concept hasReleaseMethod = requires(T t)
{
	t.release();
};

template <typename T>
class ComWrapper
{
	T t;

  public:
	~ComWrapper() = default;

	~ComWrapper() requires requires(T t) { t.release(); }
	{
		std::cout << "destructor";
		t.release();
	}
};

struct hasRelease
{
	int a = 23;
	void release()
	{
		std::cout << "in release method:\n"
				  << a;
	}
	void method()
	{
	}
};

struct notHadRelease
{
};

int main()
{
	ComWrapper<hasRelease> Ob;
	ComWrapper<notHadRelease> ob2;
};

https://godbolt.org/z/ss11d5cdd

When we declared the default destructor first, & then the constraint destructor , the Clang compiles, but the default destructor is called & the constraint destructor is discarded every time(see the executor and assembly in clang).
Whereas, GCC executes correctly.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@Quuxplusone Quuxplusone added the concepts C++20 concepts label Jan 16, 2022
@royjacobson
Copy link
Contributor

Duplicate of #45614

@royjacobson royjacobson marked this as a duplicate of #45614 Apr 20, 2022
@royjacobson royjacobson added the duplicate Resolved as duplicate label Apr 20, 2022
@EugeneZelenko EugeneZelenko added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Apr 20, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 20, 2022

@llvm/issue-subscribers-clang-frontend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts duplicate Resolved as duplicate
Projects
Status: No status
Development

No branches or pull requests

4 participants