Skip to content

Commit 04e0c9c

Browse files
committed
Improve parse_eval_all line number reporting
1 parent 19563a3 commit 04e0c9c

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/interpreter.c

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern "C" {
1515
#endif
1616

1717
extern int jl_lineno;
18+
extern const char *jl_filename;
1819

1920
static jl_value_t *eval(jl_value_t *e, jl_value_t **locals, size_t nl, size_t ngensym);
2021
static jl_value_t *eval_body(jl_array_t *stmts, jl_value_t **locals, size_t nl, size_t ngensym,
@@ -159,6 +160,7 @@ static jl_value_t *eval(jl_value_t *e, jl_value_t **locals, size_t nl, size_t ng
159160
}
160161
if (jl_is_linenode(e)) {
161162
jl_lineno = jl_linenode_line(e);
163+
jl_filename = jl_linenode_file(e)->name;
162164
}
163165
if (jl_is_newvarnode(e)) {
164166
jl_value_t *var = jl_fieldref(e,0);

src/toplevel.c

+26-9
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,12 @@ jl_value_t *jl_parse_eval_all(const char *fname, size_t len)
555555
{
556556
//jl_printf(JL_STDERR, "***** loading %s\n", fname);
557557
int last_lineno = jl_lineno;
558+
int top_lineno = -1;
558559
const char *last_filename = jl_filename;
559560
jl_lineno = 0;
560561
jl_filename = fname;
561-
jl_value_t *fn=NULL, *ln=NULL, *form=NULL, *result=jl_nothing;
562-
JL_GC_PUSH4(&fn, &ln, &form, &result);
562+
jl_value_t *fn=NULL, *ln=NULL, *form=NULL, *nest_exc=NULL, *result=jl_nothing;
563+
JL_GC_PUSH5(&fn, &ln, &form, &result, &nest_exc);
563564
JL_TRY {
564565
// handle syntax error
565566
while (1) {
@@ -574,22 +575,38 @@ jl_value_t *jl_parse_eval_all(const char *fname, size_t len)
574575
jl_interpret_toplevel_expr(form);
575576
}
576577
}
578+
top_lineno = jl_lineno; // jl_parse_next sets lineno.
577579
result = jl_toplevel_eval_flex(form, 1);
578580
}
579581
}
580582
JL_CATCH {
581583
jl_stop_parsing();
582-
fn = jl_pchar_to_string(fname, len);
583-
ln = jl_box_long(jl_lineno);
584-
jl_lineno = last_lineno;
585-
jl_filename = last_filename;
584+
586585
if (jl_loaderror_type == NULL) {
586+
// reset line and filename before throwing
587+
jl_lineno = last_lineno;
588+
jl_filename = last_filename;
587589
jl_rethrow();
588590
}
589-
else {
590-
jl_rethrow_other(jl_new_struct(jl_loaderror_type, fn, ln,
591-
jl_exception_in_transit));
591+
592+
fn = jl_pchar_to_string(jl_filename, strlen(jl_filename));
593+
ln = jl_box_long(jl_lineno);
594+
nest_exc = jl_new_struct(jl_loaderror_type, fn, ln,
595+
jl_exception_in_transit);
596+
597+
if ((strcmp(jl_filename, fname) == 0) &&
598+
jl_lineno == top_lineno) {
599+
jl_lineno = last_lineno;
600+
jl_filename = last_filename;
601+
jl_rethrow_other(nest_exc);
592602
}
603+
604+
jl_lineno = last_lineno;
605+
jl_filename = last_filename;
606+
fn = jl_pchar_to_string(fname, len);
607+
ln = jl_box_long(top_lineno);
608+
jl_rethrow_other(jl_new_struct(jl_loaderror_type, fn, ln,
609+
nest_exc));
593610
}
594611
jl_stop_parsing();
595612
jl_lineno = last_lineno;

0 commit comments

Comments
 (0)