Open Bug 1881931 Opened 7 months ago Updated 6 months ago

[meta] Build errors where C++23 return statements now implicitly move local variables

Categories

(Core :: General, task, P5)

task

Tracking

()

People

(Reporter: cpeterson, Unassigned)

References

(Blocks 1 open bug)

Details

(Keywords: meta)

This C++23 change causes problems for some functions returning nsCOMPtr or RefPtr objects.

Example error when compiling Firefox with -std=c++23:

docshell/base/BrowsingContext.cpp:1424:14: error: conversion function from 'nsCOMPtr<nsIPrincipal>' to 'nsIPrincipal *' invokes a deleted function
 1424 |       return principal;
      |              ^~~~~~~~~
obj-aarch64-apple-darwin23.2.0/dist/include/nsCOMPtr.h:764:3: note: 'operator nsIPrincipal *' has been explicitly marked deleted here
  764 |   operator T*() const&& = delete;
      |   ^

The deleted nsCOMPtr function now being invoked in C++23:
https://searchfox.org/mozilla-central/rev/f6b9653de71818550ea39fdb0cf11703c43c0cd1/xpcom/base/nsCOMPtr.h#761-764

The deleted RefPtr function now being invoked in C++23:
https://searchfox.org/mozilla-central/rev/f6b9653de71818550ea39fdb0cf11703c43c0cd1/mfbt/RefPtr.h#330-333

Simple code example of this C++23 build error: https://godbolt.org/z/MaP36Mh18

struct A {
  int m = 1;
  operator int() const& { return m; }
  operator int() const&& = delete;
};

int f() {
  // Expected error in >= C++11 because conversion function from 'A' to 'int'
  // invokes a deleted function, `operator int() const&&`.
  return A();
}

int g() {
  A a;

  // OK in <= C++20 because `operator int() const&` is called. New error in >=
  // C++23 because conversion function from 'A' to 'int' invokes a deleted
  // function, `operator int() const&&`.
  return a;
}
You need to log in before you can comment on or make changes to this bug.