Require exact return-type matches in generated protocol concepts (1/20) - #101
Require exact return-type matches in generated protocol concepts (1/20)#101jbcoe wants to merge 1 commit into
Conversation
9ef84ae to
168aa68
Compare
|
I think whether or not this is the right direction depends on what double f() { return 1.5; }
int main() {
return std::function<int()>{f}(); // OK, returns 1
}But virtual functions do not. They do allow covariant returns, though: struct A {
virtual A& foo() = 0;
};
struct B : A {
B& foo() override; // OK
};Which might be analogous to: struct Interface {
protocol_view<Interface> foo();
};
struct S {
S& foo();
};What is the rationale here for |
|
For now I'm keen to implement what's in the DRAFT.md and this is one area that we've been explicit about: Relaxed structural subtyping We require a type to have exactly matching function signatures as With some suitably compelling motivation, conformance via implicit conversions could be The strongest argument for picking this option is that it's the one that lets us change our minds later without breaking code. This approach served us well for indirect and polymorphic. With suitable motivation, I'm happy to change this part of the design, but we should land the reflection implementation of DRAFT first. I raised #121 to track this. |
protocol.j2's generated concepts used std::convertible_to<R>, letting merely-convertible return types conform; switched to std::same_as<R> per DRAFT.md's exact-match design. CovariantBImpl no longer conforms to B and moves from a dispatch test to a negative static_assert.
168aa68 to
c851094
Compare
That makes a lot of sense, thank you! |
|
Accidentally requested review from copilot while self-assigning, sorry about that. |
There was a problem hiding this comment.
Pull request overview
This PR tightens the generated protocol concepts to require exact return-type matches (aligning conformance checks with the design described in DRAFT.md) by switching from std::convertible_to<R> to std::same_as<R>. As a result, the “convertible return type” B-shaped implementation used in tests is no longer considered conforming, and the test suite is updated accordingly.
Changes:
- Update
scripts/protocol.j2to requirestd::same_as<ReturnType>for generated concept return-type constraints (includingvoid). - Adjust
protocol_test.ccto make the previously-positive covariant/convertible-return test a negative compile-time check. - Update a diagnostic comment in
scripts/test_concept_errors.pyto reflect “exact type match” semantics.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| scripts/test_concept_errors.py | Updates an explanatory comment for the “wrong return type” concept-error test case. |
| scripts/protocol.j2 | Changes generated protocol concepts from convertible_to to same_as for return-type checking. |
| protocol_test.cc | Replaces the runtime dispatch test for convertible return types with a negative static_assert conformance check. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| {% else %} | ||
| { t.{{ m.name }}({{ args_str }}) }{% if m.is_noexcept %} noexcept{% endif %} -> std::convertible_to<{{ m.return_type.name }}>; | ||
| {% endif %} | ||
| { t.{{ m.name }}({{ args_str }}) }{% if m.is_noexcept %} noexcept{% endif %} -> std::same_as<{{ m.return_type.name }}>; |
| // A B-shaped type whose methods return covariant/implicitly-convertible | ||
| // types instead of B's exact declared return types. |
protocol.j2's generated concepts used std::convertible_to, letting
merely-convertible return types conform; switched to std::same_as
per DRAFT.md's exact-match design. CovariantBImpl no longer conforms
to B and moves from a dispatch test to a negative static_assert.