Skip to content

Commit

Permalink
Make output -Wunused-parameter-clean, and add real support for _
Browse files Browse the repository at this point in the history
…unnamed parameters, closes #560

Emit `[[maybe_unused]]` on a `that` parameter if the class is empty.
Add -Wunused-parameter to my GCC and Clang regression tests.
Add `_` unnamed parameter support, and use ordinals instead of line/col numbers to keep the generated names stable.

As an example from one of the updated regression tests that had a helper function to just accept (but not use) five generic parameters, that function is now just: `call: (_, _, _, _, _) = { }`
  • Loading branch information
hsutter committed Aug 13, 2023
1 parent 62dfdcb commit 6ce2643
Show file tree
Hide file tree
Showing 27 changed files with 85 additions and 64 deletions.
4 changes: 2 additions & 2 deletions regression-tests/mixed-forwarding.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ struct X {
X(X && that) : i{that.i} { std::cout << "move X " << i << "\n"; }
};

copy_from: (copy x:_) = { }
copy_from: (copy _) = { }

use: (x:_) = {}
use: (_) = {}

// invoking each of these with an rvalue std::pair argument ...
apply_implicit_forward: (forward t: std::pair<X, X>) = {
Expand Down
6 changes: 3 additions & 3 deletions regression-tests/mixed-parameter-passing-with-forward.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <cstdlib>
#include <ctime>

copy_from: (copy x:_) = { }
copy_from: (copy _) = { }

parameter_styles: (
in a: std::string, // "in" is default
in _: std::string, // "in" is default
copy b: std::string,
inout c: std::string,
inout _: std::string,
move d: std::string,
forward e: std::string
)
Expand Down
6 changes: 3 additions & 3 deletions regression-tests/mixed-parameter-passing.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include <cstdlib>
#include <ctime>

copy_from: (copy x:_) = { }
copy_from: (copy _) = { }

parameter_styles: (
in a: std::string, // "in" is default
in _: std::string, // "in" is default
copy b: std::string,
inout c: std::string,
inout _: std::string,
move d: std::string
)
= {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

call: (v:_, w:_, x:_, y:_, z:_) = { }
call: (_, _, _, _, _) = { }

test: (a:_) -> std::string = {
return call( a,
Expand Down
4 changes: 2 additions & 2 deletions regression-tests/pure2-bugfix-for-discard-precedence.cpp2
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
quantity: type = {
number: i32;
operator=: (out this, _: std::in_place_t, x: i32) = {
operator=: (out this, i: std::in_place_t, x: i32) = {

This comment has been minimized.

Copy link
@JohelEGP

JohelEGP Aug 13, 2023

Contributor

This was really meant to be named _, as it is a tag type not used in the body.
But I had to discard it below with _ = _
because we didn't have the support added by this commit.

Changing it back would mean that this test would stop testing
that static_cast<void>(x); is fine where void(x); isn't.

This comment has been minimized.

Copy link
@JohelEGP

JohelEGP Aug 13, 2023

Contributor

Done by #587.

number = x;
_ = _;
_ = i;
}
operator+=: (inout this, that) -> forward quantity = {
number += that.number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Base: type = {
operator=: (out this) = { }
operator=: (out this, that) = std::cout << "(out this, that)\n";
operator=: (implicit out this, x) = std::cout << "(implicit out this, x)\n";
operator=: (implicit out this, _) = std::cout << "(implicit out this, _)\n";
}

Derived: type = {
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/pure2-types-basics.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ myclass : type = {

operator=: (out this, x: int, s: std::string) = {
this.data = 77;
this.more = s + " plugh";
this.more = s + std::to_string(x) + " plugh";
std::cout << "myclass: from int and string\n";
print();
}
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/pure2-types-inheritance.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Human: @interface type = {

N: namespace = {
Machine: @polymorphic_base <I:int> type = {
operator=: (out this, id: std::string) = {}
operator=: (out this, _: std::string) = {}
work: (virtual this);
}
}
Expand Down
4 changes: 2 additions & 2 deletions regression-tests/pure2-ufcs-member-access-and-chaining.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ main: () -> int = {
42.no_return();
}

no_return: (x: int) = { }
no_return: (_) = { }

ufcs: (i:int) -> int = {
return i+2;
Expand All @@ -37,5 +37,5 @@ get_i: (r:_) -> int = {
}

// And a test for non-local UFCS, which shouldn't do a [&] capture
f: (x)->int = 0;
f: (_)->int = 0;
y: int = 0.f();
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ myclass: explicit from string
myclass: default
data: 504, more: 3.141590
myclass: from int and string
data: 77, more: hair plugh
data: 77, more: hair1 plugh
x's state before assignments: data: 1, more: 504
myclass: implicit from int
data: 84, more: 504
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ for f in *.cpp
do
let count=count+1
printf "[%s] Starting clang++-12 %s\n" "$count" "$f"
clang++-12 -I../../../include -std=c++20 -pthread -o test.exe $f > $f.output 2>&1
clang++-12 -I../../../include -std=c++20 -pthread -Wunused-parameter -o test.exe $f > $f.output 2>&1
rm -f $f
if test -f "test.exe"; then
let exe_count=exe_count+1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ myclass: explicit from string
myclass: default
data: 504, more: 3.141590
myclass: from int and string
data: 77, more: hair plugh
data: 77, more: hair1 plugh
x's state before assignments: data: 1, more: 504
myclass: implicit from int
data: 84, more: 504
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/gcc-13/run-tests-gcc-13.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ for f in *.cpp
do
let count=count+1
printf "[%s] Starting gcc 13 %s\n" "$count" "$f"
g++ -I../../../include -std=c++20 -pthread -o test.exe $f > $f.output 2>&1
g++ -I../../../include -std=c++20 -pthread -Wunused-parameter -o test.exe $f > $f.output 2>&1
rm -f $f
if test -f "test.exe"; then
let exe_count=exe_count+1
Expand Down
8 changes: 4 additions & 4 deletions regression-tests/test-results/mixed-forwarding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ struct X {
};

#line 11 "mixed-forwarding.cpp2"
auto copy_from(auto x) -> void;
auto copy_from([[maybe_unused]] auto param1) -> void;

auto use(auto const& x) -> void;
auto use([[maybe_unused]] auto const& param1) -> void;

// invoking each of these with an rvalue std::pair argument ...
auto apply_implicit_forward(auto&& t) -> void
Expand All @@ -46,9 +46,9 @@ CPP2_REQUIRES (std::is_same_v<CPP2_TYPEOF(t), std::pair<X,X>>)


#line 11 "mixed-forwarding.cpp2"
auto copy_from(auto x) -> void{}
auto copy_from([[maybe_unused]] auto param1) -> void{}

auto use(auto const& x) -> void{}
auto use([[maybe_unused]] auto const& param1) -> void{}

#line 16 "mixed-forwarding.cpp2"
auto apply_implicit_forward(auto&& t) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#include <ctime>

#line 6 "mixed-parameter-passing-with-forward.cpp2"
auto copy_from(auto x) -> void;
auto copy_from([[maybe_unused]] auto param1) -> void;

auto parameter_styles(
cpp2::in<std::string> a, // "in" is default
[[maybe_unused]] cpp2::in<std::string> param1, // "in" is default
std::string b,
std::string& c,
[[maybe_unused]] std::string& param3,
std::string&& d,
auto&& e
) -> void
Expand All @@ -36,12 +36,12 @@ CPP2_REQUIRES (std::is_same_v<CPP2_TYPEOF(e), std::string>)


#line 6 "mixed-parameter-passing-with-forward.cpp2"
auto copy_from(auto x) -> void{}
auto copy_from([[maybe_unused]] auto param1) -> void{}

auto parameter_styles(
cpp2::in<std::string> a,
[[maybe_unused]] cpp2::in<std::string> param1,
std::string b,
std::string& c,
[[maybe_unused]] std::string& param3,
std::string&& d,
auto&& e
) -> void
Expand Down
12 changes: 6 additions & 6 deletions regression-tests/test-results/mixed-parameter-passing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#include <ctime>

#line 6 "mixed-parameter-passing.cpp2"
auto copy_from(auto x) -> void;
auto copy_from([[maybe_unused]] auto param1) -> void;

auto parameter_styles(
cpp2::in<std::string> a, // "in" is default
[[maybe_unused]] cpp2::in<std::string> param1, // "in" is default
std::string b,
std::string& c,
[[maybe_unused]] std::string& param3,
std::string&& d
) -> void;

Expand All @@ -32,12 +32,12 @@ auto parameter_styles(


#line 6 "mixed-parameter-passing.cpp2"
auto copy_from(auto x) -> void{}
auto copy_from([[maybe_unused]] auto param1) -> void{}

auto parameter_styles(
cpp2::in<std::string> a,
[[maybe_unused]] cpp2::in<std::string> param1,
std::string b,
std::string& c,
[[maybe_unused]] std::string& param3,
std::string&& d
) -> void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


#line 2 "mixed-postfix-expression-custom-formatting.cpp2"
auto call(auto const& v, auto const& w, auto const& x, auto const& y, auto const& z) -> void;
auto call([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2, [[maybe_unused]] auto const& param3, [[maybe_unused]] auto const& param4, [[maybe_unused]] auto const& param5) -> void;

[[nodiscard]] auto test(auto const& a) -> std::string;

Expand All @@ -24,7 +24,7 @@ auto call(auto const& v, auto const& w, auto const& x, auto const& y, auto const


#line 2 "mixed-postfix-expression-custom-formatting.cpp2"
auto call(auto const& v, auto const& w, auto const& x, auto const& y, auto const& z) -> void{}
auto call([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2, [[maybe_unused]] auto const& param3, [[maybe_unused]] auto const& param4, [[maybe_unused]] auto const& param5) -> void{}

[[nodiscard]] auto test(auto const& a) -> std::string{
return call(a,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ myclass: explicit from string
myclass: default
data: 504, more: 3.141590
myclass: from int and string
data: 77, more: hair plugh
data: 77, more: hair1 plugh
x's state before assignments: data: 1, more: 504
myclass: implicit from int
data: 84, more: 504
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class quantity;
#line 1 "pure2-bugfix-for-discard-precedence.cpp2"
class quantity {
private: cpp2::i32 number;
public: explicit quantity(cpp2::in<std::in_place_t> _, cpp2::in<cpp2::i32> x);
public: explicit quantity(cpp2::in<std::in_place_t> i, cpp2::in<cpp2::i32> x);


#line 7 "pure2-bugfix-for-discard-precedence.cpp2"
Expand All @@ -35,12 +35,12 @@ auto main() -> int;


#line 3 "pure2-bugfix-for-discard-precedence.cpp2"
quantity::quantity(cpp2::in<std::in_place_t> _, cpp2::in<cpp2::i32> x)
quantity::quantity(cpp2::in<std::in_place_t> i, cpp2::in<cpp2::i32> x)
: number{ x }
#line 3 "pure2-bugfix-for-discard-precedence.cpp2"
{

static_cast<void>(_);
static_cast<void>(i);
}
auto quantity::operator+=(quantity const& that) -> quantity&{
number += that.number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ class Derived;
#line 1 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
class Base {
public: explicit Base();
public: Base(Base const& that);
public: Base([[maybe_unused]] Base const& that);

#line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
public: auto operator=(Base const& that) -> Base& ;
public: auto operator=([[maybe_unused]] Base const& that) -> Base& ;

#line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
public: Base(Base&& that) noexcept;
public: Base([[maybe_unused]] Base&& that) noexcept;

#line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
public: auto operator=(Base&& that) noexcept -> Base& ;
public: Base(auto const& x);
public: auto operator=([[maybe_unused]] Base&& that) noexcept -> Base& ;
public: Base([[maybe_unused]] auto const& param2);
#line 4 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
public: auto operator=(auto const& x) -> Base& ;
public: auto operator=([[maybe_unused]] auto const& param2) -> Base& ;
};

class Derived: public Base {
Expand All @@ -49,22 +49,22 @@ auto main() -> int;

#line 2 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
Base::Base(){}
Base::Base (Base const& that) { std::cout << "(out this, that)\n"; }
Base::Base ([[maybe_unused]] Base const& that) { std::cout << "(out this, that)\n"; }
#line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
auto Base::operator=(Base const& that) -> Base& { std::cout << "(out this, that)\n";
auto Base::operator=([[maybe_unused]] Base const& that) -> Base& { std::cout << "(out this, that)\n";
return *this;
#line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
}
#line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
Base::Base (Base&& that) noexcept { std::cout << "(out this, that)\n"; }
Base::Base ([[maybe_unused]] Base&& that) noexcept { std::cout << "(out this, that)\n"; }
#line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
auto Base::operator=(Base&& that) noexcept -> Base& { std::cout << "(out this, that)\n";
auto Base::operator=([[maybe_unused]] Base&& that) noexcept -> Base& { std::cout << "(out this, that)\n";
return *this;
#line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
}
Base::Base(auto const& x) { std::cout << "(implicit out this, x)\n"; }
Base::Base([[maybe_unused]] auto const& param2) { std::cout << "(implicit out this, _)\n"; }
#line 4 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
auto Base::operator=(auto const& x) -> Base& { std::cout << "(implicit out this, x)\n";
auto Base::operator=([[maybe_unused]] auto const& param2) -> Base& { std::cout << "(implicit out this, _)\n";
return *this;
#line 4 "pure2-bugfix-for-memberwise-base-assignment.cpp2"
}
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/pure2-types-basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace N {

myclass::myclass(cpp2::in<int> x, cpp2::in<std::string> s)
: data{ 77 }
, more{ s + " plugh" }
, more{ s + std::to_string(x) + " plugh" }
#line 20 "pure2-types-basics.cpp2"
{

Expand Down
4 changes: 2 additions & 2 deletions regression-tests/test-results/pure2-types-inheritance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public: virtual ~Human() noexcept;

namespace N {
template<int I> class Machine {
public: explicit Machine(cpp2::in<std::string> id);
public: explicit Machine([[maybe_unused]] cpp2::in<std::string> param2);
public: virtual auto work() const -> void = 0;

public: virtual ~Machine() noexcept;
Expand Down Expand Up @@ -97,7 +97,7 @@ auto main() -> int;
#line 6 "pure2-types-inheritance.cpp2"
namespace N {

template <int I> Machine<I>::Machine(cpp2::in<std::string> id){}
template <int I> Machine<I>::Machine([[maybe_unused]] cpp2::in<std::string> param2){}

template <int I> Machine<I>::~Machine() noexcept{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


#line 24 "pure2-ufcs-member-access-and-chaining.cpp2"
auto no_return(cpp2::in<int> x) -> void;
auto no_return([[maybe_unused]] auto const& param1) -> void;

[[nodiscard]] auto ufcs(cpp2::in<int> i) -> int;
struct fun__ret { int i; };
Expand All @@ -32,7 +32,7 @@ auto no_return(cpp2::in<int> x) -> void;

#line 39 "pure2-ufcs-member-access-and-chaining.cpp2"
// And a test for non-local UFCS, which shouldn't do a [&] capture
[[nodiscard]] auto f(auto const& x) -> int;
[[nodiscard]] auto f([[maybe_unused]] auto const& param1) -> int;
extern int y;

//=== Cpp2 function definitions =================================================
Expand Down Expand Up @@ -61,7 +61,7 @@ extern int y;
CPP2_UFCS_0(no_return, 42);
}

auto no_return(cpp2::in<int> x) -> void{}
auto no_return([[maybe_unused]] auto const& param1) -> void{}

[[nodiscard]] auto ufcs(cpp2::in<int> i) -> int{
return i + 2;
Expand All @@ -79,6 +79,6 @@ auto no_return(cpp2::in<int> x) -> void{}
}

#line 40 "pure2-ufcs-member-access-and-chaining.cpp2"
[[nodiscard]] auto f(auto const& x) -> int { return 0; }
[[nodiscard]] auto f([[maybe_unused]] auto const& param1) -> int { return 0; }
int y {CPP2_UFCS_0_NONLOCAL(f, 0)};

Loading

1 comment on commit 6ce2643

@JohelEGP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two places where this was immediately useful,
where now I can omit redundant _ = _ to actually discard the unused parameters.

  • Language tag type for post increment/decrement.
    operator--: (inout this, _: int) -> point requires number_line<vector_type> = {
      _ = _;
  • Library tag types.
    operator==: (this, _: std::default_sentinel_t) -> bool           = {
      _ = _;

Please sign in to comment.