Skip to content

Commit

Permalink
Require ; after assert statement
Browse files Browse the repository at this point in the history
It's just consistent.

And update the grammar.
  • Loading branch information
hsutter committed Nov 8, 2023
1 parent a812dcc commit f10444e
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 39 deletions.
4 changes: 2 additions & 2 deletions regression-tests/mixed-bounds-safety-with-assert-2.cpp2
Expand Up @@ -9,8 +9,8 @@ main: () -> int = {

add_42_to_subrange: (inout rng:_, start:int, end:int)
= {
assert<Bounds>( 0 <= start )
assert<Bounds>( end <= rng.ssize() )
assert<Bounds>( 0 <= start );
assert<Bounds>( end <= rng.ssize() );

count := 0;
for rng
Expand Down
4 changes: 2 additions & 2 deletions regression-tests/mixed-bounds-safety-with-assert.cpp2
Expand Up @@ -7,8 +7,8 @@ main: () -> int = {
}

print_subrange: (rng:_, start:int, end:int) = {
assert<Bounds>( 0 <= start )
assert<Bounds>( end <= rng.ssize() )
assert<Bounds>( 0 <= start );
assert<Bounds>( end <= rng.ssize() );

count := 0;
for rng
Expand Down
6 changes: 3 additions & 3 deletions regression-tests/mixed-bugfix-for-literal-as-nttp.cpp2
@@ -1,7 +1,7 @@
#include <chrono>
using namespace std::chrono_literals;
main: () = {
assert( 10 as i32 == 10 )
assert( 10LL as i32 == 10 )
assert( 10s as std::chrono::seconds == 10s )
assert( 10 as i32 == 10 );
assert( 10LL as i32 == 10 );
assert( 10s as std::chrono::seconds == 10s );
}
6 changes: 3 additions & 3 deletions regression-tests/pure2-bugfix-for-assign-expression-list.cpp2
Expand Up @@ -2,9 +2,9 @@ main: () = {
vec: type == std::vector<int>;
v: vec = (0);
v = ();
assert( v == :vec = () )
assert( v == :vec = () );
v = (1);
assert( v == :vec = (1) )
assert( v == :vec = (1) );
v = (2, 3);
assert( v == :vec = (2, 3) )
assert( v == :vec = (2, 3) );
}
Expand Up @@ -3,6 +3,6 @@ t: @struct type = {
this: std::integral_constant<u, :u = (17, 29)>;
}
main: () = {
assert<Testing>( t::value[0] == 17 )
assert<Testing>( t::value[1] == 29 )
assert<Testing>( t::value[0] == 17 );
assert<Testing>( t::value[1] == 29 );
}
4 changes: 2 additions & 2 deletions regression-tests/pure2-concept-definition.cpp2
@@ -1,5 +1,5 @@
arithmetic: <T> concept = std::integral<T> || std::floating_point<T>;
main: () = {
assert<Testing>( arithmetic<i32> )
assert<Testing>( arithmetic<float> )
assert<Testing>( arithmetic<i32> );
assert<Testing>( arithmetic<float> );
}
2 changes: 1 addition & 1 deletion regression-tests/pure2-print.cpp2
Expand Up @@ -46,7 +46,7 @@ outer: @print type = {
else if !m.empty() { b(); }
else { c(); }

assert( true )
assert( true );

return :() -> std::string = (s + m[0])$; ();
}
Expand Down
25 changes: 11 additions & 14 deletions source/parse.h
Expand Up @@ -7001,9 +7001,12 @@ class parser
//G jump-statement
//G iteration-statement
//G compound-statement
//G contract
//G contract-statement
//G declaration
//G expression-statement
//G
//G contract-statement
//G contract ';'
//
//GTODO try-block
//G
Expand Down Expand Up @@ -7089,6 +7092,11 @@ class parser
error("only 'assert' contracts are allowed at statement scope");
return {};
}
if (curr().type() != lexeme::Semicolon) {
error("missing ';' after contract-statement");
return {};
}
next();
n->statement = std::move(s);
assert (n->is_contract());
return n;
Expand Down Expand Up @@ -7467,8 +7475,8 @@ class parser


//G contract:
//G contract-kind contract-group? ':' logical-or-expression ']' ']'
//G contract-kind contract-group? ':' logical-or-expression ',' string-literal ']' ']'
//G contract-kind contract-group? ':' '(' logical-or-expression ')'
//G contract-kind contract-group? ':' '(' logical-or-expression ',' string-literal ')'
//G
//G contract-group:
//G '<' id-expression '>'
Expand Down Expand Up @@ -7540,17 +7548,6 @@ class parser
}
next();

// Allow optional ';' after an assert, which is really an empty
// statement (I'm not putting it in the grammar) and so this is
// to skip the "empty statement" check and error
if (
*n->kind == "assert"
&& curr().type() == lexeme::Semicolon
)
{
next();
}

return n;
}

Expand Down
2 changes: 1 addition & 1 deletion source/reflect.h
Expand Up @@ -156,7 +156,7 @@ type_id: @polymorphic_base @copyable type =
= {
compiler_services = s;
n = n_;
assert( n, "a meta::type_id must point to a valid type_id_node, not null" )
assert( n, "a meta::type_id must point to a valid type_id_node, not null" );
}
is_wildcard : (this) -> bool = n.is_wildcard();
Expand Down
18 changes: 9 additions & 9 deletions source/reflect.h2
Expand Up @@ -107,7 +107,7 @@ compiler_services: @polymorphic_base @copyable type =
tokens := generated_lexers.back()&;
tokens*.lex( lines*, true );

assert( std::ssize(tokens* .get_map()) == 1 )
assert( std::ssize(tokens* .get_map()) == 1 );

// Now parse this single declaration from
// the lexed tokens
Expand Down Expand Up @@ -168,7 +168,7 @@ type_id: @polymorphic_base @copyable type =
= {
compiler_services = s;
n = n_;
assert( n, "a meta::type_id must point to a valid type_id_node, not null" )
assert( n, "a meta::type_id must point to a valid type_id_node, not null" );
}

is_wildcard : (this) -> bool = n*.is_wildcard();
Expand Down Expand Up @@ -204,7 +204,7 @@ declaration_base: @polymorphic_base @copyable type =
= {
compiler_services = s;
n = n_;
assert( n, "a meta::declaration must point to a valid declaration_node, not null" )
assert( n, "a meta::declaration must point to a valid declaration_node, not null" );
}

position: (override this) -> source_position = n*.position();
Expand Down Expand Up @@ -285,7 +285,7 @@ declaration: @polymorphic_base @copyable type =
pre<Type>( parent_is_type() ) // this precondition should be sufficient ...
= {
test := n*.type_member_mark_for_removal();
assert( test ) // ... to ensure this assert is true
assert( test ); // ... to ensure this assert is true
}
}

Expand All @@ -304,7 +304,7 @@ function_declaration: @copyable type =
) =
{
declaration = (n_, s);
assert( n*.is_function() )
assert( n*.is_function() );
}

index_of_parameter_named : (this, s: std::string_view) -> int = n*.index_of_parameter_named(s);
Expand Down Expand Up @@ -361,7 +361,7 @@ object_declaration: @copyable type =
) =
{
declaration = (n_, s);
assert( n*.is_object() )
assert( n*.is_object() );
}

is_const : (this) -> bool = n*.is_const();
Expand Down Expand Up @@ -397,7 +397,7 @@ type_declaration: @copyable type =
) =
{
declaration = (n_, s);
assert( n*.is_type() )
assert( n*.is_type() );
}

reserve_names: (this, name: std::string_view, forward etc...) =
Expand Down Expand Up @@ -510,7 +510,7 @@ alias_declaration: @copyable type =
) =
{
declaration = (n_, s);
assert( n*.is_alias() )
assert( n*.is_alias() );
}
}

Expand Down Expand Up @@ -1239,7 +1239,7 @@ apply_metafunctions: (
)
-> bool
= {
assert( n.is_type() )
assert( n.is_type() );

// Check for _names reserved for the metafunction implementation
for rtype.get_members()
Expand Down

0 comments on commit f10444e

Please sign in to comment.