Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid structs with embed fields with finalisers #2420

Merged
merged 1 commit into from
Dec 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/libponyc/pass/verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "../verify/call.h"
#include "../verify/control.h"
#include "../verify/fun.h"
#include "../verify/type.h"
#include "expr.h"
#include "ponyassert.h"

Expand Down Expand Up @@ -111,6 +112,7 @@ ast_result_t pass_verify(ast_t** astp, pass_opt_t* options)

switch(ast_id(ast))
{
case TK_STRUCT: r = verify_struct(options, ast); break;
case TK_ASSIGN: r = verify_assign(options, ast); break;
case TK_FUN:
case TK_NEW:
Expand Down
68 changes: 68 additions & 0 deletions src/libponyc/verify/type.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "type.h"
#include "ponyassert.h"


static bool embed_struct_field_has_finaliser(pass_opt_t* opt, ast_t* field,
ast_t* base)
{
ast_t* type = ast_type(field);
pony_assert(ast_id(type) == TK_NOMINAL);

ast_t* def = (ast_t*)ast_data(type);

ast_t* final = ast_get(def, stringtab("_final"), NULL);

if(final != NULL)
{
ast_error(opt->check.errors, base,
"a struct cannot have a field with a _final method");

if(field != base)
ast_error_continue(opt->check.errors, field, "nested field is here");

ast_error_continue(opt->check.errors, final, "_final method is here");

return false;
}

ast_t* members = ast_childidx(def, 4);
ast_t* member = ast_child(members);

bool ok = true;

while(member != NULL)
{
if(ast_id(member) == TK_EMBED)
{
if(!embed_struct_field_has_finaliser(opt, member, base))
ok = false;
}

member = ast_sibling(member);
}

return ok;
}

bool verify_struct(pass_opt_t* opt, ast_t* ast)
{
pony_assert(ast_id(ast) == TK_STRUCT);

ast_t* members = ast_childidx(ast, 4);
ast_t* member = ast_child(members);

bool ok = true;

while(member != NULL)
{
if(ast_id(member) == TK_EMBED)
{
if(!embed_struct_field_has_finaliser(opt, member, member))
ok = false;
}

member = ast_sibling(member);
}

return ok;
}
14 changes: 14 additions & 0 deletions src/libponyc/verify/type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef VERIFY_TYPE_H
#define VERIFY_TYPE_H

#include <platform.h>
#include "../ast/ast.h"
#include "../pass/pass.h"

PONY_EXTERN_C_BEGIN

bool verify_struct(pass_opt_t* opt, ast_t* ast);

PONY_EXTERN_C_END

#endif
13 changes: 13 additions & 0 deletions test/libponyc/verify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ TEST_F(VerifyTest, StructFinal)
TEST_ERRORS_1(src, "a struct cannot have a _final method");
}

TEST_F(VerifyTest, StructEmbedFieldFinal)
{
const char* src =
"class Foo\n"
" fun _final() =>\n"
" None\n"

"struct Bar\n"
" embed f: Foo = Foo";

TEST_ERRORS_1(src, "a struct cannot have a field with a _final method");
}

TEST_F(VerifyTest, PrimitiveWithTypeParamsFinal)
{
const char* src =
Expand Down