Add #no_type_assert and #type_assert to disable implicit type assertions with x.(T)

This commit is contained in:
gingerBill
2022-01-27 16:08:47 +00:00
parent 16786aac78
commit 24e7356825
8 changed files with 144 additions and 48 deletions

View File

@@ -1843,6 +1843,8 @@ void parse_proc_tags(AstFile *f, u64 *tags) {
ELSE_IF_ADD_TAG(require_results)
ELSE_IF_ADD_TAG(bounds_check)
ELSE_IF_ADD_TAG(no_bounds_check)
ELSE_IF_ADD_TAG(type_assert)
ELSE_IF_ADD_TAG(no_type_assert)
else {
syntax_error(tag_expr, "Unknown procedure type tag #%.*s", LIT(tag_name));
}
@@ -1853,6 +1855,10 @@ void parse_proc_tags(AstFile *f, u64 *tags) {
if ((*tags & ProcTag_bounds_check) && (*tags & ProcTag_no_bounds_check)) {
syntax_error(f->curr_token, "You cannot apply both #bounds_check and #no_bounds_check to a procedure");
}
if ((*tags & ProcTag_type_assert) && (*tags & ProcTag_no_type_assert)) {
syntax_error(f->curr_token, "You cannot apply both #type_assert and #no_type_assert to a procedure");
}
}
@@ -2000,11 +2006,23 @@ Ast *parse_check_directive_for_statement(Ast *s, Token const &tag_token, u16 sta
syntax_error(tag_token, "#bounds_check and #no_bounds_check cannot be applied together");
}
break;
case StateFlag_type_assert:
if ((s->state_flags & StateFlag_no_type_assert) != 0) {
syntax_error(tag_token, "#type_assert and #no_type_assert cannot be applied together");
}
break;
case StateFlag_no_type_assert:
if ((s->state_flags & StateFlag_type_assert) != 0) {
syntax_error(tag_token, "#type_assert and #no_type_assert cannot be applied together");
}
break;
}
switch (state_flag) {
case StateFlag_bounds_check:
case StateFlag_no_bounds_check:
case StateFlag_type_assert:
case StateFlag_no_type_assert:
switch (s->kind) {
case Ast_BlockStmt:
case Ast_IfStmt:
@@ -2128,6 +2146,12 @@ Ast *parse_operand(AstFile *f, bool lhs) {
} else if (name.string == "no_bounds_check") {
Ast *operand = parse_expr(f, lhs);
return parse_check_directive_for_statement(operand, name, StateFlag_no_bounds_check);
} else if (name.string == "type_assert") {
Ast *operand = parse_expr(f, lhs);
return parse_check_directive_for_statement(operand, name, StateFlag_type_assert);
} else if (name.string == "no_type_assert") {
Ast *operand = parse_expr(f, lhs);
return parse_check_directive_for_statement(operand, name, StateFlag_no_type_assert);
} else if (name.string == "relative") {
Ast *tag = ast_basic_directive(f, token, name);
tag = parse_call_expr(f, tag);
@@ -2224,6 +2248,12 @@ Ast *parse_operand(AstFile *f, bool lhs) {
if (tags & ProcTag_bounds_check) {
body->state_flags |= StateFlag_bounds_check;
}
if (tags & ProcTag_no_type_assert) {
body->state_flags |= StateFlag_no_type_assert;
}
if (tags & ProcTag_type_assert) {
body->state_flags |= StateFlag_type_assert;
}
return ast_proc_lit(f, type, body, tags, where_token, where_clauses);
} else if (allow_token(f, Token_do)) {
@@ -4611,6 +4641,12 @@ Ast *parse_stmt(AstFile *f) {
} else if (tag == "no_bounds_check") {
s = parse_stmt(f);
return parse_check_directive_for_statement(s, name, StateFlag_no_bounds_check);
} else if (tag == "type_assert") {
s = parse_stmt(f);
return parse_check_directive_for_statement(s, name, StateFlag_type_assert);
} else if (tag == "no_type_assert") {
s = parse_stmt(f);
return parse_check_directive_for_statement(s, name, StateFlag_no_type_assert);
} else if (tag == "partial") {
s = parse_stmt(f);
switch (s->kind) {