Skip to content

Commit 112ead1

Browse files
Praetonusjemc
authored andcommitted
Improve error message when capturing this in lambdas (ponylang#1201)
1 parent 79cc0b0 commit 112ead1

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

pony.g

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ tuple
209209
;
210210

211211
lambdacaptures
212-
: ('(' | LPAREN_NEW) lambdacapture (',' lambdacapture)* ')'
212+
: ('(' | LPAREN_NEW) (lambdacapture | 'this') (',' (lambdacapture | 'this'))* ')'
213213
;
214214

215215
lambdacapture

src/libponyc/ast/parser.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ DECL(parampattern);
2626
DECL(pattern);
2727
DECL(idseq_in_seq);
2828
DECL(members);
29+
DECL(thisliteral);
2930

3031

3132
/* Precedence
@@ -309,12 +310,13 @@ DEF(lambdacapture);
309310
IF(TK_ASSIGN, RULE("capture value", infix));
310311
DONE();
311312

312-
// (LPAREN | LPAREN_NEW) lambdacapture {COMMA lambdacapture} RPAREN
313+
// (LPAREN | LPAREN_NEW) (lambdacapture | thisliteral)
314+
// {COMMA (lambdacapture | thisliteral)} RPAREN
313315
DEF(lambdacaptures);
314316
AST_NODE(TK_LAMBDACAPTURES);
315317
SKIP(NULL, TK_LPAREN, TK_LPAREN_NEW);
316-
RULE("capture", lambdacapture);
317-
WHILE(TK_COMMA, RULE("capture", lambdacapture));
318+
RULE("capture", lambdacapture, thisliteral);
319+
WHILE(TK_COMMA, RULE("capture", lambdacapture, thisliteral));
318320
SKIP(NULL, TK_RPAREN);
319321
DONE();
320322

src/libponyc/pass/syntax.c

+12
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,18 @@ static ast_result_t syntax_lambda(pass_opt_t* opt, ast_t* ast)
955955
default: {}
956956
}
957957

958+
ast_t* capture = ast_child(captures);
959+
while(capture != NULL)
960+
{
961+
if(ast_id(capture) == TK_THIS)
962+
{
963+
ast_error(opt->check.errors, capture,
964+
"use a named capture to capture 'this'");
965+
return AST_ERROR;
966+
}
967+
capture = ast_sibling(capture);
968+
}
969+
958970
return AST_OK;
959971
}
960972

0 commit comments

Comments
 (0)