Skip to content

Commit

Permalink
SFINAE return type constraints
Browse files Browse the repository at this point in the history
#feat
  • Loading branch information
alandefreitas committed Jan 22, 2025
1 parent b885e2e commit 6f9bd07
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 18 deletions.
14 changes: 14 additions & 0 deletions src/lib/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,20 @@ populate(
}
else
{
// Return type SFINAE constraints
if (I.ReturnType &&
!I.ReturnType->Constraints.empty())
{
for (ExprInfo const& constraint: I.ReturnType->Constraints)
{
if (!I.Requires.Written.empty())
{
I.Requires.Written += " && ";
}
I.Requires.Written += constraint.Written;
}
}

// Iterate I.Params to find trailing requires clauses
for (auto it = I.Params.begin(); it != I.Params.end(); )
{
Expand Down
3 changes: 2 additions & 1 deletion test-files/golden-tests/core/libcxx.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Declared in `<libcxx.cpp>`
----
template<typename T>
T
sqrt(T value);
sqrt(T value)
requires std::is_integral_v<T>;
----

=== Description
Expand Down
3 changes: 2 additions & 1 deletion test-files/golden-tests/core/libcxx.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ <h3>Synopsis</h3>
<code class="source-code cpp">
template&lt;typename T&gt;
T
sqrt(T value);
sqrt(T value)
requires std::is_integral_v&lt;T&gt;;
</code>
</pre>
</div>
Expand Down
2 changes: 1 addition & 1 deletion test-files/golden-tests/core/libcxx.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<namespace id="//////////////////////////8=">
<template>
<tparam name="T" class="type"/>
<function name="sqrt" id="ulFDUE1svTCX8fV/h8EIp4NNbWs=">
<function name="sqrt" requires="std::is_integral_v&lt;T&gt;" id="ulFDUE1svTCX8fV/h8EIp4NNbWs=">
<file short-path="libcxx.cpp" source-path="libcxx.cpp" line="149"/>
<return>
<type name="T"/>
Expand Down
12 changes: 8 additions & 4 deletions test-files/golden-tests/metadata/sfinae.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ Declared in `&lt;sfinae&period;cpp&gt;`
----
template&lt;class T&gt;
T
f1(T value);
f1(T value)
requires std&colon;&colon;is&lowbar;integral&lowbar;v&lt;T&gt;;
----

[#f10]
Expand Down Expand Up @@ -294,7 +295,8 @@ Declared in `&lt;sfinae&period;cpp&gt;`
----
template&lt;class T&gt;
int
f2(T value);
f2(T value)
requires std&colon;&colon;is&lowbar;integral&lowbar;v&lt;T&gt;;
----

[#f3]
Expand All @@ -312,7 +314,8 @@ Declared in `&lt;sfinae&period;cpp&gt;`
----
template&lt;class T&gt;
<<B,B>>::<<B-C,C>>
f3(T value);
f3(T value)
requires std&colon;&colon;is&lowbar;integral&lowbar;v&lt;T&gt;;
----

[#f4]
Expand All @@ -330,7 +333,8 @@ Declared in `&lt;sfinae&period;cpp&gt;`
----
template&lt;class T&gt;
T
f4(T value);
f4(T value)
requires std&colon;&colon;is&lowbar;integral&lowbar;v&lt;T&gt;;
----

[#f5]
Expand Down
12 changes: 8 additions & 4 deletions test-files/golden-tests/metadata/sfinae.html
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ <h3>Synopsis</h3>
<code class="source-code cpp">
template&lt;class T&gt;
T
f1(T value);
f1(T value)
requires std::is_integral_v&lt;T&gt;;
</code>
</pre>
</div>
Expand Down Expand Up @@ -343,7 +344,8 @@ <h3>Synopsis</h3>
<code class="source-code cpp">
template&lt;class T&gt;
int
f2(T value);
f2(T value)
requires std::is_integral_v&lt;T&gt;;
</code>
</pre>
</div>
Expand All @@ -365,7 +367,8 @@ <h3>Synopsis</h3>
<code class="source-code cpp">
template&lt;class T&gt;
<a href="#B">B</a>::<a href="#B-C">C</a>
f3(T value);
f3(T value)
requires std::is_integral_v&lt;T&gt;;
</code>
</pre>
</div>
Expand All @@ -387,7 +390,8 @@ <h3>Synopsis</h3>
<code class="source-code cpp">
template&lt;class T&gt;
T
f4(T value);
f4(T value)
requires std::is_integral_v&lt;T&gt;;
</code>
</pre>
</div>
Expand Down
8 changes: 4 additions & 4 deletions test-files/golden-tests/metadata/sfinae.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<namespace id="//////////////////////////8=">
<template>
<tparam name="T" class="type"/>
<function name="f1" id="Bs19YS1cqZhrsbv149xgnjRFUAk=">
<function name="f1" requires="std::is_integral_v&lt;T&gt;" id="Bs19YS1cqZhrsbv149xgnjRFUAk=">
<file short-path="sfinae.cpp" source-path="sfinae.cpp" line="5"/>
<return>
<type name="T"/>
Expand All @@ -21,7 +21,7 @@
</template>
<template>
<tparam name="T" class="type"/>
<function name="f2" id="uckUKgu5WdbIitoodkl2kXQ7RWI=">
<function name="f2" requires="std::is_integral_v&lt;T&gt;" id="uckUKgu5WdbIitoodkl2kXQ7RWI=">
<file short-path="sfinae.cpp" source-path="sfinae.cpp" line="10"/>
<return>
<type name="int"/>
Expand All @@ -43,7 +43,7 @@
</namespace>
<template>
<tparam name="T" class="type"/>
<function name="f3" id="HIZ4NeNKvHJFhj0tJIjpjaLL30U=">
<function name="f3" requires="std::is_integral_v&lt;T&gt;" id="HIZ4NeNKvHJFhj0tJIjpjaLL30U=">
<file short-path="sfinae.cpp" source-path="sfinae.cpp" line="19"/>
<return>
<type id="r/5vKTgl4cXSK5TnuZ/+P7qmkJ0=" name="B::C"/>
Expand All @@ -60,7 +60,7 @@
</template>
<template>
<tparam name="T" class="type"/>
<function name="f4" id="45LJB0/nCsN3KRlVtnRqNrJG818=">
<function name="f4" requires="std::is_integral_v&lt;T&gt;" id="45LJB0/nCsN3KRlVtnRqNrJG818=">
<file short-path="sfinae.cpp" source-path="sfinae.cpp" line="24"/>
<return>
<type name="T"/>
Expand Down
3 changes: 2 additions & 1 deletion test-files/golden-tests/snippets/sqrt.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Declared in `&lt;sqrt&period;cpp&gt;`
----
template&lt;typename T&gt;
T
sqrt(T value);
sqrt(T value)
requires std&colon;&colon;is&lowbar;integral&lowbar;v&lt;T&gt;;
----

=== Description
Expand Down
3 changes: 2 additions & 1 deletion test-files/golden-tests/snippets/sqrt.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ <h3>Synopsis</h3>
<code class="source-code cpp">
template&lt;typename T&gt;
T
sqrt(T value);
sqrt(T value)
requires std::is_integral_v&lt;T&gt;;
</code>
</pre>
</div>
Expand Down
2 changes: 1 addition & 1 deletion test-files/golden-tests/snippets/sqrt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<namespace id="//////////////////////////8=">
<template>
<tparam name="T" class="type"/>
<function name="sqrt" id="ulFDUE1svTCX8fV/h8EIp4NNbWs=">
<function name="sqrt" requires="std::is_integral_v&lt;T&gt;" id="ulFDUE1svTCX8fV/h8EIp4NNbWs=">
<file short-path="sqrt.cpp" source-path="sqrt.cpp" line="15" class="def"/>
<return>
<type name="T"/>
Expand Down

0 comments on commit 6f9bd07

Please sign in to comment.