Skip to content

Commit f9ec456

Browse files
jjtoltonclaude
andcommitted
Improve syntax error reporting by including source file names
When syntax errors occur during file loading, the error messages now include the actual source file name and line number instead of just showing the predicate name where the error was caught. Before: error(syntax_error(incomplete_reduction),read_term/3:10). After: error(syntax_error(incomplete_reduction),'test_file.pl':8). Changes: - Added file_name field to MachineError struct to track source files - Updated error_form() to include file names in error context when available - Modified read_term() error handling to extract and set file names from streams - Added set_file_name() method for proper encapsulation This significantly improves debugging experience, especially when loading multiple files, as developers can immediately identify which file contains the syntax error. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent e4d9692 commit f9ec456

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

src/machine/machine_errors.rs

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ pub type MachineStubGen = Box<dyn Fn(&mut MachineState) -> MachineStub>;
2020
pub(crate) struct MachineError {
2121
stub: MachineStub,
2222
location: Option<(usize, usize)>, // line_num, col_num
23+
file_name: Option<Atom>, // source file name if available
24+
}
25+
26+
impl MachineError {
27+
#[inline]
28+
pub(crate) fn set_file_name(&mut self, file_name: Option<Atom>) {
29+
self.file_name = file_name;
30+
}
2331
}
2432

2533
// from 7.12.2 b) of 13211-1:1995
@@ -93,6 +101,7 @@ impl TypeError for HeapCellValue {
93101
MachineError {
94102
stub,
95103
location: None,
104+
file_name: None,
96105
}
97106
}
98107
}
@@ -107,6 +116,7 @@ impl TypeError for MachineStub {
107116
MachineError {
108117
stub,
109118
location: None,
119+
file_name: None,
110120
}
111121
}
112122
}
@@ -124,6 +134,7 @@ impl TypeError for Number {
124134
MachineError {
125135
stub,
126136
location: None,
137+
file_name: None,
127138
}
128139
}
129140
}
@@ -156,6 +167,7 @@ impl PermissionError for Atom {
156167
MachineError {
157168
stub,
158169
location: None,
170+
file_name: None,
159171
}
160172
}
161173
}
@@ -199,6 +211,7 @@ impl PermissionError for HeapCellValue {
199211
MachineError {
200212
stub,
201213
location: None,
214+
file_name: None,
202215
}
203216
}
204217
}
@@ -222,6 +235,7 @@ impl PermissionError for MachineStub {
222235
MachineError {
223236
stub,
224237
location: None,
238+
file_name: None,
225239
}
226240
}
227241
}
@@ -240,6 +254,7 @@ impl DomainError for HeapCellValue {
240254
MachineError {
241255
stub,
242256
location: None,
257+
file_name: None,
243258
}
244259
}
245260
}
@@ -257,6 +272,7 @@ impl DomainError for Number {
257272
MachineError {
258273
stub,
259274
location: None,
275+
file_name: None,
260276
}
261277
}
262278
}
@@ -271,6 +287,7 @@ impl DomainError for MachineStub {
271287
MachineError {
272288
stub,
273289
location: None,
290+
file_name: None,
274291
}
275292
}
276293
}
@@ -292,6 +309,7 @@ impl DomainError for ffi::Value {
292309
MachineError {
293310
stub,
294311
location: None,
312+
file_name: None,
295313
}
296314
}
297315
Value::Struct(atom, _values) => atom_as_cell!(atom).domain_error(machine_st, error),
@@ -312,6 +330,7 @@ impl MachineState {
312330
MachineError {
313331
stub,
314332
location: None,
333+
file_name: None,
315334
}
316335
}
317336

@@ -324,6 +343,7 @@ impl MachineState {
324343
MachineError {
325344
stub,
326345
location: None,
346+
file_name: None,
327347
}
328348
}
329349

@@ -346,6 +366,7 @@ impl MachineState {
346366
MachineError {
347367
stub,
348368
location: None,
369+
file_name: None,
349370
}
350371
}
351372

@@ -368,6 +389,7 @@ impl MachineState {
368389
MachineError {
369390
stub,
370391
location: None,
392+
file_name: None,
371393
}
372394
}
373395
ExistenceError::QualifiedProcedure {
@@ -386,6 +408,7 @@ impl MachineState {
386408
MachineError {
387409
stub,
388410
location: None,
411+
file_name: None,
389412
}
390413
}
391414
ExistenceError::Procedure(name, arity) => {
@@ -399,6 +422,7 @@ impl MachineState {
399422
MachineError {
400423
stub,
401424
location: None,
425+
file_name: None,
402426
}
403427
}
404428
ExistenceError::ModuleSource(source) => {
@@ -412,6 +436,7 @@ impl MachineState {
412436
MachineError {
413437
stub,
414438
location: None,
439+
file_name: None,
415440
}
416441
}
417442
ExistenceError::SourceSink(culprit) => {
@@ -423,6 +448,7 @@ impl MachineState {
423448
MachineError {
424449
stub,
425450
location: None,
451+
file_name: None,
426452
}
427453
}
428454
ExistenceError::Stream(culprit) => {
@@ -434,6 +460,7 @@ impl MachineState {
434460
MachineError {
435461
stub,
436462
location: None,
463+
file_name: None,
437464
}
438465
}
439466
ExistenceError::Process(culprit) => {
@@ -445,6 +472,7 @@ impl MachineState {
445472
MachineError {
446473
stub,
447474
location: None,
475+
file_name: None,
448476
}
449477
}
450478
ExistenceError::FfiFunction(name, arity) => {
@@ -457,6 +485,7 @@ impl MachineState {
457485
MachineError {
458486
stub,
459487
location: None,
488+
file_name: None,
460489
}
461490
}
462491
ExistenceError::FfiStructConstructor(name, arity) => {
@@ -472,6 +501,7 @@ impl MachineState {
472501
MachineError {
473502
stub,
474503
location: None,
504+
file_name: None,
475505
}
476506
}
477507
ExistenceError::FfiStructType(atom) => {
@@ -483,6 +513,7 @@ impl MachineState {
483513
MachineError {
484514
stub,
485515
location: None,
516+
file_name: None,
486517
}
487518
}
488519
}
@@ -563,6 +594,7 @@ impl MachineState {
563594
MachineError {
564595
stub,
565596
location: None,
597+
file_name: None,
566598
}
567599
}
568600

@@ -650,7 +682,11 @@ impl MachineState {
650682

651683
let stub = functor!(atom!("syntax_error"), [functor(stub)]);
652684

653-
MachineError { stub, location }
685+
MachineError {
686+
stub,
687+
location,
688+
file_name: None,
689+
}
654690
}
655691

656692
pub(super) fn representation_error(&self, flag: RepFlag) -> MachineError {
@@ -662,6 +698,7 @@ impl MachineState {
662698
MachineError {
663699
stub,
664700
location: None,
701+
file_name: None,
665702
}
666703
}
667704

@@ -677,6 +714,7 @@ impl MachineState {
677714
MachineError {
678715
stub,
679716
location: None,
717+
file_name: None,
680718
}
681719
}
682720

@@ -686,6 +724,7 @@ impl MachineState {
686724
MachineError {
687725
stub,
688726
location: None,
727+
file_name: None,
689728
}
690729
}
691730

@@ -701,6 +740,7 @@ impl MachineState {
701740
MachineError {
702741
stub,
703742
location: None,
743+
file_name: None,
704744
}
705745
}
706746
FfiError::ValueOutOfRange(domain, culprit) => self.domain_error(domain, culprit),
@@ -726,6 +766,7 @@ impl MachineState {
726766
FfiError::AllocationFailed => MachineError {
727767
stub: functor!(atom!("resource_error"), [atom_as_cell((atom!("heap")))]),
728768
location: None,
769+
file_name: None,
729770
},
730771
FfiError::LayoutError => self.representation_error(RepFlag::FfiLayout),
731772
FfiError::UnsupportedTypedef => self.representation_error(RepFlag::FfiLayout),
@@ -747,16 +788,32 @@ impl MachineState {
747788

748789
pub(super) fn error_form(&mut self, err: MachineError, src: MachineStub) -> MachineStub {
749790
if let Some((line_num, _col_num)) = err.location {
750-
functor!(
751-
atom!("error"),
752-
[
753-
functor((err.stub)),
754-
functor(
755-
(atom!(":")),
756-
[functor(src), number(line_num, (&mut self.arena))]
757-
)
758-
]
759-
)
791+
// Include file name in error if available
792+
if let Some(file_name) = err.file_name {
793+
let file_cell = atom_as_cell!(file_name);
794+
795+
functor!(
796+
atom!("error"),
797+
[
798+
functor((err.stub)),
799+
functor(
800+
(atom!(":")),
801+
[cell(file_cell), number(line_num, (&mut self.arena))]
802+
)
803+
]
804+
)
805+
} else {
806+
functor!(
807+
atom!("error"),
808+
[
809+
functor((err.stub)),
810+
functor(
811+
(atom!(":")),
812+
[functor(src), number(line_num, (&mut self.arena))]
813+
)
814+
]
815+
)
816+
}
760817
} else {
761818
functor!(atom!("error"), [functor((err.stub)), functor(src)])
762819
}

src/machine/machine_state.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,9 @@ impl MachineState {
843843
}
844844

845845
let stub = functor_stub(atom!("read_term"), 3);
846-
let err = self.syntax_error(err);
846+
let mut err = self.syntax_error(err);
847+
// Add file name to error if available
848+
err.set_file_name(stream.file_name());
847849

848850
return Err(self.error_form(err, stub));
849851
}

0 commit comments

Comments
 (0)