scryer-prolog icon indicating copy to clipboard operation
scryer-prolog copied to clipboard

Fix: Handle empty list in term_expansion/2 to remove terms without warnings

Open jjtolton opened this issue 2 months ago • 1 comments

Problem

When term_expansion/2 returns an empty list [], Scryer was treating it as a term to compile, resulting in discontiguous warnings:

Warning: overwriting []/0 because the clauses are discontiguous

Example

term_expansion((:- start_debug), []).

:- start_debug.  % This would cause a warning
foo(1).
:- start_debug.  % Another warning here
foo(2).

Solution

Modified src/loader.pl to handle empty list specially:

  1. In expand_term/2: Explicitly check for [] and pass it through without recursion
  2. In compile_term/2: Skip compilation when Terms == [] (do nothing)

New Semantics

  • term_expansion(Term, [])remove the term completely (no compilation)
  • term_expansion(Term, [NewTerm]) → replace with single term
  • term_expansion(Term, [T1, T2, ...]) → replace with multiple terms

This allows term_expansion/2 to act as a filter that can completely remove terms from compilation, which is a common and useful metaprogramming pattern.

Tests

Following the three-layer testing approach:

  • Layer 2 (Prolog Integration): src/tests/term_expansion_empty_list.pl - 5 comprehensive tests
  • Layer 3 (CLI): tests/scryer/cli/src_tests/term_expansion_empty_list.toml - End-to-end test

All tests pass:

  • ✅ 71 Rust unit tests
  • ✅ 5 new Prolog integration tests
  • ✅ New CLI test
  • ✅ No regressions

Related Issues

Fixes #2623

jjtolton avatar Oct 25 '25 19:10 jjtolton

Awesome catch, thank you a lot!

@hurufu, I would greatly appreciate if you could take a look at this, thank you a lot!

triska avatar Oct 25 '25 20:10 triska