Skip to content

Commit 209f6d5

Browse files
committed
Don't emit Cpp1 ref-qualifiers on virtual functions, closes #694
1 parent e57eca7 commit 209f6d5

8 files changed

+32
-26
lines changed

regression-tests/test-results/pure2-print.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ CPP2_REQUIRES_ (true)
2828
{
2929
protected: [[nodiscard]] static auto f() -> int;
3030

31-
public: [[nodiscard]] virtual auto g(cpp2::in<int> i) const& -> int;
31+
public: [[nodiscard]] virtual auto g(cpp2::in<int> i) const -> int;
3232

3333

3434
#line 29 "pure2-print.cpp2"
@@ -96,7 +96,7 @@ requires (true)
9696
#line 10 "pure2-print.cpp2"
9797
[[nodiscard]] auto outer::mytype::f() -> int { return 42; }
9898

99-
[[nodiscard]] auto outer::mytype::g(cpp2::in<int> i) const& -> int{
99+
[[nodiscard]] auto outer::mytype::g(cpp2::in<int> i) const -> int{
100100
auto s {"string literal"};
101101
int ret {i};
102102
int const* const p {&ret};

regression-tests/test-results/pure2-types-down-upcast.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class B;
1818
class A {
1919
public: int i {0};
2020

21-
public: virtual auto const_foo() const& -> void;
21+
public: virtual auto const_foo() const -> void;
2222
public: auto mut_foo() & -> void;
2323

2424
public: A() = default;
@@ -65,7 +65,7 @@ auto test_down() -> void;
6565

6666

6767
#line 4 "pure2-types-down-upcast.cpp2"
68-
auto A::const_foo() const& -> void{std::cout << "const foo \n"; }
68+
auto A::const_foo() const -> void{std::cout << "const foo \n"; }
6969
auto A::mut_foo() & -> void{std::cout << "foo \n"; }
7070

7171
#line 13 "pure2-types-down-upcast.cpp2"

regression-tests/test-results/pure2-types-inheritance.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Cyborg;
2727

2828
#line 2 "pure2-types-inheritance.cpp2"
2929
class Human {
30-
public: virtual auto speak() const& -> void = 0;
30+
public: virtual auto speak() const -> void = 0;
3131

3232
public: virtual ~Human() noexcept;
3333

@@ -40,7 +40,7 @@ public: virtual ~Human() noexcept;
4040
namespace N {
4141
template<int I> class Machine {
4242
public: explicit Machine([[maybe_unused]] cpp2::in<std::string> param2);
43-
public: virtual auto work() const& -> void = 0;
43+
public: virtual auto work() const -> void = 0;
4444

4545
public: virtual ~Machine() noexcept;
4646

@@ -61,10 +61,10 @@ class Cyborg: public Cyborg_name_as_base, public Human, public Cyborg_address_as
6161

6262

6363
#line 25 "pure2-types-inheritance.cpp2"
64-
public: auto speak() const& -> void override;
64+
public: auto speak() const -> void override;
6565

6666

67-
public: auto work() const& -> void override;
67+
public: auto work() const -> void override;
6868

6969

7070
public: auto print() const& -> void;
@@ -117,10 +117,10 @@ namespace N {
117117
std::cout << cpp2::to_string(name) + " checks in for the day's shift\n";
118118
}
119119

120-
auto Cyborg::speak() const& -> void {
120+
auto Cyborg::speak() const -> void {
121121
std::cout << cpp2::to_string(name) + " cracks a few jokes with a coworker\n"; }
122122

123-
auto Cyborg::work() const& -> void {
123+
auto Cyborg::work() const -> void {
124124
std::cout << cpp2::to_string(name) + " carries some half-tonne crates of Fe2O3 to cold storage\n"; }
125125

126126
auto Cyborg::print() const& -> void {

regression-tests/test-results/pure2-union.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ template<typename T> class name_or_other
4646

4747
#line 12 "pure2-union.cpp2"
4848
public: [[nodiscard]] auto to_string() const& -> std::string;
49-
49+
5050
private: std::aligned_storage_t<cpp2::max(sizeof(std::string), sizeof(T))> _storage {}; private: cpp2::i8 _discriminator {-1}; public: [[nodiscard]] auto is_name() const& -> bool;
5151
public: [[nodiscard]] auto name() const& -> std::string const&;
5252
public: [[nodiscard]] auto name() & -> std::string&;
@@ -63,15 +63,13 @@ public: ~name_or_other() noexcept;
6363
public: name_or_other() = default;
6464
public: name_or_other(name_or_other const&) = delete; /* No 'that' constructor, suppress copy */
6565
public: auto operator=(name_or_other const&) -> void = delete;
66-
67-
68-
#line 16 "pure2-union.cpp2"
66+
#line 17 "pure2-union.cpp2"
6967
};
7068

7169
auto print_name(cpp2::in<name_or_number> non) -> void;
7270

7371

74-
#line 27 "pure2-union.cpp2"
72+
#line 28 "pure2-union.cpp2"
7573
auto main() -> int;
7674

7775

@@ -102,8 +100,9 @@ auto name_or_number::destroy() & -> void{
102100
name_or_number::~name_or_number() noexcept{destroy();}
103101
#line 12 "pure2-union.cpp2"
104102
template <typename T> [[nodiscard]] auto name_or_other<T>::to_string() const& -> std::string{
105-
if (is_name()) {return name(); }
106-
else { return cpp2::as_<std::string>(other()); }
103+
if (is_name()) { return name(); }
104+
else {if (is_other()) {return cpp2::as_<std::string>(other()); }
105+
else { return "invalid value"; }}
107106
}
108107

109108

@@ -130,7 +129,7 @@ template <typename T> auto name_or_other<T>::destroy() & -> void{
130129

131130
template <typename T> name_or_other<T>::~name_or_other() noexcept{destroy();}
132131

133-
#line 18 "pure2-union.cpp2"
132+
#line 19 "pure2-union.cpp2"
134133
auto print_name(cpp2::in<name_or_number> non) -> void{
135134
if (CPP2_UFCS_0(is_name, non)) {
136135
std::cout << CPP2_UFCS_0(name, non) << "\n";

regression-tests/test-results/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.2.1 Build 8927:1553
2+
cppfront compiler v0.2.1 Build 8928:1030
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8927:1553"
1+
"8928:1030"

source/cppfront.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -5608,9 +5608,16 @@ class cppfront
56085608

56095609
switch (this_->pass) {
56105610
break;case passing_style::in:
5611-
suffix1 += " const&";
5611+
suffix1 += " const";
5612+
// Cpp1 ref-qualifiers don't belong on virtual functions
5613+
if (!this_->is_polymorphic()) {
5614+
suffix1 += "&";
5615+
}
56125616
break;case passing_style::inout:
5613-
suffix1 += " &";
5617+
// Cpp1 ref-qualifiers don't belong on virtual functions
5618+
if (!this_->is_polymorphic()) {
5619+
suffix1 += " &";
5620+
}
56145621
break;case passing_style::out:
56155622
; // constructor is handled below
56165623
break;case passing_style::move:

source/reflect.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class compiler_services
109109
) & -> std::unique_ptr<statement_node>;
110110

111111
#line 118 "reflect.h2"
112-
public: [[nodiscard]] virtual auto position() const& -> source_position;
112+
public: [[nodiscard]] virtual auto position() const -> source_position;
113113

114114

115115
#line 124 "reflect.h2"
@@ -188,7 +188,7 @@ class declaration_base
188188
);
189189

190190
#line 208 "reflect.h2"
191-
public: [[nodiscard]] auto position() const& -> source_position override;
191+
public: [[nodiscard]] auto position() const -> source_position override;
192192

193193
public: [[nodiscard]] auto print() const& -> std::string;
194194

@@ -838,7 +838,7 @@ auto newline_pos = CPP2_UFCS(find, source, '\n');
838838
);
839839
}
840840

841-
[[nodiscard]] auto compiler_services::position() const& -> source_position
841+
[[nodiscard]] auto compiler_services::position() const -> source_position
842842

843843
{
844844
return { };
@@ -889,7 +889,7 @@ compiler_services::compiler_services(compiler_services const& that)
889889
cpp2::Default.expects(n, "a meta::declaration must point to a valid declaration_node, not null");
890890
}
891891

892-
[[nodiscard]] auto declaration_base::position() const& -> source_position { return CPP2_UFCS_0(position, (*cpp2::assert_not_null(n))); }
892+
[[nodiscard]] auto declaration_base::position() const -> source_position { return CPP2_UFCS_0(position, (*cpp2::assert_not_null(n))); }
893893

894894
[[nodiscard]] auto declaration_base::print() const& -> std::string { return CPP2_UFCS(pretty_print_visualize, (*cpp2::assert_not_null(n)), 0); }
895895

0 commit comments

Comments
 (0)