llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Spurious interaction between noexcept specs. for move/copy assignment

Open ojrosten opened this issue 8 years ago • 4 comments

Bugzilla Link 38983
Version unspecified
OS MacOS X
CC @DougGregor,@zygoloid

Extended Description

The following (contrived) code appears to indicate a spurious dependency between the exception specifications for the move/copy assignment operators.

template<class T>
class wrapper
{
  T& m_Ref; 
// Reported problem disappears if reference_wrapper is used instead
public:
  wrapper(T& ref) : m_Ref{ref} {}
        
  wrapper(const wrapper&)            = default;
  wrapper(wrapper&&)                 noexcept = default;
  wrapper& operator=(const wrapper&) noexcept(false){} 
  // 'false' required to reveal bug; 
  //removing false fixes compilation
  wrapper& operator=(wrapper&&)      noexcept = default;

};

template<class T, class W>
class thing
{
private:
  W m_Member{};
public:
  thing(T& ref) : m_Member{ref} {}

  thing(const thing&) = default;
  thing(thing&&)      = default;

  thing& operator=(const thing&)     = default;
  thing& operator=(thing&&) noexcept = default;
};

int x{5};
wrapper<int> w{x}; // fine
thing<int, wrapper<int>> foo{x}; 

Compiler error message:

  exception specification of
  explicitly defaulted move assignment operator does not match the
  calculated one
    thing& operator=(thing&&) noexcept = default;

The last line of code fails to compile due to the exception spec of the move assignment operator not matching the calculated one.

Compilation may be fixed by changing the exception spec. of the copy assignment operator.

ojrosten avatar Sep 18 '18 12:09 ojrosten

Thanks for clarifying that.

I wonder if it would make sense for clang to issue a warning when a special member function is deleted if a user not merely defaults said function but explicitly provides a noexcept specification.

ojrosten avatar Sep 26 '18 19:09 ojrosten

The defaulted move assignment operator of wrapper is defined as deleted because wrapper has a reference member: http://eel.is/c++draft/class#copy.assign-7.3

As a consequence, overload resolution for the wrapper assignment in thing's defaulted move assignment ignores the move assignment of wrapper and instead uses wrapper's copy assignment operator: http://eel.is/c++draft/over#match.funcs-8.sentence-1

So Clang is correct that a defaulted move assignment operator for thing would be noexcept(false).

However...

As of C++ DR 1778 (which clang does not yet implement), the result of this situation was changed from the program being ill-formed to the assignment operator of thing being deleted: http://eel.is/c++draft/dcl.fct.def#default-2.4

... and the reason that Clang does not implement DR 1778 is because it is actually impossible to do so (because it violates necessary invariants by changing which special members are deleted after a class is completed).

@llvm/issue-subscribers-clang-frontend

Author: Oliver Rosten (ojrosten)

| | | | --- | --- | | Bugzilla Link | [38983](https://llvm.org/bz38983) | | Version | unspecified | | OS | MacOS X | | CC | @DougGregor,@zygoloid |

Extended Description

The following (contrived) code appears to indicate a spurious dependency between the exception specifications for the move/copy assignment operators.

template&lt;class T&gt;
class wrapper
{
  T&amp; m_Ref; 
// Reported problem disappears if reference_wrapper is used instead
public:
  wrapper(T&amp; ref) : m_Ref{ref} {}
        
  wrapper(const wrapper&amp;)            = default;
  wrapper(wrapper&amp;&amp;)                 noexcept = default;
  wrapper&amp; operator=(const wrapper&amp;) noexcept(false){} 
  // 'false' required to reveal bug; 
  //removing false fixes compilation
  wrapper&amp; operator=(wrapper&amp;&amp;)      noexcept = default;

};

template&lt;class T, class W&gt;
class thing
{
private:
  W m_Member{};
public:
  thing(T&amp; ref) : m_Member{ref} {}

  thing(const thing&amp;) = default;
  thing(thing&amp;&amp;)      = default;

  thing&amp; operator=(const thing&amp;)     = default;
  thing&amp; operator=(thing&amp;&amp;) noexcept = default;
};

int x{5};
wrapper&lt;int&gt; w{x}; // fine
thing&lt;int, wrapper&lt;int&gt;&gt; foo{x}; 

Compiler error message:

  exception specification of
  explicitly defaulted move assignment operator does not match the
  calculated one
    thing&amp; operator=(thing&amp;&amp;) noexcept = default;

The last line of code fails to compile due to the exception spec of the move assignment operator not matching the calculated one.

Compilation may be fixed by changing the exception spec. of the copy assignment operator.

llvmbot avatar Mar 10 '25 18:03 llvmbot

Bisected: the reported behavior stops reproducing since https://github.com/llvm/llvm-project/commit/2b45b267dab4

Godbolt: https://godbolt.org/z/3EEr7zzz8

  • Clang 8.0.0: Reports exception specification of explicitly defaulted move assignment operator does not match the calculated one at line 30
  • Clang 9.0.0: No longer reports the exception-specification error
  • Clang (trunk): Compiles without the exception-specification error

This is only an observed change in behavior. I have not checked whether the change in this range addresses the cause of this report or only stops it from reproducing; whether that resolves the issue is up to the maintainers.

Prepared with assistance from Codex.

hongtaihu avatar Nov 26 '26 08:11 hongtaihu