Skip to content

Commit b979d19

Browse files
committed
Change cpp2::in closer to its original condition, closes #317
Restore a `pass_by_value` computation, but exclude class/union/array/function types. See comment thread in #317 for details. Also don't allow the Cpp1 casts, and emit migration usability diagnostics for attempts to use them.
1 parent e294d17 commit b979d19

File tree

7 files changed

+53
-17
lines changed

7 files changed

+53
-17
lines changed

include/cpp2util.h

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238

239239
namespace cpp2 {
240240

241+
241242
//-----------------------------------------------------------------------
242243
//
243244
// Convenience names for fundamental types
@@ -519,9 +520,20 @@ template<typename T>
519520
//-----------------------------------------------------------------------
520521
//
521522
template<typename T>
523+
constexpr bool prefer_pass_by_value =
524+
sizeof(T) <= 2*sizeof(void*)
525+
&& std::is_trivially_copy_constructible_v<T>
526+
&& !std::is_class_v<T>
527+
&& !std::is_union_v<T>
528+
&& !std::is_array_v<T>
529+
&& !std::is_function_v<T>
530+
;
531+
532+
template<typename T>
533+
requires (!std::is_void_v<T>)
522534
using in =
523535
std::conditional_t <
524-
std::is_scalar_v<T>,
536+
prefer_pass_by_value<T>,
525537
T const,
526538
T const&
527539
>;
@@ -1001,15 +1013,14 @@ auto as( X const& x ) -> C const& {
10011013
}
10021014

10031015
template< typename C, typename X >
1004-
requires (std::is_base_of_v<X, C> && !std::is_same_v<C,X>)
1005-
auto as( X* x ) -> C* {
1006-
return Dynamic_cast<C*>(x);
1007-
}
1008-
1009-
template< typename C, typename X >
1010-
requires (std::is_base_of_v<X, C> && !std::is_same_v<C,X>)
1011-
auto as( X const* x ) -> C const* {
1012-
return Dynamic_cast<C const*>(x);
1016+
requires (
1017+
std::is_pointer_v<C>
1018+
&& std::is_pointer_v<X>
1019+
&& std::is_base_of_v<CPP2_TYPEOF(*std::declval<X>()), CPP2_TYPEOF(*std::declval<C>())>
1020+
&& !std::is_same_v<C, X>
1021+
)
1022+
auto as( X x ) -> C {
1023+
return Dynamic_cast<C>(x);
10131024
}
10141025

10151026

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pure2-bounds-safety-pointer-arithmetic-error.cpp2...
2-
pure2-bounds-safety-pointer-arithmetic-error.cpp2(15,13): error: 'delete' and owning raw pointers are not supported in Cpp2 - use unique.new<T>, shared.new<T>, or gc.new<T> instead (in that order)
2+
pure2-bounds-safety-pointer-arithmetic-error.cpp2(15,13): error: 'delete' and owning raw pointers are not supported in Cpp2 - use unique.new<T> or shared.new<T> instead in that order (or, in the future, gc.new<T>, but that is not yet implemented)
33

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.2.1 Build 8522:0446
2+
cppfront compiler v0.2.1 Build 8529:1436
33
Copyright(c) Herb Sutter All rights reserved
44

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

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8522:0446"
1+
"8529:1436"

source/lex.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,31 @@ auto lex_line(
16491649
//
16501650
else if (auto j = peek_is_keyword()) {
16511651
store(j, lexeme::Keyword);
1652+
1653+
if (tokens.back() == "const_cast") {
1654+
errors.emplace_back(
1655+
source_position(lineno, i),
1656+
"'const_cast' is not supported in Cpp2 - the current C++ best practice is to never cast away const, and that is const_cast's only effective use"
1657+
);
1658+
}
1659+
if (tokens.back() == "reinterpret_cast") {
1660+
errors.emplace_back(
1661+
source_position(lineno, i),
1662+
"'reinterpret_cast' is not supported in Cpp2 - use std::bit_cast instead"
1663+
);
1664+
}
1665+
if (tokens.back() == "static_cast") {
1666+
errors.emplace_back(
1667+
source_position(lineno, i),
1668+
"'static_cast<T>(val)' is not supported in Cpp2 - use 'val as T' for safe conversions instead, or if necessary unsafe_narrow<T>(val) for a possibly-lossy narrowing conversion"
1669+
);
1670+
}
1671+
if (tokens.back() == "dynamic_cast") {
1672+
errors.emplace_back(
1673+
source_position(lineno, i),
1674+
"'dynamic_cast<Derived*>(pBase)' is not supported in Cpp2 - use 'pBase as *Derived' for safe dynamic conversions instead"
1675+
);
1676+
}
16521677
}
16531678

16541679
// Identifier
@@ -1675,13 +1700,13 @@ auto lex_line(
16751700
if (tokens.back() == "union") {
16761701
errors.emplace_back(
16771702
source_position(lineno, i),
1678-
"unsafe 'union's are not supported in Cpp2 - use std::variant instead (or, in the future, the Cpp2 'union' metaclass function, but that is not yet implemented)"
1703+
"unsafe 'union' is not supported in Cpp2 - use std::variant instead (or, in the future, the Cpp2 'union' metaclass function, but that is not yet implemented)"
16791704
);
16801705
}
16811706
if (tokens.back() == "delete") {
16821707
errors.emplace_back(
16831708
source_position(lineno, i),
1684-
"'delete' and owning raw pointers are not supported in Cpp2 - use unique.new<T>, shared.new<T>, or gc.new<T> instead (in that order)"
1709+
"'delete' and owning raw pointers are not supported in Cpp2 - use unique.new<T> or shared.new<T> instead in that order (or, in the future, gc.new<T>, but that is not yet implemented)"
16851710
);
16861711
}
16871712
}

source/reflect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ namespace meta {
596596
std::deque<token>* generated_tokens_
597597
)
598598
: errors{ errors_ }
599-
, errors_original_size{ static_cast<int>(std::ssize(*cpp2::assert_not_null(errors))) }
599+
, errors_original_size{ cpp2::unsafe_narrow<int>(std::ssize(*cpp2::assert_not_null(errors))) }
600600
, generated_tokens{ generated_tokens_ }
601601
, parser{ *cpp2::assert_not_null(errors) }
602602
#line 45 "reflect.h2"

source/reflect.h2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ compiler_services: @polymorphic_base @copyable type =
4444
)
4545
= {
4646
errors = errors_;
47-
errors_original_size = static_cast<int>(std::ssize(errors*));
47+
errors_original_size = cpp2::unsafe_narrow<int>(std::ssize(errors*));
4848
generated_tokens = generated_tokens_;
4949
parser = errors*;
5050
}

0 commit comments

Comments
 (0)