From 9af5360131d98173015aaee85238249a43045496 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Mon, 19 Oct 2026 14:18:56 +0300 Subject: [PATCH] count a unit among its own imports Two modules importing each other with pub hand the unit its own interface back, and its types were defined twice. --- sex-modules.scm | 12 ++++++++++-- sexc.scm | 2 ++ tests/modules/check.scm | 15 ++++++++++++++- tests/modules/cycle-app.sex | 7 +++++++ tests/modules/cycle/a.sex | 8 ++++++++ tests/modules/cycle/b.sex | 5 +++++ 6 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/modules/cycle-app.sex create mode 100644 tests/modules/cycle/a.sex create mode 100644 tests/modules/cycle/b.sex diff --git a/sex-modules.scm b/sex-modules.scm index db9d6d5..891150f 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -15,6 +15,7 @@ (export get-modules-public-forms + mark-imported! load-persistent-module-paths check-public-interface module-dependencies @@ -29,6 +30,12 @@ ;;; once) (define +imported-modules+ (list)) +;;; The unit is one of its own imports from the start: two modules that +;;; import each other with pub hand it its own interface back otherwise, +;;; and its types are defined twice +(define (mark-imported! path) + (set! +imported-modules+ (cons (normalize-pathname path) +imported-modules+))) + (define (get-modules-public-forms import-form) ;; How Sex handles modules: ;; For each module IMPORT-FORM names, construct path, find module by @@ -43,10 +50,11 @@ (module-path (locate-module name directory))) (unless module-path (sex-error form "Failed to find module" name (get-module-paths directory))) - (if (member module-path +imported-modules+) + (if (member (normalize-pathname module-path) +imported-modules+) (list) (begin - (set! +imported-modules+ (cons module-path +imported-modules+)) + (set! +imported-modules+ (cons (normalize-pathname module-path) + +imported-modules+)) (read-public-interface module-path))))) ;;; An import is looked for beside the file it is written in, so a diff --git a/sexc.scm b/sexc.scm index 4b52e54..601a6ca 100644 --- a/sexc.scm +++ b/sexc.scm @@ -247,6 +247,8 @@ status, which is ours to pass on." ;;; The unit's own forms are checked for what its importers will need, ;;; the prelude's not: every unit has those (define (semantic-process-forms unit-forms input-source) + (when (string? input-source) + (mark-imported! (to-absolute-pathname input-source))) (let ((process (lambda () (check-public-interface unit-forms) (semen-process (append prelude unit-forms))))) diff --git a/tests/modules/check.scm b/tests/modules/check.scm index 4f09e13..890d867 100644 --- a/tests/modules/check.scm +++ b/tests/modules/check.scm @@ -33,6 +33,9 @@ ;;; book its siblings page, with `pub', and spine, without. A pub macro ;;; of page's expands into a private one, which crosses with it. ;;; +;;; Two modules may import each other with `pub': cycle/a and cycle/b, +;;; whose interfaces each import the other back. +;;; ;;; csi -s check.scm (import scheme @@ -83,6 +86,15 @@ '("pages 2 3" "number 2") (output-of "./shelf-app")) +(test-assert "modules importing each other with pub compile" + (and (output-of sexc "cycle/a.sex" "-c" "-o" "cycle-a.o") + (output-of sexc "cycle/b.sex" "-c" "-o" "cycle-b.o") + (output-of sexc "cycle-app.sex" "-o" "cycle-app" "--" "cycle-a.o" "cycle-b.o"))) + +(test "and the app runs through them" + '("cycle 1 2") + (output-of "./cycle-app")) + (test "the app runs through both" '("hello, world" "hello, sex" @@ -100,6 +112,7 @@ (output-of "./greet-app")) (for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d" - "page.o" "book.o" "shelf-app")) + "page.o" "book.o" "shelf-app" + "cycle-a.o" "cycle-b.o" "cycle-app")) (test-exit) diff --git a/tests/modules/cycle-app.sex b/tests/modules/cycle-app.sex new file mode 100644 index 0000000..9159151 --- /dev/null +++ b/tests/modules/cycle-app.sex @@ -0,0 +1,7 @@ +(include stdio.h) +(import cycle/a) + +(pub fn main () int + (var b rb (make-rb 1 2)) + (printf "cycle %d %d\n" (. b a x) (. b y)) + (return 0)) diff --git a/tests/modules/cycle/a.sex b/tests/modules/cycle/a.sex new file mode 100644 index 0000000..b0bc6d5 --- /dev/null +++ b/tests/modules/cycle/a.sex @@ -0,0 +1,8 @@ +;;; Each imports the other with pub, so either one compiled pastes the +;;; other's interface, which imports it back. b holds an ra by value, so +;;; ra is defined before b is imported, as C needs it in one unit +(pub record ra ((x int))) + +(pub import b) + +(pub fn make-ra ((x int)) ra (return #(ra : x))) diff --git a/tests/modules/cycle/b.sex b/tests/modules/cycle/b.sex new file mode 100644 index 0000000..88a7686 --- /dev/null +++ b/tests/modules/cycle/b.sex @@ -0,0 +1,5 @@ +(pub import a) + +(pub record rb ((a ra) (y int))) + +(pub fn make-rb ((x int) (y int)) rb (return #(rb : (make-ra x) y)))