Skip to content

MacOS g++-12 compile default linker error and mold linker with bus error #23

@vickiegpt

Description

@vickiegpt

When I was trying to compile the project in MacOS 13 with default linker, it outcomes

image

When I switched linker to mold, the error becomes runtime:

image

And I test the mvp with

#include <iostream>
#include <map>
#include <string>
#include <cassert>
#include <vector>
#include <proxy/proxy.h>


enum class LifetimeOperationType {
  kNone,
  kValueConstruction,
  kInitializerListConstruction,
  kCopyConstruction,
  kMoveConstruction,
  kDestruction
};

struct LifetimeOperation {
  LifetimeOperation(int id, LifetimeOperationType type) : id_(id), type_(type) {}

  bool operator==(const LifetimeOperation& rhs) const
      { return id_ == rhs.id_ && type_ == rhs.type_; }

  int id_;
  LifetimeOperationType type_;
};

struct ConstructionFailure : std::exception {
  ConstructionFailure(LifetimeOperationType type) : type_(type) {}

  LifetimeOperationType type_;
};

class LifetimeTracker {
 public:
  LifetimeTracker() = default;
  LifetimeTracker(const LifetimeTracker&) = delete;

  class Session {
   public:
    Session(LifetimeTracker* host)
        : id_(host->AllocateId(LifetimeOperationType::kValueConstruction)),
          host_(host) {}
    Session(std::initializer_list<int>, LifetimeTracker* host)
        : id_(host->AllocateId(LifetimeOperationType::kInitializerListConstruction)),
          host_(host) {}
    Session(const Session& rhs)
        : id_(rhs.host_->AllocateId(LifetimeOperationType::kCopyConstruction)),
          host_(rhs.host_) {}
    Session(Session&& rhs) noexcept :
          id_(rhs.host_->AllocateId(LifetimeOperationType::kMoveConstruction)),
          host_(rhs.host_) {}
    ~Session() { host_->ops_.emplace_back(id_, LifetimeOperationType::kDestruction); }
    Session& operator*() { return *this; }
    friend std::string to_string(const Session& self) { return "Session " + std::to_string(self.id_); }

   private:
    int id_;
    LifetimeTracker* const host_;
  };

  const std::vector<LifetimeOperation>& GetOperations() const { return ops_; }
  void ThrowOnNextConstruction() { throw_on_next_construction_ = true; }

 private:
  int AllocateId(LifetimeOperationType operation_type) {
    if (throw_on_next_construction_) {
      throw_on_next_construction_ = false;
      throw ConstructionFailure{ operation_type };
    }
    ops_.emplace_back(++max_id_, operation_type);
    return max_id_;
  }

  int max_id_ = 0;
  bool throw_on_next_construction_ = false;
  std::vector<LifetimeOperation> ops_;
};

struct ToString : pro::dispatch<std::string()> {
  template <class T>
  std::string operator()(const T& self) {
    using std::to_string;
    return to_string(self);
  }
};


struct TestFacade : pro::facade<ToString> {
  static constexpr auto minimum_copyability = pro::constraint_level::nontrivial;
};

int main() {
  LifetimeTracker tracker;
  std::vector<LifetimeOperation> expected_ops;
  {
    tracker.ThrowOnNextConstruction();
    bool exception_thrown = false;
    try {
      pro::proxy<TestFacade> p{ std::in_place_type<LifetimeTracker::Session>, &tracker };
    } catch (const ConstructionFailure& e) {
      exception_thrown = true;
      assert(e.type_ ==LifetimeOperationType::kValueConstruction);
    }
    assert(exception_thrown==true);
    assert(tracker.GetOperations() == expected_ops);
  }
  return 0;
}

The TestFacade outcomes the std::__is_constant_evaluated segfault. How to resolve this issue in MacOS g++?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions